Class Utils


  • public class Utils
    extends Object
    Utilities for Java XProc steps
    Author:
    Paul Rambags
    • Constructor Summary

      Constructors 
      Constructor Description
      Utils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Element addChild​(Node parent, String name)
      Add a new child node to a parent node The new child node will inherit the parent's namespace
      static Element addChildBefore​(Node parent, Node child, String name)
      Add a new child node to a parent node before a child node The new child node will inherit the parent's namespace If the child node is null, the new child node will be added at the end of the list of children
      static char convertBraille​(char braille)
      Convert a character from braille to lowercase text Braille control characters are not changed For characters with more than one text representation, the most common one is chosen e.g.
      static char convertBrailleNumeric​(char braille)
      Convert a numeric character from braille to lowercase text
      static Document convertToDocument​(String xml)
      Convert an XML string to a Document node
      static Document convertToDocument​(net.sf.saxon.s9api.XdmNode node)
      Convert an XdmNode to a Document node TODO this produces unwanted white space (it is converted with indent="true")
      static net.sf.saxon.s9api.XdmNode convertToXdmNode​(Document document, net.sf.saxon.s9api.Processor processor, boolean useFile)
      Convert a Document node to an XdmNode
      static String convertXmlEndtagsToHtmlEndtags​(String xml)
      Converts XML end tags to a HTML end tags Each "/>" is replaced by ">" for the correct tag
      static Node getChild​(Node node, String name)
      Get the first child in the same namespace with a given name
      static net.sf.saxon.s9api.XdmNode getChildNode​(net.sf.saxon.s9api.XdmNode node, net.sf.saxon.s9api.QName childName)
      Get the first child node
      static Node getFirstChildWithAttribute​(Node node, String name, String attributeName, String attributeValue)
      Get the first child in the same namespace with a given name and a given attribute value
      static Node getNextSiblingWithAttribute​(Node node, String name, String attributeName, String attributeValue)
      Get the first sibling in the same namespace with a given name and a given attribute value
      static String getValue​(net.sf.saxon.s9api.XdmNode node, net.sf.saxon.s9api.QName childName)
      Get the string value of a child node
      static boolean isBraille​(char c)
      Determine whether a unicode character is in the braille range
      static boolean isBrailleDecimalSeparator​(char braille)
      Determine whether a braille character is a decimal separator
      static boolean isBrailleDigit​(char braille)
      Determine whether a braille character is a digit
      static boolean isBrailleMinus​(char braille)
      Determine whether a character is the minus sign
      static boolean isBrailleMoneyZeros​(char braille)
      Determine whether a braille character can be used as a shorthand for 00 at the end of a price E.g.
      static boolean isDecimalSeparator​(char c)
      Determine whether a character is a decimal separator
      static boolean isDigit​(char c)
      Determine whether a character is a digit
      static boolean isMoneyZeros​(char c)
      Determine whether a character can be used as a shorthand for 00 at the end of a price E.g.
      static void removeNodes​(Set<Node> nodes)
      Remove nodes
      static Node renameNode​(Node node, String newNodeName)
      Rename a node and maintain the namespace
      static String toString​(net.sf.saxon.s9api.XdmNode node)
      Convert an XdmNode to an XML string
      static String toString​(Document document, net.sf.saxon.s9api.DocumentBuilder documentBuilder)
      Convert a Document node to an XML string
    • Constructor Detail

      • Utils

        public Utils()
    • Method Detail

      • convertToXdmNode

        public static net.sf.saxon.s9api.XdmNode convertToXdmNode​(Document document,
                                                                  net.sf.saxon.s9api.Processor processor,
                                                                  boolean useFile)
                                                           throws IOException,
                                                                  net.sf.saxon.s9api.SaxonApiException
        Convert a Document node to an XdmNode
        Parameters:
        document - Document node
        documentBuilder - document builder
        useFile - if true, the resulting XdmNode will be saved in a temporary file so that it has an URI
        Returns:
        XdmNode
        Throws:
        IOException - thrown on an IO exception
        net.sf.saxon.s9api.SaxonApiException - thrown on a Saxon API exception
      • toString

        public static String toString​(net.sf.saxon.s9api.XdmNode node)
                               throws net.sf.saxon.s9api.SaxonApiException
        Convert an XdmNode to an XML string
        Parameters:
        node - XdmNode
        Returns:
        XML string
        Throws:
        net.sf.saxon.s9api.SaxonApiException - thrown on a Saxon API exception
      • toString

        public static String toString​(Document document,
                                      net.sf.saxon.s9api.DocumentBuilder documentBuilder)
                               throws net.sf.saxon.s9api.SaxonApiException
        Convert a Document node to an XML string
        Parameters:
        document - Document node
        documentBuilder - document builder
        Returns:
        XML string
        Throws:
        net.sf.saxon.s9api.SaxonApiException
      • getChild

        public static Node getChild​(Node node,
                                    String name)
        Get the first child in the same namespace with a given name
        Parameters:
        node - node
        name - child node name
        Returns:
        the first child in the same namespace with the given name, or null if it doesn't exist
      • getFirstChildWithAttribute

        public static Node getFirstChildWithAttribute​(Node node,
                                                      String name,
                                                      String attributeName,
                                                      String attributeValue)
        Get the first child in the same namespace with a given name and a given attribute value
        Parameters:
        node - node
        name - child node name
        attributeName - attribute name
        attributeValue - attribute value
        Returns:
        the first child in the same namespace with the given name and with the given attribute value, or null if it doesn't exist
      • getNextSiblingWithAttribute

        public static Node getNextSiblingWithAttribute​(Node node,
                                                       String name,
                                                       String attributeName,
                                                       String attributeValue)
        Get the first sibling in the same namespace with a given name and a given attribute value
        Parameters:
        node - node
        name - sibling node name
        attributeName - attribute name
        attributeValue - attribute value
        Returns:
        the first sibling in the same namespace with the given name and with the given attribute value, or null if it doesn't exist
      • addChild

        public static Element addChild​(Node parent,
                                       String name)
        Add a new child node to a parent node The new child node will inherit the parent's namespace
        Parameters:
        parent - parent node
        name - name of the child node
        Returns:
        the new child node
      • addChildBefore

        public static Element addChildBefore​(Node parent,
                                             Node child,
                                             String name)
        Add a new child node to a parent node before a child node The new child node will inherit the parent's namespace If the child node is null, the new child node will be added at the end of the list of children
        Parameters:
        parent - parent node
        child - child node, can be null
        name - name of the child node
        Returns:
        the new child node
      • renameNode

        public static Node renameNode​(Node node,
                                      String newNodeName)
        Rename a node and maintain the namespace
        Parameters:
        node - the node that will get a new node name
        newNodeName - the new node name
        Returns:
        the renamed node
      • removeNodes

        public static void removeNodes​(Set<Node> nodes)
        Remove nodes
        Parameters:
        nodes - the nodes that must be removed
      • getChildNode

        public static net.sf.saxon.s9api.XdmNode getChildNode​(net.sf.saxon.s9api.XdmNode node,
                                                              net.sf.saxon.s9api.QName childName)
        Get the first child node
        Parameters:
        node - XdmNode
        childName - child name
        Returns:
        XdmNode the first child node, or null if it doesn't exist
      • getValue

        public static String getValue​(net.sf.saxon.s9api.XdmNode node,
                                      net.sf.saxon.s9api.QName childName)
        Get the string value of a child node
        Parameters:
        node - XdmNode
        childName - child name
        Returns:
        String value of the child, or null if it doesn't exist
      • convertXmlEndtagsToHtmlEndtags

        public static String convertXmlEndtagsToHtmlEndtags​(String xml)
        Converts XML end tags to a HTML end tags Each "/>" is replaced by ">" for the correct tag
        Parameters:
        xml - XML string
        Returns:
        HTML string
      • isBraille

        public static boolean isBraille​(char c)
        Determine whether a unicode character is in the braille range
        Parameters:
        c - unicode character
        Returns:
        true if it is in the braile range, false otherwise
      • convertBraille

        public static char convertBraille​(char braille)
        Convert a character from braille to lowercase text Braille control characters are not changed For characters with more than one text representation, the most common one is chosen e.g. (123456) is displayed as é but can also be %
        Parameters:
        braille - Braille character
        Returns:
        text character
      • isBrailleDigit

        public static boolean isBrailleDigit​(char braille)
        Determine whether a braille character is a digit
        Parameters:
        braille - braille character
        Returns:
        true if it is a digit in the braille range
      • isDigit

        public static boolean isDigit​(char c)
        Determine whether a character is a digit
        Parameters:
        c - character
        Returns:
        true if it is a digit
      • convertBrailleNumeric

        public static char convertBrailleNumeric​(char braille)
        Convert a numeric character from braille to lowercase text
        Parameters:
        braille - Braille character
        Returns:
        numeric text character
      • isBrailleMinus

        public static boolean isBrailleMinus​(char braille)
        Determine whether a character is the minus sign
        Parameters:
        braille - braille character
        Returns:
        true when the character is '-'
      • isBrailleDecimalSeparator

        public static boolean isBrailleDecimalSeparator​(char braille)
        Determine whether a braille character is a decimal separator
        Parameters:
        braille - braille character
        Returns:
        true when the braille character is a comma or period
      • isDecimalSeparator

        public static boolean isDecimalSeparator​(char c)
        Determine whether a character is a decimal separator
        Parameters:
        c - character
        Returns:
        true when the character is a comma or period
      • isBrailleMoneyZeros

        public static boolean isBrailleMoneyZeros​(char braille)
        Determine whether a braille character can be used as a shorthand for 00 at the end of a price E.g. the = sign in this price: € 6,=
        Parameters:
        braille - braille character
        Returns:
        true when the character is a - or a =
      • isMoneyZeros

        public static boolean isMoneyZeros​(char c)
        Determine whether a character can be used as a shorthand for 00 at the end of a price E.g. the = sign in this price: € 6,=
        Parameters:
        c - character
        Returns:
        true when the character is a - or a =