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