View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-2005, 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  package com.ontotext.wsmo4j.parser.xml;
19  
20  /**
21   * <p>Title: WSMO4J</p>
22   * <p>Description: WSMO API and a Reference Implementation</p>
23   * <p>Copyright:  Copyright (c) 2004-2005</p>
24   * <p>Company: OntoText Lab. / SIRMA </p>
25   * @author not attributable
26   * @version 1.0
27   *
28   */
29  
30  import java.util.*;
31  
32  import org.deri.wsmo4j.io.serializer.xml.*;
33  import org.omwg.logicalexpression.*;
34  import org.omwg.ontology.*;
35  import org.w3c.dom.*;
36  import org.wsmo.common.*;
37  import org.wsmo.common.exception.*;
38  import org.wsmo.wsml.*;
39  
40  import com.ontotext.wsmo4j.serializer.xml.*;
41  
42  /**
43   * Helper type to serialize/deserialize xml elements with logical expressions
44   * 
45   * @author not attributable
46   */
47  class NodeLogExp {
48      static Axiom deserialize(Node xmlNode, WsmlXmlParser parser) throws InvalidModelException,
49              ParserException {
50          if (xmlNode == null || parser == null || xmlNode.getNodeType() != Node.ELEMENT_NODE) {
51              throw new IllegalArgumentException();
52          }
53  
54          IRI axiomIri = parser.getFactory().createIRI(WsmlXmlHelper.getAttrValue(xmlNode, "name"));
55          Axiom axiom = parser.getFactory().createAxiom(axiomIri);
56          NodeList nodes = xmlNode.getChildNodes();
57          for (int i = 0; i < nodes.getLength(); i++) {
58              Node node = nodes.item(i);
59              if (node.getNodeName() == "nonFunctionalProperties"
60                      && node.getNodeType() == Node.ELEMENT_NODE) {
61                  NodeNFP.deserialize(axiom, node, parser);
62              }
63              else if (node.getNodeName() == "definedBy" && node.getNodeType() == Node.ELEMENT_NODE) {
64                  //TODO better integration?
65                  NodeList xmlles = node.getChildNodes();
66                  for (int n=0; n<xmlles.getLength();n++){
67                      Node logexpnode = xmlles.item(n);
68                      if (logexpnode.getNodeType() == Node.ELEMENT_NODE){
69                          LogicalExpression le = parser.getLEParser().parse(xmlles.item(n));
70                          axiom.addDefinition(le);
71                      }
72                  }
73                  //axiom.addDefinition(parser.getLEFactory().createLogicalExpression(node.toString()));
74              }
75          }
76  
77          return axiom;
78      }
79  
80      static Element serialize(String elementName, Axiom axiom, WsmlXmlSerializer serializer) {
81          
82          Element logExpElement = serializer.createElement(elementName);
83          logExpElement.setAttribute("name", axiom.getIdentifier().toString());
84  
85          if (!axiom.listNFPValues().isEmpty()) {
86              NodeNFP.serialize(logExpElement, axiom, serializer);
87          }
88  
89          if (!axiom.listDefinitions().isEmpty()) {
90              Node definedByNode = serializer.createElement("definedBy");
91              logExpElement.appendChild(definedByNode);
92                  
93              for (Iterator i = axiom.listDefinitions().iterator(); i.hasNext();) {
94                  LogicalExpression logExp = (LogicalExpression) i.next();
95                  LogExprSerializerXML leSerXML = new LogExprSerializerXML(
96                          logExpElement.getOwnerDocument());
97                  Node leNode = leSerXML.serialize(logExp);
98                  definedByNode.appendChild(leNode);
99                  break; // schema allows only one definition
100             }
101         }
102 
103         return logExpElement;
104     }
105 }
106 
107 /*
108  * $Log$
109  * Revision 1.2  2006/01/13 10:01:17  vassil_momtchev
110  * defineBy element was missing in axiom serialization
111  *
112  * Revision 1.1  2005/11/28 14:03:48  vassil_momtchev
113  * package refactored from com.ontotext.wsmo4j.xmlparser to com.ontotext.wsmo4j.parser.xml
114  *
115  * Revision 1.9  2005/09/21 06:28:56  holgerlausen
116  * removing explicit factory creations and introducing parameter maps for parsers
117  *
118  * Revision 1.8  2005/09/20 19:41:02  holgerlausen
119  * removed superflouis interfaces for IO in logical expression (since intgeration not necessary)
120  *
121  * Revision 1.7  2005/09/16 14:02:45  alex_simov
122  * Identifier.asString() removed, use Object.toString() instead
123  * (Implementations MUST override toString())
124  *
125  * Revision 1.6  2005/09/08 20:20:06  holgerlausen
126  * fixed XML serialization LE integration
127  *
128  * Revision 1.5  2005/09/06 18:35:14  holgerlausen
129  * adopted parser serializer to use datatypefactory
130  *
131  * Revision 1.4  2005/09/02 13:32:44  ohamano
132  * move logicalexpression packages from ext to core
133  * move tests from logicalexpression.test to test module
134  *
135  * Revision 1.3  2005/08/31 09:24:22  vassil_momtchev
136  * add InvalidModelException and ParserException to LogicalExpressionFactory::createLogixExpression methods
137  *
138  * Revision 1.2  2005/08/08 08:24:40  vassil_momtchev
139  * javadoc added, bugfixes
140  *
141 */