KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > impl > StatementBase


1 /*
2      (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
3      [See end of file]
4      $Id: StatementBase.java,v 1.5 2005/02/21 12:14:54 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.model.impl;
8
9 import com.hp.hpl.jena.graph.Node;
10 import com.hp.hpl.jena.rdf.model.*;
11 import com.hp.hpl.jena.shared.JenaException;
12
13 /**
14     Abstract base class for StaementImpl - pulls up the stuff that doesn't depend
15     on how statements are represented (as S/P/O or as Triples).
16     
17     @author hedgehog
18 */

19 public abstract class StatementBase
20     {
21     protected final ModelCom model;
22
23     protected StatementBase( ModelCom model )
24         {
25         if (model == null) throw new JenaException( "Statement models must no be null" );
26         this.model = model;
27         }
28
29     public Model getModel()
30         {
31         return model;
32         }
33
34     /**
35      * replace this StaementImpl [ie the one of which this is the base] with (S,
36      * P, n). Answer the new StaementImpl. Abstract here to allow methods to be
37      * pulled up.
38      */

39     protected abstract StatementImpl replace( RDFNode n );
40
41     /**
42      * Answer the object of this statement as a Literal, or throw a
43      * LiteralRequiredException.
44      */

45     public abstract Literal getLiteral();
46     
47     public abstract Resource getResource();
48     
49     public abstract Resource getSubject();
50     
51     public abstract Property getPredicate();
52     
53     public abstract RDFNode getObject();
54
55     protected StatementImpl stringReplace(String JavaDoc s, String JavaDoc lang,
56             boolean wellFormed)
57         {
58         return replace(new LiteralImpl(Node.createLiteral(s, lang, wellFormed),
59                 model));
60         }
61
62     /**
63      * "replace" the Object of this statement with the literal string value _s_.
64      * NOTE: this is a convenience function to eliminate the use of a deprecated
65      * constructor; when data-types are put properly into Jena, it will likely
66      * disappear.
67      */

68     protected StatementImpl stringReplace( String JavaDoc s )
69         {
70         return stringReplace( s, "", false );
71         }
72
73     public Statement changeObject( boolean o )
74         {
75         return stringReplace( String.valueOf( o ) );
76         }
77
78     public Statement changeObject( long o )
79         {
80         return stringReplace( String.valueOf( o ) );
81         }
82
83     public Statement changeObject( char o )
84         {
85         return stringReplace( String.valueOf( o ) );
86         }
87
88     public Statement changeObject( float o )
89         {
90         return stringReplace( String.valueOf( o ) );
91         }
92
93     public Statement changeObject( double o )
94         {
95         return stringReplace( String.valueOf( o ) );
96         }
97
98     public Statement changeObject( String JavaDoc o )
99         {
100         return stringReplace( String.valueOf( o ) );
101         }
102
103     public Statement changeObject( String JavaDoc o, boolean wellFormed )
104         {
105         return stringReplace( String.valueOf( o ), "", wellFormed );
106         }
107
108     public Statement changeObject( String JavaDoc o, String JavaDoc l )
109         {
110         return stringReplace( String.valueOf( o ), l, false );
111         }
112
113     public Statement changeObject( String JavaDoc o, String JavaDoc l, boolean wellFormed )
114         {
115         return stringReplace( String.valueOf( o ), l, wellFormed );
116         }
117
118     public Statement changeObject( RDFNode o )
119         {
120         return replace( o );
121         }
122
123     public Statement changeObject( Object JavaDoc o )
124         {
125         return o instanceof RDFNode
126             ? replace( (RDFNode) o )
127             : stringReplace( o.toString() );
128         }
129
130     public boolean getBoolean()
131         {
132         return getLiteral().getBoolean();
133         }
134
135     public byte getByte()
136         {
137         return getLiteral().getByte();
138         }
139
140     public short getShort()
141         {
142         return getLiteral().getShort();
143         }
144
145     public int getInt()
146         {
147         return getLiteral().getInt();
148         }
149
150     public long getLong()
151         {
152         return getLiteral().getLong();
153         }
154
155     public char getChar()
156         {
157         return getLiteral().getChar();
158         }
159
160     public float getFloat()
161         {
162         return getLiteral().getFloat();
163         }
164
165     public double getDouble()
166         {
167         return getLiteral().getDouble();
168         }
169
170     public String JavaDoc getString()
171         {
172         return getLiteral().getLexicalForm();
173         }
174
175     /**
176      * utility: check that node is a Resource, throw otherwise
177      */

178     protected Resource mustBeResource(RDFNode n)
179         {
180         if (n instanceof Resource)
181             return (Resource) n;
182         else
183             throw new ResourceRequiredException(n);
184         }
185
186     public String JavaDoc getLanguage()
187         {
188         return getLiteral().getLanguage();
189         }
190
191     public boolean getWellFormed()
192         {
193         return getLiteral().getWellFormed();
194         }
195
196     /**
197         Answer a string describing this Statement in a vagely pretty way, with the
198         representations of the subject, predicate, and object in that order.
199     */

200     public String JavaDoc toString()
201         {
202         return
203             "["
204             + getSubject().toString()
205             + ", " + getPredicate().toString()
206             + ", " + objectString( getObject() )
207             + "]";
208         }
209
210     /**
211         Answer a string describing <code>object</code>, quoting it if it is a literal.
212     */

213     protected String JavaDoc objectString( RDFNode object )
214         {
215         return object.asNode().toString( null, true );
216         }
217
218     }
219
220 /*
221      (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP All rights
222      reserved. Redistribution and use in source and binary forms, with or without
223      modification, are permitted provided that the following conditions are met:
224      1. Redistributions of source code must retain the above copyright notice,
225      this list of conditions and the following disclaimer. 2. Redistributions in
226      binary form must reproduce the above copyright notice, this list of
227      conditions and the following disclaimer in the documentation and/or other
228      materials provided with the distribution. 3. The name of the author may not
229      be used to endorse or promote products derived from this software without
230      specific prior written permission.
231       
232      THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
233      WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
234      MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
235      EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
236      SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
237      PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
238      OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
239      WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
240      OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
241      ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
242 */

243
Popular Tags