KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.xml.transform.*;
9 import javax.xml.transform.dom.*;
10 import javax.xml.transform.sax.*;
11 import org.w3c.dom.*;
12
13 import com.hp.hpl.jena.rdf.model.*;
14 import com.hp.hpl.jena.shared.*;
15
16 /**
17  * Transform DOM nodes of RDF.XML into Jena Models.
18  * Known not to work with Java 1.4.1.
19  * @author Jeremy J. Carroll
20  *
21  */

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

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

59     public DOM2Model(String JavaDoc base, Model m, String JavaDoc lang)
60             throws MalformedURIException {
61         super(base, m, lang);
62     }
63 /**
64  * Parse a DOM Node with the RDF/XML parser, loading
65  * the triples into the associated Model.
66  * Known not to work with Java 1.4.1.
67  * @param document
68  */

69     public void load(Node document) {
70         Source input = new DOMSource(document);
71
72         // Make a SAXResult object using this handler
73
SAXResult output = new SAXResult(this);
74         output.setLexicalHandler(this);
75
76         // Run transform
77
TransformerFactory xformFactory = TransformerFactory.newInstance();
78         try {
79         Transformer idTransform = xformFactory.newTransformer();
80         idTransform.transform(input, output);
81         }
82         catch (RuntimeException JavaDoc rte){
83             throw rte;
84         }
85         catch (Exception JavaDoc nrte){
86             throw new JenaException(nrte);
87         }
88     }
89
90 }
91
92 /*
93  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP All rights
94  * reserved.
95  *
96  * Redistribution and use in source and binary forms, with or without
97  * modification, are permitted provided that the following conditions are met:
98  * 1. Redistributions of source code must retain the above copyright notice,
99  * this list of conditions and the following disclaimer. 2. Redistributions in
100  * binary form must reproduce the above copyright notice, this list of
101  * conditions and the following disclaimer in the documentation and/or other
102  * materials provided with the distribution. 3. The name of the author may not
103  * be used to endorse or promote products derived from this software without
104  * specific prior written permission.
105  *
106  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
107  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
108  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
109  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
110  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
111  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
112  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
113  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
114  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
115  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
116  */

117
118
Popular Tags