1 16 17 package org.apache.ws.jaxme.xs.impl; 18 19 import java.io.IOException ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.UndeclaredThrowableException ; 22 23 import javax.xml.parsers.ParserConfigurationException ; 24 25 import org.apache.ws.jaxme.xs.XSContentHandler; 26 import org.apache.ws.jaxme.xs.XSSchema; 27 import org.apache.ws.jaxme.xs.parser.XSContext; 28 import org.apache.ws.jaxme.xs.parser.XsSAXParser; 29 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException; 30 import org.apache.ws.jaxme.xs.xml.XsESchema; 31 import org.apache.ws.jaxme.xs.xml.XsObjectFactory; 32 import org.xml.sax.Attributes ; 33 import org.xml.sax.Locator ; 34 import org.xml.sax.SAXException ; 35 36 37 40 class XSContentHandlerImpl implements XSContentHandler { 41 private final XSLogicalParser parser; 42 private final XsSAXParser xsSAXParser; 43 private boolean finished; 44 private int level = 0; 45 private final String systemId; 46 47 XSContentHandlerImpl(XSLogicalParser pParser, String pSystemId) throws SAXException { 48 parser = pParser; 49 systemId = pSystemId; 50 XSContext data = parser.getData(); 51 data.setXSLogicalParser(parser); 52 parser.clearSyntaxSchemas(); 53 XsObjectFactory factory = data.getXsObjectFactory(); 54 XsESchema mySchema = factory.newXsESchema(); 55 parser.setSchema(data.getXSObjectFactory().newXSSchema(data, mySchema)); 56 xsSAXParser = factory.newXsSAXParser(mySchema); 57 parser.addSyntaxSchema(mySchema); 58 data.setCurrentContentHandler(xsSAXParser); 59 } 60 61 public XSSchema getXSSchema() { 62 if (!finished) { 63 throw new IllegalStateException ("The endDocument() method has not yet been invoked."); 64 } 65 return parser.getSchema(); 66 } 67 68 public void startDocument() throws SAXException { 69 xsSAXParser.startDocument(); 70 } 71 72 public void endDocument() throws SAXException { 73 xsSAXParser.endDocument(); 74 XsESchema syntaxSchema = (XsESchema) xsSAXParser.getBean(); 75 parser.removeSyntaxSchema(); 76 XSContext data = parser.getData(); 77 data.setCurrentContentHandler(null); 78 79 try { 80 parser.parse(syntaxSchema, systemId); 81 XSSchema mySchema = parser.getSchema(); 82 parser.createSubstitutionGroups(mySchema); 83 mySchema.validate(); 84 finished = true; 85 } catch (IOException e) { 86 throw new SAXException(e); 87 } catch (ParserConfigurationException e) { 88 throw new SAXException(e); 89 } catch (RuntimeException e) { 90 Exception ex = e; 91 for (;;) { 92 UndeclaredThrowableException te = null; 93 Throwable t; 94 if (ex instanceof UndeclaredThrowableException ) { 95 te = ((UndeclaredThrowableException ) ex); 96 t = te.getUndeclaredThrowable(); 97 } else if (ex instanceof InvocationTargetException ) { 98 t = ((InvocationTargetException ) ex).getTargetException(); 99 } else { 100 break; 101 } 102 if (t instanceof Exception ) { 103 ex = (Exception ) t; 104 } else { 105 if (te == null) { 106 te = new UndeclaredThrowableException (t); 107 } 108 t.printStackTrace(); 109 throw te; 110 } 111 } 112 throw new LocSAXException(ex.getClass().getName() + ": " + ex.getMessage(), 113 parser.getData().getLocator(), ex); 114 } 115 } 116 117 public void characters(char[] pChars, int pStart, int pLen) throws SAXException { 118 xsSAXParser.characters(pChars, pStart, pLen); 119 } 120 121 public void ignorableWhitespace(char[] pChars, int pStart, int pLen) throws SAXException { 122 xsSAXParser.ignorableWhitespace(pChars, pStart, pLen); 123 } 124 125 public void endPrefixMapping(String pPrefix) throws SAXException { 126 xsSAXParser.endPrefixMapping(pPrefix); 127 } 128 129 public void skippedEntity(String pEntity) throws SAXException { 130 xsSAXParser.skippedEntity(pEntity); 131 } 132 133 public void setDocumentLocator(Locator pLocator) { 134 xsSAXParser.setDocumentLocator(pLocator); 135 } 136 137 public void processingInstruction(String pTarget, String pData) throws SAXException { 138 xsSAXParser.processingInstruction(pTarget, pData); 139 } 140 141 public void startPrefixMapping(String pPrefix, String pURI) throws SAXException { 142 xsSAXParser.startPrefixMapping(pPrefix, pURI); 143 } 144 145 public void endElement(String pNamespaceURI, String pLocalName, String pQName) throws SAXException { 146 xsSAXParser.endElement(pNamespaceURI, pLocalName, pQName); 147 --level; 148 } 149 150 public void startElement(String pNamespaceURI, String pLocalName, String pQName, Attributes pAttrs) 151 throws SAXException { 152 xsSAXParser.startElement(pNamespaceURI, pLocalName, pQName, pAttrs); 153 } 154 } | Popular Tags |