001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.model.impl;
007:
008: import javax.xml.datatype.XMLGregorianCalendar;
009:
010: import org.openrdf.model.BNode;
011: import org.openrdf.model.Literal;
012: import org.openrdf.model.URI;
013: import org.openrdf.model.ValueFactory;
014: import org.openrdf.model.datatypes.XMLDatatypeUtil;
015: import org.openrdf.model.vocabulary.XMLSchema;
016:
017: /**
018: * Abstract base class for {@link ValueFactory} implementations that implements
019: * the utility methods for creating literals for basic types by calling the
020: * generic {@link ValueFactory#createLiteral(String, URI)} with the appropriate
021: * value and datatype.
022: *
023: * @author Arjohn Kampman
024: */
025: public abstract class ValueFactoryBase implements ValueFactory {
026:
027: /*-----------*
028: * Variables *
029: *-----------*/
030:
031: /**
032: * The ID for the next bnode that is created.
033: */
034: private int nextBNodeID;
035:
036: /**
037: * The prefix for any new bnode IDs.
038: */
039: private String bnodePrefix;
040:
041: /*--------------*
042: * Constructors *
043: *--------------*/
044:
045: public ValueFactoryBase() {
046: initBNodeParams();
047: }
048:
049: /*---------*
050: * Methods *
051: *---------*/
052:
053: /**
054: * Generates a new bnode prefix based on <tt>currentTimeMillis()</tt> and
055: * resets <tt>_nextBNodeID</tt> to <tt>1</tt>.
056: */
057: protected void initBNodeParams() {
058: // BNode prefix is based on currentTimeMillis(). Combined with a
059: // sequential number per session, this gives a unique identifier.
060: bnodePrefix = "node"
061: + Long.toString(System.currentTimeMillis(), 32) + "x";
062: nextBNodeID = 1;
063: }
064:
065: public BNode createBNode() {
066: if (nextBNodeID == Integer.MAX_VALUE) {
067: // Start with a new bnode prefix
068: initBNodeParams();
069: }
070:
071: return createBNode(bnodePrefix + nextBNodeID++);
072: }
073:
074: /**
075: * Calls {@link ValueFactory#createLiteral(String, URI)} with the
076: * String-value of the supplied value and {@link XMLSchema#BOOLEAN} as
077: * parameters.
078: */
079: public Literal createLiteral(boolean b) {
080: return createLiteral(Boolean.toString(b), XMLSchema.BOOLEAN);
081: }
082:
083: /**
084: * Calls {@link #createIntegerLiteral(long, URI)} with the supplied value and
085: * {@link XMLSchema#BYTE} as parameters.
086: */
087: public Literal createLiteral(byte value) {
088: return createIntegerLiteral(value, XMLSchema.BYTE);
089: }
090:
091: /**
092: * Calls {@link #createIntegerLiteral(long, URI)} with the supplied value and
093: * {@link XMLSchema#SHORT} as parameters.
094: */
095: public Literal createLiteral(short value) {
096: return createIntegerLiteral(value, XMLSchema.SHORT);
097: }
098:
099: /**
100: * Calls {@link #createIntegerLiteral(long, URI)} with the supplied value and
101: * {@link XMLSchema#INT} as parameters.
102: */
103: public Literal createLiteral(int value) {
104: return createIntegerLiteral(value, XMLSchema.INT);
105: }
106:
107: /**
108: * Calls {@link #createIntegerLiteral(long, URI)} with the supplied value and
109: * {@link XMLSchema#LONG} as parameters.
110: */
111: public Literal createLiteral(long value) {
112: return createIntegerLiteral(value, XMLSchema.LONG);
113: }
114:
115: /**
116: * Calls {@link #createNumericLiteral(Number, URI)} with the supplied value
117: * and datatype as parameters.
118: */
119: protected Literal createIntegerLiteral(Number value, URI datatype) {
120: return createNumericLiteral(value, datatype);
121: }
122:
123: /**
124: * Calls {@link #createFPLiteral(Number, URI)} with the supplied value and
125: * {@link XMLSchema#FLOAT} as parameters.
126: */
127: public Literal createLiteral(float value) {
128: return createFPLiteral(value, XMLSchema.FLOAT);
129: }
130:
131: /**
132: * Calls {@link #createFPLiteral(Number, URI)} with the supplied value and
133: * {@link XMLSchema#DOUBLE} as parameters.
134: */
135: public Literal createLiteral(double value) {
136: return createFPLiteral(value, XMLSchema.DOUBLE);
137: }
138:
139: /**
140: * Calls {@link #createNumericLiteral(Number, URI)} with the supplied value
141: * and datatype as parameters.
142: */
143: protected Literal createFPLiteral(Number value, URI datatype) {
144: return createNumericLiteral(value, datatype);
145: }
146:
147: /**
148: * Calls {@link ValueFactory#createLiteral(String, URI)} with the
149: * String-value of the supplied number and the supplied datatype as
150: * parameters.
151: */
152: protected Literal createNumericLiteral(Number number, URI datatype) {
153: return createLiteral(number.toString(), datatype);
154: }
155:
156: /**
157: * Calls {@link ValueFactory#createLiteral(String, URI)} with the
158: * String-value of the supplied calendar and the appropriate datatype as
159: * parameters.
160: *
161: * @see XMLGregorianCalendar#toXMLFormat()
162: * @see XMLGregorianCalendar#getXMLSchemaType()
163: * @see XMLDatatypeUtil#qnameToURI(javax.xml.namespace.QName)
164: */
165: public Literal createLiteral(XMLGregorianCalendar calendar) {
166: return createLiteral(calendar.toXMLFormat(), XMLDatatypeUtil
167: .qnameToURI(calendar.getXMLSchemaType()));
168: }
169: }
|