001: /*
002: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * [See end of file]
004: */
005:
006: package com.hp.hpl.jena.rdf.arp;
007:
008: import org.xml.sax.SAXParseException;
009:
010: import com.hp.hpl.jena.rdf.model.Model;
011: import com.hp.hpl.jena.rdf.model.RDFErrorHandler;
012: import com.hp.hpl.jena.rdf.model.impl.RDFDefaultErrorHandler;
013: import com.hp.hpl.jena.shared.JenaException;
014:
015: /**
016: * Use arbitrary SAX input to Jena. See <a
017: * href="../../../../../../../ARP/sax.html">ARP SAX documentation</a>.
018: *
019: * @author Jeremy J. Carroll
020: *
021: */
022: public class SAX2Model extends SAX2RDF {
023: /**
024: * Factory method to create a new SAX2Model.
025: *
026: * @param base
027: * The retrieval URL, or the base URI to be used while parsing.
028: * @param m
029: * A Jena Model in which to put the triples, this can be null. If
030: * it is null, then use {@link SAX2RDF#getHandlers} or
031: * {@link SAX2RDF#setHandlersWith} to provide a
032: * {@link StatementHandler}, and usually an
033: * {@link org.xml.sax.ErrorHandler}
034: * @return A new SAX2Model
035: * @throws MalformedURIException
036: * @deprecated Use {@link #create(String, Model)}
037: */
038: static public SAX2Model newInstance(String base, Model m)
039: throws MalformedURIException {
040: return new SAX2Model(base, m, "");
041:
042: }
043:
044: /**
045: * Factory method to create a new SAX2Model.
046: *
047: * @param base
048: * The retrieval URL, or the base URI to be used while parsing.
049: * @param m
050: * A Jena Model in which to put the triples, this can be null. If
051: * it is null, then use {@link SAX2RDF#getHandlers} or
052: * {@link SAX2RDF#setHandlersWith} to provide a
053: * {@link StatementHandler}, and usually an
054: * {@link org.xml.sax.ErrorHandler}
055: * @return A new SAX2Model
056: * @throws SAXParseException
057: * On a fatal error during setup, maybe malformed base URI
058: */
059: static public SAX2Model create(String base, Model m)
060: throws SAXParseException {
061: return new SAX2Model(base, m, "", 0);
062: }
063:
064: /**
065: * Factory method to create a new SAX2Model. This is particularly intended
066: * for when parsing a non-root element within an XML document. In which case
067: * the application needs to find this value in the outer context.
068: * Optionally, namespace prefixes can be passed from the outer context using
069: * {@link #startPrefixMapping}.
070: *
071: * @param base
072: * The retrieval URL, or the base URI to be used while parsing.
073: * @param m
074: * A Jena Model in which to put the triples, this can be null. If
075: * it is null, then use {@link SAX2RDF#getHandlers} or
076: * {@link SAX2RDF#setHandlersWith} to provide a
077: * {@link StatementHandler}, and usually an
078: * {@link org.xml.sax.ErrorHandler}
079: * @param lang
080: * The current value of <code>xml:lang</code> when parsing
081: * starts, usually "".
082: * @return A new SAX2Model
083: * @throws SAXParseException
084: * On a fatal error during setup, maybe malformed base URI
085: */
086: static public SAX2Model create(String base, Model m, String lang)
087: throws SAXParseException {
088: return new SAX2Model(base, m, lang, 0);
089: }
090:
091: /**
092: * Factory method to create a new SAX2Model. This is particularly intended
093: * for when parsing a non-root element within an XML document. In which case
094: * the application needs to find this value in the outer context.
095: * Optionally, namespace prefixes can be passed from the outer context using
096: * {@link #startPrefixMapping}.
097: *
098: * @param base
099: * The retrieval URL, or the base URI to be used while parsing.
100: * @param m
101: * A Jena Model in which to put the triples, this can be null. If
102: * it is null, then use {@link SAX2RDF#getHandlers} or
103: * {@link SAX2RDF#setHandlersWith} to provide a
104: * {@link StatementHandler}, and usually an
105: * {@link org.xml.sax.ErrorHandler}
106: * @param lang
107: * The current value of <code>xml:lang</code> when parsing
108: * starts, usually "".
109: * @return A new SAX2Model
110: * @throws MalformedURIException
111: * @deprecated Use {@link #create(String, Model, String)}
112: */
113: static public SAX2Model newInstance(String base, Model m,
114: String lang) throws MalformedURIException {
115:
116: return new SAX2Model(base, m, lang);
117: }
118:
119: /**
120: * Begin the scope of a prefix-URI Namespace mapping.
121: *
122: * <p>
123: * This is passed to any {@link NamespaceHandler} associated with this
124: * parser. It can be called before the initial <code>startElement</code>
125: * event, or other events associated with the elements being processed. When
126: * building a Jena Model, it is not required to match this with
127: * corresponding <code>endPrefixMapping</code> events. Other
128: * {@link NamespaceHandler}s may be fussier. When building a Jena Model,
129: * the prefix bindings are remembered with the Model, and may be used in
130: * some output routines. It is permitted to not call this method for
131: * prefixes declared in the outer context, in which case, any output routine
132: * will need to use a gensym for such namespaces.
133: * </p>
134: *
135: * @param prefix
136: * The Namespace prefix being declared.
137: * @param uri
138: * The Namespace URI the prefix is mapped to.
139: *
140: */
141: public void startPrefixMapping(String prefix, String uri)
142: throws SAXParseException {
143: super .startPrefixMapping(prefix, uri);
144: }
145:
146: private RDFErrorHandler errorHandler = new RDFDefaultErrorHandler();
147:
148: final private JenaHandler handler;
149:
150: // TODO: deprecate, and make throw MalformedURIException
151: /**
152: * Constructor, see {@link #create(String, Model, String)} for top-level
153: * javadoc.
154: *
155: * @param base
156: * @param m
157: * @param lang
158: * @throws MalformedURIException
159: * (If base is malformed, and treated as error rather than
160: * warning)
161: */
162: protected SAX2Model(String base, Model m, String lang)
163: throws MalformedURIException {
164: super (base, lang, true);
165: handler = initHandler(m);
166: initParseX(base, lang);
167: }
168:
169: protected SAX2Model(String base, Model m, String lang, int dummy)
170: throws SAXParseException {
171: super (base, lang, true);
172: handler = initHandler(m);
173: initParse(base, lang);
174: }
175:
176: // TODO: not for 2.3: revisit constructors here. Error handlers need to be
177: // set before first error.
178:
179: /**
180: * @deprecated
181: */
182: SAX2Model(String base, Model m, String lang, boolean dummy) {
183: super (base, lang, dummy);
184: handler = initHandler(m);
185: }
186:
187: private JenaHandler initHandler(Model m) {
188: if (m == null)
189: return null;
190: JenaHandler rslt = new JenaHandler(m, errorHandler);
191: rslt.useWith(getHandlers());
192: return rslt;
193:
194: }
195:
196: private boolean closed = false;
197:
198: public void close() {
199: // System.err.println("closing;");
200: if (!closed) {
201: super .close();
202: if (handler != null)
203: handler.bulkUpdate();
204: closed = true;
205: }
206: }
207:
208: /**
209: * Change the error handler.
210: * <p>
211: * Note that errors of class {@link ParseException}can be promoted using
212: * the {@link ParseException#promote}method. See ARP documentation for
213: * {@link org.xml.sax.ErrorHandler}for the details of error promotion.
214: *
215: * @param errHandler
216: * The new error handler.
217: * @return The old error handler.
218: */
219: public RDFErrorHandler setErrorHandler(RDFErrorHandler errHandler) {
220: RDFErrorHandler old = this .errorHandler;
221: this .errorHandler = errHandler;
222: if (handler != null) {
223: handler.setErrorHandler(errHandler);
224: }
225: return old;
226: }
227:
228: /**
229: *
230: * Change a property of the RDF or XML parser.
231: * <p>
232: * I do not believe that many of the XML features or properties are in fact
233: * useful for ARP users. The ARP properties allow fine-grained control over
234: * error reporting.
235: * <p>
236: * This interface can be used to set and get:
237: * <dl>
238: * <dt>SAX2 features</dt>
239: * <dd>See <a href="http://xml.apache.org/xerces-j/features.html">Xerces
240: * features </a>. Value should be given as a String "true" or "false" or a
241: * Boolean.</dd>
242: * <dt>SAX2 properties</dt>
243: * <dd>See <a href="http://xml.apache.org/xerces-j/properties.html">Xerces
244: * properties </a>.</dd>
245: * <dt>Xerces features</dt>
246: * <dd>See <a href="http://xml.apache.org/xerces-j/features.html">Xerces
247: * features </a>. Value should be given as a String "true" or "false" or a
248: * Boolean.</dd>
249: * <dt>Xerces properties</dt>
250: * <dd>See <a href="http://xml.apache.org/xerces-j/properties.html">Xerces
251: * properties </a>.</dd>
252: * <dt>ARP properties</dt>
253: * <dd>These are referred to either by their property name, (see below) or
254: * by an absolute URL of the form
255: * <code>http://jena.hpl.hp.com/arp/properties/<PropertyName></code>.
256: * The value should be a String, an Integer or a Boolean depending on the
257: * property. <br>
258: * ARP property names and string values are case insensitive. <br>
259: * <TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0">
260: * <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
261: * <TD COLSPAN=4><FONT SIZE="+2"> <B>ARP Properties </B> </FONT></TD>
262: * </TR>
263: * <tr BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
264: * <th>Property Name</th>
265: * <th>Description</th>
266: * <th>Value class</th>
267: * <th>Legal Values</th>
268: * </tr>
269: * <tr BGCOLOR="white" CLASS="TableRowColor">
270: * <td><CODE>error-mode</CODE></td>
271: * <td>{@link ARPOptions#setDefaultErrorMode}<br>
272: * {@link ARPOptions#setLaxErrorMode}<br>
273: * {@link ARPOptions#setStrictErrorMode()}<br>
274: * {@link ARPOptions#setStrictErrorMode(int)}<br>
275: * </td>
276: * <td>String</td>
277: * <td><CODE>default</CODE><br>
278: * <CODE>lax</CODE><br>
279: * <CODE>strict</CODE><br>
280: * <CODE>strict-ignore</CODE><br>
281: * <CODE>strict-warning</CODE><br>
282: * <CODE>strict-error</CODE><br>
283: * <CODE>strict-fatal</CODE><br>
284: * </td>
285: * </tr>
286: * <tr BGCOLOR="white" CLASS="TableRowColor">
287: * <td><CODE>embedding</CODE></td>
288: * <td>{@link ARP#setEmbedding}</td>
289: * <td>String or Boolean</td>
290: * <td><CODE>true</CODE> or <CODE>false</CODE></td>
291: * </tr>
292: * <tr BGCOLOR="white" CLASS="TableRowColor">
293: * <td><code>ERR_<XXX></code><br>
294: * <code>WARN_<XXX></code><br>
295: * <code>IGN_<XXX></code></td>
296: * <td>{@link ARPErrorNumbers}<br>
297: * Any of the error condition numbers listed. <br>
298: * {@link ARPOptions#setErrorMode(int, int)}</td>
299: * <td>String or Integer</td>
300: * <td>{@link ARPErrorNumbers#EM_IGNORE EM_IGNORE}<br>
301: * {@link ARPErrorNumbers#EM_WARNING EM_WARNING}<br>
302: * {@link ARPErrorNumbers#EM_ERROR EM_ERROR}<br>
303: * {@link ARPErrorNumbers#EM_FATAL EM_FATAL}<br>
304: * </td>
305: * </tr>
306: * </table></dd>
307: * </dl>
308: *
309: * @param str
310: * The property to set.
311: * @param value
312: * The new value; values of class String will be converted into
313: * appropriate classes. Values of class Boolean or Integer will
314: * be used for appropriate properties.
315: * @throws JenaException
316: * For bad values.
317: * @return The old value, or null if none, or old value is inaccesible.
318: * @see SAX2RDF#getOptions()
319: * @see ARPOptions
320: */
321: public Object setProperty(String str, Object value)
322: throws JenaException {
323: Object obj = value;
324: if (str.startsWith("http:")) {
325: if (str.startsWith(JenaReader.arpPropertiesURL)) {
326: str = str.substring(JenaReader.arpPropertiesURLLength);
327: }
328: }
329: return setArpProperty(str, obj);
330: }
331:
332: private Object setArpProperty(String str, Object v) {
333: return JenaReader.setArpProperty(getOptions(), str, v,
334: errorHandler);
335: }
336: }
337:
338: /*
339: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP All rights
340: * reserved.
341: *
342: * Redistribution and use in source and binary forms, with or without
343: * modification, are permitted provided that the following conditions are met:
344: * 1. Redistributions of source code must retain the above copyright notice,
345: * this list of conditions and the following disclaimer. 2. Redistributions in
346: * binary form must reproduce the above copyright notice, this list of
347: * conditions and the following disclaimer in the documentation and/or other
348: * materials provided with the distribution. 3. The name of the author may not
349: * be used to endorse or promote products derived from this software without
350: * specific prior written permission.
351: *
352: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
353: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
354: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
355: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
356: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
357: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
358: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
359: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
360: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
361: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
362: */
|