001 /*
002 * (c) Copyright 2009 University of Bristol
003 * All rights reserved.
004 * [See end of file]
005 */
006 package net.rootdev.javardfa;
007
008 import com.hp.hpl.jena.rdf.model.Literal;
009 import com.hp.hpl.jena.rdf.model.Model;
010 import com.hp.hpl.jena.rdf.model.Property;
011 import com.hp.hpl.jena.rdf.model.Resource;
012 import java.util.HashMap;
013 import java.util.Map;
014
015 /**
016 * @author Damian Steer <pldms@mac.com>
017 */
018 public class JenaStatementSink implements StatementSink {
019
020 private final Model model;
021 private Map<String, Resource> bnodeLookup;
022
023 public JenaStatementSink(Model model) {
024 this.model = model;
025 }
026
027 //@Override
028 public void start() {
029 bnodeLookup = new HashMap<String, Resource>();
030 }
031
032 //@Override
033 public void end() {
034 bnodeLookup = null;
035 }
036
037 //@Override
038 public void addObject(String subject, String predicate, String object) {
039 Resource s = getResource(subject);
040 Property p = model.createProperty(predicate);
041 Resource o = getResource(object);
042 model.add(s, p, o);
043 }
044
045 //@Override
046 public void addLiteral(String subject, String predicate, String lex, String lang, String datatype) {
047 Resource s = getResource(subject);
048 Property p = model.createProperty(predicate);
049 Literal o;
050 if (lang == null && datatype == null) {
051 o = model.createLiteral(lex);
052 } else if (lang != null) {
053 o = model.createLiteral(lex, lang);
054 } else {
055 o = model.createTypedLiteral(lex, datatype);
056 }
057 model.add(s, p, o);
058 }
059
060 private Resource getResource(String res) {
061 if (res.startsWith("_:")) {
062 if (bnodeLookup.containsKey(res)) {
063 return bnodeLookup.get(res);
064 }
065 Resource bnode = model.createResource();
066 bnodeLookup.put(res, bnode);
067 return bnode;
068 } else {
069 return model.createResource(res);
070 }
071 }
072 }
073
074 /*
075 * (c) Copyright 2009 University of Bristol
076 * All rights reserved.
077 *
078 * Redistribution and use in source and binary forms, with or without
079 * modification, are permitted provided that the following conditions
080 * are met:
081 * 1. Redistributions of source code must retain the above copyright
082 * notice, this list of conditions and the following disclaimer.
083 * 2. Redistributions in binary form must reproduce the above copyright
084 * notice, this list of conditions and the following disclaimer in the
085 * documentation and/or other materials provided with the distribution.
086 * 3. The name of the author may not be used to endorse or promote products
087 * derived from this software without specific prior written permission.
088 *
089 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
090 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
091 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
092 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
093 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
094 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
095 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
096 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
097 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
098 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
099 */