KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > datatypes > BaseDatatype


1 /******************************************************************
2  * File: BaseDatatype.java
3  * Created by: Dave Reynolds
4  * Created on: 08-Dec-02
5  *
6  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: BaseDatatype.java,v 1.9 2005/02/21 12:01:50 andy_seaborne Exp $
9  *****************************************************************/

10
11 package com.hp.hpl.jena.datatypes;
12
13 import com.hp.hpl.jena.graph.impl.LiteralLabel;
14
15 /**
16  * Base level implementation of datatype from which real implementations
17  * can inherit.
18  *
19  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
20  * @version $Revision: 1.9 $ on $Date: 2005/02/21 12:01:50 $
21  */

22 public class BaseDatatype implements RDFDatatype {
23     
24     /** The URI label for this data type */
25     protected String JavaDoc uri;
26     
27     /**
28      * Constructor.
29      * @param uri the URI label to use for this datatype
30      */

31     public BaseDatatype(String JavaDoc uri) {
32         this.uri = uri;
33     }
34     
35     /**
36      * Return the URI which is the label for this datatype
37      */

38     public String JavaDoc getURI() {
39         return uri;
40     }
41     
42     /**
43      * Convert a value of this datatype out
44      * to lexical form.
45      */

46     public String JavaDoc unparse(Object JavaDoc value) {
47         return value.toString();
48     }
49     
50     /**
51      * Parse a lexical form of this datatype to a value
52      * @throws DatatypeFormatException if the lexical form is not legal
53      */

54     public Object JavaDoc parse(String JavaDoc lexicalForm) throws DatatypeFormatException {
55         return lexicalForm;
56     }
57     
58     /**
59      * Test whether the given string is a legal lexical form
60      * of this datatype.
61      */

62     public boolean isValid(String JavaDoc lexicalForm) {
63         try {
64             parse(lexicalForm);
65             return true;
66         } catch (DatatypeFormatException e) {
67             return false;
68         }
69     }
70     
71     /**
72      * Test whether the given LiteralLabel is a valid instance
73      * of this datatype. This takes into accound typing information
74      * as well as lexical form - for example an xsd:string is
75      * never considered valid as an xsd:integer (even if it is
76      * lexically legal like "1").
77      */

78     public boolean isValidLiteral(LiteralLabel lit) {
79         // default is that only literals with the same type are valid
80
return equals(lit.getDatatype());
81     }
82      
83     /**
84      * Test whether the given object is a legal value form
85      * of this datatype.
86      */

87     public boolean isValidValue(Object JavaDoc valueForm) {
88         // Default to brute force
89
return isValid(unparse(valueForm));
90     }
91     
92     /**
93      * Compares two instances of values of the given datatype.
94      * This default requires value and datatype equality.
95      */

96     public boolean isEqual(LiteralLabel value1, LiteralLabel value2) {
97         return value1.getDatatype() == value2.getDatatype()
98              && value1.getValue().equals(value2.getValue());
99     }
100     
101     /**
102      * Helper function to compare language tag values
103      */

104     public boolean langTagCompatible(LiteralLabel value1, LiteralLabel value2) {
105         if (value1.language() == null) {
106             return (value2.language() == null || value2.language().equals(""));
107         } else {
108             return value1.language().equalsIgnoreCase(value2.language());
109         }
110     }
111     
112     /**
113      * Returns the java class which is used to represent value
114      * instances of this datatype.
115      */

116     public Class JavaDoc getJavaClass() {
117         return null;
118     }
119     
120     /**
121      * Returns an object giving more details on the datatype.
122      * This is type system dependent. In the case of XSD types
123      * this will be an instance of
124      * <code>org.apache.xerces.impl.xs.psvi.XSTypeDefinition</code>.
125      */

126     public Object JavaDoc extendedTypeDefinition() {
127         return null;
128     }
129     
130     /**
131      * Display format
132      */

133     public String JavaDoc toString() {
134         return "Datatype[" + uri
135               + (getJavaClass() == null ? "" : " -> " + getJavaClass())
136               + "]";
137     }
138 }
139
140 /*
141     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
142     All rights reserved.
143
144     Redistribution and use in source and binary forms, with or without
145     modification, are permitted provided that the following conditions
146     are met:
147
148     1. Redistributions of source code must retain the above copyright
149        notice, this list of conditions and the following disclaimer.
150
151     2. Redistributions in binary form must reproduce the above copyright
152        notice, this list of conditions and the following disclaimer in the
153        documentation and/or other materials provided with the distribution.
154
155     3. The name of the author may not be used to endorse or promote products
156        derived from this software without specific prior written permission.
157
158     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
159     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
160     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
161     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
162     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
163     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
164     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
165     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
166     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
167     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168 */

169
Popular Tags