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.exception.*;
36  
37  import com.ontotext.wsmo4j.serializer.xml.*;
38  
39  /**
40   * Helper type to serialize/deserialize xml element relation instance
41   * 
42   * @author not attributable
43   */
44  class NodeRelationInstance {
45      static RelationInstance deserialize(Node xmlNode, WsmlXmlParser parser)
46              throws InvalidModelException {
47          if (xmlNode == null || parser == null || xmlNode.getNodeName() != "relationInstance"
48                  || xmlNode.getNodeType() != Node.ELEMENT_NODE) {
49              throw new IllegalArgumentException();
50          }
51  
52          Identifier relInstanceId = null;
53          RelationInstance relInstance = null;
54          if (xmlNode.getAttributes().getNamedItem("name") == null)
55              relInstanceId = parser.getFactory().createAnonymousID();
56          else {
57              relInstanceId = parser.getFactory().createIRI(
58                  WsmlXmlHelper.getAttrValue(xmlNode, "name"));
59          }
60  
61          NodeList nodes = xmlNode.getChildNodes();
62          byte paramArity = 0;
63          for (int i = 0; i < nodes.getLength(); i++) {
64              Node node = nodes.item(i);
65              if (node.getNodeName() == "memberOf" && node.getNodeType() == Node.ELEMENT_NODE) {
66                  Identifier relationId = null;
67                  if (WsmlXmlHelper.getElementText(node).equals("_#"))
68                      relationId = parser.getFactory().createAnonymousID();
69                  else    
70                      relationId = parser.getFactory().createIRI(WsmlXmlHelper.getElementText(node));
71                  relInstance = parser.getFactory().createRelationInstance(relInstanceId,
72                          parser.getFactory().getRelation(relationId));
73              }
74              else if (node.getNodeName() == "nonFunctionalProperties"
75                      && node.getNodeType() == Node.ELEMENT_NODE) {
76                  NodeNFP.deserialize(relInstance, node, parser);
77              }
78              else if (node.getNodeName() == "parameterValue"
79                      && node.getNodeType() == Node.ELEMENT_NODE) {                
80                  NodeList childNodes = node.getChildNodes();
81                  for (int j = 0; j < childNodes.getLength(); j++) {
82                      Node childNode = childNodes.item(j);
83                      if (childNode.getNodeName() == "value") {
84                          relInstance.setParameterValue(paramArity, 
85                                  NodeValue.deserialize(childNode, parser));
86                      }
87                  }
88              }
89          }
90  
91          return relInstance;
92      }
93  
94      static Element serialize(RelationInstance relInstance, WsmlXmlSerializer serializer) {
95          Element relInstanceElement = serializer.createElement("relationInstance");
96  
97          if (!relInstance.listNFPValues().isEmpty()) {
98              NodeNFP.serialize(relInstanceElement, relInstance, serializer);
99          }
100 
101         if (relInstance.getRelation() != null) {
102             Element memberOfElement = serializer.createElement("memberOf");
103             memberOfElement.appendChild(serializer.createTextNode(
104                     relInstance.getIdentifier().toString()));
105             relInstanceElement.appendChild(memberOfElement);
106         }
107 
108         if (!relInstance.listParameterValues().isEmpty()) {
109             Element parameterElement = serializer.createElement("parameterValue");
110             for (Iterator i = relInstance.listParameterValues().iterator(); i.hasNext();) {
111                 parameterElement.appendChild(NodeValue.serialize(i.next(), serializer));
112             }
113         }
114 
115         return relInstanceElement;
116     }
117 }
118 
119 /*
120  * $Log$
121  * Revision 1.2  2006/03/29 11:20:51  vassil_momtchev
122  * mediator support added; some code refactored; minor bugs fixed
123  *
124  * Revision 1.1  2005/11/28 14:03:48  vassil_momtchev
125  * package refactored from com.ontotext.wsmo4j.xmlparser to com.ontotext.wsmo4j.parser.xml
126  *
127  * Revision 1.5  2005/09/16 14:02:45  alex_simov
128  * Identifier.asString() removed, use Object.toString() instead
129  * (Implementations MUST override toString())
130  *
131  * Revision 1.4  2005/09/13 09:01:22  vassil_momtchev
132  * use WsmoFactory.createRelationInstance(Identifier, Relation) instead of WsmoFactory.createRelationInstance(Identifier)  RelationInstance.setRelation(Relation)
133  *
134  * Revision 1.3  2005/08/08 08:24:40  vassil_momtchev
135  * javadoc added, bugfixes
136  *
137 */