001: /*
002: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP [See end of file]
003: */
004:
005: package com.hp.hpl.jena.rdf.arp;
006:
007: import java.util.Arrays;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.rdf.arp.impl.ARPSaxErrorHandler;
011: import com.hp.hpl.jena.rdf.model.*;
012: import com.hp.hpl.jena.shared.JenaException;
013: import com.hp.hpl.jena.shared.PrefixMapping;
014: import com.hp.hpl.jena.shared.impl.PrefixMappingImpl;
015:
016: final class JenaHandler extends ARPSaxErrorHandler implements
017: StatementHandler, NamespaceHandler {
018: static private final int BULK_UPDATE_SIZE = 1000;
019:
020: private final BulkUpdateHandler bulk;
021:
022: private final PrefixMapping prefixMapping;
023:
024: protected final Triple triples[];
025:
026: protected int here = 0;
027:
028: public JenaHandler(Model m, RDFErrorHandler e) {
029: this (m.getGraph(), e);
030: }
031:
032: public JenaHandler(Graph g, Model m, RDFErrorHandler e) {
033: this (g, modelToPrefixMapping(m), e);
034: }
035:
036: private JenaHandler(Graph graph, RDFErrorHandler e) {
037: this (graph, graph.getPrefixMapping(), e);
038: }
039:
040: private JenaHandler(Graph graph, PrefixMapping prefixMapping,
041: RDFErrorHandler errorHandler) {
042: super (errorHandler);
043: this .bulk = graph.getBulkUpdateHandler();
044: this .triples = new Triple[BULK_UPDATE_SIZE];
045: this .prefixMapping = prefixMapping;
046: }
047:
048: private static PrefixMapping modelToPrefixMapping(Model model) {
049: return model == null ? PrefixMapping.Factory.create() : model
050: .getGraph().getPrefixMapping();
051: }
052:
053: public void useWith(ARPHandlers h) {
054: h.setStatementHandler(this );
055: h.setErrorHandler(this );
056: h.setNamespaceHandler(this );
057: }
058:
059: public void statement(AResource subj, AResource pred, AResource obj) {
060: try {
061: triples[here++] = JenaReader.convert(subj, pred, obj);
062: } catch (JenaException e) {
063: errorHandler.error(e);
064: }
065: if (here == BULK_UPDATE_SIZE)
066: bulkUpdate();
067: }
068:
069: public void statement(AResource subj, AResource pred, ALiteral lit) {
070: try {
071: triples[here++] = JenaReader.convert(subj, pred, lit);
072: } catch (JenaException e) {
073: errorHandler.error(e);
074: }
075: if (here == BULK_UPDATE_SIZE)
076: bulkUpdate();
077: }
078:
079: public void bulkUpdate() {
080: try {
081: if (here == BULK_UPDATE_SIZE)
082: bulk.add(triples);
083: else
084: bulk.add(Arrays.asList(triples).subList(0, here));
085: } catch (JenaException e) {
086: errorHandler.error(e);
087: } finally {
088: here = 0;
089: }
090: }
091:
092: public void startPrefixMapping(String prefix, String uri) {
093: if (PrefixMappingImpl.isNiceURI(uri))
094: prefixMapping.setNsPrefix(prefix, uri);
095: }
096:
097: public void endPrefixMapping(String prefix) {
098: }
099: }
100:
101: /*
102: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP All
103: * rights reserved.
104: *
105: * Redistribution and use in source and binary forms, with or without
106: * modification, are permitted provided that the following conditions are met:
107: * 1. Redistributions of source code must retain the above copyright notice,
108: * this list of conditions and the following disclaimer. 2. Redistributions in
109: * binary form must reproduce the above copyright notice, this list of
110: * conditions and the following disclaimer in the documentation and/or other
111: * materials provided with the distribution. 3. The name of the author may not
112: * be used to endorse or promote products derived from this software without
113: * specific prior written permission.
114: *
115: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
116: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
117: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
118: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
119: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
120: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
121: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
122: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
123: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
124: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
125: */
|