00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030  
00031 package org.objectweb.proactive.core.xml.io;
00032 
00033 import java.io.IOException;
00034 
00035 import org.apache.log4j.Logger;
00036 import org.apache.xerces.parsers.SAXParser;
00037 import org.objectweb.proactive.core.util.log.Loggers;
00038 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00039 import org.xml.sax.SAXException;
00040 import org.xml.sax.SAXNotRecognizedException;
00041 import org.xml.sax.SAXNotSupportedException;
00042 
00043 
00052 public class StreamReader implements XMLReader {
00053     static Logger logger = ProActiveLogger.getLogger(Loggers.XML);
00054     private org.xml.sax.XMLReader parser;
00055     private org.xml.sax.InputSource inputSource;
00056 
00057     public StreamReader(java.io.InputStream in, XMLHandler xmlHandler)
00058         throws java.io.IOException {
00059         this(new org.xml.sax.InputSource(in), xmlHandler);
00060     }
00061 
00062     public StreamReader(java.io.Reader reader, XMLHandler xmlHandler)
00063         throws java.io.IOException {
00064         this(new org.xml.sax.InputSource(reader), xmlHandler);
00065     }
00066 
00067     public StreamReader(org.xml.sax.InputSource inputSource,
00068         XMLHandler xmlHandler) throws java.io.IOException {
00069         this(inputSource, xmlHandler, null, null);
00070     }
00071 
00072     public StreamReader(org.xml.sax.InputSource inputSource,
00073         XMLHandler xmlHandler, java.net.URL schema,
00074         org.xml.sax.ErrorHandler errorHandler) throws java.io.IOException {
00075         this.inputSource = inputSource;
00076 
00077         DefaultHandlerAdapter adaptor = new DefaultHandlerAdapter(xmlHandler);
00078 
00079         
00080         
00081         parser = new SAXParser();
00082         
00083         parser.setContentHandler(adaptor);
00084 
00085         if ((schema != null) || (errorHandler != null)) {
00086             try {
00087                 parser.setErrorHandler((errorHandler == null)
00088                     ? new SAXParserErrorHandler() : errorHandler);
00089 
00090                 if (schema != null) {
00091                     parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
00092                         schema.toString());
00093 
00095                 }
00096 
00097                 parser.setFeature("http://xml.org/sax/features/validation", true);
00098                 parser.setFeature("http://apache.org/xml/features/validation/schema",
00099                     true);
00100 
00101                 
00102             } catch (SAXNotRecognizedException e) {
00103                 logger.error("unrecognised feature: ");
00104                 logger.error("http://xml.org/sax/features/validation");
00105             } catch (SAXNotSupportedException e) {
00106                 logger.error("unrecognised feature: ");
00107                 logger.error("http://apache.org/xml/features/validation/schema");
00108             }
00109         }
00110     }
00111 
00112     
00113     public void read() throws SAXException, IOException {
00114             
00115             
00116             parser.parse(inputSource);
00117     }
00118 }