KickJava   Java API By Example, From Geeks To Geeks.

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


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

5
6 package com.hp.hpl.jena.rdf.arp;
7
8 import org.xml.sax.*;
9
10 import com.hp.hpl.jena.rdf.model.*;
11 import com.hp.hpl.jena.shared.*;
12 import com.hp.hpl.jena.rdf.model.impl.RDFDefaultErrorHandler;
13
14 /**
15  * Use arbitrary SAX input to Jena.
16  * See
17  * <a HREF="../../../../../../../ARP/sax.html">ARP SAX documentation</a>.
18  *
19  * @author Jeremy J. Carroll
20  *
21  */

22 public class SAX2Model extends SAX2RDF {
23     /**
24      * Factory method to create a new SAX2RDF.
25      * @param base The retrieval URL, or the base URI to be
26      * used while parsing.
27      * @param m A Jena Model in which to put the triples,
28      * this can be null. If it is null, then use
29      * {@link #getHandlers} or {@link #setHandlersWith} to provide
30      * a {@link StatementHandler}, and usually an {@link org.xml.sax.ErrorHandler}
31      * @return A new SAX2RDF
32      * @throws MalformedURIException
33      */

34     static public SAX2Model newInstance(String JavaDoc base, Model m) throws MalformedURIException {
35         return new SAX2Model(base,m,"");
36     }
37     /**
38      * Factory method to create a new SAX2RDF.
39      * This is particularly
40      * intended for when parsing a non-root element within
41      * an XML document. In which case the application
42      * needs to find this value in the outer context.
43      * Optionally, namespace prefixes can be passed from the
44      * outer context using {@link #startPrefixMapping}.
45      * @param base The retrieval URL, or the base URI to be
46      * used while parsing.
47      * @param m A Jena Model in which to put the triples,
48      * this can be null. If it is null, then use
49      * {@link #getHandlers} or {@link #setHandlersWith} to provide
50      * a {@link StatementHandler}, and usually an {@link org.xml.sax.ErrorHandler}
51      * @param lang The current value of <code>xml:lang</code> when parsing starts, usually "".
52      * @return A new SAX2RDF
53      * @throws MalformedURIException
54      */

55     static public SAX2Model newInstance(String JavaDoc base, Model m, String JavaDoc lang) throws MalformedURIException {
56         return new SAX2Model(base,m,lang);
57     }
58     /**
59      * Begin the scope of a prefix-URI Namespace mapping.
60      *
61      *<p>This is passed to any {@link NamespaceHandler} associated
62      *with this parser.
63      *It can be called before the initial
64      *{@link #startElement} event, or other events associated
65      *with the elements being processed.
66      *When building a Jena Model, it is not required to match this
67      *with corresponding {@link #endPrefixMapping} events.
68      *Other {@link NamespaceHandler}s may be fussier.
69      *When building a Jena Model, the prefix bindings are
70      *remembered with the Model, and may be used in some
71      *output routines. It is permitted to not call this method
72      *for prefixes declared in the outer context, in which case,
73      *any output routine will need to use a gensym for such
74      *namespaces.
75      *</p>
76      * @param prefix The Namespace prefix being declared.
77      * @param uri The Namespace URI the prefix is mapped to.
78      *
79      */

80     public void startPrefixMapping (String JavaDoc prefix, String JavaDoc uri)
81      { super.startPrefixMapping(prefix,uri);
82     }
83
84     private RDFErrorHandler errorHandler = new RDFDefaultErrorHandler();
85
86     final private JenaHandler handler;
87
88     protected SAX2Model(String JavaDoc base, Model m, String JavaDoc lang) throws MalformedURIException {
89         super(base,lang);
90         handler = new JenaHandler(m,errorHandler);
91         handler.useWith(getHandlers());
92     }
93 private boolean closed = false;
94     public void close() throws SAXException{
95     // System.err.println("closing;");
96
if (!closed) {
97         super.close();
98         handler.bulkUpdate();
99         closed = true;
100         }
101     }
102     /**
103      * Change the error handler.
104      * <p>
105      * Note that errors of class {@link ParseException}can be promoted using
106      * the {@link ParseException#promote}method. See ARP documentation for
107      * {@link org.xml.sax.ErrorHandler}for the details of error promotion.
108      *
109      * @param errHandler
110      * The new error handler.
111      * @return The old error handler.
112      */

113     public RDFErrorHandler setErrorHandler(RDFErrorHandler errHandler) {
114         RDFErrorHandler old = this.errorHandler;
115         this.errorHandler = errHandler;
116         if (handler != null) {
117             handler.setErrorHandler(errHandler);
118         }
119         return old;
120     }
121     /**
122      *
123      * Change a property of the RDF or XML parser.
124      * <p>
125      * This method is untested.
126      * <p>
127      * I do not believe that many of the XML features or properties are in fact
128      * useful for ARP users. The ARP properties allow fine-grained control over
129      * error reporting.
130      * <p>
131      * This interface can be used to set and get:
132      * <dl>
133      * <dt>SAX2 features</dt>
134      * <dd>See <a HREF="http://xml.apache.org/xerces-j/features.html">Xerces
135      * features </a>. Value should be given as a String "true" or "false" or a
136      * Boolean.</dd>
137      * <dt>SAX2 properties</dt>
138      * <dd>See <a HREF="http://xml.apache.org/xerces-j/properties.html">Xerces
139      * properties </a>.</dd>
140      * <dt>Xerces features</dt>
141      * <dd>See <a HREF="http://xml.apache.org/xerces-j/features.html">Xerces
142      * features </a>. Value should be given as a String "true" or "false" or a
143      * Boolean.</dd>
144      * <dt>Xerces properties</dt>
145      * <dd>See <a HREF="http://xml.apache.org/xerces-j/properties.html">Xerces
146      * properties </a>.</dd>
147      * <dt>ARP properties</dt>
148      * <dd>These are referred to either by their property name, (see below) or
149      * by an absolute URL of the form
150      * <code>http://jena.hpl.hp.com/arp/properties/&lt;PropertyName&gt;</code>.
151      * The value should be a String, an Integer or a Boolean depending on the
152      * property. <br>
153      * ARP property names and string values are case insensitive. <br>
154      * <TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0">
155      * <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
156      * <TD COLSPAN=4><FONT SIZE="+2"> <B>ARP Properties </B> </FONT></TD>
157      * </TR>
158      * <tr BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
159      * <th>Property Name</th>
160      * <th>Description</th>
161      * <th>Value class</th>
162      * <th>Legal Values</th>
163      * </tr>
164      * <tr BGCOLOR="white" CLASS="TableRowColor">
165      * <td><CODE>error-mode</CODE></td>
166      * <td>{@link ARP#setDefaultErrorMode}<br>
167      * {@link ARP#setLaxErrorMode}<br>
168      * {@link ARP#setStrictErrorMode}<br>
169      * {@link ARP#setStrictErrorMode(int)}<br>
170      * </td>
171      * <td>String</td>
172      * <td><CODE>default</CODE><br>
173      * <CODE>lax</CODE><br>
174      * <CODE>strict</CODE><br>
175      * <CODE>strict-ignore</CODE><br>
176      * <CODE>strict-warning</CODE><br>
177      * <CODE>strict-error</CODE><br>
178      * <CODE>strict-fatal</CODE><br>
179      * </td>
180      * </tr>
181      * <tr BGCOLOR="white" CLASS="TableRowColor">
182      * <td><CODE>embedding</CODE></td>
183      * <td>{@link ARP#setEmbedding}</td>
184      * <td>String or Boolean</td>
185      * <td><CODE>true</CODE> or <CODE>false</CODE></td>
186      * </tr>
187      * <tr BGCOLOR="white" CLASS="TableRowColor">
188      * <td><code>ERR_&lt;XXX&gt;</code><br>
189      * <code>WARN_&lt;XXX&gt;</code><br>
190      * <code>IGN_&lt;XXX&gt;</code></td>
191      * <td>{@link ARPErrorNumbers}<br>
192      * Any of the error condition numbers listed. <br>
193      * {@link ARP#setErrorMode(int, int)}</td>
194      * <td>String or Integer</td>
195      * <td>{@link ARPErrorNumbers#EM_IGNORE EM_IGNORE}<br>
196      * {@link ARPErrorNumbers#EM_WARNING EM_WARNING}<br>
197      * {@link ARPErrorNumbers#EM_ERROR EM_ERROR}<br>
198      * {@link ARPErrorNumbers#EM_FATAL EM_FATAL}<br>
199      * </td>
200      * </tr>
201      * </table></dd>
202      * </dl>
203      *
204      * @param str
205      * The property to set.
206      * @param value
207      * The new value; values of class String will be converted into
208      * appropriate classes. Values of class Boolean or Integer will
209      * be used for appropriate properties.
210      * @throws JenaException
211      * For bad values.
212      * @return The old value, or null if none, or old value is inaccesible.
213      */

214     public Object JavaDoc setProperty(String JavaDoc str, Object JavaDoc value) throws JenaException {
215         Object JavaDoc obj = value;
216         if (str.startsWith("http:")) {
217             if (str.startsWith(JenaReader.arpPropertiesURL)) {
218                 str = str.substring(JenaReader.arpPropertiesURLLength);
219             }
220         }
221         return setArpProperty(str, obj);
222     }
223     private Object JavaDoc setArpProperty( String JavaDoc str, Object JavaDoc v) {
224         return JenaReader.setArpProperty(getOptions(),str,v,errorHandler);
225     }
226 }
227
228
229 /*
230  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
231  * All rights reserved.
232  *
233  * Redistribution and use in source and binary forms, with or without
234  * modification, are permitted provided that the following conditions
235  * are met:
236  * 1. Redistributions of source code must retain the above copyright
237  * notice, this list of conditions and the following disclaimer.
238  * 2. Redistributions in binary form must reproduce the above copyright
239  * notice, this list of conditions and the following disclaimer in the
240  * documentation and/or other materials provided with the distribution.
241  * 3. The name of the author may not be used to endorse or promote products
242  * derived from this software without specific prior written permission.
243  *
244  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
245  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
246  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
247  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
248  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
249  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
250  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
253  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
254  */

255  
256
Popular Tags