View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2005, OntoText Lab. / SIRMA
5                        University of Innsbruck, Austria
6   
7    This library is free software; you can redistribute it and/or modify it under
8    the terms of the GNU Lesser General Public License as published by the Free
9    Software Foundation; either version 2.1 of the License, or (at your option)
10   any later version.
11   This library is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14   details.
15   You should have received a copy of the GNU Lesser General Public License along
16   with this library; if not, write to the Free Software Foundation, Inc.,
17   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   */
19  
20  package com.ontotext.wsmo4j.ontology;
21  
22  /**
23   * <p>Title: WSMO4J</p>
24   * <p>Description: WSMO API and a Reference Implementation</p>
25   * <p>Copyright:  Copyright (c) 2004-2005</p>
26   * <p>Company: OntoText Lab. / SIRMA </p>
27   * @author not attributable
28   * @version 1.0
29   *
30   */
31  
32  import java.math.*;
33  import java.util.*;
34  
35  import org.deri.wsmo4j.io.serializer.wsml.*;
36  import org.omwg.logicalexpression.terms.*;
37  import org.omwg.ontology.*;
38  
39  public class ComplexDataValueImpl implements ComplexDataValue {
40  
41      private WsmlDataType dataType;
42      private SimpleDataValue[] values;
43      private Object value;
44  
45      public ComplexDataValueImpl(ComplexDataType type, SimpleDataValue[] vals) {
46          assert type != null;
47          assert vals != null;
48          this.dataType = type;
49          int arity = vals.length;
50          this.values = new SimpleDataValue[arity];
51          System.arraycopy(vals, 0, this.values, 0, arity);
52          this.value = createJavaValueFromSimpleTypes(type, vals);
53      }
54      
55      public ComplexDataValueImpl(ComplexDataType type, SimpleDataValue vals) {
56          this(type, new SimpleDataValue[] {vals});
57      }
58  
59      public WsmlDataType getType() {
60          return dataType;
61      }
62      
63      public int getArity() {
64      	return values.length;
65      }
66  
67      public SimpleDataValue getArgumentValue(byte pos) {
68          if (pos < 0
69                  || pos >= values.length) {
70              throw new IllegalArgumentException(
71                      "Invalid argument position for type '"
72                      + dataType.toString()
73                      + "'!");
74          }
75          return values[pos];
76      }
77  
78      public String asString() {
79          VisitorSerializeWSMLTerms v = new VisitorSerializeWSMLTerms(null);
80          this.accept(v);
81          return v.getSerializedObject();
82      }
83  
84      public Object getValue() {
85          return value;
86      }
87  
88      public String toString() {
89          return asString();
90      }
91      
92      public void accept(Visitor v) {
93      	v.visitComplexDataValue(this);
94      }
95  
96      public boolean equals(Object object) {
97          if (object == this) {
98              return true;
99          }
100         if (object == null
101                 || false == object instanceof ComplexDataValue) {
102             return false;
103         }
104         ComplexDataValue testVal = (ComplexDataValue) object;
105         if (false == dataType.equals(testVal.getType())) {
106             return false;
107         }
108         if (dataType.getIRI().toString().equals(WsmlDataType.WSML_DOUBLE)||
109                 dataType.getIRI().toString().equals(WsmlDataType.WSML_FLOAT)){
110             return value.equals(testVal.getValue());
111         }
112         
113         if (testVal.getArity()!=getArity()){
114             return false;
115         }
116         
117         for(byte i = 0; i < values.length; i++) {
118             if (!values[i].equals(testVal.getArgumentValue(i))) {
119                 return false;
120             }
121         }
122         return true;
123     }
124 
125     public int hashCode() {
126         return dataType.hashCode();
127     }
128     
129     private Object createJavaValueFromSimpleTypes(ComplexDataType type, SimpleDataValue[] v){
130         String typeIRI = type.getIRI().toString();
131         if (typeIRI.equals(WsmlDataType.WSML_FLOAT)){
132             return new Float(values[0].toString());
133         }else if (typeIRI.equals(WsmlDataType.WSML_DOUBLE)){
134             return new Double(values[0].toString());
135         }else if (typeIRI.equals(WsmlDataType.WSML_BOOLEAN)){
136             return Boolean.valueOf(values[0].toString());
137         }else if (typeIRI.equals(WsmlDataType.WSML_DURATION)){
138             //TODO: equality will return wrong results with respect to spec
139             //http://www.w3.org/TR/xmlschema-2/#duration
140             return values[0].toString();
141         }else if (typeIRI.equals(WsmlDataType.WSML_DATETIME)){
142             Calendar cal;
143             if (values.length==8){ //including offset
144                 int offset = (((BigInteger) values[6].getValue()).intValue()*60+
145                     ((BigInteger) values[7].getValue()).intValue())*1000;
146                 cal = new GregorianCalendar(new SimpleTimeZone(offset,""));
147             }else{
148                 cal = new GregorianCalendar();
149             }
150             cal.set(
151                     ((BigInteger) values[0].getValue()).intValue(),
152                     ((BigInteger) values[1].getValue()).intValue()-1,
153                     ((BigInteger) values[2].getValue()).intValue(),
154                     ((BigInteger) values[3].getValue()).intValue(),
155                     ((BigInteger) values[4].getValue()).intValue(),
156                     ((BigDecimal) values[5].getValue()).intValue());
157             return cal;
158         }else if (typeIRI.equals(WsmlDataType.WSML_DATE)){
159             Calendar cal;
160             if (values.length==5){ //including offset
161                 int offset = (((BigInteger) values[3].getValue()).intValue()*60+
162                     ((BigInteger) values[4].getValue()).intValue())*1000;
163                 cal = new GregorianCalendar(new SimpleTimeZone(offset,""));
164             }else{
165                 cal = new GregorianCalendar();
166             }
167             cal.set(
168                     ((BigInteger) values[0].getValue()).intValue(),
169                     ((BigInteger) values[1].getValue()).intValue()-1,
170                     ((BigInteger) values[2].getValue()).intValue());
171             return cal;
172         }else if (typeIRI.equals(WsmlDataType.WSML_TIME)){
173             Calendar cal;
174             if (values.length==5){ //including offset
175                 int offset = (((BigInteger) values[3].getValue()).intValue()*60+
176                     ((BigInteger) values[4].getValue()).intValue())*1000;
177                 cal = new GregorianCalendar(new SimpleTimeZone(offset,""));
178             }else{
179                 cal = new GregorianCalendar();
180             }
181             cal.set(1970,1,1,
182                     ((BigInteger) values[0].getValue()).intValue(),
183                     ((BigInteger) values[1].getValue()).intValue(),
184                     ((BigDecimal) values[2].getValue()).intValue());
185             return cal;
186         }else if (typeIRI.equals(WsmlDataType.WSML_IRI)){
187             throw new UnsupportedOperationException("Data type "+typeIRI+" not supported!");
188         }else if (typeIRI.equals(WsmlDataType.WSML_SQNAME)){
189             throw new UnsupportedOperationException("Data type "+typeIRI+" not supported!");
190         }else if (typeIRI.equals(WsmlDataType.WSML_GYEARMONTH)){
191             return new Integer[] {
192                     new Integer(""+values[0].getValue()),
193                     new Integer(""+values[1].getValue())};
194         }else if (typeIRI.equals(WsmlDataType.WSML_GMONTHDAY)){
195             return new Integer[] {
196                     new Integer(""+values[0].getValue()),
197                     new Integer(""+values[1].getValue())};
198         }else if (typeIRI.equals(WsmlDataType.WSML_GYEAR)){
199             return new Integer(""+values[0].getValue());
200         }else if (typeIRI.equals(WsmlDataType.WSML_GDAY)){
201             return new Integer(""+values[0].getValue());
202         }else if(typeIRI.equals(WsmlDataType.WSML_GMONTH)){
203             return new Integer(""+values[0].getValue());
204         }else if (typeIRI.equals(WsmlDataType.WSML_HEXBINARY)){
205             return ""+values[0];
206         }else if (typeIRI.equals(WsmlDataType.WSML_BASE64BINARY)){
207             return ""+values[0];
208         }else{
209             throw new UnsupportedOperationException("Data type "+typeIRI+" not supported!");
210         }
211     }
212 }
213 
214 /*
215  * $Log$
216  * Revision 1.8  2007/09/04 08:18:06  lcekov
217  * DataTypeFactory.createWsmlTime must support milliseconds
218  * https://sourceforge.net/tracker/?func=detail&atid=665346&aid=1723466&group_id=113501
219  *
220  * Revision 1.7  2007/06/22 12:35:09  lcekov
221  * DataTypeFactory.createWsmlDateTime must support milliseconds
222  * https://sourceforge.net/tracker/?func=detail&atid=665346&aid=1723466&group_id=113501
223  *
224  * Revision 1.6  2007/04/02 12:13:21  morcen
225  * Generics support added to wsmo-api, wsmo4j and wsmo-test
226  *
227  * Revision 1.5  2006/11/10 14:18:44  holgerlausen
228  * Fixed Bug with Illegalargument Exception when compaing datatypes with different arities
229  *
230  * Revision 1.4  2005/10/17 17:36:37  holgerlausen
231  * toString() to behave equaly then Serializer
232  *
233  * Revision 1.3  2005/09/30 11:56:11  alex_simov
234  * minor fixes
235  *
236  * Revision 1.2  2005/09/21 08:15:39  holgerlausen
237  * fixing java doc, removing asString()
238  *
239  * Revision 1.1  2005/09/06 18:33:42  holgerlausen
240  *   support for datatypes: updated implementation according to interfaces
241  *   renamed WsmlDataTypeImpl to SimpleDataTypeImpl
242  *   moved DataValueImpl to ComplexDataValueImpl
243  *   added SimpleDataValueImpl for basic strings, decimal, ints
244  *
245  * Revision 1.7  2005/09/02 13:32:45  ohamano
246  * move logicalexpression packages from ext to core
247  * move tests from logicalexpression.test to test module
248  *
249  * Revision 1.6  2005/09/02 09:43:32  ohamano
250  * integrate wsmo-api and le-api on api and reference implementation level; full parser, serializer and validator integration will be next step
251  *
252  * Revision 1.5  2005/08/19 09:00:49  marin_dimitrov
253  * -
254  *
255  * Revision 1.4  2005/07/04 15:25:16  alex_simov
256  * DataValue/DataType changes
257  *
258  * Revision 1.3  2005/06/25 13:19:44  damyan_rm
259  * serializing to String changed
260  *
261  * Revision 1.2  2005/06/23 10:47:46  alex_simov
262  * hashCode() added
263  *
264  * Revision 1.1  2005/06/01 12:09:05  marin_dimitrov
265  * v0.4.0
266  *
267  * Revision 1.3  2005/05/30 15:07:05  alex
268  * toString() delegates to asString()
269  *
270  * Revision 1.2  2005/05/26 10:06:47  damian
271  * asString modified
272  *
273  * Revision 1.1  2005/05/12 15:19:39  alex
274  * initial commit
275  *
276  *
277  */