KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > arp > JenaHandler


1 /*
2  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP [See end of file]
3  */

4
5 package com.hp.hpl.jena.rdf.arp;
6
7 import java.util.Arrays JavaDoc;
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 JavaDoc prefix, String JavaDoc uri) {
89         if (model != null && PrefixMappingImpl.isNiceURI(uri))
90             model.setNsPrefix(prefix, uri);
91     }
92
93     public void endPrefixMapping(String JavaDoc prefix) {
94     }
95 }
96
97 /*
98  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP All rights
99  * reserved.
100  *
101  * Redistribution and use in source and binary forms, with or without
102  * modification, are permitted provided that the following conditions are met:
103  * 1. Redistributions of source code must retain the above copyright notice,
104  * this list of conditions and the following disclaimer. 2. Redistributions in
105  * binary form must reproduce the above copyright notice, this list of
106  * conditions and the following disclaimer in the documentation and/or other
107  * materials provided with the distribution. 3. The name of the author may not
108  * be used to endorse or promote products derived from this software without
109  * specific prior written permission.
110  *
111  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
112  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
113  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
114  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
115  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
116  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
117  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
118  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
119  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
120  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
121  */

122
123
Popular Tags