Package snap.util

Class XMLArchiver

java.lang.Object
snap.util.XMLArchiver
Direct Known Subclasses:
ViewArchiver

public class XMLArchiver extends Object
This class manages archival and unarchival to/from XMLElements.

For archival, objects simply implement the toXML() method to configure and return XMLElements. Archiver's toXML() method manages the process, allowing for object references.

For unarchival, classes register for particular element names. Then during Archiver.fromXML(), Archiver will call fromXML() on the classes for encountered tags to reconstruct the object graph.

  • Constructor Details

    • XMLArchiver

      public XMLArchiver()
  • Method Details

    • getSourceURL

      public WebURL getSourceURL()
      Returns the WebURL of the currently loading archive.
    • setSourceURL

      public void setSourceURL(WebURL aURL)
      Sets the WebURL of the currently loading archive.
    • getOwner

      public Object getOwner()
      Returns the owner.
    • setOwner

      public void setOwner(Object anOwner)
      Sets the owner.
    • getOwnerClass

      public Class getOwnerClass()
      Returns the owner class.
    • getRootObject

      public Object getRootObject()
      Returns the object that the archiver should read "into".
    • setRootObject

      public void setRootObject(Object anObj)
      Sets the object that the archiver should read "into".
    • getVersion

      public double getVersion()
      Returns the version of the document.
    • setVersion

      public void setVersion(double aVersion)
      Sets the version of the document.
    • isIgnoreCase

      public boolean isIgnoreCase()
      Returns whether element should ignore case when asking for attributes/elements by name.
    • setIgnoreCase

      public void setIgnoreCase(boolean aVal)
      Sets whether element should ignore case when asking for attributes/elements by name.
    • getRootXML

      public XMLElement getRootXML()
      Returns the root xml.
    • getClassMap

      public Map<String,Class> getClassMap()
      Returns the class map.
    • createClassMap

      protected Map<String,Class> createClassMap()
      Creates the class map.
    • readFromXMLSource

      public Object readFromXMLSource(Object aSource)
      Returns a root object unarchived from a generic input source (a File, String path, InputStream, URL, byte[], etc.).
    • readFromXMLString

      public Object readFromXMLString(String xmlString)
      Returns a root object unarchived from a generic XML source (File, String path, InputStream, URL, byte[], etc.).
    • readFromXMLBytes

      public Object readFromXMLBytes(byte[] theBytes)
      Returns a root object unarchived from an RMByteSource.
    • readFromXML

      public Object readFromXML(XMLElement theXML)
      Returns a root object unarchived from the XML source (a File, String path, InputStream, URL, byte[], etc.). You can also provide a root object to be read "into", and an owner that the object is being read "for".
    • writeToXML

      public XMLElement writeToXML(Object anObj)
      Returns an xml element for a given object. This top level method encodes resources, in addition to doing the basic toXML stuff.
    • writeToXMLBytes

      public byte[] writeToXMLBytes(Object anObj)
      Writes given object to XML and returns the XML bytes.
    • fromXML

      public Object fromXML(XMLElement anElement, Object anOwner)
      Returns an object unarchived from the given element.
    • fromXML

      public <T> T fromXML(XMLElement anElement, Class<T> aClass, Object anOwner)
      Returns an object unarchived from the given element by instantiating the given class.
    • fromXML

      public Object fromXML(XMLElement anElement, Object anObj, Object anOwner)
      Calls fromXML on given object.
    • toXML

      public XMLElement toXML(Object anObj)
      Writes the given object to XML elements.
    • toXML

      public XMLElement toXML(Object anObj, Object anOwner)
      Writes the given object to XML elements.
    • getClass

      public Class getClass(String aName)
      Returns the class for a given element name.
    • getClassForXML

      protected Class<?> getClassForXML(XMLElement anElement)
      Returns the class for a given element.
    • newInstance

      protected Object newInstance(Class aClass)
      Returns a new instance of an object given a class.
    • getReference

      public int getReference(Object anObj)
      Returns a reference id for the given object (used in archival).
    • getReference

      public int getReference(Object anObj, boolean add)
      Returns a reference id for given object if in references list with option to add if absent (used in archival).
    • getReference

      public Object getReference(String aName, XMLElement anElement)
      Returns an object for a given reference (used in unarchival).
    • indexOf

      public int indexOf(XMLElement anElement, Class aClass)
      Returns the index of the first child element with the given name.
    • indexOf

      public int indexOf(XMLElement anElement, Class aClass, int startIndex)
      Returns the index of the first child element with the given name at or beyond the given index.
    • fromXMLList

      public List fromXMLList(XMLElement anElement, String aName, Class aClass, Object anOwner)
      Returns the list of objects of the given name and/or class (either can be null) unarchived from the given element.
    • copy

      public <T> T copy(T anObj)
      Returns a copy of the given object using archival.
    • getParent

      public Object getParent()
      Returns the top parent from the parent stack.
    • getParent

      public <T> T getParent(Class<T> aClass)
      Returns the first parent from the parent stack of given class.
    • pushParent

      protected void pushParent(Object anObj)
      Pushes a parent on the parent stack.
    • popParent

      protected Object popParent()
      Pops a parent from the parent stack.
    • getResources

      public List<XMLArchiver.Resource> getResources()
      Returns the list of optional resources associated with this archiver.
    • getResource

      public XMLArchiver.Resource getResource(int anIndex)
      Returns an individual resource associated with this archiver, by index.
    • getResource

      public byte[] getResource(String aName)
      Returns an individual resource associated with this archiver, by name.
    • addResource

      public String addResource(byte[] bytes, String aName)
      Adds a byte array resource to this archiver (only if absent).
    • getResources

      protected void getResources(XMLElement anElement)
      Reads resources from elements in given xml (top-level) element, converts from ASCII encoding and adds to archiver.