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 ; 12 import org.xml.sax.InputSource ; 13 import org.xml.sax.SAXException ; 14 import org.xml.sax.XMLReader ; 15 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.net.URL ; 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 in, PropertyMap properties) 32 throws IOException , SAXException , 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 in2 = new InputSource (); 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 ("character stream input sources not supported for auto-detection"); 45 else { 46 InputStream byteStream = in.getByteStream(); 47 if (byteStream == null) { 48 String systemId = in.getSystemId(); 49 if (systemId == null) 50 throw new IllegalArgumentException ("null systemId and null byteStream"); 51 byteStream = new URL (systemId).openStream(); 52 } 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 xr = xrc.createXMLReader(); 62 ErrorHandler 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 e) { 81 Exception nested = e.getException(); 83 if (nested instanceof RuntimeException ) 84 sf.unwrapException((RuntimeException )nested); 85 throw e; 86 } 87 catch (RuntimeException e) { 88 throw sf.unwrapException(e); 89 } 90 } 91 92 public Option getOption(String uri) { 93 return srf.getOption(uri); 94 } 95 } 96 | Popular Tags |