KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > validate > schematron > ValidatorImpl


1 package com.thaiopensource.validate.schematron;
2
3 import com.thaiopensource.util.PropertyMap;
4 import com.thaiopensource.validate.ValidateProperty;
5 import com.thaiopensource.validate.Validator;
6 import com.thaiopensource.xml.sax.DelegatingContentHandler;
7 import org.xml.sax.ContentHandler JavaDoc;
8 import org.xml.sax.DTDHandler JavaDoc;
9 import org.xml.sax.ErrorHandler JavaDoc;
10 import org.xml.sax.InputSource JavaDoc;
11 import org.xml.sax.Locator JavaDoc;
12 import org.xml.sax.SAXException JavaDoc;
13
14 import javax.xml.transform.Templates JavaDoc;
15 import javax.xml.transform.Transformer JavaDoc;
16 import javax.xml.transform.TransformerConfigurationException JavaDoc;
17 import javax.xml.transform.TransformerException JavaDoc;
18 import javax.xml.transform.sax.SAXResult JavaDoc;
19 import javax.xml.transform.sax.SAXSource JavaDoc;
20
21 class ValidatorImpl extends DelegatingContentHandler implements Validator {
22   private final Transformer JavaDoc transformer;
23   private Locator JavaDoc locator;
24   private TransformerException JavaDoc transformerException;
25
26   private final Object JavaDoc contentHandlerMonitor = new Object JavaDoc();
27   private final Object JavaDoc parseMonitor = new Object JavaDoc();
28   private final Object JavaDoc transformMonitor = new Object JavaDoc();
29   private Thread JavaDoc transformThread;
30   private final ContentHandler outputHandler;
31
32   class BlockingReader extends XMLReaderImpl {
33     public void parse(InputSource JavaDoc in) throws SAXException JavaDoc {
34       synchronized (parseMonitor) {
35         synchronized (contentHandlerMonitor) {
36           contentHandlerMonitor.notify();
37         }
38         try {
39           parseMonitor.wait();
40         }
41         catch (InterruptedException JavaDoc e) {
42           throw new SAXException JavaDoc(e);
43         }
44       }
45     }
46
47     public void setContentHandler(ContentHandler handler) {
48       setDelegate(handler);
49     }
50
51     public ContentHandler getContentHandler() {
52       return getDelegate();
53     }
54
55   }
56
57   ValidatorImpl(Templates JavaDoc templates, PropertyMap properties) {
58     ErrorHandler JavaDoc eh = ValidateProperty.ERROR_HANDLER.get(properties);
59     outputHandler = new OutputHandler(eh);
60     try {
61       transformer = templates.newTransformer();
62       // XXX set up transformer with a resolver that uses the resolver
63
// XXX and XMLReaderCreator from properties
64
// XXX set up transformer with an ErrorListener that just throws
65
}
66     catch (TransformerConfigurationException JavaDoc e) {
67       throw new RuntimeException JavaDoc("could not create transformer");
68     }
69   }
70
71   public ContentHandler getContentHandler() {
72     return this;
73   }
74
75   public DTDHandler JavaDoc getDTDHandler() {
76     return null;
77   }
78
79   public void reset() {
80     if (transformThread != null) {
81       synchronized (transformMonitor) {
82         transformThread.interrupt();
83         try {
84           transformMonitor.wait();
85         }
86         catch (InterruptedException JavaDoc e) { }
87         transformThread = null;
88       }
89     }
90     transformerException = null;
91     locator = null;
92   }
93
94   public void setDocumentLocator(Locator JavaDoc locator) {
95     this.locator = locator;
96   }
97
98   public void startDocument()
99           throws SAXException JavaDoc {
100     final SAXSource JavaDoc source = new SAXSource JavaDoc(new BlockingReader(),
101                                            new InputSource JavaDoc(""));
102     transformThread = new Thread JavaDoc(new Runnable JavaDoc() {
103       public void run() {
104         try {
105           transformer.transform(source, new SAXResult JavaDoc(outputHandler));
106         }
107         catch (TransformerException JavaDoc e) {
108           transformerException = e;
109         }
110         finally {
111           synchronized (transformMonitor) {
112             transformMonitor.notify();
113           }
114         }
115       }
116     }, "Transform");
117     synchronized (contentHandlerMonitor) {
118       transformThread.start();
119       try {
120         contentHandlerMonitor.wait();
121       }
122       catch (InterruptedException JavaDoc e) {
123         throw new SAXException JavaDoc(e);
124       }
125     }
126     if (locator != null)
127       super.setDocumentLocator(locator);
128     super.startDocument();
129   }
130
131   public void endDocument()
132           throws SAXException JavaDoc {
133     super.endDocument();
134     synchronized (transformMonitor) {
135       synchronized (parseMonitor) {
136         parseMonitor.notify();
137       }
138       try {
139         transformMonitor.wait();
140       }
141       catch (InterruptedException JavaDoc e) {
142         throw new SAXException JavaDoc(e);
143       }
144       finally {
145         transformThread = null;
146       }
147     }
148     if (transformerException != null)
149       throw toSAXException(transformerException);
150   }
151
152   static SAXException JavaDoc toSAXException(TransformerException JavaDoc transformerException) {
153     // Unwrap where possible
154
Throwable JavaDoc wrapped = transformerException.getException();
155     if (wrapped instanceof SAXException JavaDoc)
156       return (SAXException JavaDoc)wrapped;
157     if (wrapped instanceof RuntimeException JavaDoc)
158       throw (RuntimeException JavaDoc)wrapped;
159     if (wrapped instanceof Exception JavaDoc)
160       return new SAXException JavaDoc((Exception JavaDoc)wrapped);
161     return new SAXException JavaDoc(transformerException);
162   }
163 }
164
Popular Tags