Class XmlFileArtifact

  • All Implemented Interfaces:
    IArtifact, IArtifactChangedListener, IFileSystemArtifact, IXmlContainer, IActualValueProvider, IStringValueProvider, IVilType

    public class XmlFileArtifact
    extends FileArtifact
    implements IXmlContainer
    Represents a parsed XML file artifact. Due to Java internal intelligent processing, it might be that the original encoding name is replaced by a normalized one. If this is not intended, the official encoding table of the JDK needs to be redefined (system property "com.sun.org.apache.xalan.internal.serialize.encodings"), e.g., to exclude the specific encoding... with possible side effects. Please note, that there is a comment end-of-line workaround in this class which is required for proper operation on some JDKs (OpenJDK 1.7). In future, this may be circumvented by using a distinct version of XALAN instead of the builtin one....
    Author:
    Holger Eichelberger, Patrik Pastuschek
    • Field Detail

      • doc

        private org.w3c.dom.Document doc
      • file

        private java.io.File file
      • dtd

        private Dtd dtd
      • omitXmlDeclaration

        private boolean omitXmlDeclaration
      • omitXmlStandalone

        private boolean omitXmlStandalone
      • synchronizeAttributeSequence

        private boolean synchronizeAttributeSequence
      • indentation

        private int indentation
      • lastModification

        private long lastModification
      • lastPersisted

        private long lastPersisted
      • encoding

        private java.lang.String encoding
    • Constructor Detail

      • XmlFileArtifact

        XmlFileArtifact​(java.io.File file,
                        ArtifactModel model)
                 throws VilException
        Creates a new XML file artifact.
        Parameters:
        file - the file to read the artifact contents from
        model - the artifact model to create this folder artifact within
        Throws:
        VilException - if artifact could not be created.
    • Method Detail

      • initialize

        private void initialize()
                         throws VilException
        (Re)initializes this artifact.
        Throws:
        VilException - if artifact could not be created.
      • create

        public static FileArtifact create()
                                   throws VilException
        Creates a temporary file artifact.
        Returns:
        the created file artifact
        Throws:
        VilException - in case that the creation fails
      • getRootElement

        public XmlElement getRootElement()
        Returns the Root Element.
        Returns:
        the representation of the root element
      • createRootElement

        public XmlElement createRootElement​(java.lang.String name)
                                     throws VilException
        Creates a root element if it does not exist.
        Parameters:
        name - the name of the root element
        Returns:
        the actual root element
        Throws:
        VilException - in case that creating the root element fails
      • selectChilds

        public Set<? extends XmlElement> selectChilds()
                                               throws VilException
        Returns a set of all childs of the root element.
        Returns:
        Set of all childs of the root element.
        Throws:
        VilException
      • load

        private void load​(java.io.File file)
                   throws VilException
        Loads and builds the jdom-tree.
        Parameters:
        file - the file that is to be parsed.
        Throws:
        VilException - in case domTree could not be built.
      • initFromDoc

        private org.w3c.dom.Document initFromDoc​(org.w3c.dom.Document doc)
        Initializes this instance from doc.
        Parameters:
        doc - the document to initialize from (may be null, is ignored then)
        Returns:
        doc
      • pruneWhitespaces

        private void pruneWhitespaces​(org.w3c.dom.NodeList nodes)
        Removes start and end childs within nodes, whyever xerces suddenly introduced this "feature".
        Parameters:
        nodes - the nodes to prune
      • deleteNode

        private void deleteNode​(org.w3c.dom.Node node)
        Deletes a node from its parent.
        Parameters:
        node - the node to be deleted (may be null)
      • toEmptyTextNode

        private org.w3c.dom.Node toEmptyTextNode​(org.w3c.dom.Node node)
        Turns a node into an empty text node if it is a text node and its content is empty (after trimming).
        Parameters:
        node - the node (may be null)
        Returns:
        node or null
      • build

        private XmlNode build​(org.w3c.dom.Node node,
                              XmlElement parent)
        Building up the representing object structure.
        Parameters:
        node - the node to parse. (Will parse all childs aswell).
        parent - the representation of the parent node. (Null if root element).
        Returns:
        an array of XmlElements including all child elements.
      • fill

        private XmlElement fill​(org.w3c.dom.Node node,
                                XmlElement element,
                                java.util.List<java.lang.Integer> childIndexes)
        Fills an XML element with data.
        Parameters:
        node - the XML node to take the data from
        element - the XML element
        childIndexes - the child indexes indicating the children to take over
        Returns:
        element
      • fill

        private XmlComment fill​(org.w3c.dom.Node node,
                                XmlComment element)
        Fills an XML comment with data.
        Parameters:
        node - the node to take data from
        element - the XML comment to fill
        Returns:
        comment
      • getLineEndedComments

        private java.util.List<java.lang.String> getLineEndedComments()
        Return all line ended comments in sequence of their occurrence in file. This is part of a workaround as in some JDKs the line ends of comments seem to be omitted.
        Returns:
        the line ends in sequence
      • ensureLineEndedComments

        private void ensureLineEndedComments​(java.util.List<java.lang.String> lineEndedComments)
        Ensures line the line ends in file according to the previously collected line ends by getLineEndedComments(). This is part of a workaround as in some JDKs the line ends of comments seem to be omitted.
        Parameters:
        lineEndedComments - the line ended comments in sequence of their occurrence
      • isLineEndChar

        private static boolean isLineEndChar​(char ch)
        Returns whether ch is a line end character.
        Parameters:
        ch - the character to be analyzed
        Returns:
        true if it is a line end character, false else
      • configureTransformer

        protected void configureTransformer​(javax.xml.transform.Transformer transformer)
        Configures the transformer.
        Parameters:
        transformer - the transformer to be configured
      • toYesNo

        private java.lang.String toYesNo​(boolean yes)
        Turns a boolean into a transformer "yes" or "no" string.
        Parameters:
        yes - whether the output shall represent "yes"
        Returns:
        "yes" or "no"
      • createAttributes

        private XmlAttribute[] createAttributes​(org.w3c.dom.Node node,
                                                XmlElement element)
        Creates and returns all Attributes for given node and XmlElement.
        Parameters:
        node - The node for which to return all attributes
        element - The element that represents the node.
        Returns:
        Array with XmlAttributes
      • selectByName

        public Set<XmlElement> selectByName​(java.lang.String name)
                                     throws VilException
        Description copied from interface: IXmlContainer
        Returns a Set with all XmlElements matching name. Childs are processed aswell.
        Uses case sensitivity by default.
        Specified by:
        selectByName in interface IXmlContainer
        Parameters:
        name - The required name.
        Returns:
        set with matching XmlElements.
        Throws:
        VilException - in case that invalid Elements are used.
      • selectByPath

        public Set<XmlElement> selectByPath​(java.lang.String path)
                                     throws VilException
        Description copied from interface: IXmlContainer
        Selects XML elements according to a given path. We may go for XPath expressions in future.
        Specified by:
        selectByPath in interface IXmlContainer
        Parameters:
        path - the path to the elements
        Returns:
        the matching elements (may be empty)
        Throws:
        VilException - in case invalid XmlElements are used.
      • selectByXPath

        public Set<XmlElement> selectByXPath​(java.lang.String path)
                                      throws VilException
        Description copied from interface: IXmlContainer
        Selects XML elements according to a given XPath.
        Specified by:
        selectByXPath in interface IXmlContainer
        Parameters:
        path - the XPath to the elements
        Returns:
        the matching elements (may be empty)
        Throws:
        VilException - in case invalid XmlElements are used.
      • selectByRegEx

        public Set<XmlElement> selectByRegEx​(java.lang.String regEx)
                                      throws VilException
        Returns a set of all XmlElements that start with prefix and end with suffix. To get a set of all XmlElements having a certain pre- and suffix use: "prefix.*suffix" (replace prefix and suffix).
        Parameters:
        regEx - a regular expression specifying the elements to be selected.
        Returns:
        Set of XmlElements matching the regEx.
        Throws:
        VilException - in case that invalid Elements are used.
      • cleanTree

        private void cleanTree​(org.w3c.dom.Node parent)
        Cleans the dom tree from empty text nodes. These nodes are used for internal indent representation. These empty text nodes cause bugs in indentation if not removed.
        Parameters:
        parent - The parentNode that is to clean.
      • readDtd

        private void readDtd()
                      throws VilException
        Checks the base file for embedded DTDs.
        Throws:
        VilException - If File not found.
      • convert

        public static FileArtifact convert​(java.lang.String val)
                                    throws VilException
        Conversion operation.
        Parameters:
        val - the value to be converted
        Returns:
        the converted value
        Throws:
        VilException - in case that creating the artifact fails
      • convert

        public static XmlFileArtifact convert​(IFileSystemArtifact val)
        Conversion operation.
        Parameters:
        val - the value to be converted
        Returns:
        the converted value
      • setIndentation

        public void setIndentation​(int indentation)
        Sets the number of whitespaces to use for auto-indenting this artifact while writing/storing.
        Parameters:
        indentation - the number of whitespaces (default 4), a negative value disables indentation
      • setEncoding

        public void setEncoding​(java.lang.String encoding)
        Defines the (input) encoding.
        Parameters:
        encoding - a valid encoding name, may be null for none
      • getEncoding

        public java.lang.String getEncoding()
        Returns the encoding.
        Returns:
        the encoding
      • setXmlStandalone

        public void setXmlStandalone​(boolean standalone)
        Defines whether the XML artifact is standalone.
        Parameters:
        standalone - whether it is standalone or not
        See Also:
        setOmitXmlStandalone(boolean)
      • getXmlStandalone

        public boolean getXmlStandalone()
        Returns the XML standalone flag.
        Returns:
        the standalone flag, always false if no document is present
      • setOmitXmlDeclaration

        public void setOmitXmlDeclaration​(boolean omitXmlDeclaration)
        Defines whether the usual XML header declaration shall be omitted or printed while writing/storing this artifact.
        Parameters:
        omitXmlDeclaration - omit if true, emit if false (default)
      • setOmitXmlStandalone

        public void setOmitXmlStandalone​(boolean omitXmlStandalone)
        Defines whether the usual XML header standalone attribute shall be omitted or printed while writing/storing this artifact.
        Parameters:
        omitXmlStandalone - omit if true (default), emit if false
      • synchronizeAttributeSequence

        public boolean synchronizeAttributeSequence()
        Returns whether the attribute sequence of XML elements shall be synchronized between document and artifact fragments, i.e., the fragment sequence defines the sequence of the attributes. Else attributes are sorted by name (default XALAN behavior).
        Returns:
        true (default), false for sorted
      • setSynchronizeAttributeSequence

        public void setSynchronizeAttributeSequence​(boolean synchronizeAttributeSequence)
        Changes whether the attribute sequence of XML elements shall be synchronized between document and artifact fragments, i.e., the fragment sequence defines the sequence of the attributes. Else attributes are sorted by name (default XALAN behavior).
        Parameters:
        synchronizeAttributeSequence - true (default), false for sorted
      • getTransformerFactory

        protected javax.xml.transform.TransformerFactory getTransformerFactory()
        Returns the transformer builder factory.
        Returns:
        the factory
      • getDocumentBuilderFactory

        protected javax.xml.parsers.DocumentBuilderFactory getDocumentBuilderFactory()
        Returns the document builder factory.
        Returns:
        the factory
      • format

        public void format()
                    throws VilException
        Enforces re-loading, formatting and writing of this artifact.
        Throws:
        VilException - in case that loading, formatting or writing fails
      • synchronizeAttributes

        private void synchronizeAttributes​(java.io.InputStream stream)
        Synchronizes the XML attributes (depends on synchronizeAttributeSequence).
        Parameters:
        stream - the stream to synchronize
      • synchronizeAttributes

        private void synchronizeAttributes​(java.io.File file)
        Synchronizes the XML attributes (depends on synchronizeAttributeSequence).
        Parameters:
        file - the file to synchronize