1 58 59 package org.oddjob.arooa.xml; 60 61 import java.io.IOException ; 62 import java.io.UnsupportedEncodingException ; 63 64 import org.oddjob.arooa.ArooaException; 65 import org.oddjob.arooa.ArooaHandler; 66 import org.oddjob.arooa.ArooaContext; 67 import org.oddjob.arooa.Location; 68 import org.oddjob.arooa.RootHandler; 69 import org.xml.sax.InputSource ; 70 import org.xml.sax.SAXException ; 71 import org.xml.sax.SAXParseException ; 72 import org.xml.sax.XMLReader ; 73 import org.xml.sax.helpers.DefaultHandler ; 74 75 80 public class XMLDefinitionHelper { 81 82 private final ArooaContext context; 83 84 public XMLDefinitionHelper(ArooaContext context) { 85 this.context = context; 86 } 87 88 96 public void parse(InputSource inputSource, ArooaHandler mainHandler) 97 throws ArooaException { 98 99 RootHandler handler = new RootHandler(context, mainHandler); 100 101 try { 102 105 XMLReader parser = JAXPUtils.getNamespaceXMLReader(); 106 107 DefaultHandler hb = handler; 108 109 parser.setContentHandler(hb); 110 parser.setEntityResolver(hb); 111 parser.setErrorHandler(hb); 112 parser.setDTDHandler(hb); 113 parser.parse(inputSource); 114 } catch (SAXParseException exc) { 115 Location location = new Location(exc.getSystemId(), 116 exc.getLineNumber(), exc.getColumnNumber()); 117 118 Throwable t = exc.getException(); 119 if (t == null) { 120 throw new ArooaException(exc.getMessage(), exc, location); 121 } 122 if (t instanceof ArooaException) { 123 ArooaException be = (ArooaException) t; 124 if (be.getLocation() == Location.UNKNOWN_LOCATION) { 125 be.setLocation(location); 126 } 127 throw be; 128 } 129 throw new ArooaException(exc.getMessage(), t, location); 130 } catch (SAXException exc) { 131 Throwable t = exc.getException(); 132 if (t instanceof ArooaException) { 133 throw (ArooaException) t; 134 } 135 throw new ArooaException(exc.getMessage(), t); 136 } catch (UnsupportedEncodingException exc) { 137 throw new ArooaException("Encoding of input is invalid.", 138 exc); 139 } catch (IOException exc) { 140 throw new ArooaException("Error reading input.", 141 exc); 142 } 143 } 144 } 145 | Popular Tags |