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 javax.xml.transform.Source;
009: import javax.xml.transform.Transformer;
010: import javax.xml.transform.TransformerFactory;
011: import javax.xml.transform.dom.DOMSource;
012: import javax.xml.transform.sax.SAXResult;
013:
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016: import org.w3c.dom.Node;
017: import org.xml.sax.SAXParseException;
018:
019: import com.hp.hpl.jena.rdf.model.Model;
020: import com.hp.hpl.jena.shared.JenaException;
021:
022: /**
023: * Transform DOM nodes of RDF.XML into Jena Models. Known not to work with Java
024: * 1.4.1.
025: *
026: * @author Jeremy J. Carroll
027: *
028: */
029: public class DOM2Model extends SAX2Model {
030:
031: static Log logger = LogFactory.getLog(DOM2Model.class);
032:
033: /**
034: * Create a new DOM2Model.
035: *
036: * @param base
037: * The retrieval URL, or the base URI to be used while parsing.
038: * @param m
039: * A Jena Model in which to put the triples, this can be null. If
040: * it is null, then use {@link SAX2RDF#getHandlers}or
041: * {@link SAX2RDF#setHandlersWith}to provide a {@link StatementHandler},
042: * and usually an {@link org.xml.sax.ErrorHandler}
043: * @throws SAXParseException
044: */
045: static public DOM2Model createD2M(String base, Model m)
046: throws SAXParseException {
047: return new DOM2Model(base, m, "", true);
048: }
049:
050: /**
051: * Create a new DOM2Model. This is particularly intended for when parsing a
052: * non-root element within an XML document. In which case the application
053: * needs to find this value in the outer context. Optionally, namespace
054: * prefixes can be passed from the outer context using
055: * {@link SAX2RDF#startPrefixMapping}.
056: *
057: * @param base
058: * The retrieval URL, or the base URI to be used while parsing.
059: * @param m
060: * A Jena Model in which to put the triples, this can be null. If
061: * it is null, then use {@link SAX2RDF#getHandlers}or
062: * {@link SAX2RDF#setHandlersWith}to provide a {@link StatementHandler},
063: * and usually an {@link org.xml.sax.ErrorHandler}
064: * @param lang
065: * The current value of <code>xml:lang</code> when parsing
066: * starts, usually "".
067: * @throws SAXParseException
068: */
069: static public DOM2Model createD2M(String base, Model m, String lang)
070: throws SAXParseException {
071: return new DOM2Model(base, m, lang, true);
072: }
073:
074: /**
075: * Create a new DOM2Model.
076: *
077: * @param base
078: * The retrieval URL, or the base URI to be used while parsing.
079: * @param m
080: * A Jena Model in which to put the triples, this can be null. If
081: * it is null, then use {@link SAX2RDF#getHandlers}or
082: * {@link SAX2RDF#setHandlersWith}to provide a {@link StatementHandler},
083: * and usually an {@link org.xml.sax.ErrorHandler}
084: * @throws MalformedURIException
085: * @deprecated Use {@link #createD2M(String, Model)}
086: */
087: public DOM2Model(String base, Model m) throws MalformedURIException {
088: this (base, m, "");
089: }
090:
091: /**
092: * Create a new DOM2Model. This is particularly intended for when parsing a
093: * non-root element within an XML document. In which case the application
094: * needs to find this value in the outer context. Optionally, namespace
095: * prefixes can be passed from the outer context using
096: * {@link SAX2RDF#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 {@link StatementHandler},
104: * and usually an {@link org.xml.sax.ErrorHandler}
105: * @param lang
106: * The current value of <code>xml:lang</code> when parsing
107: * starts, usually "".
108: * @throws MalformedURIException
109: * @deprecated Use {@link #createD2M(String, Model, String)}
110: */
111: public DOM2Model(String base, Model m, String lang)
112: throws MalformedURIException {
113: super (base, m, lang);
114: }
115:
116: DOM2Model(String base, Model m, String lang, boolean dummy)
117: throws SAXParseException {
118: super (base, m, lang, 0);
119: }
120:
121: /**
122: * Parse a DOM Node with the RDF/XML parser, loading the triples into the
123: * associated Model. Known not to work with Java 1.4.1.
124: *
125: * @param document
126: */
127: public void load(Node document) {
128: Source input = new DOMSource(document);
129:
130: // Make a SAXResult object using this handler
131: SAXResult output = new SAXResult(this );
132: output.setLexicalHandler(this );
133:
134: // Run transform
135: TransformerFactory xformFactory = TransformerFactory
136: .newInstance();
137: try {
138: Transformer idTransform = xformFactory.newTransformer();
139: idTransform.transform(input, output);
140: } catch (FatalParsingErrorException e) {
141: // Old code ignored this,
142: // given difficult bug report, don't be silent.
143: logger.error("Unexpected exception in DOM2Model", e);
144:
145: } catch (RuntimeException rte) {
146: throw rte;
147: } catch (Exception nrte) {
148: throw new JenaException(nrte);
149: } finally {
150: close();
151: }
152: }
153:
154: }
155:
156: /*
157: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP All rights
158: * reserved.
159: *
160: * Redistribution and use in source and binary forms, with or without
161: * modification, are permitted provided that the following conditions are met:
162: * 1. Redistributions of source code must retain the above copyright notice,
163: * this list of conditions and the following disclaimer. 2. Redistributions in
164: * binary form must reproduce the above copyright notice, this list of
165: * conditions and the following disclaimer in the documentation and/or other
166: * materials provided with the distribution. 3. The name of the author may not
167: * be used to endorse or promote products derived from this software without
168: * specific prior written permission.
169: *
170: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
171: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
172: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
173: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
174: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
175: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
176: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
177: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
178: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
179: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
180: */
|