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 org.omwg.ontology.*;
31  import org.w3c.dom.*;
32  import org.wsmo.common.*;
33  import org.wsmo.common.exception.*;
34  import org.wsmo.wsml.*;
35  
36  import com.ontotext.wsmo4j.serializer.xml.*;
37  
38  /**
39   * Helper type to serialize/deserialize xml element ontology
40   * 
41   * @author not attributable
42   */
43  class NodeOntology {
44  
45      static Ontology deserialize(Node xmlNode, WsmlXmlParser parser) throws InvalidModelException,
46              ParserException {
47          if (parser == null || xmlNode == null || xmlNode.getNodeName() != "ontology") {
48              throw new IllegalArgumentException();
49          }
50  
51          IRI iri = parser.getFactory().createIRI(WsmlXmlHelper.getAttrValue(xmlNode, "name"));
52          Ontology ontology = parser.getFactory().createOntology(iri);
53          NodeTopEntity.processTopEntityNode(ontology, xmlNode, parser);
54  
55          NodeList nodes = xmlNode.getChildNodes();
56          for (int i = 0; i < nodes.getLength(); i++) {
57              Node node = nodes.item(i);
58              if (node.getNodeName() == "nonFunctionalProperties") {
59                  NodeNFP.deserialize(ontology, node, parser);
60              }
61              else if (node.getNodeName() == "concept") {
62                  ontology.addConcept(NodeConcept.deserialize(node, parser));
63              }
64              else if (node.getNodeName() == "relation") {
65                  ontology.addRelation(NodeRelation.deserialize(node, parser));
66              }
67              else if (node.getNodeName() == "instance") {
68                  ontology.addInstance(NodeInstance.deserialize(node, parser));
69              }
70              else if (node.getNodeName() == "relationInstance") {
71                  ontology.addRelationInstance(NodeRelationInstance.deserialize(node, parser));
72              }
73              else if (node.getNodeName() == "axiom") {
74                  ontology.addAxiom(NodeLogExp.deserialize(node, parser));
75              }
76          }
77  
78          return ontology;
79      }
80  
81      static Element serialize(Ontology ontology, WsmlXmlSerializer serializer) {
82          Element ontologyElement = serializer.createElement("ontology");
83          ontologyElement.setAttribute("name", ontology.getIdentifier().toString());
84          NodeTopEntity.serializeTopEntity(ontologyElement, ontology, serializer);
85  
86          if (!ontology.listNFPValues().isEmpty()) {
87              NodeNFP.serialize(ontologyElement, ontology, serializer);
88          }
89  
90          if (!ontology.listConcepts().isEmpty()) {
91              Object[] concepts = ontology.listConcepts().toArray();
92              for (int i = 0; i < concepts.length; i++) {
93                  ontologyElement.appendChild(NodeConcept
94                          .serialize((Concept) concepts[i], serializer));
95              }
96          }
97  
98          if (!ontology.listInstances().isEmpty()) {
99              Object[] instances = ontology.listInstances().toArray();
100             for (int i = 0; i < instances.length; i++) {
101                 ontologyElement.appendChild(NodeInstance.serialize((Instance) instances[i],
102                         serializer));
103             }
104         }
105 
106         if (!ontology.listRelations().isEmpty()) {
107             Object[] relations = ontology.listRelations().toArray();
108             for (int i = 0; i < relations.length; i++)
109                 ontologyElement.appendChild(NodeRelation.serialize((Relation) relations[i],
110                         serializer));
111         }
112 
113         if (!ontology.listRelationInstances().isEmpty()) {
114             Object[] relationInstances = ontology.listRelationInstances().toArray();
115             for (int i = 0; i < relationInstances.length; i++) {
116                 ontologyElement.appendChild(NodeRelationInstance.serialize(
117                         (RelationInstance) relationInstances[i], serializer));
118             }
119         }
120 
121         if (!ontology.listAxioms().isEmpty()) {
122             Object[] axioms = ontology.listAxioms().toArray();
123             for (int i = 0; i < axioms.length; i++) {
124                 ontologyElement.appendChild(
125                         NodeLogExp.serialize("axiom", (Axiom) axioms[i], serializer));
126             }
127         }
128 
129         return ontologyElement;
130     }
131 }
132 
133 /*
134  * $Log$
135  * Revision 1.2  2006/03/29 11:20:51  vassil_momtchev
136  * mediator support added; some code refactored; minor bugs fixed
137  *
138  * Revision 1.1  2005/11/28 14:03:48  vassil_momtchev
139  * package refactored from com.ontotext.wsmo4j.xmlparser to com.ontotext.wsmo4j.parser.xml
140  *
141  * Revision 1.5  2005/09/16 14:02:45  alex_simov
142  * Identifier.asString() removed, use Object.toString() instead
143  * (Implementations MUST override toString())
144  *
145  * Revision 1.4  2005/08/31 09:24:22  vassil_momtchev
146  * add InvalidModelException and ParserException to LogicalExpressionFactory::createLogixExpression methods
147  *
148  * Revision 1.3  2005/08/08 08:24:40  vassil_momtchev
149  * javadoc added, bugfixes
150  *
151 */