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 ; 8 import org.xml.sax.DTDHandler ; 9 import org.xml.sax.ErrorHandler ; 10 import org.xml.sax.InputSource ; 11 import org.xml.sax.Locator ; 12 import org.xml.sax.SAXException ; 13 14 import javax.xml.transform.Templates ; 15 import javax.xml.transform.Transformer ; 16 import javax.xml.transform.TransformerConfigurationException ; 17 import javax.xml.transform.TransformerException ; 18 import javax.xml.transform.sax.SAXResult ; 19 import javax.xml.transform.sax.SAXSource ; 20 21 class ValidatorImpl extends DelegatingContentHandler implements Validator { 22 private final Transformer transformer; 23 private Locator locator; 24 private TransformerException transformerException; 25 26 private final Object contentHandlerMonitor = new Object (); 27 private final Object parseMonitor = new Object (); 28 private final Object transformMonitor = new Object (); 29 private Thread transformThread; 30 private final ContentHandler outputHandler; 31 32 class BlockingReader extends XMLReaderImpl { 33 public void parse(InputSource in) throws SAXException { 34 synchronized (parseMonitor) { 35 synchronized (contentHandlerMonitor) { 36 contentHandlerMonitor.notify(); 37 } 38 try { 39 parseMonitor.wait(); 40 } 41 catch (InterruptedException e) { 42 throw new SAXException (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 templates, PropertyMap properties) { 58 ErrorHandler eh = ValidateProperty.ERROR_HANDLER.get(properties); 59 outputHandler = new OutputHandler(eh); 60 try { 61 transformer = templates.newTransformer(); 62 } 66 catch (TransformerConfigurationException e) { 67 throw new RuntimeException ("could not create transformer"); 68 } 69 } 70 71 public ContentHandler getContentHandler() { 72 return this; 73 } 74 75 public DTDHandler 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 e) { } 87 transformThread = null; 88 } 89 } 90 transformerException = null; 91 locator = null; 92 } 93 94 public void setDocumentLocator(Locator locator) { 95 this.locator = locator; 96 } 97 98 public void startDocument() 99 throws SAXException { 100 final SAXSource source = new SAXSource (new BlockingReader(), 101 new InputSource ("")); 102 transformThread = new Thread (new Runnable () { 103 public void run() { 104 try { 105 transformer.transform(source, new SAXResult (outputHandler)); 106 } 107 catch (TransformerException 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 e) { 123 throw new SAXException (e); 124 } 125 } 126 if (locator != null) 127 super.setDocumentLocator(locator); 128 super.startDocument(); 129 } 130 131 public void endDocument() 132 throws SAXException { 133 super.endDocument(); 134 synchronized (transformMonitor) { 135 synchronized (parseMonitor) { 136 parseMonitor.notify(); 137 } 138 try { 139 transformMonitor.wait(); 140 } 141 catch (InterruptedException e) { 142 throw new SAXException (e); 143 } 144 finally { 145 transformThread = null; 146 } 147 } 148 if (transformerException != null) 149 throw toSAXException(transformerException); 150 } 151 152 static SAXException toSAXException(TransformerException transformerException) { 153 Throwable wrapped = transformerException.getException(); 155 if (wrapped instanceof SAXException ) 156 return (SAXException )wrapped; 157 if (wrapped instanceof RuntimeException ) 158 throw (RuntimeException )wrapped; 159 if (wrapped instanceof Exception ) 160 return new SAXException ((Exception )wrapped); 161 return new SAXException (transformerException); 162 } 163 } 164 | Popular Tags |