FileDocCategorySizeDatePackage
AbstractUnmarshallerImpl.javaAPI DocJava SE 6 API14439Tue Jun 10 00:27:04 BST 2008javax.xml.bind.helpers

AbstractUnmarshallerImpl

public abstract class AbstractUnmarshallerImpl extends Object implements Unmarshaller
Partial default Unmarshaller implementation.

This class provides a partial default implementation for the {@link javax.xml.bind.Unmarshaller}interface.

A JAXB Provider has to implement five methods (getUnmarshallerHandler, unmarshal(Node), unmarshal(XMLReader,InputSource), unmarshal(XMLStreamReader), and unmarshal(XMLEventReader).

author
  • Kohsuke Kawaguchi, Sun Microsystems, Inc.
version
$Revision: 1.13 $ $Date: 2005/07/28 22:18:12 $
see
javax.xml.bind.Unmarshaller
since
JAXB1.0

Fields Summary
private ValidationEventHandler
eventHandler
handler that will be used to process errors and warnings during unmarshal
protected boolean
validating
whether or not the unmarshaller will validate
private XMLReader
reader
XMLReader that will be used to parse a document.
Constructors Summary
Methods Summary
protected javax.xml.bind.UnmarshalExceptioncreateUnmarshalException(org.xml.sax.SAXException e)
Creates an UnmarshalException from a SAXException. This is an utility method provided for the derived classes.

When a provider-implemented ContentHandler wants to throw a JAXBException, it needs to wrap the exception by a SAXException. If the unmarshaller implementation blindly wrap SAXException by JAXBException, such an exception will be a JAXBException wrapped by a SAXException wrapped by another JAXBException. This is silly.

This method checks the nested exception of SAXException and reduce those excessive wrapping.

return
the resulting UnmarshalException

        // check the nested exception to see if it's an UnmarshalException
        Exception nested = e.getException();
        if(nested instanceof UnmarshalException)
            return (UnmarshalException)nested;
        
        if(nested instanceof RuntimeException)
            // typically this is an unexpected exception,
            // just throw it rather than wrap it, so that the full stack
            // trace can be displayed.
            throw (RuntimeException)nested;
                
        
        // otherwise simply wrap it
        if(nested!=null)
            return new UnmarshalException(nested);
        else
            return new UnmarshalException(e);
    
public AgetAdapter(java.lang.Class type)

        throw new UnsupportedOperationException();
    
public javax.xml.bind.attachment.AttachmentUnmarshallergetAttachmentUnmarshaller()

        throw new UnsupportedOperationException();
    
public javax.xml.bind.ValidationEventHandlergetEventHandler()
Return the current event handler or the default event handler if one hasn't been set.

return
the current ValidationEventHandler or the default event handler if it hasn't been set
throws
JAXBException if an error was encountered while getting the current event handler

        return eventHandler;
    
public ListenergetListener()

        throw new UnsupportedOperationException();
    
public java.lang.ObjectgetProperty(java.lang.String name)
Default implementation of the getProperty method always throws PropertyException since there are no required properties. If a provider needs to handle additional properties, it should override this method in a derived class.

            
        if( name == null ) {
            throw new IllegalArgumentException(
                Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
        }

        throw new PropertyException(name);
    
public javax.xml.validation.SchemagetSchema()

        throw new UnsupportedOperationException();
    
protected org.xml.sax.XMLReadergetXMLReader()
Obtains a configured XMLReader. This method is used when the client-specified {@link SAXSource} object doesn't have XMLReader. {@link Unmarshaller} is not re-entrant, so we will only use one instance of XMLReader.


                                          
         
        if(reader==null) {
            try {
                SAXParserFactory parserFactory;
                parserFactory = SAXParserFactory.newInstance();
                parserFactory.setNamespaceAware(true);
                // there is no point in asking a validation because 
                // there is no guarantee that the document will come with
                // a proper schemaLocation.
                parserFactory.setValidating(false);
                reader = parserFactory.newSAXParser().getXMLReader();
            } catch( ParserConfigurationException e ) {
                throw new JAXBException(e);
            } catch( SAXException e ) {
                throw new JAXBException(e);
            }
        }
        return reader;
    
public booleanisValidating()
Indicates whether or not the Unmarshaller is configured to validate during unmarshal operations.

Note: I named this method isValidating() to stay in-line with JAXP, as opposed to naming it getValidating().

return
true if the Unmarshaller is configured to validate during unmarshal operations, false otherwise
throws
JAXBException if an error occurs while retrieving the validating flag

        return validating;
    
public voidsetAdapter(javax.xml.bind.annotation.adapters.XmlAdapter adapter)

        if(adapter==null)
            throw new IllegalArgumentException();
        setAdapter((Class)adapter.getClass(),adapter);
    
public voidsetAdapter(java.lang.Class type, A adapter)

        throw new UnsupportedOperationException();
    
public voidsetAttachmentUnmarshaller(javax.xml.bind.attachment.AttachmentUnmarshaller au)

        throw new UnsupportedOperationException();
    
public voidsetEventHandler(javax.xml.bind.ValidationEventHandler handler)
Allow an application to register a validation event handler.

The validation event handler will be called by the JAXB Provider if any validation errors are encountered during calls to any of the unmarshal methods. If the client application does not register a validation event handler before invoking the unmarshal methods, then all validation events will be silently ignored and may result in unexpected behaviour.

param
handler the validation event handler
throws
JAXBException if an error was encountered while setting the event handler

        
        if( handler == null ) {
            eventHandler = new DefaultValidationEventHandler();
        } else {
            eventHandler = handler;
        }
    
public voidsetListener(Listener listener)

        throw new UnsupportedOperationException();
    
public voidsetProperty(java.lang.String name, java.lang.Object value)
Default implementation of the setProperty method always throws PropertyException since there are no required properties. If a provider needs to handle additional properties, it should override this method in a derived class.


        if( name == null ) {
            throw new IllegalArgumentException(
                Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
        }

        throw new PropertyException(name, value);
    
public voidsetSchema(javax.xml.validation.Schema schema)

        throw new UnsupportedOperationException();
    
public voidsetValidating(boolean validating)
Specifies whether or not the Unmarshaller should validate during unmarshal operations. By default, the Unmarshaller does not validate.

This method may only be invoked before or after calling one of the unmarshal methods.

param
validating true if the Unmarshaller should validate during unmarshal, false otherwise
throws
JAXBException if an error occurred while enabling or disabling validation at unmarshal time

        this.validating = validating;
    
private static org.xml.sax.InputSourcestreamSourceToInputSource(javax.xml.transform.stream.StreamSource ss)

        InputSource is = new InputSource();
        is.setSystemId( ss.getSystemId() );
        is.setByteStream( ss.getInputStream() );
        is.setCharacterStream( ss.getReader() );
        
        return is;
    
public final java.lang.Objectunmarshal(java.io.Reader reader)

        if( reader == null ) {
            throw new IllegalArgumentException(
                Messages.format( Messages.MUST_NOT_BE_NULL, "reader" ) );
        }

        InputSource isrc = new InputSource( reader );
        return unmarshal( isrc );
    
public java.lang.Objectunmarshal(javax.xml.stream.XMLEventReader reader)

        
        throw new UnsupportedOperationException();
    
public java.lang.Objectunmarshal(javax.xml.transform.Source source)

        if( source == null ) {
            throw new IllegalArgumentException(
                Messages.format( Messages.MUST_NOT_BE_NULL, "source" ) );
        }
        
        if(source instanceof SAXSource)
            return unmarshal( (SAXSource)source );
        if(source instanceof StreamSource)
            return unmarshal( streamSourceToInputSource((StreamSource)source));
        if(source instanceof DOMSource)
            return unmarshal( ((DOMSource)source).getNode() );
        
        // we don't handle other types of Source
        throw new IllegalArgumentException();
    
public java.lang.Objectunmarshal(javax.xml.stream.XMLStreamReader reader)

        
        throw new UnsupportedOperationException();
    
public javax.xml.bind.JAXBElementunmarshal(org.w3c.dom.Node node, java.lang.Class expectedType)

        throw new UnsupportedOperationException();
    
public javax.xml.bind.JAXBElementunmarshal(javax.xml.transform.Source source, java.lang.Class expectedType)

        throw new UnsupportedOperationException();
    
public javax.xml.bind.JAXBElementunmarshal(javax.xml.stream.XMLStreamReader reader, java.lang.Class expectedType)

        throw new UnsupportedOperationException();
    
public javax.xml.bind.JAXBElementunmarshal(javax.xml.stream.XMLEventReader reader, java.lang.Class expectedType)

        throw new UnsupportedOperationException();
    
private java.lang.Objectunmarshal(javax.xml.transform.sax.SAXSource source)

        
        XMLReader reader = source.getXMLReader();
        if( reader == null )
            reader = getXMLReader();
        
        return unmarshal( reader, source.getInputSource() );
    
protected abstract java.lang.Objectunmarshal(org.xml.sax.XMLReader reader, org.xml.sax.InputSource source)
Unmarshals an object by using the specified XMLReader and the InputSource. The callee should call the setErrorHandler method of the XMLReader so that errors are passed to the client-specified ValidationEventHandler.

public final java.lang.Objectunmarshal(org.xml.sax.InputSource source)

        if( source == null ) {
            throw new IllegalArgumentException(
                Messages.format( Messages.MUST_NOT_BE_NULL, "source" ) );
        }

        return unmarshal( getXMLReader(), source );
    
private java.lang.Objectunmarshal(java.lang.String url)

        return unmarshal( new InputSource(url) );
    
public final java.lang.Objectunmarshal(java.net.URL url)

        if( url == null ) {
            throw new IllegalArgumentException(
                Messages.format( Messages.MUST_NOT_BE_NULL, "url" ) );
        }

        return unmarshal( url.toExternalForm() );
    
public final java.lang.Objectunmarshal(java.io.File f)

        if( f == null ) {
            throw new IllegalArgumentException(
                Messages.format( Messages.MUST_NOT_BE_NULL, "file" ) );
        }

        try {
            // copied from JAXP
	    String path = f.getAbsolutePath();
	    if (File.separatorChar != '/")
	    	path = path.replace(File.separatorChar, '/");
	    if (!path.startsWith("/"))
	    	path = "/" + path;
	    if (!path.endsWith("/") && f.isDirectory())
	    	path = path + "/";
	    return unmarshal(new URL("file", "", path));
        } catch( MalformedURLException e ) {
            throw new IllegalArgumentException(e.getMessage());
        }
    
public final java.lang.Objectunmarshal(java.io.InputStream is)

            
        if( is == null ) {
            throw new IllegalArgumentException(
                Messages.format( Messages.MUST_NOT_BE_NULL, "is" ) );
        }

        InputSource isrc = new InputSource( is );
        return unmarshal( isrc );