001: /*
002: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: StatementBase.java,v 1.13 2008/01/02 12:05:01 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model.impl;
008:
009: import com.hp.hpl.jena.graph.Node;
010: import com.hp.hpl.jena.rdf.model.*;
011: import com.hp.hpl.jena.shared.JenaException;
012:
013: /**
014: Abstract base class for StaementImpl - pulls up the stuff that doesn't depend
015: on how statements are represented (as S/P/O or as Triples).
016:
017: @author hedgehog
018: */
019: public abstract class StatementBase {
020: protected final ModelCom model;
021:
022: protected StatementBase(ModelCom model) {
023: if (model == null)
024: throw new JenaException("Statement models must no be null");
025: this .model = model;
026: }
027:
028: public Model getModel() {
029: return model;
030: }
031:
032: /**
033: * replace this StaementImpl [ie the one of which this is the base] with (S,
034: * P, n). Answer the new StaementImpl. Abstract here to allow methods to be
035: * pulled up.
036: */
037: protected abstract StatementImpl replace(RDFNode n);
038:
039: /**
040: * Answer the object of this statement as a Literal, or throw a
041: * LiteralRequiredException.
042: */
043: public abstract Literal getLiteral();
044:
045: public abstract Resource getResource();
046:
047: public abstract Resource getSubject();
048:
049: public abstract Property getPredicate();
050:
051: public abstract RDFNode getObject();
052:
053: protected StatementImpl stringReplace(String s, String lang,
054: boolean wellFormed) {
055: return replace(new LiteralImpl(Node.createLiteral(s, lang,
056: wellFormed), model));
057: }
058:
059: /**
060: * "replace" the Object of this statement with the literal string value _s_.
061: * NOTE: this is a convenience function to eliminate the use of a deprecated
062: * constructor; when data-types are put properly into Jena, it will likely
063: * disappear.
064: */
065: protected StatementImpl stringReplace(String s) {
066: return stringReplace(s, "", false);
067: }
068:
069: public Statement changeLiteralObject(boolean o) {
070: return changeObject(model.createTypedLiteral(o));
071: }
072:
073: public Statement changeObject(boolean o) {
074: return stringReplace(String.valueOf(o));
075: }
076:
077: public Statement changeObject(long o) {
078: return stringReplace(String.valueOf(o));
079: }
080:
081: public Statement changeLiteralObject(long o) {
082: return changeObject(model.createTypedLiteral(o));
083: }
084:
085: public Statement changeLiteralObject(char o) {
086: return changeObject(model.createTypedLiteral(o));
087: }
088:
089: public Statement changeLiteralObject(double o) {
090: return changeObject(model.createTypedLiteral(o));
091: }
092:
093: public Statement changeLiteralObject(float o) {
094: return changeObject(model.createTypedLiteral(o));
095: }
096:
097: public Statement changeLiteralObject(int o) {
098: return changeObject(model.createTypedLiteral(o));
099: }
100:
101: public Statement changeObject(float o) {
102: return changeObject(String.valueOf(o));
103: }
104:
105: public Statement changeObject(double o) {
106: return stringReplace(String.valueOf(o));
107: }
108:
109: public Statement changeTypedObject(double o) {
110: return changeObject(model.createTypedLiteral(o));
111: }
112:
113: public Statement changeObject(String o) {
114: return stringReplace(String.valueOf(o));
115: }
116:
117: public Statement changeObject(String o, boolean wellFormed) {
118: return stringReplace(String.valueOf(o), "", wellFormed);
119: }
120:
121: public Statement changeObject(String o, String l) {
122: return stringReplace(String.valueOf(o), l, false);
123: }
124:
125: public Statement changeObject(String o, String l, boolean wellFormed) {
126: return stringReplace(String.valueOf(o), l, wellFormed);
127: }
128:
129: public Statement changeObject(RDFNode o) {
130: return replace(o);
131: }
132:
133: public Statement changeObject(Object o) {
134: return o instanceof RDFNode ? replace((RDFNode) o)
135: : stringReplace(o.toString());
136: }
137:
138: public boolean getBoolean() {
139: return getLiteral().getBoolean();
140: }
141:
142: public byte getByte() {
143: return getLiteral().getByte();
144: }
145:
146: public short getShort() {
147: return getLiteral().getShort();
148: }
149:
150: public int getInt() {
151: return getLiteral().getInt();
152: }
153:
154: public long getLong() {
155: return getLiteral().getLong();
156: }
157:
158: public char getChar() {
159: return getLiteral().getChar();
160: }
161:
162: public float getFloat() {
163: return getLiteral().getFloat();
164: }
165:
166: public double getDouble() {
167: return getLiteral().getDouble();
168: }
169:
170: public String getString() {
171: return getLiteral().getLexicalForm();
172: }
173:
174: /**
175: * utility: check that node is a Resource, throw otherwise
176: */
177: protected Resource mustBeResource(RDFNode n) {
178: if (n instanceof Resource)
179: return (Resource) n;
180: else
181: throw new ResourceRequiredException(n);
182: }
183:
184: public String getLanguage() {
185: return getLiteral().getLanguage();
186: }
187:
188: public boolean getWellFormed() {
189: return hasWellFormedXML();
190: }
191:
192: public boolean hasWellFormedXML() {
193: return getLiteral().isWellFormedXML();
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 toString() {
201: return "[" + getSubject().toString() + ", "
202: + getPredicate().toString() + ", "
203: + objectString(getObject()) + "]";
204: }
205:
206: /**
207: Answer a string describing <code>object</code>, quoting it if it is a literal.
208: */
209: protected String objectString(RDFNode object) {
210: return object.asNode().toString(null, true);
211: }
212:
213: }
214:
215: /*
216: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP All rights
217: reserved. Redistribution and use in source and binary forms, with or without
218: modification, are permitted provided that the following conditions are met:
219: 1. Redistributions of source code must retain the above copyright notice,
220: this list of conditions and the following disclaimer. 2. Redistributions in
221: binary form must reproduce the above copyright notice, this list of
222: conditions and the following disclaimer in the documentation and/or other
223: materials provided with the distribution. 3. The name of the author may not
224: be used to endorse or promote products derived from this software without
225: specific prior written permission.
226:
227: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
228: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
229: MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
230: EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
232: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
233: OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
234: WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
235: OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
236: ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237: */
|