KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > xml > io > StreamReader


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.core.xml.io;
32
33 import org.apache.log4j.Logger;
34
35 import org.apache.xerces.parsers.SAXParser;
36
37 import org.objectweb.proactive.core.config.ProActiveConfiguration;
38
39
40 import org.xml.sax.SAXNotRecognizedException JavaDoc;
41 import org.xml.sax.SAXNotSupportedException JavaDoc;
42
43
44 /**
45  *
46  * Implement an XLMReader based on SAX reading from a stream
47  *
48  * @author Lionel Mestre
49  * @version 0.91
50  *
51  */

52 public class StreamReader implements XMLReader {
53     static Logger logger = Logger.getLogger(StreamReader.class.getName());
54     private org.xml.sax.XMLReader JavaDoc parser;
55     private org.xml.sax.InputSource JavaDoc inputSource;
56
57     public StreamReader(java.io.InputStream JavaDoc in, XMLHandler xmlHandler)
58         throws java.io.IOException JavaDoc {
59         this(new org.xml.sax.InputSource JavaDoc(in), xmlHandler);
60     }
61
62     public StreamReader(java.io.Reader JavaDoc reader, XMLHandler xmlHandler)
63         throws java.io.IOException JavaDoc {
64         this(new org.xml.sax.InputSource JavaDoc(reader), xmlHandler);
65     }
66
67     public StreamReader(org.xml.sax.InputSource JavaDoc inputSource,
68         XMLHandler xmlHandler) throws java.io.IOException JavaDoc {
69         this.inputSource = inputSource;
70         DefaultHandlerAdapter adaptor = new DefaultHandlerAdapter(xmlHandler);
71
72         // javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
73
// parser = factory.newSAXParser().getXMLReader();
74
parser = new SAXParser();
75         //parser = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
76
parser.setContentHandler(adaptor);
77         if ("enable".equals(ProActiveConfiguration.getSchemaValidationState())) {
78             try {
79                 parser.setErrorHandler(new SAXParserErrorHandler());
80                 parser.setFeature("http://xml.org/sax/features/validation", true);
81                 parser.setFeature("http://apache.org/xml/features/validation/schema",
82                     true);
83
84                 // parser.parse(inputSource);
85
} catch (SAXNotRecognizedException JavaDoc e) {
86                 logger.error("unrecognised feature: ");
87                 logger.error("http://xml.org/sax/features/validation");
88             } catch (SAXNotSupportedException JavaDoc e) {
89                 logger.error("unrecognised feature: ");
90                 logger.error("http://apache.org/xml/features/validation/schema");
91             }
92         }
93     }
94
95     // -- implements XMLReader ------------------------------------------------------
96
public void read() throws java.io.IOException JavaDoc {
97         try {
98             //parser.setFeature("http://xml.org/sax/features/validation",true);
99
//parser.setFeature("http://apache.org/xml/features/validation/schema",true);
100
parser.parse(inputSource);
101         } catch (org.xml.sax.SAXException JavaDoc e) {
102             e.printStackTrace();
103             throw new java.io.IOException JavaDoc(e.toString());
104         }
105     }
106 }
107
Popular Tags