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