Class XMLUtils


  • public class XMLUtils
    extends java.lang.Object
    A helper for computing SourceCorrespondence objects from an XML file Creates an annotated DOM tree, adding start and end line numbers to each XML element
    Author:
    Ben Holland
    • Field Detail

      • START_LINE_NUMBER_USER_DATA

        public static final java.lang.String START_LINE_NUMBER_USER_DATA
        See Also:
        Constant Field Values
      • END_LINE_NUMBER_USER_DATA

        public static final java.lang.String END_LINE_NUMBER_USER_DATA
        See Also:
        Constant Field Values
      • START_COLUMN_NUMBER_USER_DATA

        public static final java.lang.String START_COLUMN_NUMBER_USER_DATA
        See Also:
        Constant Field Values
      • END_COLUMN_NUMBER_USER_DATA

        public static final java.lang.String END_COLUMN_NUMBER_USER_DATA
        See Also:
        Constant Field Values
      • START_CHARACTER_OFFSET_USER_DATA

        public static final java.lang.String START_CHARACTER_OFFSET_USER_DATA
        See Also:
        Constant Field Values
      • END_CHARACTER_OFFSET_USER_DATA

        public static final java.lang.String END_CHARACTER_OFFSET_USER_DATA
        See Also:
        Constant Field Values
      • START_LINE_NUMBER_CHARACTER_OFFSET_USER_DATA

        public static final java.lang.String START_LINE_NUMBER_CHARACTER_OFFSET_USER_DATA
        See Also:
        Constant Field Values
      • END_LINE_NUMBER_CHARACTER_OFFSET_USER_DATA

        public static final java.lang.String END_LINE_NUMBER_CHARACTER_OFFSET_USER_DATA
        See Also:
        Constant Field Values
    • Method Detail

      • xmlElementToSourceCorrespondence

        public static com.ensoftcorp.atlas.core.index.common.SourceCorrespondence xmlElementToSourceCorrespondence​(java.io.File file,
                                                                                                                   org.w3c.dom.Element element)
        Returns a SourceCorrespondence from an element in an annotated DOM
        Parameters:
        file -
        element -
        Returns:
      • xmlFileToSourceCorrespondence

        public static com.ensoftcorp.atlas.core.index.common.SourceCorrespondence xmlFileToSourceCorrespondence​(java.io.File file)
        Returns a SourceCorrespondence set to the first line of the XML file from an element in an annotated DOM
        Parameters:
        file -
        leftElement -
        Returns:
      • printDOM

        public static void printDOM​(org.w3c.dom.Document doc)
                             throws javax.xml.transform.TransformerFactoryConfigurationError,
                                    javax.xml.transform.TransformerConfigurationException,
                                    javax.xml.transform.TransformerException
        Prints the DOM Tree to the console
        Parameters:
        doc -
        Throws:
        javax.xml.transform.TransformerFactoryConfigurationError
        javax.xml.transform.TransformerConfigurationException
        javax.xml.transform.TransformerException
      • getAnnotatedDOM

        public static org.w3c.dom.Document getAnnotatedDOM​(java.io.File file)
                                                    throws java.io.IOException,
                                                           org.xml.sax.SAXException
        Just a helper wrapper around the getAnnotatedDOM(InputStream inputStream) method
        Throws:
        java.io.IOException
        org.xml.sax.SAXException
      • getAnnotatedDOM

        public static org.w3c.dom.Document getAnnotatedDOM​(java.lang.String xmlString)
                                                    throws java.io.IOException,
                                                           org.xml.sax.SAXException
        Just a helper wrapper around the getAnnotatedDOM(InputStream inputStream) method
        Throws:
        java.io.IOException
        org.xml.sax.SAXException
      • getAnnotatedDOM

        public static org.w3c.dom.Document getAnnotatedDOM​(java.io.InputStream inputStream)
                                                    throws java.io.IOException,
                                                           org.xml.sax.SAXException
        Returns an XML DOM Document object that has been annotated with User Data representing the line numbers and byte offsets of elements. User data attribute names are provided as public final strings in this class. Supported: START_LINE_NUMBER_USER_DATA, START_COLUMN_NUMBER_USER_DATA, START_CHARACTER_OFFSET_USER_DATA, END_LINE_NUMBER_USER_DATA, END_COLUMN_NUMBER_USER_DATA, END_CHARACTER_OFFSET_USER_DATA Example: Node node = doc.getElementsByTagName("foo").item(0); System.out.println("Line number: " + node.getUserData(START_LINE_NUMBER_USER_DATA)); Note: This method will close the input stream! This method was inspired by: http://stackoverflow.com/questions/4915422/get-line-number-from-xml-node-java and http://eyalsch.wordpress.com/2010/11/30/xml-dom-2/ and http://stackoverflow.com/questions/1077865/how-do-i-get-the-correct-starting-ending-locations-of-a-xml-tag-with-sax
        Throws:
        java.io.IOException
        org.xml.sax.SAXException