KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > validate > auto > AutoSchemaReader


1 package com.thaiopensource.validate.auto;
2
3 import com.thaiopensource.util.PropertyMap;
4 import com.thaiopensource.util.PropertyMapBuilder;
5 import com.thaiopensource.validate.IncorrectSchemaException;
6 import com.thaiopensource.validate.Schema;
7 import com.thaiopensource.validate.SchemaReader;
8 import com.thaiopensource.validate.ValidateProperty;
9 import com.thaiopensource.validate.Option;
10 import com.thaiopensource.xml.sax.XMLReaderCreator;
11 import org.xml.sax.ErrorHandler JavaDoc;
12 import org.xml.sax.InputSource JavaDoc;
13 import org.xml.sax.SAXException JavaDoc;
14 import org.xml.sax.XMLReader JavaDoc;
15
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.net.URL JavaDoc;
19
20 public class AutoSchemaReader implements SchemaReader {
21   private final SchemaReceiverFactory srf;
22
23   public AutoSchemaReader() {
24     this(new SchemaReceiverLoader());
25   }
26
27   public AutoSchemaReader(SchemaReceiverFactory srf) {
28     this.srf = srf == null ? new SchemaReceiverLoader() : srf;
29   }
30
31   public Schema createSchema(InputSource JavaDoc in, PropertyMap properties)
32           throws IOException JavaDoc, SAXException JavaDoc, IncorrectSchemaException {
33     if (SchemaReceiverFactory.PROPERTY.get(properties) != srf) {
34       PropertyMapBuilder builder = new PropertyMapBuilder(properties);
35       SchemaReceiverFactory.PROPERTY.put(builder, srf);
36       properties = builder.toPropertyMap();
37     }
38     InputSource JavaDoc in2 = new InputSource JavaDoc();
39     in2.setSystemId(in.getSystemId());
40     in2.setPublicId(in.getPublicId());
41     in2.setEncoding(in.getEncoding());
42     Rewindable rewindable;
43     if (in.getCharacterStream() != null)
44       throw new IllegalArgumentException JavaDoc("character stream input sources not supported for auto-detection");
45     else {
46       InputStream JavaDoc byteStream = in.getByteStream();
47       if (byteStream == null) {
48         String JavaDoc systemId = in.getSystemId();
49         if (systemId == null)
50           throw new IllegalArgumentException JavaDoc("null systemId and null byteStream");
51         byteStream = new URL JavaDoc(systemId).openStream();
52         // XXX should use encoding from MIME header
53
}
54       RewindableInputStream rewindableByteStream = new RewindableInputStream(byteStream);
55       in.setByteStream(rewindableByteStream);
56       in2.setByteStream(rewindableByteStream);
57       rewindable = rewindableByteStream;
58     }
59     SchemaReceiver sr = new AutoSchemaReceiver(properties, rewindable);
60     XMLReaderCreator xrc = ValidateProperty.XML_READER_CREATOR.get(properties);
61     XMLReader JavaDoc xr = xrc.createXMLReader();
62     ErrorHandler JavaDoc eh = ValidateProperty.ERROR_HANDLER.get(properties);
63     if (eh != null)
64       xr.setErrorHandler(eh);
65     SchemaFuture sf = sr.installHandlers(xr);
66     try {
67       try {
68         xr.parse(in);
69         return sf.getSchema();
70       }
71       catch (ReparseException e) {
72         rewindable.rewind();
73         rewindable.willNotRewind();
74         return e.reparse(in2);
75       }
76       finally {
77         rewindable.willNotRewind();
78       }
79     }
80     catch (SAXException JavaDoc e) {
81       // Work around broken SAX parsers that catch and wrap runtime exceptions thrown by handlers
82
Exception JavaDoc nested = e.getException();
83       if (nested instanceof RuntimeException JavaDoc)
84         sf.unwrapException((RuntimeException JavaDoc)nested);
85       throw e;
86     }
87     catch (RuntimeException JavaDoc e) {
88       throw sf.unwrapException(e);
89     }
90   }
91
92   public Option getOption(String JavaDoc uri) {
93     return srf.getOption(uri);
94   }
95 }
96
Popular Tags