1 package com.thaiopensource.validate.auto; 2 3 import org.xml.sax.XMLReader ; 4 import org.xml.sax.SAXException ; 5 import org.xml.sax.Locator ; 6 import org.xml.sax.Attributes ; 7 import org.xml.sax.ContentHandler ; 8 import org.xml.sax.SAXParseException ; 9 import org.xml.sax.helpers.DefaultHandler ; 10 11 import java.io.IOException ; 12 import java.util.Vector ; 13 14 import com.thaiopensource.validate.Schema; 15 import com.thaiopensource.validate.IncorrectSchemaException; 16 import com.thaiopensource.util.Localizer; 17 import com.thaiopensource.util.PropertyMap; 18 19 public class AutoSchemaReceiver implements SchemaReceiver { 20 private final PropertyMap properties; 21 private final Rewindable rewindable; 22 23 private class Handler extends DefaultHandler implements SchemaFuture { 24 private final XMLReader xr; 25 private SchemaFuture sf = null; 26 private Locator locator = null; 27 private final Vector prefixMappings = new Vector (); 28 29 private Handler(XMLReader xr) { 30 this.xr = xr; 31 } 32 33 public void setDocumentLocator(Locator locator) { 34 this.locator = locator; 35 } 36 37 public void startPrefixMapping(String prefix, String uri) { 38 prefixMappings.addElement(prefix); 39 prefixMappings.addElement(uri); 40 } 41 42 public void startElement(String uri, String localName, 43 String qName, Attributes attributes) 44 throws SAXException { 45 SchemaReceiverFactory srf = SchemaReceiverFactory.PROPERTY.get(properties); 46 SchemaReceiver sr = srf.createSchemaReceiver(uri, properties); 47 if (sr == null) { 48 Localizer localizer = new Localizer(AutoSchemaReceiver.class); 49 String detail = ("".equals(uri) 50 ? localizer.message("no_namespace") 51 : localizer.message("unknown_namespace", uri)); 52 throw new SAXParseException (detail, locator); 53 } 54 sf = sr.installHandlers(xr); 55 rewindable.willNotRewind(); 56 ContentHandler contentHandler = xr.getContentHandler(); 57 if (contentHandler == null) 58 return; 59 if (locator != null) { 60 contentHandler.setDocumentLocator(locator); 61 contentHandler = xr.getContentHandler(); 62 } 63 contentHandler.startDocument(); 64 contentHandler = xr.getContentHandler(); 65 for (int i = 0, len = prefixMappings.size(); i < len; i += 2) { 66 contentHandler.startPrefixMapping((String )prefixMappings.elementAt(i), 67 (String )prefixMappings.elementAt(i + 1)); 68 contentHandler = xr.getContentHandler(); 69 } 70 contentHandler.startElement(uri, localName, qName, attributes); 71 } 72 73 public Schema getSchema() throws IncorrectSchemaException, SAXException , IOException { 74 if (sf == null) 75 throw new IncorrectSchemaException(); 76 return sf.getSchema(); 77 } 78 79 public RuntimeException unwrapException(RuntimeException e) throws SAXException , IOException , IncorrectSchemaException { 80 if (sf == null) 81 return e; 82 return sf.unwrapException(e); 83 } 84 } 85 86 public AutoSchemaReceiver(PropertyMap properties, Rewindable rewindable) { 87 this.properties = properties; 88 this.rewindable = rewindable; 89 } 90 91 public SchemaFuture installHandlers(XMLReader xr) { 92 Handler h = new Handler (xr); 93 xr.setContentHandler(h); 94 return h; 95 } 96 } 97 | Popular Tags |