001: /*
002: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * (c) Copyright 2003, Plugged In Software
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: * 3. The name of the author may not be used to endorse or promote products
016: * derived from this software without specific prior written permission.
017:
018: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
019: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
020: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
021: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
022: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
023: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
024: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
025: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
027: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: $Id: ARP.java,v 1.28 2008/01/02 12:06:46 andy_seaborne Exp $
030: AUTHOR: Jeremy J. Carroll
031: with modification from PI Software
032: */
033: /*
034: * ARP.java
035: *
036: * Created on June 22, 2001, 6:20 AM
037: *
038: * *
039: *Possible options
040: *
041: * embedded RDF
042: *
043: *
044: *
045: */
046:
047: package com.hp.hpl.jena.rdf.arp;
048:
049: import java.io.IOException;
050: import java.io.InputStream;
051: import java.io.Reader;
052:
053: import org.xml.sax.ErrorHandler;
054: import org.xml.sax.InputSource;
055: import org.xml.sax.Locator;
056: import org.xml.sax.SAXException;
057:
058: import com.hp.hpl.jena.rdf.arp.impl.RDFXMLParser;
059:
060: /** Another RDF Parser.
061: * To load an RDF file:
062: * <ol>
063: * <li>Create an ARP.</li>
064: * <li>Set its handlers, by calling the {@link #getHandlers}
065: * method, and then.</li>
066: * <ul>
067: * <li>Setting the statement handler.</li>
068: * <li>Optionally setting the other handlers.</li>
069: * </ul>
070: *
071: * </li>
072: * <li>Call a load method.</li>
073: * </ol>
074: * <p>
075: * Xerces is used for parsing the XML.
076: * The SAXEvents generated by Xerces are then
077: * analysed as RDF by ARP.
078: * Errors may occur
079: * in either the XML or the RDF part, see
080: * {@link ARPHandlers#setErrorHandler} for details
081: * of how to distinguish between them.</p>
082: * <p>
083: * For very large files, ARP does not use any additional
084: * memory except when either the {@link ExtendedHandler#discardNodesWithNodeID}
085: * returns false or when the {@link AResource#setUserData} method has been
086: * used. In these cases ARP needs to remember the <code>rdf:nodeID</code>
087: * usage through the file life time. </p>
088: * <p>See <a href="../../../../../../../ARP/index.html">ARP documentation</a> for more information.</p>
089: * @author Jeremy Carroll with contributions from Simon Raboczi
090: * and Andrew Newman
091: */
092: public class ARP implements ARPConfig {
093:
094: final private RDFXMLParser arpf;
095:
096: /** Creates a new RDF Parser.
097: * Can parse one file at a time.
098: */
099: public ARP() {
100: arpf = RDFXMLParser.create();
101: }
102:
103: /**
104: * When parsing a file, this returns a Locator giving the
105: * position of the last XML event processed by ARP.
106: * This may return null or misleading results before any
107: * tokens have been processed.
108: * @return Locator
109: */
110: public Locator getLocator() {
111: return arpf.getLocator();
112: }
113:
114: /** Load RDF/XML from a Reader.
115: * @param in The input XML document.
116: * @param xmlBase The base URI for the document.
117: * @throws SAXException More serious error during XML or RDF processing; or thrown from the ErrorHandler.
118: * @throws IOException Occurring during XML processing.
119: */
120: public void load(Reader in, String xmlBase) throws SAXException,
121: IOException {
122: InputSource inputS = new InputSource(in);
123: inputS.setSystemId(xmlBase);
124: // arpf.initParse(xmlBase,"");
125: arpf.parse(inputS);
126: }
127:
128: void load(InputSource is) throws SAXException, IOException {
129:
130: // arpf.initParse("","");
131: arpf.parse(is);
132: }
133:
134: /** Load RDF/XML from an InputStream.
135: * @param in The input XML document.
136: * @param xmlBase The base URI for the document.
137: * @throws SAXException More serious error during XML or RDF processing; or thrown from the ErrorHandler.
138: * @throws IOException Occurring during XML processing.
139: */
140: public void load(InputStream in, String xmlBase)
141: throws SAXException, IOException {
142: //load(new InputStreamReader(in),xmlBase);
143: InputSource inputS = new InputSource(in);
144: inputS.setSystemId(xmlBase);
145: // arpf.initParse(xmlBase,"");
146: arpf.parse(inputS, xmlBase);
147: }
148:
149: /** Load RDF/XML from an InputStream, leaving relative URIs as relative.
150: * @param in The input XML document.
151: * @throws SAXException More serious error during XML or RDF processing; or thrown from the ErrorHandler.
152: * @throws IOException Occurring during XML processing.
153: */
154: public void load(InputStream in) throws SAXException, IOException {
155: load(in, "");
156: }
157:
158: /** Load RDF/XML from a Reader, leaving relative URIs as relative.
159: * @param in The input XML document.
160: * @throws SAXException More serious error during XML or RDF processing; or thrown from the ErrorHandler.
161: * @throws IOException Occurring during XML processing.
162: */
163: public void load(Reader in) throws SAXException, IOException {
164: load(in, "");
165: }
166:
167: /**
168: * The handlers used during parsing.
169: * The handlers can be changed by calling this method
170: * and then using the <code>set..Handler</code> methods
171: * in {@link ARPHandlers}.
172: * The handlers can be copied onto another ARP instance
173: * using the {@link #setHandlersWith} method.
174: * @see ARPHandlers#setStatementHandler(StatementHandler)
175: * @see ARPHandlers#setErrorHandler(ErrorHandler)
176: * @see ARPHandlers#setExtendedHandler(ExtendedHandler)
177: * @see ARPHandlers#setNamespaceHandler(NamespaceHandler)
178: * @see #setHandlersWith
179: * @return The handlers used during parsing.
180: */
181: public ARPHandlers getHandlers() {
182: return arpf.getHandlers();
183: }
184:
185: /**
186: * Copies the handlers from the argument
187: * to be used by this instance.
188: * To make further modifications it is necessary
189: * to call {@link #getHandlers} to retrieve this
190: * instance's copy of the handler information.
191: * @param handlers The new values to use.
192: */
193: public void setHandlersWith(ARPHandlers handlers) {
194: arpf.setHandlersWith(handlers);
195: }
196:
197: /**
198: * The options used during parsing.
199: * The options can be changed by calling this method
200: * and then using the <code>set..</code> methods
201: * in {@link ARPOptions}.
202: * The options can be copied onto another ARP instance
203: * using the {@link #setOptionsWith} method.
204: * @see ARPOptions#setDefaultErrorMode()
205: * @see ARPOptions#setLaxErrorMode()
206: * @see ARPOptions#setStrictErrorMode()
207: * @see ARPOptions#setStrictErrorMode(int)
208: * @see ARPOptions#setEmbedding(boolean)
209: * @see ARPOptions#setErrorMode(int, int)
210: *
211: * @see #setOptionsWith
212: * @return The handlers used during parsing.
213: */
214:
215: public ARPOptions getOptions() {
216: return arpf.getOptions();
217: }
218:
219: /**
220: * Copies the options from the argument
221: * to be used by this instance.
222: * To make further modifications it is necessary
223: * to call {@link #getOptions} to retrieve this
224: * instance's copy of the options.
225: * @param opts The new values to use.
226: */
227: public void setOptionsWith(ARPOptions opts) {
228: arpf.setOptionsWith(opts);
229: }
230:
231: /**
232: @deprecated Use {@link #getHandlers}.{@link ARPHandlers#setExtendedHandler setExtendedHandler(eh)}
233: */
234: public ExtendedHandler setExtendedHandler(ExtendedHandler eh) {
235:
236: return getHandlers().setExtendedHandler(eh);
237: }
238:
239: /**
240: @deprecated Use {@link #getHandlers}.{@link ARPHandlers#setNamespaceHandler setNamespaceHandler(nh)}
241: */
242: public NamespaceHandler setNamespaceHandler(NamespaceHandler nh) {
243:
244: return getHandlers().setNamespaceHandler(nh);
245: }
246:
247: /**
248: @deprecated Use {@link #getHandlers}.{@link ARPHandlers#setStatementHandler setStatementHandler(sh)}
249: */
250: public StatementHandler setStatementHandler(StatementHandler sh) {
251:
252: return getHandlers().setStatementHandler(sh);
253: }
254:
255: /**
256: @deprecated Use {@link #getHandlers}.{@link ARPHandlers#setErrorHandler setErrorHandler(eh)}
257: */
258: public void setErrorHandler(ErrorHandler eh) {
259: getHandlers().setErrorHandler(eh);
260: }
261:
262: /**
263: *
264: @deprecated Use {@link #getOptions}.{@link ARPOptions#setErrorMode(int,int) setErrorMode(errno,mode)}
265: */
266: public int setErrorMode(int errno, int mode) {
267: return getOptions().setErrorMode(errno, mode);
268:
269: }
270:
271: /**
272: @deprecated Use {@link #getOptions}.{@link ARPOptions#setDefaultErrorMode() setDefaultErrorMode()}
273: */
274: public void setDefaultErrorMode() {
275:
276: getOptions().setDefaultErrorMode();
277: }
278:
279: /**
280: @deprecated Use {@link #getOptions}.{@link ARPOptions#setLaxErrorMode() setLaxErrorMode()}
281: */
282: public void setLaxErrorMode() {
283:
284: getOptions().setLaxErrorMode();
285: }
286:
287: /**
288: @deprecated Use {@link #getOptions}.{@link ARPOptions#setStrictErrorMode() setStrictErrorMode()}
289: */
290: public void setStrictErrorMode() {
291:
292: getOptions().setStrictErrorMode();
293: }
294:
295: /**
296: @deprecated Use {@link #getOptions}.{@link ARPOptions#setStrictErrorMode(int) setStrictErrorMode(nonErrorMode)}
297: */
298: public void setStrictErrorMode(int nonErrorMode) {
299:
300: getOptions().setStrictErrorMode(nonErrorMode);
301: }
302:
303: /**
304: @deprecated Use {@link #getOptions}.{@link ARPOptions#setEmbedding(boolean) setEmbedding(embed)}
305: */
306: public void setEmbedding(boolean embed) {
307:
308: getOptions().setEmbedding(embed);
309: }
310:
311: void setBadStatementHandler(StatementHandler sh) {
312: arpf.setBadStatementHandler(sh);
313:
314: }
315: }
|