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.logicalexpression.*;
33  import org.omwg.ontology.*;
34  import org.w3c.dom.*;
35  import org.wsmo.common.*;
36  import org.wsmo.common.Entity;
37  import org.wsmo.common.exception.*;
38  
39  import com.ontotext.wsmo4j.serializer.xml.*;
40  
41  /**
42   * Helper type to serialize/deserialize xml element concept
43   * 
44   * @author not attributable
45   */
46  class NodeConcept {
47      static Concept deserialize(Node xmlNode, WsmlXmlParser parser) throws InvalidModelException {
48          if (xmlNode == null || parser == null || xmlNode.getNodeName() != "concept") {
49              throw new IllegalArgumentException();
50          }
51  
52          IRI iriConcept = parser.getFactory().createIRI(WsmlXmlHelper.getAttrValue(xmlNode, "name"));
53          Concept concept = parser.getFactory().createConcept(iriConcept);
54  
55          NodeList nodes = xmlNode.getChildNodes();
56          for (int i = 0; i < nodes.getLength(); i++) {
57              Node node = nodes.item(i);
58  
59              if (node.getNodeName() == "superConcept" && node.getNodeType() == Node.ELEMENT_NODE) {
60                  IRI scIri = parser.getFactory().createIRI(WsmlXmlHelper.getElementText(node));
61                  Concept superConcept = parser.getFactory().getConcept(scIri);
62                  concept.addSuperConcept(superConcept);
63                  superConcept.addSubConcept(concept);
64              }
65              else if (node.getNodeName() == "nonFunctionalProperties"
66                      && node.getNodeType() == Node.ELEMENT_NODE) {
67                  NodeNFP.deserialize(concept, node, parser);
68              }
69              else if (node.getNodeName() == "attribute" && node.getNodeType() == Node.ELEMENT_NODE) {
70                  IRI attrIri = parser.getFactory().createIRI(
71                          WsmlXmlHelper.getAttrValue(node, "name"));
72                  Attribute attr = concept.createAttribute(attrIri);
73                  String attrType = WsmlXmlHelper.getAttrValue(node, "type");
74  
75                  if (attrType.equals("inferring")) {
76                      attr.setConstraining(false);
77                  }
78                  else {
79                      attr.setConstraining(true);
80                  }
81  
82                  NodeList attrNodes = node.getChildNodes();
83                  IRI inverseAttrIRI = null;
84                  for (int j = 0; j < attrNodes.getLength(); j++) {
85                      Node attrNode = attrNodes.item(j);
86                      if (attrNode.getNodeName() == "range"
87                              && attrNode.getNodeType() == Node.ELEMENT_NODE) {
88                          ConstantTransformer cf = ConstantTransformer.getInstance();
89                          String type = WsmlXmlHelper.getElementText(attrNode);
90                          if (cf.isDataType(type)) {
91                              attr.addType(parser.getDataFactory().createWsmlDataType(type));
92                          }
93                          else {
94                              attr.addType(parser.getFactory().createConcept(
95                                      parser.getFactory().createIRI(type)));
96                          }
97                      }
98                      else if (attrNode.getNodeName() == "symmetric"
99                              && attrNode.getNodeType() == Node.ELEMENT_NODE) {
100                         attr.setSymmetric(true);
101                     }
102                     else if (attrNode.getNodeName() == "transitive"
103                             && attrNode.getNodeType() == Node.ELEMENT_NODE) {
104                         attr.setTransitive(true);
105                     }
106                     else if (attrNode.getNodeName() == "reflexive"
107                             && attrNode.getNodeType() == Node.ELEMENT_NODE) {
108                         attr.setReflexive(true);
109                     }
110                     else if (attrNode.getNodeName() == "inverseOf"
111                             && attrNode.getNodeType() == Node.ELEMENT_NODE) {
112                         inverseAttrIRI = parser.getFactory().createIRI(
113                                 WsmlXmlHelper.getAttrValue(attrNode, "type"));
114                         attr.setInverseOf(inverseAttrIRI);
115                     }
116                     else if (attrNode.getNodeName() == "minCardinality"
117                             && attrNode.getNodeType() == Node.ELEMENT_NODE) {
118                         attr.setMinCardinality(Integer.parseInt(WsmlXmlHelper
119                                 .getElementText(attrNode)));
120                     }
121                     else if (attrNode.getNodeName() == "maxCardinality"
122                             && attrNode.getNodeType() == Node.ELEMENT_NODE) {
123                         attr.setMaxCardinality(Integer.parseInt(WsmlXmlHelper
124                                 .getElementText(attrNode)));
125                     }
126                     else if (attrNode.getNodeName() == "nonFunctionalProperties"
127                             && attrNode.getNodeType() == Node.ELEMENT_NODE) {
128                         NodeNFP.deserialize(attr, attrNode, parser);
129                     }
130                 }
131             }
132         }
133 
134         return concept;
135     }
136 
137     static Element serialize(Concept concept, WsmlXmlSerializer serializer) {
138         Element conceptElement = serializer.createElement("concept");
139         conceptElement.setAttribute("name", concept.getIdentifier().toString());
140 
141         if (!concept.listSuperConcepts().isEmpty()) {
142             Object[] entities = concept.listSuperConcepts().toArray();
143             for (int i = 0; i < entities.length; i++) {
144                 Element superConcept = serializer.createElement("superConcept");
145                 conceptElement.appendChild(superConcept);
146                 superConcept.appendChild(serializer.createTextNode(((Entity) entities[i])
147                         .getIdentifier().toString()));
148             }
149         }
150         if (!concept.listNFPValues().isEmpty()) {
151             NodeNFP.serialize(conceptElement, concept, serializer);
152         }
153         if (!concept.listAttributes().isEmpty()) {
154             Object[] attrList = concept.listAttributes().toArray();
155             for (int i = 0; i < attrList.length; i++) {
156                 Element attrElement = serializer.createElement("attribute");
157                 conceptElement.appendChild(attrElement);
158 
159                 Attribute attribute = (Attribute) attrList[i];
160                 attrElement.setAttribute("name", attribute.getIdentifier().toString());
161                 attrElement.setAttribute("type", attribute.isConstraining() ? "constraining"
162                         : "inferring");
163 
164                 if (!attribute.listTypes().isEmpty()) {
165                     Element range = serializer.createElement("range");
166                     attrElement.appendChild(range);
167                     for (Iterator j = attribute.listTypes().iterator(); j.hasNext();) {
168                         Object o  = j.next();
169                         if (o instanceof Concept)
170                             range.appendChild(serializer.createTextNode(((Concept) o)
171                                     .getIdentifier().toString()));
172                         else if (o instanceof WsmlDataType) {
173                             range.appendChild(serializer.createTextNode(
174                                     ((WsmlDataType) o).getIRI().toString()));                         
175                         }
176                         else {
177                             throw new RuntimeException("Unknown range for attribute!");
178                         }
179                         break; // only 1 is allowed in the schema?!
180                     }
181                 }
182 
183                 if (attribute.isSymmetric()) {
184                     attrElement.appendChild(serializer.createElement("symmetric"));
185                 }
186                 if (attribute.isTransitive()) {
187                     attrElement.appendChild(serializer.createElement("transitive"));
188                 }
189                 if (attribute.isReflexive()) {
190                     attrElement.appendChild(serializer.createElement("reflexive"));
191                 }
192                 if (attribute.getInverseOf() != null) {
193                     Element inverseOf = serializer.createElement("inverseOf");
194                     attrElement.appendChild(inverseOf);
195                     inverseOf.setAttribute("type", attribute.getInverseOf().toString());
196                 }
197                 if (attribute.getMinCardinality() > 0) {
198                     Element minCardinality = serializer.createElement("minCardinality");
199                     attrElement.appendChild(minCardinality);
200                     minCardinality.appendChild(serializer.createTextNode(String.valueOf(attribute
201                             .getMinCardinality())));
202                 }
203                 if (attribute.getMaxCardinality() < Integer.MAX_VALUE) {
204                     Element maxCardinality = serializer.createElement("maxCardinality");
205                     attrElement.appendChild(maxCardinality);
206                     maxCardinality.appendChild(serializer.createTextNode(String.valueOf(attribute
207                             .getMaxCardinality())));
208                 }
209                 if (!attribute.listNFPValues().isEmpty()) {
210                     NodeNFP.serialize(attrElement, attribute, serializer);
211                 }
212             }
213         }
214 
215         assert conceptElement != null;
216         return conceptElement;
217     }
218 }
219 
220 /*
221  * $Log$
222  * Revision 1.6  2006/07/04 14:30:30  vassil_momtchev
223  * datatype parsing problem fixed
224  *
225  * Revision 1.5  2006/03/29 11:20:51  vassil_momtchev
226  * mediator support added; some code refactored; minor bugs fixed
227  *
228  * Revision 1.4  2006/02/16 09:56:50  vassil_momtchev
229  * setInverseOf(Attribute) changed to setInverseOf(Identifier)
230  *
231  * Revision 1.3  2006/02/10 15:06:23  vassil_momtchev
232  * addapted to the latest api changes
233  *
234  * Revision 1.2  2006/01/17 11:51:53  vassil_momtchev
235  * range can be WsmlDataType also
236  *
237  * Revision 1.1  2005/11/28 14:03:48  vassil_momtchev
238  * package refactored from com.ontotext.wsmo4j.xmlparser to com.ontotext.wsmo4j.parser.xml
239  *
240  * Revision 1.4  2005/09/16 14:02:45  alex_simov
241  * Identifier.asString() removed, use Object.toString() instead
242  * (Implementations MUST override toString())
243  *
244  * Revision 1.3  2005/09/01 14:56:59  vassil_momtchev
245  * createAttribute method moved from WsmoFactory to Concept interface
246  *
247  * Revision 1.2  2005/08/08 08:24:40  vassil_momtchev
248  * javadoc added, bugfixes
249  *
250 */