1 4 5 package com.hp.hpl.jena.rdf.arp; 6 7 import java.util.Arrays ; 8 9 import com.hp.hpl.jena.graph.BulkUpdateHandler; 10 import com.hp.hpl.jena.graph.Triple; 11 import com.hp.hpl.jena.rdf.model.Model; 12 import com.hp.hpl.jena.rdf.model.RDFErrorHandler; 13 import com.hp.hpl.jena.shared.JenaException; 14 import com.hp.hpl.jena.shared.impl.PrefixMappingImpl; 15 import com.hp.hpl.jena.graph.*; 16 17 final class JenaHandler extends ARPSaxErrorHandler implements StatementHandler, 18 NamespaceHandler { 19 static private final int BULK_UPDATE_SIZE = 1000; 20 21 private final BulkUpdateHandler bulk; 22 23 private final Model model; 24 25 final Triple triples[]; 26 27 int ix = 0; 28 JenaHandler(Model m, 29 RDFErrorHandler e) { 30 this(m.getGraph(),m,e); 31 } 32 JenaHandler(Graph g, 33 RDFErrorHandler e) { 34 this(g,null,e); 35 } 36 JenaHandler(Graph g, Model m, 37 RDFErrorHandler e) { 38 this(g.getBulkUpdateHandler(),m,e); 39 } 40 private JenaHandler(BulkUpdateHandler bulk, Model model, 41 RDFErrorHandler errorHandler) { 42 super(errorHandler); 43 this.bulk = bulk; 44 this.model = model; 45 triples = new Triple[BULK_UPDATE_SIZE]; 46 } 47 48 void useWith(ARPHandlers h) { 49 h.setStatementHandler(this); 50 h.setErrorHandler(this); 51 h.setNamespaceHandler(this); 52 } 53 54 public void statement(AResource subj, AResource pred, AResource obj) { 55 try { 56 triples[ix++] = JenaReader.convert(subj, pred, obj); 57 } catch (JenaException e) { 58 errorHandler.error(e); 59 } 60 if (ix == BULK_UPDATE_SIZE) 61 bulkUpdate(); 62 } 63 64 public void statement(AResource subj, AResource pred, ALiteral lit) { 65 try { 66 triples[ix++] = JenaReader.convert(subj, pred, lit); 67 } catch (JenaException e) { 68 errorHandler.error(e); 69 } 70 if (ix == BULK_UPDATE_SIZE) 71 bulkUpdate(); 72 } 73 74 void bulkUpdate() { 75 try { 76 if (ix == BULK_UPDATE_SIZE) 77 bulk.add(triples); 78 else 79 bulk.add(Arrays.asList(triples).subList(0, ix)); 80 81 } catch (JenaException e) { 82 errorHandler.error(e); 83 } finally { 84 ix = 0; 85 } 86 } 87 88 public void startPrefixMapping(String prefix, String uri) { 89 if (model != null && PrefixMappingImpl.isNiceURI(uri)) 90 model.setNsPrefix(prefix, uri); 91 } 92 93 public void endPrefixMapping(String prefix) { 94 } 95 } 96 97 122 123 | Popular Tags |