001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: AssemblerBase.java,v 1.12 2008/01/02 12:09:36 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.assemblers;
008:
009: import java.util.List;
010:
011: import com.hp.hpl.jena.assembler.*;
012: import com.hp.hpl.jena.assembler.exceptions.*;
013: import com.hp.hpl.jena.rdf.model.*;
014: import com.hp.hpl.jena.shared.JenaException;
015: import com.hp.hpl.jena.util.IteratorCollection;
016: import com.hp.hpl.jena.util.iterator.Map1;
017: import com.hp.hpl.jena.vocabulary.RDF;
018:
019: public abstract class AssemblerBase implements Assembler {
020: protected static class MapObjectToContent implements Map1 {
021: protected final Assembler a;
022:
023: public MapObjectToContent(Assembler a) {
024: this .a = a;
025: }
026:
027: public Object map1(Object o) {
028: return a.open(getResource((Statement) o));
029: }
030: }
031:
032: static final Map1 getObject = new Map1() {
033: public Object map1(Object o) {
034: return ((Statement) o).getObject();
035: }
036: };
037:
038: public final Object open(Resource root) {
039: return open(this , root);
040: }
041:
042: public final Object open(Assembler a, Resource root) {
043: return open(a, root, Mode.DEFAULT);
044: }
045:
046: public abstract Object open(Assembler a, Resource root, Mode mode);
047:
048: protected static Resource getUniqueResource(Resource root,
049: Property property) {
050: return (Resource) getUnique(root, property);
051: }
052:
053: protected static Literal getUniqueLiteral(Resource root,
054: Property property) {
055: return (Literal) getUnique(root, property);
056: }
057:
058: protected static Statement getUniqueStatement(Resource root,
059: Property property) {
060: List statements = IteratorCollection.iteratorToList(root
061: .listProperties(property));
062: if (statements.size() == 0)
063: return null;
064: if (statements.size() == 1)
065: return (Statement) statements.get(0);
066: throw new NotUniqueException(root, property);
067: }
068:
069: protected static RDFNode getUnique(Resource root, Property property) {
070: List nodes = IteratorCollection.iteratorToList(root
071: .listProperties(property).mapWith(getObject));
072: if (nodes.size() == 0)
073: return null;
074: if (nodes.size() == 1)
075: return (RDFNode) nodes.get(0);
076: throw new NotUniqueException(root, property);
077: }
078:
079: protected void checkType(Resource root, Resource type) {
080: if (!root.hasProperty(RDF.type, type))
081: throw new CannotConstructException(this .getClass(), root,
082: type);
083: }
084:
085: public Model openModel(Resource root, Mode mode) {
086: return (Model) open(this , root, mode);
087: }
088:
089: public Model openModel(Resource root) {
090: return openModel(root, Mode.DEFAULT);
091: }
092:
093: public static Resource getRequiredResource(Resource root, Property p) {
094: Resource R = getUniqueResource(root, p);
095: if (R == null)
096: throw new PropertyRequiredException(root, p);
097: return R;
098: }
099:
100: protected Literal getRequiredLiteral(Resource root, Property p) {
101: Literal L = getUniqueLiteral(root, p);
102: if (L == null)
103: throw new PropertyRequiredException(root, p);
104: return L;
105: }
106:
107: protected static Resource getResource(Statement s) {
108: return AssemblerHelp.getResource(s);
109: }
110:
111: protected static String getString(Statement s) {
112: return AssemblerHelp.getString(s);
113: }
114:
115: protected static String getUniqueString(Resource root,
116: Property property) {
117: Statement s = getUniqueStatement(root, property);
118: return s == null ? null : AssemblerHelp.getString(s);
119: }
120:
121: protected static Class loadClass(Resource root, String className) {
122: try {
123: return Class.forName(className);
124: } catch (ClassNotFoundException e) {
125: throw new CannotLoadClassException(root, className, e);
126: }
127: }
128:
129: /**
130: Answer the string described by the value of the unique optional
131: <code>classProperty</code> property of <code>root</code>,
132: or null if there's no such property. The value may be a URI, in which case
133: it must be a <b>java:</b> URI with content the class name; or it may
134: be a literal, in which case its lexical form is its class name; otherwise,
135: BOOM.
136: */
137: public static String getOptionalClassName(Resource root,
138: Property classProperty) {
139: RDFNode classNode = getUnique(root, classProperty);
140: return classNode == null ? null
141: : classNode.isLiteral() ? classNode.asNode()
142: .getLiteralLexicalForm() : classNode
143: .isResource() ? mustBeJava(classNode.asNode()
144: .getURI()) : null;
145: }
146:
147: /**
148: Throw an exception if <code>uri</code> doesn't start with "java:",
149: otherwise answer the string beyond the ":".
150: */
151: private static String mustBeJava(String uri) { // TODO replace JenaException
152: if (uri.startsWith("java:"))
153: return uri.substring(5);
154: throw new JenaException(
155: "class name URI must start with 'java:': " + uri);
156: }
157: }
158:
159: /*
160: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
161: * All rights reserved.
162: *
163: * Redistribution and use in source and binary forms, with or without
164: * modification, are permitted provided that the following conditions
165: * are met:
166: * 1. Redistributions of source code must retain the above copyright
167: * notice, this list of conditions and the following disclaimer.
168: * 2. Redistributions in binary form must reproduce the above copyright
169: * notice, this list of conditions and the following disclaimer in the
170: * documentation and/or other materials provided with the distribution.
171: * 3. The name of the author may not be used to endorse or promote products
172: * derived from this software without specific prior written permission.
173: *
174: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
175: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
176: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
177: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
178: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
179: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
180: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
181: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
182: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
183: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
184: */
|