View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-2006, Ontotext Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  
19  /**
20   * <p>Title: WSMO4J</p>
21   * <p>Description: WSMO API and a Reference Implementation</p>
22   * <p>Copyright:  Copyright (c) 2004-2006</p>
23   * <p>Company: Ontotext Lab. / SIRMA </p>
24   */
25  
26  package com.ontotext.wsmo4j.parser.wsml;
27  
28  import java.util.*;
29  
30  import org.deri.wsmo4j.io.parser.*;
31  import org.deri.wsmo4j.logicalexpression.*;
32  import org.omwg.logicalexpression.*;
33  import org.wsmo.common.*;
34  import org.wsmo.factory.*;
35  import org.wsmo.wsml.*;
36  import org.wsmo.wsml.compiler.node.*;
37  
38  public class IdentifierAnalysis extends ASTAnalysis {
39  
40      private WsmoFactory factory;
41      private ASTAnalysisContainer container;
42      private HashMap nsPrefixCache = new HashMap();
43      private Stack namespaces; // handle to namespace stack
44      private Stack identifiers; // handle to identifier stack
45      private Stack identifierLists; // handle to identifierList stack
46  
47      public IdentifierAnalysis(ASTAnalysisContainer container, WsmoFactory factory) {
48          if (factory == null || container == null) {
49              throw new IllegalArgumentException();
50          }
51          this.factory = factory;
52          this.container = container;
53  
54          // register the handled nodes for namespaces
55          container.registerNodeHandler(ADefaultnsPrefixdefinitionlist.class, this);
56          container.registerNodeHandler(ADefaultPrefixdefinition.class, this);
57          container.registerNodeHandler(ANamespacedefPrefixdefinition.class, this);
58  
59          // register the handled nodes for ids
60          container.registerNodeHandler(AAnonymousId.class, this);
61          container.registerNodeHandler(AIriId.class, this);
62          container.registerNodeHandler(AIriIri.class, this);
63          container.registerNodeHandler(AAnySqname.class, this);
64          container.registerNodeHandler(AIdIdlist.class, this);
65          container.registerNodeHandler(AIdlistIdlist.class, this);
66          container.registerNodeHandler(AUniversalTruthId.class, this);
67          container.registerNodeHandler(AUniversalFalsehoodId.class, this);
68          container.registerNodeHandler(PId.class, this);
69  
70          namespaces = container.getStack(Namespace.class);
71          identifiers = container.getStack(Identifier.class);
72          identifierLists = container.getStack(Identifier[].class);
73      }
74  
75      public void setNameSpaces(HashMap ns){
76          this.nsPrefixCache = ns;
77      }
78  
79      // Hanlde namespaces
80  
81      public void inADefaultnsPrefixdefinitionlist(ADefaultnsPrefixdefinitionlist node1) {
82          String namespaceIRI = _strip(node1.getFullIri().getText().trim());
83          String namespacePrefix = "_"; // this is always the default namespace prefix
84          Namespace namespace = factory.createNamespace(namespacePrefix,
85                  factory.createIRI(namespaceIRI));
86          namespaces.push(namespace);
87          nsPrefixCache.put(namespacePrefix, namespaceIRI);
88      }
89  
90      public void inADefaultPrefixdefinition(ADefaultPrefixdefinition node1) {
91          String namespaceIRI = _strip(node1.getFullIri().getText().trim());
92          String namespacePrefix = "_"; // this is always the default namespace prefix
93          Namespace namespace = factory.createNamespace(namespacePrefix,
94                  factory.createIRI(namespaceIRI));
95          Stack namespaces = container.getStack(Namespace.class);
96          namespaces.push(namespace);
97          nsPrefixCache.put(namespacePrefix, namespaceIRI);
98      }
99  
100     public void inANamespacedefPrefixdefinition(ANamespacedefPrefixdefinition node1) {
101         String namespaceIRI = _strip(node1.getFullIri().getText().trim());
102         String namespacePrefix = node1.getName().getText().trim();
103         Namespace namespace = factory.createNamespace(unEscape(namespacePrefix),
104                 factory.createIRI(namespaceIRI));
105         Stack namespaces = container.getStack(Namespace.class);
106         namespaces.push(namespace);
107         nsPrefixCache.put(namespacePrefix, namespaceIRI);
108     }
109 
110     // Handle identifier
111 
112     public void caseAIriId(AIriId node) {
113         if (node.getIri() != null)
114             node.getIri().apply(this);
115     }
116 
117     public void caseASqnameIri(ASqnameIri node) {
118         if (node.getSqname() != null)
119             node.getSqname().apply(this);
120     }
121 
122     public void caseAAnonymousId(AAnonymousId node) {
123         identifiers.push(factory.createAnonymousID());
124         node.replaceBy(null);
125     }
126 
127     public void caseAIriIri(AIriIri node) {
128         try{
129             identifiers.push(factory.createIRI(_strip(node.getFullIri().getText())));
130         }catch (IllegalArgumentException e){
131             ParserException pe = new ParserException("relative IRIs are not allowed", null);
132             Token t = node.getFullIri();
133             pe.setErrorLine(t.getLine());
134             pe.setErrorPos(t.getPos());
135             pe.setFoundToken(t.getText());
136             throw new WrappedParsingException(pe);
137         }
138         node.replaceBy(null);
139     }
140 
141     public void caseALocalkeywordSqname(ALocalkeywordSqname node) {
142         //TODO: Check why there special Sqname {localkeyword} case in wsml grammar??
143         TName tn = new TName(node.getAnykeyword().toString());
144         String prefix = null;
145         if ((APrefix)node.getPrefix()!=null){
146             tn = ((APrefix)node.getPrefix()).getName();
147             prefix = tn.getText().trim();
148         }
149         sQNameHandler(new TName(node.getAnykeyword().toString()).getText().trim(),
150                 prefix, tn);
151         node.replaceBy(null);
152     }
153 
154     // copy pasted from IdentifierProcessing.java QName inner class
155 
156     public void caseAAnySqname(AAnySqname node) {
157         TName tn = node.getName();
158         String prefix = null;
159         if ((APrefix)node.getPrefix()!=null){
160             tn = ((APrefix)node.getPrefix()).getName();
161             prefix = tn.getText().trim();
162         }
163         sQNameHandler(node.getName().getText().trim(),prefix,tn);
164         node.replaceBy(null);
165     }
166 
167     private void sQNameHandler(String name, String prefix, TName tn) {
168         //built-ins
169         String iri = ConstantTransformer.getInstance().findIri(name);
170         if (iri != null) {
171             identifiers.push(factory.createIRI(iri));
172             return;
173         }
174 
175         //check on non existing or non sufficient ns container
176         String error = null;
177         if (prefix == null && nsPrefixCache.get("_") == null) {
178             error= ParserException.NO_NAMESPACE;
179         }
180         else if (prefix != null && nsPrefixCache.get(prefix) == null) {
181             error = ParserException.NAMESPACE_PREFIX_NOT_FOUND;
182         }
183         if (error != null) {
184             ParserException e = new ParserException(error, null);
185             e.setErrorLine(tn.getLine());
186             e.setErrorPos(tn.getPos());
187             if (prefix != null){
188                 e.setFoundToken(prefix+"#"+name);
189             }else{
190                 e.setFoundToken(name);
191             }
192             throw new WrappedParsingException(e);
193         }
194 
195         //create IRI
196         String namespace = null;
197         if (prefix !=null){
198             namespace = (String) nsPrefixCache.get(prefix);
199         }
200         else {
201             namespace = (String) nsPrefixCache.get("_");
202         }
203 
204         identifiers.push(factory.createIRI(namespace + unEscape(name)));
205     }
206 
207     public static String unEscape(String localname) {
208         //remove \ from input.
209         if (localname.indexOf('\\') == -1) {
210             return localname;
211         }
212         StringBuffer result = new StringBuffer(localname.length());
213         for (int i = 0; i < localname.length(); i++) {
214             if (localname.charAt(i) != '\\') {
215                 result.append(localname.charAt(i));
216             }
217         }
218         return result.toString();
219     }
220 
221     // copy pasted from QNameProcessing.java
222 
223     private String _strip(String str) {
224         if (null == str || str.length() < 3) return str;
225         return str.substring(2, str.length() - 1);
226     }
227 
228     // handle Identifier list
229 
230     Object lastItem;
231 
232     public void inAIdIdlist(AIdIdlist node) {
233         if (identifiers.isEmpty()) {
234             lastItem = null;
235         }
236         else {
237             lastItem = identifiers.peek();
238         }
239     }
240 
241     public void outAIdIdlist(AIdIdlist node) {
242         Vector ids = new Vector();
243         while (!identifiers.isEmpty() &&
244                 identifiers.peek() != lastItem) {
245             ids.add(0, identifiers.pop());
246         }
247         if (ids.isEmpty()) {
248             throw new RuntimeException("Id list contained no ids!");
249         }
250         identifierLists.push(ids.toArray(new Identifier[]{}));
251     }
252 
253     public void inAIdlistIdlist(AIdlistIdlist node) {
254         inAIdIdlist(null); // same implementaion
255     }
256 
257     public void outAIdlistIdlist(AIdlistIdlist node) {
258         outAIdIdlist(null); // same implementation
259     }
260 
261     public void caseAUniversalFalsehoodId(AUniversalFalsehoodId node) {
262         identifiers.push(factory.createIRI(Constants.UNIV_FALSE));
263         node.replaceBy(null);
264     }
265 
266     public void caseAUniversalTruthId(AUniversalTruthId node) {
267         identifiers.push(factory.createIRI(Constants.UNIV_TRUE));
268         node.replaceBy(null);
269     }
270 }
271 
272 /*
273  * $Log$
274  * Revision 1.3  2006/05/04 14:17:09  alex_simov
275  * bugfix: invalid sQName parsing/serializing - illegal symbols not escaped
276  *
277  * Revision 1.2  2005/11/30 17:17:41  holgerlausen
278  * throwing parser exception with more information in case of relative IRIs
279  *
280  * Revision 1.1  2005/11/28 13:55:26  vassil_momtchev
281  * AST analyses
282  *
283 */