001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: ModelAssembler.java,v 1.13 2008/01/03 15:19:05 chris-dollin Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.assemblers;
008:
009: import java.util.*;
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.*;
015: import com.hp.hpl.jena.util.iterator.Map1;
016: import com.hp.hpl.jena.vocabulary.RDF;
017:
018: public abstract class ModelAssembler extends AssemblerBase implements
019: Assembler {
020: protected abstract Model openEmptyModel(Assembler a, Resource root,
021: Mode mode);
022:
023: protected Model openModel(Assembler a, Resource root,
024: Content initial, Mode mode) {
025: Model m = openEmptyModel(a, root, mode);
026: if (!initial.isEmpty())
027: addContent(root, m, initial);
028: return m;
029: }
030:
031: public Object open(Assembler a, Resource root, Mode mode) {
032: Model m = openModel(a, root, getInitialContent(a, root), mode);
033: addContent(root, m, getContent(a, root));
034: m.setNsPrefixes(getPrefixMapping(a, root));
035: return m;
036: }
037:
038: protected void addContent(Resource root, Model m, Content c) {
039: if (m.supportsTransactions()) {
040: m.begin();
041: try {
042: c.fill(m);
043: m.commit();
044: } catch (Throwable t) {
045: m.abort();
046: throw new TransactionAbortedException(root, t);
047: }
048: } else
049: c.fill(m);
050: }
051:
052: public static ReificationStyle getReificationStyle(Resource root) {
053: Resource r = getUniqueResource(root, JA.reificationMode);
054: return r == null ? ReificationStyle.Standard
055: : styleFor(root, r);
056: }
057:
058: public static ReificationStyle styleFor(Resource root, Resource r) {
059: if (r.equals(JA.minimal))
060: return ReificationStyle.Minimal;
061: if (r.equals(JA.standard))
062: return ReificationStyle.Standard;
063: if (r.equals(JA.convenient))
064: return ReificationStyle.Convenient;
065: throw new UnknownStyleException(root, r);
066: }
067:
068: private PrefixMapping getPrefixMapping(Assembler a, Resource root) {
069: return PrefixMappingAssembler.getPrefixes(a, root,
070: PrefixMapping.Factory.create());
071: }
072:
073: public Model openModel(Resource root, Mode mode) {
074: return (Model) open(this , root, mode);
075: }
076:
077: protected Content getInitialContent(Assembler a, Resource root) {
078: Model partial = ModelFactory.createDefaultModel();
079: Resource combined = partial.createResource();
080: for (StmtIterator it = root.listProperties(JA.initialContent); it
081: .hasNext();)
082: transferContentProperties(partial, it.nextStatement()
083: .getResource(), combined);
084: return contentFromModel(a, root, partial, combined);
085: }
086:
087: private Content contentFromModel(Assembler a, Resource root,
088: Model partial, Resource combined) {
089: return partial.isEmpty() ? Content.empty : (Content) a
090: .open(completedClone(root, combined, partial));
091: }
092:
093: protected Content getContent(Assembler a, Resource root) {
094: final Resource newRoot = oneLevelClone(root);
095: final Model fragment = newRoot.getModel();
096: return fragment.isEmpty() ? Content.empty : (Content) a.open(a,
097: completedClone(root, newRoot, fragment));
098: }
099:
100: private Resource completedClone(Resource root, Resource newRoot,
101: Model fragment) {
102: Model typed = fragment.add(newRoot, RDF.type, JA.Content);
103: return (Resource) newRoot.inModel(ModelFactory.createUnion(root
104: .getModel(), typed));
105: }
106:
107: private Resource oneLevelClone(Resource root) {
108: Model partialCopy = ModelFactory.createDefaultModel();
109: Resource newRoot = partialCopy.createResource();
110: transferContentProperties(partialCopy, root, newRoot);
111: return newRoot;
112: }
113:
114: private void transferContentProperties(Model partial,
115: Resource someInitial, Resource combined) {
116: Map1 replace = replaceSubjectMap(partial, combined);
117: for (Iterator it = ContentAssembler.contentProperties
118: .iterator(); it.hasNext();)
119: partial.add(copyProperties(someInitial, replace,
120: (Property) it.next()));
121: }
122:
123: private List copyProperties(Resource root, Map1 replace,
124: Property property) {
125: return root.listProperties(property).mapWith(replace).toList();
126: }
127:
128: private Map1 replaceSubjectMap(final Model inModel,
129: final Resource newSubject) {
130: Map1 replace = new Map1() {
131: public Object map1(Object o) {
132: Statement s = (Statement) o;
133: return inModel.createStatement(newSubject, s
134: .getPredicate(), s.getObject());
135: }
136: };
137: return replace;
138: }
139: }
140:
141: /*
142: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
143: * All rights reserved.
144: *
145: * Redistribution and use in source and binary forms, with or without
146: * modification, are permitted provided that the following conditions
147: * are met:
148: * 1. Redistributions of source code must retain the above copyright
149: * notice, this list of conditions and the following disclaimer.
150: * 2. Redistributions in binary form must reproduce the above copyright
151: * notice, this list of conditions and the following disclaimer in the
152: * documentation and/or other materials provided with the distribution.
153: * 3. The name of the author may not be used to endorse or promote products
154: * derived from this software without specific prior written permission.
155: *
156: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
157: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
158: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
159: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
160: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
161: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
162: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
163: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
164: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
165: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
166: */
|