< prev index next >

src/java.desktop/share/classes/java/beans/XMLDecoder.java

Print this page




  36 import org.xml.sax.InputSource;
  37 import org.xml.sax.helpers.DefaultHandler;
  38 
  39 /**
  40  * The {@code XMLDecoder} class is used to read XML documents
  41  * created using the {@code XMLEncoder} and is used just like
  42  * the {@code ObjectInputStream}. For example, one can use
  43  * the following fragment to read the first object defined
  44  * in an XML document written by the {@code XMLEncoder}
  45  * class:
  46  * <pre>
  47  *       XMLDecoder d = new XMLDecoder(
  48  *                          new BufferedInputStream(
  49  *                              new FileInputStream("Test.xml")));
  50  *       Object result = d.readObject();
  51  *       d.close();
  52  * </pre>
  53  *
  54  *<p>
  55  * For more information you might also want to check out
  56  * <a
  57  href="http://java.sun.com/products/jfc/tsc/articles/persistence3">Long Term Persistence of JavaBeans Components: XML Schema</a>,
  58  * an article in <em>The Swing Connection.</em>
  59  * @see XMLEncoder
  60  * @see java.io.ObjectInputStream
  61  *
  62  * @since 1.4
  63  *
  64  * @author Philip Milne
  65  */
  66 public class XMLDecoder implements AutoCloseable {
  67     private final AccessControlContext acc = AccessController.getContext();
  68     private final DocumentHandler handler = new DocumentHandler();
  69     private final InputSource input;
  70     private Object owner;
  71     private Object[] array;
  72     private int index;
  73 
  74     /**
  75      * Creates a new input stream for reading archives
  76      * created by the {@code XMLEncoder} class.
  77      *


 267      * Gets the owner of this decoder.
 268      *
 269      * @return The owner of this decoder.
 270      *
 271      * @see #setOwner
 272      */
 273     public Object getOwner() {
 274         return owner;
 275     }
 276 
 277     /**
 278      * Creates a new handler for SAX parser
 279      * that can be used to parse embedded XML archives
 280      * created by the {@code XMLEncoder} class.
 281      *
 282      * The {@code owner} should be used if parsed XML document contains
 283      * the method call within context of the &lt;java&gt; element.
 284      * The {@code null} value may cause illegal parsing in such case.
 285      * The same problem may occur, if the {@code owner} class
 286      * does not contain expected method to call. See details <a
 287      * href="http://java.sun.com/products/jfc/tsc/articles/persistence3/">here</a>.

 288      *
 289      * @param owner  the owner of the default handler
 290      *               that can be used as a value of &lt;java&gt; element
 291      * @param el     the exception handler for the parser,
 292      *               or {@code null} to use the default exception handler
 293      * @param cl     the class loader used for instantiating objects,
 294      *               or {@code null} to use the default class loader
 295      * @return an instance of {@code DefaultHandler} for SAX parser
 296      *
 297      * @since 1.7
 298      */
 299     public static DefaultHandler createHandler(Object owner, ExceptionListener el, ClassLoader cl) {
 300         DocumentHandler handler = new DocumentHandler();
 301         handler.setOwner(owner);
 302         handler.setExceptionListener(el);
 303         handler.setClassLoader(cl);
 304         return handler;
 305     }
 306 }


  36 import org.xml.sax.InputSource;
  37 import org.xml.sax.helpers.DefaultHandler;
  38 
  39 /**
  40  * The {@code XMLDecoder} class is used to read XML documents
  41  * created using the {@code XMLEncoder} and is used just like
  42  * the {@code ObjectInputStream}. For example, one can use
  43  * the following fragment to read the first object defined
  44  * in an XML document written by the {@code XMLEncoder}
  45  * class:
  46  * <pre>
  47  *       XMLDecoder d = new XMLDecoder(
  48  *                          new BufferedInputStream(
  49  *                              new FileInputStream("Test.xml")));
  50  *       Object result = d.readObject();
  51  *       d.close();
  52  * </pre>
  53  *
  54  *<p>
  55  * For more information you might also want to check out
  56  * <a href="http://www.oracle.com/technetwork/java/persistence3-139471.html">
  57  * Long Term Persistence of JavaBeans Components: XML Schema</a>,
  58  * an article in <em>The Swing Connection.</em>
  59  * @see XMLEncoder
  60  * @see java.io.ObjectInputStream
  61  *
  62  * @since 1.4
  63  *
  64  * @author Philip Milne
  65  */
  66 public class XMLDecoder implements AutoCloseable {
  67     private final AccessControlContext acc = AccessController.getContext();
  68     private final DocumentHandler handler = new DocumentHandler();
  69     private final InputSource input;
  70     private Object owner;
  71     private Object[] array;
  72     private int index;
  73 
  74     /**
  75      * Creates a new input stream for reading archives
  76      * created by the {@code XMLEncoder} class.
  77      *


 267      * Gets the owner of this decoder.
 268      *
 269      * @return The owner of this decoder.
 270      *
 271      * @see #setOwner
 272      */
 273     public Object getOwner() {
 274         return owner;
 275     }
 276 
 277     /**
 278      * Creates a new handler for SAX parser
 279      * that can be used to parse embedded XML archives
 280      * created by the {@code XMLEncoder} class.
 281      *
 282      * The {@code owner} should be used if parsed XML document contains
 283      * the method call within context of the &lt;java&gt; element.
 284      * The {@code null} value may cause illegal parsing in such case.
 285      * The same problem may occur, if the {@code owner} class
 286      * does not contain expected method to call. See details <a
 287      * href="http://www.oracle.com/technetwork/java/persistence3-139471.html">
 288      * here</a>.
 289      *
 290      * @param owner  the owner of the default handler
 291      *               that can be used as a value of &lt;java&gt; element
 292      * @param el     the exception handler for the parser,
 293      *               or {@code null} to use the default exception handler
 294      * @param cl     the class loader used for instantiating objects,
 295      *               or {@code null} to use the default class loader
 296      * @return an instance of {@code DefaultHandler} for SAX parser
 297      *
 298      * @since 1.7
 299      */
 300     public static DefaultHandler createHandler(Object owner, ExceptionListener el, ClassLoader cl) {
 301         DocumentHandler handler = new DocumentHandler();
 302         handler.setOwner(owner);
 303         handler.setExceptionListener(el);
 304         handler.setClassLoader(cl);
 305         return handler;
 306     }
 307 }
< prev index next >