KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > n3 > N3JenaReader


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

5
6 package com.hp.hpl.jena.n3;
7
8 import com.hp.hpl.jena.graph.GraphEvents;
9 import com.hp.hpl.jena.rdf.model.*;
10 import java.net.* ;
11 import java.io.* ;
12 import com.hp.hpl.jena.shared.*;
13
14 // To do: buffereing version that does the whole file before adding statements (error catcher)
15

16 /**
17  * @author Andy Seaborne
18  * @version $Id: N3JenaReader.java,v 1.15 2005/02/21 12:04:04 andy_seaborne Exp $
19  */

20
21
22
23 public class N3JenaReader implements RDFReader
24 {
25     RDFErrorHandler errorHandler = null ;
26     N3toRDF converter = null ;
27     
28     public N3JenaReader() { converter = new N3toRDF() ; }
29     
30     // Jena's Reader interface
31

32     public void read(Model model, Reader r, String JavaDoc base)
33     {
34         read(model, r, base, null) ;
35     }
36
37     public void read(Model model, java.lang.String JavaDoc url)
38     {
39           try {
40             URLConnection conn = new URL(url).openConnection();
41             String JavaDoc encoding = conn.getContentEncoding();
42             if ( encoding == null )
43                read(model,new InputStreamReader(conn.getInputStream()), url, url);
44             else
45                read(model,new InputStreamReader(conn.getInputStream(),encoding), url, url);
46         }
47         catch (JenaException e)
48         {
49             if ( errorHandler == null )
50                 throw e;
51             errorHandler.error(e) ;
52         }
53         catch (Exception JavaDoc ex)
54         {
55             if ( errorHandler == null ) throw new JenaException(ex) ;
56             errorHandler.error(ex) ;
57         }
58     }
59     
60     public void read(Model model, Reader r, String JavaDoc base, String JavaDoc sourceName)
61     {
62         // check reader is it charset safe ie not file reader
63
//i.e. InputStreamReader
64

65         try {
66             model.notifyEvent( GraphEvents.startRead );
67             converter.setBase(base) ;
68             converter.setModel(model);
69             N3Parser p = new N3Parser(r, converter) ;
70             p.parse() ;
71         }
72         catch (JenaException e)
73         {
74             if ( errorHandler == null )
75                 throw e;
76             errorHandler.error(e) ;
77         }
78         catch (Exception JavaDoc ex)
79         {
80             if ( errorHandler == null ) throw new JenaException(ex) ;
81             errorHandler.error(ex) ;
82         }
83         finally
84         {
85             model.notifyEvent( GraphEvents.finishRead );
86         }
87     }
88     
89
90     public void read(Model model, InputStream in, String JavaDoc base)
91     {
92         read(model, in, base, null) ;
93     }
94
95     
96     public void read(Model model, InputStream in, String JavaDoc base, String JavaDoc sourceName)
97     {
98         try {
99             model.notifyEvent( GraphEvents.startRead );
100             converter.setBase(base) ;
101             converter.setModel(model);
102             N3Parser p = new N3Parser(in, converter) ;
103             p.parse() ;
104         }
105         catch (JenaException e)
106         {
107             if ( errorHandler == null )
108                 throw e;
109             errorHandler.error(e) ;
110         }
111         catch (Exception JavaDoc ex)
112         {
113             if ( errorHandler == null ) throw new JenaException(ex) ;
114             errorHandler.error(ex) ;
115         }
116         finally
117         {
118             model.notifyEvent( GraphEvents.finishRead );
119         }
120
121     }
122     
123     public RDFErrorHandler setErrorHandler(RDFErrorHandler errHandler)
124     {
125         RDFErrorHandler old = errorHandler ;
126         errorHandler = errHandler ;
127         return old ;
128     }
129     public Object JavaDoc setProperty(String JavaDoc propName, Object JavaDoc propValue)
130     {
131         return null ;
132     }
133
134 }
135
136 /*
137  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
138  * All rights reserved.
139  *
140  * Redistribution and use in source and binary forms, with or without
141  * modification, are permitted provided that the following conditions
142  * are met:
143  * 1. Redistributions of source code must retain the above copyright
144  * notice, this list of conditions and the following disclaimer.
145  * 2. Redistributions in binary form must reproduce the above copyright
146  * notice, this list of conditions and the following disclaimer in the
147  * documentation and/or other materials provided with the distribution.
148  * 3. The name of the author may not be used to endorse or promote products
149  * derived from this software without specific prior written permission.
150  *
151  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
152  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
153  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
154  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
155  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
156  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
157  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
158  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
159  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
160  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
161  */

162
Popular Tags