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.math.*;
31  import java.util.*;
32  
33  import org.deri.wsmo4j.logicalexpression.*;
34  import org.omwg.ontology.*;
35  import org.w3c.dom.*;
36  import org.wsmo.common.*;
37  
38  import com.ontotext.wsmo4j.serializer.xml.*;
39  
40  /**
41   * Helper type to serialize/deserialize xml element of data values
42   * 
43   * @author not attributable
44   */
45  class NodeValue {
46  
47      private static ConstantTransformer conTran = ConstantTransformer.getInstance();
48  
49      static Value deserialize(Node xmlNode, WsmlXmlParser parser) {
50          if (xmlNode == null || parser == null || xmlNode.getNodeName() != "value"
51                  || xmlNode.getNodeType() != Node.ELEMENT_NODE) {
52              throw new IllegalArgumentException();
53          }
54  
55          String type = xmlNode.getAttributes().getNamedItem("type").getNodeValue();
56          
57          if (conTran.isSimpleDataType(type)) { // SimpleDataType
58              WsmlDataType dt = parser.getDataFactory().createWsmlDataType(type);
59              String valueText = WsmlXmlHelper.getElementText(xmlNode).trim();
60              return parser.getDataFactory().createDataValueFromJavaObject(dt, valueText);
61          }
62          else if (conTran.isDataType(type)) {
63              ComplexDataType cdt = (ComplexDataType) parser.getDataFactory()
64                      .createWsmlDataType(type);
65              NodeList nodes = xmlNode.getChildNodes();
66              Vector <SimpleDataValue> argVal = new Vector <SimpleDataValue>();
67              for (int i = 0; i < nodes.getLength(); i++) {
68                  Node node = nodes.item(i);
69                  if (node.getNodeType() == Node.ELEMENT_NODE
70                          && node.getNodeName().equals("argument")) {
71                      String val = WsmlXmlHelper.getElementText(node).trim();
72  
73                      // try to guess the type of each argument
74                      try {
75                          Integer.parseInt(val);
76                          argVal.add(parser.getDataFactory().createWsmlInteger(new BigInteger(val)));
77                          continue;
78                      }
79                      catch (NumberFormatException e) {
80                      }
81                      try {
82                          Double.parseDouble(val);
83                          argVal.add(parser.getDataFactory().createWsmlDecimal(new BigDecimal(val)));
84                      }
85                      catch (NumberFormatException e1) {
86                          argVal.add(parser.getDataFactory().createWsmlString(val));
87                      }
88                  }
89              }
90              return parser.getDataFactory().createDataValue(cdt, argVal.toArray(new SimpleDataValue[] {}));
91          }
92          else { // Instance IRI
93              return parser.getFactory().getInstance(parser.getFactory().createIRI(type));
94          }
95      }
96  
97      static Element serialize(Object value, WsmlXmlSerializer serializer) {
98          if (value == null || serializer == null) {
99              throw new IllegalArgumentException();
100         }
101 
102         Element valueElement = serializer.createElement("value");
103 
104         if (value instanceof SimpleDataValue) {
105             valueElement.setAttribute("type", ((SimpleDataValue) value).getType().getIRI()
106                     .toString());
107             Text numVal = serializer
108                     .createTextNode(((SimpleDataValue) value).getValue().toString());
109             valueElement.appendChild(numVal);
110         }
111         else if (value instanceof ComplexDataValue) {
112             ComplexDataValue t = (ComplexDataValue) value;
113             valueElement.setAttribute("type", t.getType().getIRI().toString());
114             int nbParams = t.getArity();
115             for (byte i = 0; i < nbParams; i++) {
116                 Element argumentElement = serializer.createElement("argument");
117                 argumentElement.appendChild(serializer.createTextNode(t.getArgumentValue(i)
118                         .toString()));
119                 valueElement.appendChild(argumentElement);
120             }
121         }
122         else if (value instanceof IRI) {
123             valueElement.setAttribute("type", ((IRI) value).toString());
124         }
125         else if (value instanceof Instance) {
126             valueElement.setAttribute("type", ((Instance) value).getIdentifier().toString());
127         }
128         else {
129             throw new RuntimeException("Invalid value to serialize!");
130         }
131 
132         return valueElement;
133     }
134 }
135 
136 /*
137  * $Log$
138  * Revision 1.4  2007/04/02 12:13:22  morcen
139  * Generics support added to wsmo-api, wsmo4j and wsmo-test
140  *
141  * Revision 1.3  2006/01/17 11:52:16  vassil_momtchev
142  * value can be instance also
143  *
144  * Revision 1.2  2006/01/13 10:00:01  vassil_momtchev
145  * fixed sf:1401579 and sf:1401577
146  *
147  * Revision 1.1  2005/11/28 14:03:48  vassil_momtchev
148  * package refactored from com.ontotext.wsmo4j.xmlparser to com.ontotext.wsmo4j.parser.xml
149  *
150  * Revision 1.5  2005/09/17 08:56:25  vassil_momtchev
151  * Factory.createDataFactory(map) replaced new DataFactoryImpl()
152  *
153  * Revision 1.4  2005/09/16 14:02:45  alex_simov
154  * Identifier.asString() removed, use Object.toString() instead
155  * (Implementations MUST override toString())
156  *
157  * Revision 1.3  2005/09/06 18:35:14  holgerlausen
158  * adopted parser serializer to use datatypefactory
159  *
160  * Revision 1.2  2005/08/08 08:24:40  vassil_momtchev
161  * javadoc added, bugfixes
162  *
163  */