< prev index next >

src/java.xml.bind/share/classes/javax/xml/bind/helpers/AbstractUnmarshallerImpl.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  30 import org.xml.sax.XMLReader;
  31 import org.w3c.dom.Node;
  32 
  33 import javax.xml.bind.JAXBException;
  34 import javax.xml.bind.PropertyException;
  35 import javax.xml.bind.UnmarshalException;
  36 import javax.xml.bind.Unmarshaller;
  37 import javax.xml.bind.ValidationEventHandler;
  38 import javax.xml.bind.JAXBElement;
  39 import javax.xml.bind.annotation.adapters.XmlAdapter;
  40 import javax.xml.bind.attachment.AttachmentUnmarshaller;
  41 import javax.xml.parsers.ParserConfigurationException;
  42 import javax.xml.parsers.SAXParserFactory;
  43 import javax.xml.stream.XMLEventReader;
  44 import javax.xml.stream.XMLStreamReader;
  45 import javax.xml.transform.Source;
  46 import javax.xml.transform.dom.DOMSource;
  47 import javax.xml.transform.sax.SAXSource;
  48 import javax.xml.transform.stream.StreamSource;
  49 import javax.xml.validation.Schema;
  50 import java.io.File;
  51 import java.io.Reader;
  52 import java.net.MalformedURLException;
  53 import java.net.URL;
  54 
  55 /**
  56  * Partial default {@code Unmarshaller} implementation.
  57  *
  58  * <p>
  59  * This class provides a partial default implementation for the
  60  * {@link javax.xml.bind.Unmarshaller}interface.
  61  *
  62  * <p>
  63  * A JAXB Provider has to implement five methods (getUnmarshallerHandler,
  64  * unmarshal(Node), unmarshal(XMLReader,InputSource),
  65  * unmarshal(XMLStreamReader), and unmarshal(XMLEventReader).
  66  *
  67  * @author <ul>
  68  *         <li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li>
  69  *         </ul>
  70  * @see javax.xml.bind.Unmarshaller
  71  * @since 1.6, JAXB 1.0
  72  */


 161     private Object unmarshal( String url ) throws JAXBException {
 162         return unmarshal( new InputSource(url) );
 163     }
 164 
 165     public final Object unmarshal( URL url ) throws JAXBException {
 166         if( url == null ) {
 167             throw new IllegalArgumentException(
 168                 Messages.format( Messages.MUST_NOT_BE_NULL, "url" ) );
 169         }
 170 
 171         return unmarshal( url.toExternalForm() );
 172     }
 173 
 174     public final Object unmarshal( File f ) throws JAXBException {
 175         if( f == null ) {
 176             throw new IllegalArgumentException(
 177                 Messages.format( Messages.MUST_NOT_BE_NULL, "file" ) );
 178         }
 179 
 180         try {
 181             // copied from JAXP
 182             String path = f.getAbsolutePath();
 183             if (File.separatorChar != '/')
 184                 path = path.replace(File.separatorChar, '/');
 185             if (!path.startsWith("/"))
 186                 path = "/" + path;
 187             if (!path.endsWith("/") && f.isDirectory())
 188                 path = path + "/";
 189             return unmarshal(new URL("file", "", path));
 190         } catch( MalformedURLException e ) {
 191             throw new IllegalArgumentException(e.getMessage());
 192         }
 193     }
 194 
 195     public final Object unmarshal( java.io.InputStream is )
 196         throws JAXBException {
 197 
 198         if( is == null ) {
 199             throw new IllegalArgumentException(
 200                 Messages.format( Messages.MUST_NOT_BE_NULL, "is" ) );
 201         }
 202 
 203         InputSource isrc = new InputSource( is );
 204         return unmarshal( isrc );
 205     }
 206 
 207     public final Object unmarshal( Reader reader ) throws JAXBException {
 208         if( reader == null ) {
 209             throw new IllegalArgumentException(
 210                 Messages.format( Messages.MUST_NOT_BE_NULL, "reader" ) );


   1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  30 import org.xml.sax.XMLReader;
  31 import org.w3c.dom.Node;
  32 
  33 import javax.xml.bind.JAXBException;
  34 import javax.xml.bind.PropertyException;
  35 import javax.xml.bind.UnmarshalException;
  36 import javax.xml.bind.Unmarshaller;
  37 import javax.xml.bind.ValidationEventHandler;
  38 import javax.xml.bind.JAXBElement;
  39 import javax.xml.bind.annotation.adapters.XmlAdapter;
  40 import javax.xml.bind.attachment.AttachmentUnmarshaller;
  41 import javax.xml.parsers.ParserConfigurationException;
  42 import javax.xml.parsers.SAXParserFactory;
  43 import javax.xml.stream.XMLEventReader;
  44 import javax.xml.stream.XMLStreamReader;
  45 import javax.xml.transform.Source;
  46 import javax.xml.transform.dom.DOMSource;
  47 import javax.xml.transform.sax.SAXSource;
  48 import javax.xml.transform.stream.StreamSource;
  49 import javax.xml.validation.Schema;
  50 import java.io.*;


  51 import java.net.URL;
  52 
  53 /**
  54  * Partial default {@code Unmarshaller} implementation.
  55  *
  56  * <p>
  57  * This class provides a partial default implementation for the
  58  * {@link javax.xml.bind.Unmarshaller}interface.
  59  *
  60  * <p>
  61  * A JAXB Provider has to implement five methods (getUnmarshallerHandler,
  62  * unmarshal(Node), unmarshal(XMLReader,InputSource),
  63  * unmarshal(XMLStreamReader), and unmarshal(XMLEventReader).
  64  *
  65  * @author <ul>
  66  *         <li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li>
  67  *         </ul>
  68  * @see javax.xml.bind.Unmarshaller
  69  * @since 1.6, JAXB 1.0
  70  */


 159     private Object unmarshal( String url ) throws JAXBException {
 160         return unmarshal( new InputSource(url) );
 161     }
 162 
 163     public final Object unmarshal( URL url ) throws JAXBException {
 164         if( url == null ) {
 165             throw new IllegalArgumentException(
 166                 Messages.format( Messages.MUST_NOT_BE_NULL, "url" ) );
 167         }
 168 
 169         return unmarshal( url.toExternalForm() );
 170     }
 171 
 172     public final Object unmarshal( File f ) throws JAXBException {
 173         if( f == null ) {
 174             throw new IllegalArgumentException(
 175                 Messages.format( Messages.MUST_NOT_BE_NULL, "file" ) );
 176         }
 177 
 178         try {
 179             return unmarshal(new BufferedInputStream(new FileInputStream(f)));
 180         } catch( FileNotFoundException e ) {








 181             throw new IllegalArgumentException(e.getMessage());
 182         }
 183     }
 184 
 185     public final Object unmarshal( java.io.InputStream is )
 186         throws JAXBException {
 187 
 188         if( is == null ) {
 189             throw new IllegalArgumentException(
 190                 Messages.format( Messages.MUST_NOT_BE_NULL, "is" ) );
 191         }
 192 
 193         InputSource isrc = new InputSource( is );
 194         return unmarshal( isrc );
 195     }
 196 
 197     public final Object unmarshal( Reader reader ) throws JAXBException {
 198         if( reader == null ) {
 199             throw new IllegalArgumentException(
 200                 Messages.format( Messages.MUST_NOT_BE_NULL, "reader" ) );


< prev index next >