KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > ontology > daml > impl > DAMLDataInstanceImpl


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena
7  * Created 17 Sept 2001
8  * Filename $RCSfile: DAMLDataInstanceImpl.java,v $
9  * Revision $Revision: 1.14 $
10  * Release status Preview-release $State: Exp $
11  *
12  * Last modified on $Date: 2005/02/21 12:05:18 $
13  * by $Author: andy_seaborne $
14  *
15  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
16  * (see footer for full conditions)
17  *****************************************************************************/

18
19 // Package
20
///////////////
21
package com.hp.hpl.jena.ontology.daml.impl;
22
23
24 // Imports
25
///////////////
26

27 import com.hp.hpl.jena.rdf.model.Resource;
28 import com.hp.hpl.jena.datatypes.*;
29 import com.hp.hpl.jena.enhanced.*;
30 import com.hp.hpl.jena.graph.Node;
31 import com.hp.hpl.jena.ontology.*;
32 import com.hp.hpl.jena.ontology.daml.*;
33 import com.hp.hpl.jena.util.iterator.ClosableIterator;
34 import com.hp.hpl.jena.vocabulary.*;
35
36 import java.util.Iterator JavaDoc;
37
38 import org.apache.commons.logging.LogFactory;
39
40
41
42 /**
43  * <p>A data instance is a specific type of DAML object that represents the instantiation
44  * of a DAML datatype. The instance is a resource whose <code>rdf:value</code> is a typed literal.</p>
45  *
46  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com">email</a>)
47  * @version CVS info: $Id: DAMLDataInstanceImpl.java,v 1.14 2005/02/21 12:05:18 andy_seaborne Exp $
48  */

49 public class DAMLDataInstanceImpl
50     extends DAMLInstanceImpl
51     implements DAMLDataInstance
52 {
53     // Constants
54
//////////////////////////////////
55

56
57     // Static variables
58
//////////////////////////////////
59

60     /**
61      * A factory for generating DAMLDataInstance facets from nodes in enhanced graphs.
62      * Note: should not be invoked directly by user code: use
63      * {@link com.hp.hpl.jena.rdf.model.RDFNode#as as()} instead.
64      */

65     public static Implementation factory = new Implementation() {
66         public EnhNode wrap( Node n, EnhGraph eg ) {
67             if (canWrap( n, eg )) {
68                 return new DAMLDataInstanceImpl( n, eg );
69             }
70             else {
71                 throw new ConversionException( "Cannot convert node " + n.toString() + " to DAMLDataInstance" );
72             }
73         }
74             
75         public boolean canWrap( Node node, EnhGraph eg ) {
76             return eg.asGraph().contains( node, RDF.type.asNode(), Node.ANY );
77         }
78     };
79
80     // Instance variables
81
//////////////////////////////////
82

83
84     // Constructors
85
//////////////////////////////////
86

87     /**
88      * <p>
89      * Construct a DAML data instance represented by the given node in the given graph.
90      * </p>
91      *
92      * @param n The node that represents the resource
93      * @param g The enh graph that contains n
94      */

95     public DAMLDataInstanceImpl( Node n, EnhGraph g ) {
96         super( n, g );
97     }
98
99
100     // External signature methods
101
//////////////////////////////////
102

103
104     /**
105      * <p>Answer the typed value translator for values encoded by the datatype of this
106      * instance.</p>
107      *
108      * @return The datatype translator defined for the <code>rdf:type</code> of this instance
109      */

110     public RDFDatatype getDatatype() {
111         // search for an RDF type that we have a translator for
112
for (Iterator i = listRDFTypes( true ); i.hasNext(); ) {
113             Resource rType = (Resource) i.next();
114             if (rType.isAnon()) {
115                 continue;
116             }
117             
118             RDFDatatype dt = TypeMapper.getInstance().getTypeByName( rType.getURI() );
119             
120             if (dt != null) {
121                 // found a candidate datatype
122
if (i instanceof ClosableIterator) {
123                     ((ClosableIterator) i).close();
124                 }
125                 
126                 return dt;
127             }
128         }
129         
130         return null;
131     }
132
133
134     /**
135      * <p>Answer the value of this instance as a Java object, translated from the
136      * serialised RDF representation by the Dataype's type mapper.</p>
137      *
138      * @return The value of this instance, or null if either the translator or the
139      * serialised value is defined
140      */

141     public Object JavaDoc getValue() {
142         if (hasProperty( RDF.value )) {
143             RDFDatatype dType = getDatatype();
144             
145             if (dType == null) {
146                 LogFactory.getLog( getClass() ).warn( "No RDFDatatype defined for DAML data instance " + this );
147             }
148             else {
149                 return dType.parse( getRequiredProperty( RDF.value ).getString() );
150             }
151         }
152         
153         return null;
154     }
155
156
157     /**
158      * <p>Set the value of this instance to the given Java value, which will be
159      * serialised into the RDF graph by the datatype's translator.</p>
160      *
161      * @param value The value to be encoded as a typed literal
162      */

163     public void setValue( Object JavaDoc value ) {
164         setPropertyValue( RDF.value, "", getModel().createTypedLiteral( value, getDatatype() ) );
165     }
166
167
168
169     // Internal implementation methods
170
//////////////////////////////////
171

172
173     //==============================================================================
174
// Inner class definitions
175
//==============================================================================
176

177
178 }
179
180
181 /*
182     (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
183     All rights reserved.
184
185     Redistribution and use in source and binary forms, with or without
186     modification, are permitted provided that the following conditions
187     are met:
188
189     1. Redistributions of source code must retain the above copyright
190        notice, this list of conditions and the following disclaimer.
191
192     2. Redistributions in binary form must reproduce the above copyright
193        notice, this list of conditions and the following disclaimer in the
194        documentation and/or other materials provided with the distribution.
195
196     3. The name of the author may not be used to endorse or promote products
197        derived from this software without specific prior written permission.
198
199     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
200     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
201     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
202     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
203     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
204     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
205     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
206     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
207     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
208     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
209 */

210
211
Popular Tags