001: package org.ontoware.rdf2go.impl.jena24;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005: import org.ontoware.rdf2go.exception.ModelRuntimeException;
006: import org.ontoware.rdf2go.model.node.BlankNode;
007: import org.ontoware.rdf2go.model.node.DatatypeLiteral;
008: import org.ontoware.rdf2go.model.node.LanguageTagLiteral;
009: import org.ontoware.rdf2go.model.node.PlainLiteral;
010: import org.ontoware.rdf2go.model.node.URI;
011: import org.ontoware.rdf2go.model.node.Variable;
012: import org.ontoware.rdf2go.model.node.impl.AbstractBlankNodeImpl;
013: import org.ontoware.rdf2go.model.node.impl.DatatypeLiteralImpl;
014: import org.ontoware.rdf2go.model.node.impl.LanguageTagLiteralImpl;
015: import org.ontoware.rdf2go.model.node.impl.PlainLiteralImpl;
016: import org.ontoware.rdf2go.model.node.impl.URIImpl;
017:
018: import com.hp.hpl.jena.graph.Node;
019: import com.hp.hpl.jena.graph.impl.LiteralLabel;
020: import com.hp.hpl.jena.rdf.model.AnonId;
021: import com.hp.hpl.jena.rdf.model.Literal;
022: import com.hp.hpl.jena.shared.impl.JenaParameters;
023:
024: /**
025: * converter between java objects and jena nodes (both ways)
026: *
027: * @author xam
028: *
029: */
030: public class TypeConversion {
031:
032: /**
033: * the logger
034: */
035: static Log log = LogFactory.getLog(TypeConversion.class);
036:
037: /**
038: * The new Implementation: model is needed because Jena can only create data
039: * typed literals with unknown data types if it creates them directly in the
040: * context of a model instance.
041: */
042: public static Node toJenaNode(Object o,
043: com.hp.hpl.jena.rdf.model.Model model)
044: throws RuntimeException {
045: assert o != null;
046: if (o instanceof URI) {
047: log.debug("instanceof URI");
048: return Node.createURI(((URI) o).toString());
049: }
050:
051: if (o instanceof String) {
052: // plain literal
053: log.debug("instanceof String");
054: return Node.createLiteral((String) o, null, false);
055: }
056:
057: if (o instanceof PlainLiteral) {
058: // plain literal
059: log.debug("instanceof String");
060: return Node.createLiteral(((PlainLiteral) o).getValue(),
061: null, false);
062: }
063:
064: if (o instanceof DatatypeLiteral) {
065: // datatyped
066: log.debug("instanceof DatatypeLiteral");
067:
068: boolean originalFlag = JenaParameters.enableSilentAcceptanceOfUnknownDatatypes;
069: JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = true;
070:
071: Literal l = model.createTypedLiteral(((DatatypeLiteral) o)
072: .getValue(), ((DatatypeLiteral) o).getDatatype()
073: .toString());
074:
075: JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = originalFlag;
076:
077: return l.asNode();
078:
079: }
080:
081: if (o instanceof LanguageTagLiteral) {
082: // langtag
083: log.debug("instanceof LanguageTagLiteral");
084: return Node.createLiteral(((LanguageTagLiteral) o)
085: .getValue(), ((LanguageTagLiteral) o)
086: .getLanguageTag(), null);
087: }
088:
089: if (o instanceof BlankNode) {
090: // blank node
091: log.debug("instanceof BlankNode");
092: assert o instanceof AbstractBlankNodeImpl : "expected a BlankNodeImpl and found a "
093: + o.getClass();
094: AnonId anonId = new AnonId(((AbstractBlankNodeImpl) o)
095: .getUnderlyingBlankNode().toString());
096: Node jenaNode = Node.createAnon(anonId);
097: return jenaNode;
098: }
099:
100: if (o instanceof Variable) {
101: // variable
102: log.debug("instanceof Variable");
103: return Node.ANY;
104: }
105:
106: throw new RuntimeException("no transformation from "
107: + o.getClass() + " to jena has been implemented");
108: }
109:
110: /**
111: * transforms the given object into a Jena-node
112: *
113: * @param o
114: * The object to transform
115: * @return The jena node
116: * @throws RuntimeException
117: * if the object can't be transformed in a jena node
118: */
119: public static Node toJenaNode(Object o) throws RuntimeException {
120: assert o != null;
121: if (o instanceof URI) {
122: log.debug("instanceof URI");
123: return Node.createURI(((URI) o).toString());
124: }
125:
126: if (o instanceof String) {
127: // plain literal
128: log.debug("instanceof String");
129: return Node.createLiteral((String) o, null, false);
130: }
131:
132: if (o instanceof PlainLiteral) {
133: // plain literal
134: log.debug("instanceof String");
135: return Node.createLiteral(((PlainLiteral) o).getValue(),
136: null, false);
137: }
138:
139: if (o instanceof DatatypeLiteral) {
140: // datatyped
141: log.debug("instanceof DatatypeLiteral");
142: // there is an issue with jena datatypes
143: // the language (middle part parameter) has to be null
144: // the RDF specification says it must not be set!
145: // (this is left in JENA because of backward compatibility)
146: return Node.createLiteral(((DatatypeLiteral) o).getValue(),
147: null, new GeneralDataType(((DatatypeLiteral) o)
148: .getDatatype()
149: + ""));
150: }
151:
152: if (o instanceof LanguageTagLiteral) {
153: // langtag
154: log.debug("instanceof LanguageTagLiteral");
155: return Node.createLiteral(((LanguageTagLiteral) o)
156: .getValue(), ((LanguageTagLiteral) o)
157: .getLanguageTag(), null);
158: }
159:
160: if (o instanceof BlankNode) {
161: // blank node
162: log.debug("instanceof BlankNode");
163: assert o instanceof AbstractBlankNodeImpl : "expected a BlankNodeImpl and found a "
164: + o.getClass();
165: AnonId anonId = new AnonId(((AbstractBlankNodeImpl) o)
166: .getUnderlyingBlankNode().toString());
167: Node jenaNode = Node.createAnon(anonId);
168: return jenaNode;
169: }
170:
171: if (o instanceof Variable) {
172: // variable
173: log.debug("instanceof Variable");
174: return Node.ANY;
175: }
176:
177: throw new RuntimeException("no transformation from "
178: + o.getClass() + " to jena has been implemented");
179: }
180:
181: /**
182: * Transforms a Jena node into a java object. Possible node types: uri,
183: * variable, literal (datatype, languageTag), blank node
184: *
185: * @param n
186: * The node to transform
187: * @return A specific java object
188: * @throws RuntimeException
189: * @throws ModelRuntimeException
190: */
191: public static org.ontoware.rdf2go.model.node.Node toRDF2Go(Node n)
192: throws ModelRuntimeException {
193: assert n != null;
194: if (n.isURI())
195: return new URIImpl(n.getURI());
196:
197: if (n.isVariable())
198: throw new RuntimeException(
199: "Cannot convert a Jena variable to an RDF2Go node");
200:
201: if (n.isLiteral()) {
202: LiteralLabel lit = n.getLiteral();
203: // datatype
204: if (lit.getDatatypeURI() != null) {
205: return new DatatypeLiteralImpl(lit.getLexicalForm(),
206: new URIImpl(lit.getDatatypeURI()));
207: }
208:
209: // language tagged
210: if (lit.language() != null && !lit.language().equals(""))
211: return new LanguageTagLiteralImpl(lit.getLexicalForm(),
212: lit.language());
213:
214: // plain
215: return new PlainLiteralImpl(lit.getLexicalForm());
216: }
217:
218: if (n.isBlank())
219: return new JenaBlankNode(n);
220:
221: // none of the above - don't know how to transform that
222: throw new RuntimeException("no transformation defined from "
223: + n + " to java");
224: }
225:
226: }
|