Clover coverage report - Maven Clover report
Coverage timestamp: Tue Sep 16 2008 01:16:37 EEST
file stats: LOC: 163   Methods: 2
NCLOC: 88   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NodeValue.java 0% 0% 0% 0%
coverage
 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  0 static Value deserialize(Node xmlNode, WsmlXmlParser parser) {
 50  0 if (xmlNode == null || parser == null || xmlNode.getNodeName() != "value"
 51    || xmlNode.getNodeType() != Node.ELEMENT_NODE) {
 52  0 throw new IllegalArgumentException();
 53    }
 54   
 55  0 String type = xmlNode.getAttributes().getNamedItem("type").getNodeValue();
 56   
 57  0 if (conTran.isSimpleDataType(type)) { // SimpleDataType
 58  0 WsmlDataType dt = parser.getDataFactory().createWsmlDataType(type);
 59  0 String valueText = WsmlXmlHelper.getElementText(xmlNode).trim();
 60  0 return parser.getDataFactory().createDataValueFromJavaObject(dt, valueText);
 61    }
 62  0 else if (conTran.isDataType(type)) {
 63  0 ComplexDataType cdt = (ComplexDataType) parser.getDataFactory()
 64    .createWsmlDataType(type);
 65  0 NodeList nodes = xmlNode.getChildNodes();
 66  0 Vector <SimpleDataValue> argVal = new Vector <SimpleDataValue>();
 67  0 for (int i = 0; i < nodes.getLength(); i++) {
 68  0 Node node = nodes.item(i);
 69  0 if (node.getNodeType() == Node.ELEMENT_NODE
 70    && node.getNodeName().equals("argument")) {
 71  0 String val = WsmlXmlHelper.getElementText(node).trim();
 72   
 73    // try to guess the type of each argument
 74  0 try {
 75  0 Integer.parseInt(val);
 76  0 argVal.add(parser.getDataFactory().createWsmlInteger(new BigInteger(val)));
 77  0 continue;
 78    }
 79    catch (NumberFormatException e) {
 80    }
 81  0 try {
 82  0 Double.parseDouble(val);
 83  0 argVal.add(parser.getDataFactory().createWsmlDecimal(new BigDecimal(val)));
 84    }
 85    catch (NumberFormatException e1) {
 86  0 argVal.add(parser.getDataFactory().createWsmlString(val));
 87    }
 88    }
 89    }
 90  0 return parser.getDataFactory().createDataValue(cdt, argVal.toArray(new SimpleDataValue[] {}));
 91    }
 92    else { // Instance IRI
 93  0 return parser.getFactory().getInstance(parser.getFactory().createIRI(type));
 94    }
 95    }
 96   
 97  0 static Element serialize(Object value, WsmlXmlSerializer serializer) {
 98  0 if (value == null || serializer == null) {
 99  0 throw new IllegalArgumentException();
 100    }
 101   
 102  0 Element valueElement = serializer.createElement("value");
 103   
 104  0 if (value instanceof SimpleDataValue) {
 105  0 valueElement.setAttribute("type", ((SimpleDataValue) value).getType().getIRI()
 106    .toString());
 107  0 Text numVal = serializer
 108    .createTextNode(((SimpleDataValue) value).getValue().toString());
 109  0 valueElement.appendChild(numVal);
 110    }
 111  0 else if (value instanceof ComplexDataValue) {
 112  0 ComplexDataValue t = (ComplexDataValue) value;
 113  0 valueElement.setAttribute("type", t.getType().getIRI().toString());
 114  0 int nbParams = t.getArity();
 115  0 for (byte i = 0; i < nbParams; i++) {
 116  0 Element argumentElement = serializer.createElement("argument");
 117  0 argumentElement.appendChild(serializer.createTextNode(t.getArgumentValue(i)
 118    .toString()));
 119  0 valueElement.appendChild(argumentElement);
 120    }
 121    }
 122  0 else if (value instanceof IRI) {
 123  0 valueElement.setAttribute("type", ((IRI) value).toString());
 124    }
 125  0 else if (value instanceof Instance) {
 126  0 valueElement.setAttribute("type", ((Instance) value).getIdentifier().toString());
 127    }
 128    else {
 129  0 throw new RuntimeException("Invalid value to serialize!");
 130    }
 131   
 132  0 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    */