1 7 package org.jboss.xml.binding.parser.sax; 8 9 import org.jboss.xml.binding.parser.JBossXBParser; 10 import org.jboss.xml.binding.Unmarshaller; 11 import org.jboss.xml.binding.JBossXBException; 12 import org.jboss.util.xml.JBossEntityResolver; 13 import org.jboss.logging.Logger; 14 import org.xml.sax.XMLReader ; 15 import org.xml.sax.ErrorHandler ; 16 import org.xml.sax.SAXParseException ; 17 import org.xml.sax.SAXException ; 18 import org.xml.sax.Locator ; 19 import org.xml.sax.Attributes ; 20 import org.xml.sax.InputSource ; 21 import org.xml.sax.EntityResolver ; 22 23 import javax.xml.parsers.SAXParserFactory ; 24 import javax.xml.parsers.SAXParser ; 25 import java.io.Reader ; 26 import java.io.InputStream ; 27 28 32 public class SaxJBossXBParser 33 implements JBossXBParser 34 { 35 private static final Logger log = Logger.getLogger(SaxJBossXBParser.class); 36 37 private final SAXParser parser; 38 private JBossXBParser.ContentHandler contentHandler; 39 40 public SaxJBossXBParser() 41 throws JBossXBException 42 { 43 SAXParserFactory saxFactory = SAXParserFactory.newInstance(); 44 saxFactory.setValidating(true); 45 saxFactory.setNamespaceAware(true); 46 47 try 48 { 49 parser = saxFactory.newSAXParser(); 50 } 51 catch(Exception e) 52 { 53 throw new JBossXBException("Failed to create a new SAX parser", e); 54 } 55 56 XMLReader reader = null; 57 try 58 { 59 reader = parser.getXMLReader(); 60 } 61 catch(SAXException e) 62 { 63 throw new JBossXBException("Failed to get parser's XMLReader", e); 64 } 65 66 reader.setContentHandler(new DelegatingContentHandler()); 67 reader.setErrorHandler(new MetaDataErrorHandler()); 68 reader.setEntityResolver(new JBossEntityResolver()); 69 70 setFeature(Unmarshaller.VALIDATION, true); 71 setFeature(Unmarshaller.SCHEMA_VALIDATION, true); 72 setFeature(Unmarshaller.SCHEMA_FULL_CHECKING, true); 73 setFeature(Unmarshaller.DYNAMIC_VALIDATION, true); 74 setFeature(Unmarshaller.NAMESPACES, true); 75 } 76 77 79 public void setEntityResolver(EntityResolver entityResolver) 80 throws JBossXBException 81 { 82 try 83 { 84 parser.getXMLReader().setEntityResolver(entityResolver); 85 } 86 catch(SAXException e) 87 { 88 throw new JBossXBException("Failed to set EntityResolver", e); 89 } 90 } 91 92 public void setProperty(String name, Object value) throws JBossXBException 93 { 94 try 95 { 96 parser.getXMLReader().setProperty(name, value); 97 } 98 catch(SAXException e) 99 { 100 throw new JBossXBException("Failed to get parser's XMLReader", e); 101 } 102 } 103 104 public void setFeature(String name, boolean value) throws JBossXBException 105 { 106 try 107 { 108 parser.getXMLReader().setFeature(name, value); 109 } 110 catch(SAXException e) 111 { 112 throw new JBossXBException("Failed to get parser's XMLReader", e); 113 } 114 } 115 116 public void parse(String systemId, ContentHandler handler) throws JBossXBException 117 { 118 this.contentHandler = handler; 119 try 120 { 121 parser.getXMLReader().parse(systemId); 122 } 123 catch(Exception e) 124 { 125 throw new JBossXBException("Failed to parse source.", e); 126 } 127 } 128 129 public void parse(InputStream is, ContentHandler handler) throws JBossXBException 130 { 131 this.contentHandler = handler; 132 try 133 { 134 parser.getXMLReader().parse(new InputSource (is)); 135 } 136 catch(Exception e) 137 { 138 throw new JBossXBException("Failed to parse source.", e); 139 } 140 } 141 142 public void parse(Reader reader, ContentHandler handler) throws JBossXBException 143 { 144 this.contentHandler = handler; 145 try 146 { 147 parser.getXMLReader().parse(new InputSource (reader)); 148 } 149 catch(Exception e) 150 { 151 throw new JBossXBException("Failed to parse source.", e); 152 } 153 } 154 155 157 private final class DelegatingContentHandler 158 implements org.xml.sax.ContentHandler 159 { 160 public void endDocument() 161 { 162 } 163 164 public void startDocument() 165 { 166 } 167 168 public void characters(char ch[], int start, int length) 169 { 170 int i = start; 173 while(i < start + length) 174 { 175 if(!Character.isWhitespace(ch[i++])) 176 { 177 contentHandler.characters(ch, start, length); 178 break; 179 } 180 } 181 } 182 183 public void ignorableWhitespace(char ch[], int start, int length) 184 { 185 } 186 187 public void endPrefixMapping(String prefix) 188 { 189 contentHandler.endPrefixMapping(prefix); 190 } 191 192 public void skippedEntity(String name) 193 { 194 } 195 196 public void setDocumentLocator(Locator locator) 197 { 198 } 199 200 public void processingInstruction(String target, String data) 201 { 202 } 203 204 public void startPrefixMapping(String prefix, String uri) 205 { 206 contentHandler.startPrefixMapping(prefix, uri); 207 } 208 209 public void endElement(String namespaceURI, String localName, String qName) 210 { 211 contentHandler.endElement(namespaceURI, localName, qName); 212 } 213 214 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 215 { 216 contentHandler.startElement(namespaceURI, localName, qName, atts, null); 217 } 218 } 219 220 private static final class MetaDataErrorHandler 221 implements ErrorHandler 222 { 223 public void warning(SAXParseException exception) 224 { 225 log.warn(formatMessage(exception)); 226 } 227 228 public void error(SAXParseException exception) 229 throws SAXException 230 { 231 throw new SAXException (formatMessage(exception)); 232 } 233 234 public void fatalError(SAXParseException exception) 235 throws SAXException 236 { 237 throw new SAXException (formatMessage(exception)); 238 } 239 240 public String formatMessage(SAXParseException exception) 241 { 242 StringBuffer buffer = new StringBuffer (50); 243 buffer.append(exception.getMessage()).append(" @ "); 244 String location = exception.getPublicId(); 245 if(location != null) 246 { 247 buffer.append(location); 248 } 249 else 250 { 251 location = exception.getSystemId(); 252 if(location != null) 253 { 254 buffer.append(location); 255 } 256 else 257 { 258 buffer.append("*unknown*"); 259 } 260 } 261 buffer.append('['); 262 buffer.append(exception.getLineNumber()).append(','); 263 buffer.append(exception.getColumnNumber()).append(']'); 264 return buffer.toString(); 265 } 266 } 267 } 268 | Popular Tags |