View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2006, University of Innsbruck, Austria
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 org.deri.wsmo4j.io.serializer.rdf;
19  
20  import java.math.*;
21  
22  import org.omwg.ontology.*;
23  import org.openrdf.model.*;
24  import org.openrdf.model.Value;
25  import org.openrdf.vocabulary.*;
26  import org.wsmo.common.*;
27  
28  /**
29   * Helper type to serialize/deserialize xml element of data values
30   * 
31   * @author not attributable
32   */
33  class NodeValue {
34  
35      static Value serialize(Object value, WsmlRdfSerializer serializer) {
36          if (value == null || serializer == null) {
37              throw new IllegalArgumentException();
38          }
39          Value valueElement = null;
40  
41          if (value instanceof SimpleDataValue) {
42              URI type = serializer.getFactory().createURI(
43                      XmlSchema.NAMESPACE +
44                      ((SimpleDataValue) value).getType().getIRI().getLocalName().toString());
45              String val = ((SimpleDataValue) value).getValue().toString();
46              valueElement = serializer.getFactory().createLiteral(val,type);
47          }
48          else if (value instanceof ComplexDataValue) {
49              ComplexDataValue t = (ComplexDataValue) value;
50              URI type = serializer.getFactory().createURI(
51                      XmlSchema.NAMESPACE +
52                      t.getType().getIRI().getLocalName().toString());
53              
54              valueElement = serializer.getFactory().createLiteral(
55                      serializeComplexDataValue(t),type);
56          }
57          else if (value instanceof IRI) {
58              valueElement = serializer.getFactory().createURI(((IRI)value).toString());
59          }
60          else if (value instanceof Instance) {
61              valueElement = serializer.getFactory().createURI(
62                      ((Instance)value).getIdentifier().toString());
63          }
64          else {
65              throw new RuntimeException("Invalid value to serialize!");
66          }
67  
68          return valueElement;
69      }
70      
71      private static String serializeComplexDataValue(ComplexDataValue val) {
72          if(val.getType().getIRI().toString().equals(WsmlDataType.WSML_DATE)){
73              return serializeDate(val);
74          } else if(val.getType().getIRI().toString().equals(WsmlDataType.WSML_FLOAT)) {
75              return val.getArgumentValue((byte)0).getValue().toString();
76          } else if(val.getType().getIRI().toString().equals(WsmlDataType.WSML_DOUBLE)) {
77              return val.getArgumentValue((byte)0).getValue().toString();
78          } else if(val.getType().getIRI().toString().equals(WsmlDataType.WSML_BOOLEAN)) {
79              return val.getArgumentValue((byte)0).getValue().toString();
80          } else if(val.getType().getIRI().toString().equals(WsmlDataType.WSML_DATETIME)) {
81              return serializeDateTime(val);
82          } else if(val.getType().getIRI().toString().equals(WsmlDataType.WSML_TIME )) {
83              return serializeTime(val);
84          } else {
85              int nbParams = val.getArity();
86              StringBuffer buf = new StringBuffer();
87              for (byte i = 0; i < nbParams; i++) {
88                  buf.append(val.getArgumentValue(i).toString()+" ");
89              }
90              return buf.toString();
91          }
92      }
93      
94      private static String serializeDate(ComplexDataValue val) {
95          String ret = val.getArgumentValue((byte)0).getValue() + "-" +
96              ensure00(val.getArgumentValue((byte)1).getValue().toString()) + "-" +
97              ensure00(val.getArgumentValue((byte)2).getValue().toString());
98          switch(val.getArity()) {
99              case 4:
100                 return ret + ensure00(serializeTimezone(
101                     val.getArgumentValue((byte)3).getValue(),null));
102             case 5:
103                 return ret + ensure00(serializeTimezone(
104                         val.getArgumentValue((byte)3).getValue(),
105                         val.getArgumentValue((byte)4).getValue()));
106         }
107         return ret;
108     }
109     
110     private static String serializeTime(ComplexDataValue val) {
111         String ret =  ensure00(val.getArgumentValue((byte)0).getValue().toString()) + ":" +
112             ensure00(val.getArgumentValue((byte)1).getValue().toString()) + ":" +
113             ensure00(val.getArgumentValue((byte)2).getValue().toString());
114         switch(val.getArity()) {
115             case 4:
116                 return ret + ensure00(serializeTimezone(
117                     val.getArgumentValue((byte)3).getValue(),null));
118             case 5:
119                 return ret + ensure00(serializeTimezone(
120                         val.getArgumentValue((byte)3).getValue(),
121                         val.getArgumentValue((byte)4).getValue()));
122         }
123         return ret;
124     }
125     
126     private static String serializeDateTime(ComplexDataValue val) {
127         String ret =  val.getArgumentValue((byte)0).getValue() + "-" +
128             ensure00(val.getArgumentValue((byte)1).getValue().toString()) + "-" +
129             ensure00(val.getArgumentValue((byte)2).getValue().toString()) + "T" +
130             ensure00(val.getArgumentValue((byte)3).getValue().toString()) + ":" +
131             ensure00(val.getArgumentValue((byte)4).getValue().toString()) + ":" +
132             ensure00(val.getArgumentValue((byte)5).getValue().toString());
133         switch(val.getArity()) {
134             case 7:
135                 return ret + ensure00(
136                         serializeTimezone(
137                                 val.getArgumentValue((byte)6).getValue(),null));
138             case 8:
139                 return ret + ensure00(
140                         serializeTimezone(
141                             val.getArgumentValue((byte)6).getValue(),
142                             val.getArgumentValue((byte)7).getValue()));
143         }
144         return ret;
145     }
146     
147     private static String serializeTimezone(Object h, Object m) {
148         String ret = h.toString();
149         if(((BigInteger)h).signum()>0) { ret = "+" + ret;  }
150         if(m!=null) return ret + ":" + ensure00(m.toString()); else return ret;
151     }
152     
153     private static String ensure00(String s) {
154         if(s.length()==1) {
155             return "0" + s;
156         } else {
157             return s;
158         }
159     }
160 }
161 
162 /*
163  * $Log$
164  * Revision 1.2  2006/11/10 15:38:49  ohamano
165  * *** empty log message ***
166  *
167  * Revision 1.1  2006/11/10 15:02:59  ohamano
168  * *** empty log message ***
169  *
170  * Revision 1.1  2006/11/10 10:08:42  ohamano
171  * *** empty log message ***
172  *
173  *
174  */