KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thaiopensource.validate.auto;
2
3 import org.xml.sax.XMLReader JavaDoc;
4 import org.xml.sax.SAXException JavaDoc;
5 import org.xml.sax.Locator JavaDoc;
6 import org.xml.sax.Attributes JavaDoc;
7 import org.xml.sax.ContentHandler JavaDoc;
8 import org.xml.sax.SAXParseException JavaDoc;
9 import org.xml.sax.helpers.DefaultHandler JavaDoc;
10
11 import java.io.IOException JavaDoc;
12 import java.util.Vector JavaDoc;
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 JavaDoc implements SchemaFuture {
24     private final XMLReader JavaDoc xr;
25     private SchemaFuture sf = null;
26     private Locator JavaDoc locator = null;
27     private final Vector JavaDoc prefixMappings = new Vector JavaDoc();
28
29     private Handler(XMLReader JavaDoc xr) {
30       this.xr = xr;
31     }
32
33     public void setDocumentLocator(Locator JavaDoc locator) {
34       this.locator = locator;
35     }
36
37     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
38       prefixMappings.addElement(prefix);
39       prefixMappings.addElement(uri);
40     }
41
42     public void startElement(String JavaDoc uri, String JavaDoc localName,
43                              String JavaDoc qName, Attributes JavaDoc attributes)
44             throws SAXException JavaDoc {
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 JavaDoc detail = ("".equals(uri)
50                          ? localizer.message("no_namespace")
51                          : localizer.message("unknown_namespace", uri));
52         throw new SAXParseException JavaDoc(detail, locator);
53       }
54       sf = sr.installHandlers(xr);
55       rewindable.willNotRewind();
56       ContentHandler JavaDoc 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 JavaDoc)prefixMappings.elementAt(i),
67                                           (String JavaDoc)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 JavaDoc, IOException JavaDoc {
74       if (sf == null)
75         throw new IncorrectSchemaException();
76       return sf.getSchema();
77     }
78
79     public RuntimeException JavaDoc unwrapException(RuntimeException JavaDoc e) throws SAXException JavaDoc, IOException JavaDoc, 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 JavaDoc xr) {
92     Handler JavaDoc h = new Handler JavaDoc(xr);
93     xr.setContentHandler(h);
94     return h;
95   }
96 }
97
Popular Tags