1 5 package javax.xml.bind.helpers; 6 7 import org.xml.sax.InputSource ; 8 import org.xml.sax.SAXException ; 9 import org.xml.sax.XMLReader ; 10 import org.w3c.dom.Node ; 11 12 import javax.xml.bind.JAXBException; 13 import javax.xml.bind.PropertyException; 14 import javax.xml.bind.UnmarshalException; 15 import javax.xml.bind.Unmarshaller; 16 import javax.xml.bind.ValidationEventHandler; 17 import javax.xml.bind.JAXBElement; 18 import javax.xml.bind.annotation.adapters.XmlAdapter; 19 import javax.xml.bind.attachment.AttachmentUnmarshaller; 20 import javax.xml.parsers.ParserConfigurationException ; 21 import javax.xml.parsers.SAXParserFactory ; 22 import javax.xml.stream.XMLEventReader; 23 import javax.xml.stream.XMLStreamReader; 24 import javax.xml.transform.Source ; 25 import javax.xml.transform.dom.DOMSource ; 26 import javax.xml.transform.sax.SAXSource ; 27 import javax.xml.transform.stream.StreamSource ; 28 import javax.xml.validation.Schema ; 29 import java.io.File ; 30 import java.io.Reader ; 31 import java.net.MalformedURLException ; 32 import java.net.URL ; 33 34 53 public abstract class AbstractUnmarshallerImpl implements Unmarshaller 54 { 55 56 private ValidationEventHandler eventHandler = 57 new DefaultValidationEventHandler(); 58 59 60 protected boolean validating = false; 61 62 65 private XMLReader reader = null; 66 67 76 protected XMLReader getXMLReader() throws JAXBException { 77 if(reader==null) { 78 try { 79 SAXParserFactory parserFactory; 80 parserFactory = SAXParserFactory.newInstance(); 81 parserFactory.setNamespaceAware(true); 82 parserFactory.setValidating(false); 86 reader = parserFactory.newSAXParser().getXMLReader(); 87 } catch( ParserConfigurationException e ) { 88 throw new JAXBException(e); 89 } catch( SAXException e ) { 90 throw new JAXBException(e); 91 } 92 } 93 return reader; 94 } 95 96 public Object unmarshal( Source source ) throws JAXBException { 97 if( source == null ) { 98 throw new IllegalArgumentException ( 99 Messages.format( Messages.MUST_NOT_BE_NULL, "source" ) ); 100 } 101 102 if(source instanceof SAXSource ) 103 return unmarshal( (SAXSource )source ); 104 if(source instanceof StreamSource ) 105 return unmarshal( streamSourceToInputSource((StreamSource )source)); 106 if(source instanceof DOMSource ) 107 return unmarshal( ((DOMSource )source).getNode() ); 108 109 throw new IllegalArgumentException (); 111 } 112 113 private Object unmarshal( SAXSource source ) throws JAXBException { 115 116 XMLReader reader = source.getXMLReader(); 117 if( reader == null ) 118 reader = getXMLReader(); 119 120 return unmarshal( reader, source.getInputSource() ); 121 } 122 123 129 protected abstract Object unmarshal( XMLReader reader, InputSource source ) throws JAXBException; 130 131 public final Object unmarshal( InputSource source ) throws JAXBException { 132 if( source == null ) { 133 throw new IllegalArgumentException ( 134 Messages.format( Messages.MUST_NOT_BE_NULL, "source" ) ); 135 } 136 137 return unmarshal( getXMLReader(), source ); 138 } 139 140 141 private Object unmarshal( String url ) throws JAXBException { 142 return unmarshal( new InputSource (url) ); 143 } 144 145 public final Object unmarshal( URL url ) throws JAXBException { 146 if( url == null ) { 147 throw new IllegalArgumentException ( 148 Messages.format( Messages.MUST_NOT_BE_NULL, "url" ) ); 149 } 150 151 return unmarshal( url.toExternalForm() ); 152 } 153 154 public final Object unmarshal( File f ) throws JAXBException { 155 if( f == null ) { 156 throw new IllegalArgumentException ( 157 Messages.format( Messages.MUST_NOT_BE_NULL, "file" ) ); 158 } 159 160 try { 161 String path = f.getAbsolutePath(); 163 if (File.separatorChar != '/') 164 path = path.replace(File.separatorChar, '/'); 165 if (!path.startsWith("/")) 166 path = "/" + path; 167 if (!path.endsWith("/") && f.isDirectory()) 168 path = path + "/"; 169 return unmarshal(new URL ("file", "", path)); 170 } catch( MalformedURLException e ) { 171 throw new IllegalArgumentException (e.getMessage()); 172 } 173 } 174 175 public final Object unmarshal( java.io.InputStream is ) 176 throws JAXBException { 177 178 if( is == null ) { 179 throw new IllegalArgumentException ( 180 Messages.format( Messages.MUST_NOT_BE_NULL, "is" ) ); 181 } 182 183 InputSource isrc = new InputSource ( is ); 184 return unmarshal( isrc ); 185 } 186 187 public final Object unmarshal( Reader reader ) throws JAXBException { 188 if( reader == null ) { 189 throw new IllegalArgumentException ( 190 Messages.format( Messages.MUST_NOT_BE_NULL, "reader" ) ); 191 } 192 193 InputSource isrc = new InputSource ( reader ); 194 return unmarshal( isrc ); 195 } 196 197 198 private static InputSource streamSourceToInputSource( StreamSource ss ) { 199 InputSource is = new InputSource (); 200 is.setSystemId( ss.getSystemId() ); 201 is.setByteStream( ss.getInputStream() ); 202 is.setCharacterStream( ss.getReader() ); 203 204 return is; 205 } 206 207 208 220 public boolean isValidating() throws JAXBException { 221 return validating; 222 } 223 224 238 public void setEventHandler(ValidationEventHandler handler) 239 throws JAXBException { 240 241 if( handler == null ) { 242 eventHandler = new DefaultValidationEventHandler(); 243 } else { 244 eventHandler = handler; 245 } 246 } 247 248 261 public void setValidating(boolean validating) throws JAXBException { 262 this.validating = validating; 263 } 264 265 274 public ValidationEventHandler getEventHandler() throws JAXBException { 275 return eventHandler; 276 } 277 278 279 298 protected UnmarshalException createUnmarshalException( SAXException e ) { 299 Exception nested = e.getException(); 301 if(nested instanceof UnmarshalException) 302 return (UnmarshalException)nested; 303 304 if(nested instanceof RuntimeException ) 305 throw (RuntimeException )nested; 309 310 311 if(nested!=null) 313 return new UnmarshalException(nested); 314 else 315 return new UnmarshalException(e); 316 } 317 318 324 public void setProperty( String name, Object value ) 325 throws PropertyException { 326 327 if( name == null ) { 328 throw new IllegalArgumentException ( 329 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); 330 } 331 332 throw new PropertyException(name, value); 333 } 334 335 341 public Object getProperty( String name ) 342 throws PropertyException { 343 344 if( name == null ) { 345 throw new IllegalArgumentException ( 346 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); 347 } 348 349 throw new PropertyException(name); 350 } 351 352 public Object unmarshal(XMLEventReader reader) throws JAXBException { 353 354 throw new UnsupportedOperationException (); 355 } 356 357 public Object unmarshal(XMLStreamReader reader) throws JAXBException { 358 359 throw new UnsupportedOperationException (); 360 } 361 362 public <T> JAXBElement<T> unmarshal(Node node, Class <T> expectedType) throws JAXBException { 363 throw new UnsupportedOperationException (); 364 } 365 366 public <T> JAXBElement<T> unmarshal(Source source, Class <T> expectedType) throws JAXBException { 367 throw new UnsupportedOperationException (); 368 } 369 370 public <T> JAXBElement<T> unmarshal(XMLStreamReader reader, Class <T> expectedType) throws JAXBException { 371 throw new UnsupportedOperationException (); 372 } 373 374 public <T> JAXBElement<T> unmarshal(XMLEventReader reader, Class <T> expectedType) throws JAXBException { 375 throw new UnsupportedOperationException (); 376 } 377 378 public void setSchema(Schema schema) { 379 throw new UnsupportedOperationException (); 380 } 381 382 public Schema getSchema() { 383 throw new UnsupportedOperationException (); 384 } 385 386 public void setAdapter(XmlAdapter adapter) { 387 if(adapter==null) 388 throw new IllegalArgumentException (); 389 setAdapter((Class )adapter.getClass(),adapter); 390 } 391 392 public <A extends XmlAdapter> void setAdapter(Class <A> type, A adapter) { 393 throw new UnsupportedOperationException (); 394 } 395 396 public <A extends XmlAdapter> A getAdapter(Class <A> type) { 397 throw new UnsupportedOperationException (); 398 } 399 400 public void setAttachmentUnmarshaller(AttachmentUnmarshaller au) { 401 throw new UnsupportedOperationException (); 402 } 403 404 public AttachmentUnmarshaller getAttachmentUnmarshaller() { 405 throw new UnsupportedOperationException (); 406 } 407 408 public void setListener(Listener listener) { 409 throw new UnsupportedOperationException (); 410 } 411 412 public Listener getListener() { 413 throw new UnsupportedOperationException (); 414 } 415 } 416 | Popular Tags |