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.omwg.ontology.*;
33  import org.w3c.dom.*;
34  import org.wsmo.common.*;
35  import org.wsmo.common.Entity;
36  import org.wsmo.common.exception.*;
37  
38  import com.ontotext.wsmo4j.serializer.xml.*;
39  
40  /**
41   * Helper type to serialize/deserialize xml element instance
42   * 
43   * @author not attributable
44   */
45  class NodeInstance {
46      static Instance deserialize(Node xmlNode, WsmlXmlParser parser) throws InvalidModelException {
47          if (xmlNode == null || parser == null || xmlNode.getNodeName() != "instance"
48                  || xmlNode.getNodeType() != Node.ELEMENT_NODE) {
49              throw new IllegalArgumentException();
50          }
51  
52          IRI instanceIri = parser.getFactory()
53                  .createIRI(WsmlXmlHelper.getAttrValue(xmlNode, "name"));
54          Instance instance = parser.getFactory().createInstance(instanceIri);
55  
56          NodeList nodes = xmlNode.getChildNodes();
57          for (int i = 0; i < nodes.getLength(); i++) {
58              Node node = nodes.item(i);
59              if (node.getNodeName() == "memberOf" && node.getNodeType() == Node.ELEMENT_NODE) {
60                  IRI conceptIri = parser.getFactory().createIRI(WsmlXmlHelper.getElementText(node));
61                  instance.addConcept(parser.getFactory().getConcept(conceptIri));
62              }
63              else if (node.getNodeName() == "nonFunctionalProperties"
64                      && node.getNodeType() == Node.ELEMENT_NODE) {
65                  NodeNFP.deserialize(instance, node, parser);
66              }
67              else if (node.getNodeName() == "attributeValue"
68                      && node.getNodeType() == Node.ELEMENT_NODE) {
69                  IRI attrIri = parser.getFactory().createIRI(
70                          WsmlXmlHelper.getAttrValue(node, "name"));
71                  Value value = NodeValue.deserialize(WsmlXmlHelper.getFirstChildElement(node),
72                          parser);
73                  instance.addAttributeValue(attrIri, value);
74              }
75          }
76  
77          return instance;
78      }
79  
80      static Element serialize(Instance instance, WsmlXmlSerializer serializer) {
81          Element instanceElement = serializer.createElement("instance");
82          instanceElement.setAttribute("name", instance.getIdentifier().toString());
83  
84          if (!instance.listConcepts().isEmpty()) {
85              Object[] entities = instance.listConcepts().toArray();
86              for (int i = 0; i < entities.length; i++) {
87                  Element memberOf = serializer.createElement("memberOf");
88                  instanceElement.appendChild(memberOf);
89                  memberOf.appendChild(serializer.createTextNode(((Entity) entities[i])
90                          .getIdentifier().toString()));
91              }
92          }
93          if (!instance.listNFPValues().isEmpty()) {
94              NodeNFP.serialize(instanceElement, instance, serializer);
95          }
96          if (!instance.listAttributeValues().isEmpty()) {
97              for (Iterator i = instance.listAttributeValues().keySet().iterator(); i.hasNext();) {
98                  Identifier attrId = (Identifier) i.next();
99                  Element attributeValElement = serializer.createElement("attributeValue");
100                 attributeValElement.setAttribute("name", attrId.toString());
101                 instanceElement.appendChild(attributeValElement);
102 
103                 Set datavalues = instance.listAttributeValues(attrId);
104                 for (Iterator j = datavalues.iterator(); j.hasNext();) {
105                     attributeValElement.appendChild(NodeValue.serialize(j.next(), serializer));
106                 }
107             }
108         }
109 
110         return instanceElement;
111     }
112 }
113 
114 /*
115  * $Log$
116  * Revision 1.2  2006/02/10 15:06:23  vassil_momtchev
117  * addapted to the latest api changes
118  *
119  * Revision 1.1  2005/11/28 14:03:48  vassil_momtchev
120  * package refactored from com.ontotext.wsmo4j.xmlparser to com.ontotext.wsmo4j.parser.xml
121  *
122  * Revision 1.3  2005/09/16 14:02:45  alex_simov
123  * Identifier.asString() removed, use Object.toString() instead
124  * (Implementations MUST override toString())
125  *
126  * Revision 1.2  2005/08/08 08:24:40  vassil_momtchev
127  * javadoc added, bugfixes
128  *
129 */