Class XMLMemento

  • All Implemented Interfaces:
    IMemento

    public final class XMLMemento
    extends java.lang.Object
    implements IMemento
    This class represents the default implementation of the IMemento interface.

    This class is not intended to be extended by clients.

    See Also:
    IMemento
    • Constructor Summary

      Constructors 
      Constructor Description
      XMLMemento​(org.w3c.dom.Document document, org.w3c.dom.Element element)
      Creates a memento for the specified document and element.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      IMemento copyChild​(IMemento child)
      Create a copy of the child node and append it to this node.
      IMemento createChild​(java.lang.String type)
      Creates a new child of this memento with the given type.
      IMemento createChild​(java.lang.String type, java.lang.String id)
      Creates a new child of this memento with the given type and id.
      static XMLMemento createReadRoot​(java.io.Reader reader)
      Creates a Document from the Reader and returns a memento on the first Element for reading the document.
      static XMLMemento createReadRoot​(java.io.Reader reader, java.lang.String baseDir)
      Creates a Document from the Reader and returns a memento on the first Element for reading the document.
      static XMLMemento createWriteRoot​(java.lang.String type)
      Returns a root memento for writing a document.
      java.lang.String[] getAttributeKeys()
      Returns an array of all the attribute keys of the memento.
      java.lang.Boolean getBoolean​(java.lang.String key)
      Returns the boolean value of the given key.
      IMemento getChild​(java.lang.String type)
      Returns the first child with the given type id.
      IMemento[] getChildren()
      Returns all children of this node.
      IMemento[] getChildren​(java.lang.String type)
      Returns all children with the given type id.
      java.lang.Float getFloat​(java.lang.String key)
      Returns the floating point value of the given key.
      java.lang.String getID()
      Returns the id for this memento.
      java.lang.Integer getInteger​(java.lang.String key)
      Returns the integer value of the given key.
      java.lang.String getString​(java.lang.String key)
      Returns the string value of the given key.
      java.lang.String getTextData()
      Returns the data of the Text node of the memento.
      java.lang.String getType()
      Returns the type for this memento.
      void putBoolean​(java.lang.String key, boolean value)
      Sets the value of the given key to the given boolean value.
      void putFloat​(java.lang.String key, float f)
      Sets the value of the given key to the given floating point number.
      void putInteger​(java.lang.String key, int n)
      Sets the value of the given key to the given integer.
      void putMemento​(IMemento memento)
      Copy the attributes and children from memento to the receiver.
      void putString​(java.lang.String key, java.lang.String value)
      Sets the value of the given key to the given string.
      void putTextData​(java.lang.String data)
      Sets the memento's Text node to contain the given data.
      void save​(java.io.Writer writer)
      Saves this memento's document current values to the specified writer.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • XMLMemento

        public XMLMemento​(org.w3c.dom.Document document,
                          org.w3c.dom.Element element)
        Creates a memento for the specified document and element.

        Clients should use createReadRoot and createWriteRoot to create the initial memento on a document.

        Parameters:
        document - the document for the memento
        element - the element node for the memento
    • Method Detail

      • createReadRoot

        public static XMLMemento createReadRoot​(java.io.Reader reader)
                                         throws WorkbenchException
        Creates a Document from the Reader and returns a memento on the first Element for reading the document.

        Same as calling createReadRoot(reader, null)

        Parameters:
        reader - the Reader used to create the memento's document
        Returns:
        a memento on the first Element for reading the document
        Throws:
        WorkbenchException - if IO problems, invalid format, or no element.
      • createReadRoot

        public static XMLMemento createReadRoot​(java.io.Reader reader,
                                                java.lang.String baseDir)
                                         throws WorkbenchException
        Creates a Document from the Reader and returns a memento on the first Element for reading the document.
        Parameters:
        reader - the Reader used to create the memento's document
        baseDir - the directory used to resolve relative file names in the XML document. This directory must exist and include the trailing separator. The directory format, including the separators, must be valid for the platform. Can be null if not needed.
        Returns:
        a memento on the first Element for reading the document
        Throws:
        WorkbenchException - if IO problems, invalid format, or no element.
      • createWriteRoot

        public static XMLMemento createWriteRoot​(java.lang.String type)
                                          throws org.w3c.dom.DOMException
        Returns a root memento for writing a document.
        Parameters:
        type - the element node type to create on the document
        Returns:
        the root memento for writing a document
        Throws:
        org.w3c.dom.DOMException - when the element could not be created for the passed type
      • createChild

        public IMemento createChild​(java.lang.String type)
                             throws org.w3c.dom.DOMException
        Creates a new child of this memento with the given type.

        The getChild and getChildren methods are used to retrieve children of a given type.

        Specified by:
        createChild in interface IMemento
        Parameters:
        type - the type
        Returns:
        a new child memento
        Throws:
        org.w3c.dom.DOMException - if the child cannot be created
        See Also:
        getChild(java.lang.String), getChildren()
      • createChild

        public IMemento createChild​(java.lang.String type,
                                    java.lang.String id)
                             throws org.w3c.dom.DOMException
        Creates a new child of this memento with the given type and id. The id is stored in the child memento (using a special reserved key, TAG_ID) and can be retrieved using getId.

        The getChild and getChildren methods are used to retrieve children of a given type.

        Specified by:
        createChild in interface IMemento
        Parameters:
        type - the type
        id - the child id
        Returns:
        a new child memento with the given type and id
        Throws:
        org.w3c.dom.DOMException - if the child cannot be created
        See Also:
        getID()
      • copyChild

        public IMemento copyChild​(IMemento child)
                           throws org.w3c.dom.DOMException
        Create a copy of the child node and append it to this node.
        Parameters:
        child - the child to copy
        Returns:
        An IMenento for the new child node.
        Throws:
        org.w3c.dom.DOMException - if the child cannot be created
      • getChild

        public IMemento getChild​(java.lang.String type)
        Description copied from interface: IMemento
        Returns the first child with the given type id.
        Specified by:
        getChild in interface IMemento
        Parameters:
        type - the type id
        Returns:
        the first child with the given type. May return null .
      • getChildren

        public IMemento[] getChildren()
        Description copied from interface: IMemento
        Returns all children of this node.
        Specified by:
        getChildren in interface IMemento
        Returns:
        an array of children of this node. This will not be null. If there are no children, an array of length zero will be returned.
      • getChildren

        public IMemento[] getChildren​(java.lang.String type)
        Description copied from interface: IMemento
        Returns all children with the given type id.
        Specified by:
        getChildren in interface IMemento
        Parameters:
        type - the type id
        Returns:
        an array of children with the given type. This will not be null. If there are no keys, an array of length zero will be returned.
      • getFloat

        public java.lang.Float getFloat​(java.lang.String key)
        Description copied from interface: IMemento
        Returns the floating point value of the given key.
        Specified by:
        getFloat in interface IMemento
        Parameters:
        key - the key
        Returns:
        the value, or null if the key was not found or was found but was not a floating point number
      • getInteger

        public java.lang.Integer getInteger​(java.lang.String key)
        Description copied from interface: IMemento
        Returns the integer value of the given key.
        Specified by:
        getInteger in interface IMemento
        Parameters:
        key - the key
        Returns:
        the value, or null if the key was not found or was found but was not an integer
      • getString

        public java.lang.String getString​(java.lang.String key)
        Description copied from interface: IMemento
        Returns the string value of the given key.
        Specified by:
        getString in interface IMemento
        Parameters:
        key - the key
        Returns:
        the value, or null if the key was not found
      • getBoolean

        public java.lang.Boolean getBoolean​(java.lang.String key)
        Description copied from interface: IMemento
        Returns the boolean value of the given key.
        Specified by:
        getBoolean in interface IMemento
        Parameters:
        key - the key
        Returns:
        the value, or null if the key was not found
        Since:
        3.4
      • getTextData

        public java.lang.String getTextData()
                                     throws org.w3c.dom.DOMException
        Returns the data of the Text node of the memento. Each memento is allowed only one Text node.
        Specified by:
        getTextData in interface IMemento
        Returns:
        the data of the Text node of the memento, or null if the memento has no Text node.
        Throws:
        org.w3c.dom.DOMException - if the text node is too big
        Since:
        2.0
      • getAttributeKeys

        public java.lang.String[] getAttributeKeys()
        Description copied from interface: IMemento
        Returns an array of all the attribute keys of the memento. This will not be null. If there are no keys, an array of length zero will be returned.
        Specified by:
        getAttributeKeys in interface IMemento
        Returns:
        an array with all the attribute keys of the memento
        Since:
        3.4
      • putFloat

        public void putFloat​(java.lang.String key,
                             float f)
                      throws org.w3c.dom.DOMException
        Sets the value of the given key to the given floating point number.
        Specified by:
        putFloat in interface IMemento
        Parameters:
        key - the key
        f - the value
        Throws:
        org.w3c.dom.DOMException - if the attribute cannot be set
      • putInteger

        public void putInteger​(java.lang.String key,
                               int n)
                        throws org.w3c.dom.DOMException
        Sets the value of the given key to the given integer.
        Specified by:
        putInteger in interface IMemento
        Parameters:
        key - the key
        n - the value
        Throws:
        org.w3c.dom.DOMException - if the attribute cannot be set
      • putMemento

        public void putMemento​(IMemento memento)
                        throws org.w3c.dom.DOMException
        Copy the attributes and children from memento to the receiver.
        Specified by:
        putMemento in interface IMemento
        Parameters:
        memento - the IMemento to be copied.
        Throws:
        org.w3c.dom.DOMException - if the attributes or children cannot be copied to this node.
      • putString

        public void putString​(java.lang.String key,
                              java.lang.String value)
                       throws org.w3c.dom.DOMException
        Sets the value of the given key to the given string.
        Specified by:
        putString in interface IMemento
        Parameters:
        key - the key
        value - the value
        Throws:
        org.w3c.dom.DOMException - if the attribute cannot be set
      • putBoolean

        public void putBoolean​(java.lang.String key,
                               boolean value)
                        throws org.w3c.dom.DOMException
        Sets the value of the given key to the given boolean value.
        Specified by:
        putBoolean in interface IMemento
        Parameters:
        key - the key
        value - the value
        Throws:
        org.w3c.dom.DOMException - if the attribute cannot be set
        Since:
        3.4
      • putTextData

        public void putTextData​(java.lang.String data)
                         throws org.w3c.dom.DOMException
        Sets the memento's Text node to contain the given data. Creates the Text node if none exists. If a Text node does exist, it's current contents are replaced. Each memento is allowed only one text node.
        Specified by:
        putTextData in interface IMemento
        Parameters:
        data - the data to be placed on the Text node
        Throws:
        org.w3c.dom.DOMException - if the text node cannot be created under this node.
        Since:
        2.0
      • save

        public void save​(java.io.Writer writer)
                  throws java.io.IOException
        Saves this memento's document current values to the specified writer.
        Parameters:
        writer - the writer used to save the memento's document
        Throws:
        java.io.IOException - if there is a problem serializing the document to the stream.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object