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