001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: ContentAssembler.java,v 1.13 2008/01/02 16:16:34 chris-dollin Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.assemblers;
008:
009: import java.io.StringReader;
010: import java.util.*;
011:
012: import com.hp.hpl.jena.assembler.*;
013: import com.hp.hpl.jena.assembler.exceptions.UnknownEncodingException;
014: import com.hp.hpl.jena.graph.Node;
015: import com.hp.hpl.jena.rdf.model.*;
016: import com.hp.hpl.jena.util.*;
017: import com.hp.hpl.jena.vocabulary.*;
018:
019: public class ContentAssembler extends AssemblerBase implements
020: Assembler {
021: protected final FileManager defaultFileManager;
022:
023: public ContentAssembler() {
024: this (null);
025: }
026:
027: public ContentAssembler(FileManager fm) {
028: this .defaultFileManager = fm;
029: }
030:
031: public Object open(Assembler a, Resource root, Mode irrelevant) {
032: checkType(root, JA.Content);
033: return new Content(loadContent(new ArrayList(), a, root));
034: }
035:
036: public static Set contentProperties = new HashSetWith().with(
037: JA.content).with(JA.literalContent)
038: .with(JA.externalContent).with(JA.quotedContent);
039:
040: static class HashSetWith extends HashSet {
041: public HashSetWith with(Object x) {
042: this .add(x);
043: return this ;
044: }
045: }
046:
047: public List loadContent(List contents, Assembler a, Resource root) {
048: FileManager fm = getFileManager(a, root);
049: addLiteralContent(contents, root);
050: addQuotedContent(contents, root);
051: addExternalContents(contents, fm, root);
052: addIndirectContent(contents, a, root);
053: return contents;
054: }
055:
056: private static void addIndirectContent(List contents, Assembler a,
057: Resource root) {
058: StmtIterator it = root.listProperties(JA.content);
059: while (it.hasNext())
060: contents.add(a.open(getResource(it.nextStatement())));
061: }
062:
063: protected void addExternalContents(List contents, FileManager fm,
064: Resource root) {
065: StmtIterator it = root.listProperties(JA.externalContent);
066: while (it.hasNext())
067: contents.add(objectAsContent(fm, it.nextStatement()));
068: }
069:
070: private static void addQuotedContent(List contents, Resource root) {
071: StmtIterator it = root.listProperties(JA.quotedContent);
072: while (it.hasNext()) {
073: Resource q = getResource(it.nextStatement());
074: Model m = ResourceUtils.reachableClosure(q);
075: contents.add(newModelContent(m));
076: }
077: }
078:
079: protected static void addLiteralContent(List contents, Resource root) {
080: String encoding = getEncoding(root);
081: StmtIterator it = root.listProperties(JA.literalContent);
082: while (it.hasNext()) {
083: String s = getString(it.nextStatement());
084: Model model = parseAs(root, encoding, s);
085: contents.add(newModelContent(model));
086: }
087: }
088:
089: private static Model parseAs(Resource root, String encoding,
090: String lexicalForm) {
091: String enc = encoding == null ? guessFrom(lexicalForm)
092: : encoding;
093: if (enc.equals("N3"))
094: return parseAsN3(lexicalForm);
095: if (enc.equals("RDF/XML"))
096: return parseAsXML(lexicalForm);
097: throw new UnknownEncodingException(root, encoding);
098: }
099:
100: private static Model parseAsXML(String lexicalForm) {
101: String pre = "<?xml version='1.0'?>"
102: + "<rdf:RDF"
103: + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'"
104: + " xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#'"
105: + " xmlns:xsd='http://www.w3.org/2001/XMLSchema#'"
106: + " xmlns:owl='http://www.w3.org/2002/07/owl#'"
107: + " xmlns:dc='http://purl.org/dc/elements/1.1/'" + ">";
108: String post = "</rdf:RDF>";
109: StringReader r = new StringReader(pre + lexicalForm + post);
110: return ModelFactory.createDefaultModel().read(r, "", "RDF/XML");
111: }
112:
113: private static String guessFrom(String lexicalForm) {
114: return "N3";
115: }
116:
117: private static String getEncoding(Resource root) {
118: Literal L = getUniqueLiteral(root, JA.contentEncoding);
119: return L == null ? null : L.getLexicalForm();
120: }
121:
122: protected static Content newModelContent(final Model m) {
123: return new Content() {
124: public Model fill(Model x) {
125: x.setNsPrefixes(m);
126: return x.add(m);
127: }
128:
129: public boolean isEmpty() {
130: return m.isEmpty();
131: }
132: };
133: }
134:
135: protected Content objectAsContent(FileManager fm, Statement s) {
136: final Model m = fm.loadModel(getModelName(s));
137: return newModelContent(m);
138: }
139:
140: private String getModelName(Statement s) {
141: Node o = s.getObject().asNode();
142: return o.isLiteral() ? o.getLiteralLexicalForm() : o.getURI();
143: }
144:
145: private FileManager getFileManager(Assembler a, Resource root) {
146: Resource fm = getUniqueResource(root, JA.fileManager);
147: return fm != null ? (FileManager) a.open(fm)
148: : defaultFileManager == null ? FileManager.get()
149: : defaultFileManager;
150: }
151:
152: static final String preamble = "@prefix rdf: <" + RDF.getURI()
153: + "> ." + "\n@prefix rdfs: <" + RDFS.getURI() + "> ."
154: + "\n@prefix owl: <" + OWL.getURI() + "> ."
155: + "\n@prefix xsd: <http://www.w3.org/2001/XMLSchema#> ."
156: + "\n@prefix dc: <" + DC_11.getURI() + "> .";
157:
158: protected static Model parseAsN3(String value) {
159: Model result = ModelFactory.createDefaultModel();
160: StringReader r = new StringReader(preamble + "\n" + value);
161: result.read(r, "", "N3");
162: return result;
163: }
164:
165: public Object getFileManager() {
166: return defaultFileManager;
167: }
168: }
169:
170: /*
171: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
172: * All rights reserved.
173: *
174: * Redistribution and use in source and binary forms, with or without
175: * modification, are permitted provided that the following conditions
176: * are met:
177: * 1. Redistributions of source code must retain the above copyright
178: * notice, this list of conditions and the following disclaimer.
179: * 2. Redistributions in binary form must reproduce the above copyright
180: * notice, this list of conditions and the following disclaimer in the
181: * documentation and/or other materials provided with the distribution.
182: * 3. The name of the author may not be used to endorse or promote products
183: * derived from this software without specific prior written permission.
184: *
185: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
188: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
189: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
190: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
191: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
192: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
193: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
194: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
195: */
|