1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package com.ontotext.wsmo4j.parser.wsml;
28
29 import java.util.*;
30
31 import org.deri.wsmo4j.io.parser.*;
32 import org.wsmo.common.*;
33 import org.wsmo.factory.*;
34 import org.wsmo.wsml.*;
35 import org.wsmo.wsml.compiler.node.*;
36
37 public class TopEntityAnalysis extends ASTAnalysis {
38
39 private ASTAnalysisContainer container;
40
41 private WsmoFactory factory;
42
43 private Stack topEntityStack;
44
45 public TopEntityAnalysis(ASTAnalysisContainer container, WsmoFactory factory) {
46 if (container == null) {
47 throw new IllegalArgumentException();
48 }
49 this.container = container;
50 this.factory = factory;
51 topEntityStack = container.getStack(TopEntity.class);
52
53
54 container.registerNodeHandler(AImportsontology.class, this);
55 container.registerNodeHandler(AUsesmediator.class, this);
56 container.registerNodeHandler(AWsmlvariant.class, this);
57 }
58
59 public void outAImportsontology(AImportsontology node) {
60 if (topEntityStack.isEmpty()) {
61 return;
62 }
63 Identifier[] iris = (Identifier[]) container.popFromStack(Identifier[].class,
64 Identifier[].class);
65 TopEntity topEntity = (TopEntity) topEntityStack.peek();
66 for (int i = 0; i < iris.length; i++) {
67 topEntity.addOntology(factory.getOntology((IRI) iris[i]));
68 }
69 }
70
71 public void outAUsesmediator(AUsesmediator node) {
72 if (topEntityStack.isEmpty()) {
73 return;
74 }
75 Identifier[] iris = (Identifier[]) container.popFromStack(Identifier[].class,
76 Identifier[].class);
77 TopEntity topEntity = (TopEntity) topEntityStack.peek();
78 for (int i = 0; i < iris.length; i++) {
79 if (iris[i] instanceof IRI) {
80 topEntity.addMediator((IRI) iris[i]);
81 }
82 else {
83 ParserException pe = new ParserException("usesMediator could "
84 + "refer Mediators only by IRI!", null);
85 pe.setErrorLine(node.getTUsemediator().getLine());
86 pe.setErrorPos(node.getTUsemediator().getPos());
87 throw new WrappedParsingException(pe);
88 }
89 }
90 }
91
92 public void inAWsmlvariant(AWsmlvariant node) {
93 String wsmlVariant = node.getFullIri().toString().trim();
94 wsmlVariant = wsmlVariant.substring(2, wsmlVariant.length() - 1);
95 container.getStack(AWsmlvariant.class).push(wsmlVariant);
96 }
97
98 public static void addNamespaceAndVariant(TopEntity entity, Stack namespaces, Stack variant) {
99 for (int i = 0; i < namespaces.size(); i++) {
100 Namespace namespace = (Namespace) namespaces.get(i);
101 if (namespace.getPrefix().equals("_")) {
102 entity.setDefaultNamespace(namespace);
103 }
104 else {
105 entity.addNamespace(namespace);
106 }
107 }
108
109 if (!variant.isEmpty()) {
110 entity.setWsmlVariant((String) variant.peek());
111 }
112 }
113
114
115
116
117
118
119
120 public static void isValidTopEntityIdentifier(PId id, Token token) {
121 if (id instanceof AIriId == false) {
122 ParserException pe =
123 new ParserException("Expected IRI but found Identifier,", null);
124 pe.setExpectedToken("IRI");
125 if (id!=null){
126 pe.setFoundToken(id.toString());
127 }
128 if (id instanceof AAnonymousId) {
129 Token t = ((AAnonymousId) id).getAnonymous();
130 pe.setErrorLine(t.getLine());
131 pe.setErrorPos(t.getPos());
132 }
133
134 if (id == null && token != null){
135 pe.setErrorLine(token.getLine());
136 pe.setErrorPos(token.getPos()+token.getText().length());
137 }
138 throw new WrappedParsingException(pe);
139 }
140 }
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167