Clover coverage report - Maven Clover report
Coverage timestamp: Tue Sep 16 2008 01:16:37 EEST
file stats: LOC: 283   Methods: 20
NCLOC: 197   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
IdentifierAnalysis.java 0% 0% 0% 0%
coverage
 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  0 public IdentifierAnalysis(ASTAnalysisContainer container, WsmoFactory factory) {
 48  0 if (factory == null || container == null) {
 49  0 throw new IllegalArgumentException();
 50    }
 51  0 this.factory = factory;
 52  0 this.container = container;
 53   
 54    // register the handled nodes for namespaces
 55  0 container.registerNodeHandler(ADefaultnsPrefixdefinitionlist.class, this);
 56  0 container.registerNodeHandler(ADefaultPrefixdefinition.class, this);
 57  0 container.registerNodeHandler(ANamespacedefPrefixdefinition.class, this);
 58   
 59    // register the handled nodes for ids
 60  0 container.registerNodeHandler(AAnonymousId.class, this);
 61  0 container.registerNodeHandler(AIriId.class, this);
 62  0 container.registerNodeHandler(AIriIri.class, this);
 63  0 container.registerNodeHandler(AAnySqname.class, this);
 64  0 container.registerNodeHandler(AIdIdlist.class, this);
 65  0 container.registerNodeHandler(AIdlistIdlist.class, this);
 66  0 container.registerNodeHandler(AUniversalTruthId.class, this);
 67  0 container.registerNodeHandler(AUniversalFalsehoodId.class, this);
 68  0 container.registerNodeHandler(PId.class, this);
 69   
 70  0 namespaces = container.getStack(Namespace.class);
 71  0 identifiers = container.getStack(Identifier.class);
 72  0 identifierLists = container.getStack(Identifier[].class);
 73    }
 74   
 75  0 public void setNameSpaces(HashMap ns){
 76  0 this.nsPrefixCache = ns;
 77    }
 78   
 79    // Hanlde namespaces
 80   
 81  0 public void inADefaultnsPrefixdefinitionlist(ADefaultnsPrefixdefinitionlist node1) {
 82  0 String namespaceIRI = _strip(node1.getFullIri().getText().trim());
 83  0 String namespacePrefix = "_"; // this is always the default namespace prefix
 84  0 Namespace namespace = factory.createNamespace(namespacePrefix,
 85    factory.createIRI(namespaceIRI));
 86  0 namespaces.push(namespace);
 87  0 nsPrefixCache.put(namespacePrefix, namespaceIRI);
 88    }
 89   
 90  0 public void inADefaultPrefixdefinition(ADefaultPrefixdefinition node1) {
 91  0 String namespaceIRI = _strip(node1.getFullIri().getText().trim());
 92  0 String namespacePrefix = "_"; // this is always the default namespace prefix
 93  0 Namespace namespace = factory.createNamespace(namespacePrefix,
 94    factory.createIRI(namespaceIRI));
 95  0 Stack namespaces = container.getStack(Namespace.class);
 96  0 namespaces.push(namespace);
 97  0 nsPrefixCache.put(namespacePrefix, namespaceIRI);
 98    }
 99   
 100  0 public void inANamespacedefPrefixdefinition(ANamespacedefPrefixdefinition node1) {
 101  0 String namespaceIRI = _strip(node1.getFullIri().getText().trim());
 102  0 String namespacePrefix = node1.getName().getText().trim();
 103  0 Namespace namespace = factory.createNamespace(unEscape(namespacePrefix),
 104    factory.createIRI(namespaceIRI));
 105  0 Stack namespaces = container.getStack(Namespace.class);
 106  0 namespaces.push(namespace);
 107  0 nsPrefixCache.put(namespacePrefix, namespaceIRI);
 108    }
 109   
 110    // Handle identifier
 111   
 112  0 public void caseAIriId(AIriId node) {
 113  0 if (node.getIri() != null)
 114  0 node.getIri().apply(this);
 115    }
 116   
 117  0 public void caseASqnameIri(ASqnameIri node) {
 118  0 if (node.getSqname() != null)
 119  0 node.getSqname().apply(this);
 120    }
 121   
 122  0 public void caseAAnonymousId(AAnonymousId node) {
 123  0 identifiers.push(factory.createAnonymousID());
 124  0 node.replaceBy(null);
 125    }
 126   
 127  0 public void caseAIriIri(AIriIri node) {
 128  0 try{
 129  0 identifiers.push(factory.createIRI(_strip(node.getFullIri().getText())));
 130    }catch (IllegalArgumentException e){
 131  0 ParserException pe = new ParserException("relative IRIs are not allowed", null);
 132  0 Token t = node.getFullIri();
 133  0 pe.setErrorLine(t.getLine());
 134  0 pe.setErrorPos(t.getPos());
 135  0 pe.setFoundToken(t.getText());
 136  0 throw new WrappedParsingException(pe);
 137    }
 138  0 node.replaceBy(null);
 139    }
 140   
 141  0 public void caseALocalkeywordSqname(ALocalkeywordSqname node) {
 142    //TODO: Check why there special Sqname {localkeyword} case in wsml grammar??
 143  0 TName tn = new TName(node.getAnykeyword().toString());
 144  0 String prefix = null;
 145  0 if ((APrefix)node.getPrefix()!=null){
 146  0 tn = ((APrefix)node.getPrefix()).getName();
 147  0 prefix = tn.getText().trim();
 148    }
 149  0 sQNameHandler(new TName(node.getAnykeyword().toString()).getText().trim(),
 150    prefix, tn);
 151  0 node.replaceBy(null);
 152    }
 153   
 154    // copy pasted from IdentifierProcessing.java QName inner class
 155   
 156  0 public void caseAAnySqname(AAnySqname node) {
 157  0 TName tn = node.getName();
 158  0 String prefix = null;
 159  0 if ((APrefix)node.getPrefix()!=null){
 160  0 tn = ((APrefix)node.getPrefix()).getName();
 161  0 prefix = tn.getText().trim();
 162    }
 163  0 sQNameHandler(node.getName().getText().trim(),prefix,tn);
 164  0 node.replaceBy(null);
 165    }
 166   
 167  0 private void sQNameHandler(String name, String prefix, TName tn) {
 168    //built-ins
 169  0 String iri = ConstantTransformer.getInstance().findIri(name);
 170  0 if (iri != null) {
 171  0 identifiers.push(factory.createIRI(iri));
 172  0 return;
 173    }
 174   
 175    //check on non existing or non sufficient ns container
 176  0 String error = null;
 177  0 if (prefix == null && nsPrefixCache.get("_") == null) {
 178  0 error= ParserException.NO_NAMESPACE;
 179    }
 180  0 else if (prefix != null && nsPrefixCache.get(prefix) == null) {
 181  0 error = ParserException.NAMESPACE_PREFIX_NOT_FOUND;
 182    }
 183  0 if (error != null) {
 184  0 ParserException e = new ParserException(error, null);
 185  0 e.setErrorLine(tn.getLine());
 186  0 e.setErrorPos(tn.getPos());
 187  0 if (prefix != null){
 188  0 e.setFoundToken(prefix+"#"+name);
 189    }else{
 190  0 e.setFoundToken(name);
 191    }
 192  0 throw new WrappedParsingException(e);
 193    }
 194   
 195    //create IRI
 196  0 String namespace = null;
 197  0 if (prefix !=null){
 198  0 namespace = (String) nsPrefixCache.get(prefix);
 199    }
 200    else {
 201  0 namespace = (String) nsPrefixCache.get("_");
 202    }
 203   
 204  0 identifiers.push(factory.createIRI(namespace + unEscape(name)));
 205    }
 206   
 207  0 public static String unEscape(String localname) {
 208    //remove \ from input.
 209  0 if (localname.indexOf('\\') == -1) {
 210  0 return localname;
 211    }
 212  0 StringBuffer result = new StringBuffer(localname.length());
 213  0 for (int i = 0; i < localname.length(); i++) {
 214  0 if (localname.charAt(i) != '\\') {
 215  0 result.append(localname.charAt(i));
 216    }
 217    }
 218  0 return result.toString();
 219    }
 220   
 221    // copy pasted from QNameProcessing.java
 222   
 223  0 private String _strip(String str) {
 224  0 if (null == str || str.length() < 3) return str;
 225  0 return str.substring(2, str.length() - 1);
 226    }
 227   
 228    // handle Identifier list
 229   
 230    Object lastItem;
 231   
 232  0 public void inAIdIdlist(AIdIdlist node) {
 233  0 if (identifiers.isEmpty()) {
 234  0 lastItem = null;
 235    }
 236    else {
 237  0 lastItem = identifiers.peek();
 238    }
 239    }
 240   
 241  0 public void outAIdIdlist(AIdIdlist node) {
 242  0 Vector ids = new Vector();
 243  0 while (!identifiers.isEmpty() &&
 244    identifiers.peek() != lastItem) {
 245  0 ids.add(0, identifiers.pop());
 246    }
 247  0 if (ids.isEmpty()) {
 248  0 throw new RuntimeException("Id list contained no ids!");
 249    }
 250  0 identifierLists.push(ids.toArray(new Identifier[]{}));
 251    }
 252   
 253  0 public void inAIdlistIdlist(AIdlistIdlist node) {
 254  0 inAIdIdlist(null); // same implementaion
 255    }
 256   
 257  0 public void outAIdlistIdlist(AIdlistIdlist node) {
 258  0 outAIdIdlist(null); // same implementation
 259    }
 260   
 261  0 public void caseAUniversalFalsehoodId(AUniversalFalsehoodId node) {
 262  0 identifiers.push(factory.createIRI(Constants.UNIV_FALSE));
 263  0 node.replaceBy(null);
 264    }
 265   
 266  0 public void caseAUniversalTruthId(AUniversalTruthId node) {
 267  0 identifiers.push(factory.createIRI(Constants.UNIV_TRUE));
 268  0 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    */