Class PMRDOMUtils

java.lang.Object
org.episteme.natural.chemistry.loaders.cml.util.PMRDOMUtils

public class PMRDOMUtils extends Object
re-usable routines to support PMR* interfaces
Since:
1.0
Author:
Silvere Martin-Michiellot, Gemini AI (Google DeepMind)
  • Field Details

  • Constructor Details

    • PMRDOMUtils

      public PMRDOMUtils()
  • Method Details

    • deleteWhitespaceDescendants

      public static void deleteWhitespaceDescendants(Element element)
      recursively deletes all whitespace descendants
      Parameters:
      element - Description of the Parameter
    • removeNode

      public static void removeNode(Node node)
      remove a node from its parent
      Parameters:
      node - to be removed
    • removeChildNodes

      public static void removeChildNodes(Element element)
      remove all child nodes
      Parameters:
      element - to remove child nodes from
    • depthFirstEnumeration

      public static Enumeration<Node> depthFirstEnumeration(Element element)
      return all descendants in document order
      Parameters:
      element - Description of the Parameter
      Returns:
      Description of the Return Value
    • getChildElements

      public static Vector<Element> getChildElements(Element element)
      get all Element children If none, returns empty Vector
      Parameters:
      element - Description of the Parameter
      Returns:
      The childElements value
    • getChildElement

      public static Element getChildElement(Element element, int index)
      get the index'th Element child If none returns null Count ignores non-Element nodes
      Parameters:
      element - Description of the Parameter
      index - Description of the Parameter
      Returns:
      The childElement value
    • getAncestors

      public static Vector<Element> getAncestors(Element element)
      gets all ancestors of 'element' including the document Element but not Document. [element is not its own ancestor]
      Parameters:
      element - Description of the Parameter
      Returns:
      Element the ancestor (null = none)
    • getAncestor

      public static Element getAncestor(Element element, String tagName)
      gets first ancestor of 'element' with tagName; [element is not its own ancestor]
      Parameters:
      element - Description of the Parameter
      tagName - Description of the Parameter
      Returns:
      Element the ancestor (null = none)
    • createVector

      public static Vector<Node> createVector(NodeList nodes)
      converts a NodeList to a vector

      the vector (but not its elements) is safely mutable. shallowCopies the items in the node list

      Parameters:
      nodes - NodeList to copy
      Returns:
      the equiavlent vector
    • createVector

      public static Vector<Node> createVector(NamedNodeMap nodeMap)
      converts a NamedNodeMap to a vector

      the vector (but not its elements) is safely mutable. shallowCopies the items in the nodemap

      Parameters:
      nodeMap - NodeMap to copy
      Returns:
      the equiavlent vector
    • getAttribute

      public static String getAttribute(Element element, String attName)
      Kludge to overcome cases where getAttribute(attName) has been subverted
      Parameters:
      element - Description of the Parameter
      attName - Description of the Parameter
      Returns:
      String the attribute String; null if none
    • getAttributeString

      public static String getAttributeString(Element element)
      returns the attributes in concatenated text form (suitable for insertion to start tags). If none, returns "". Example:
      xml:link="simple" href="http://www.some/where"
      Parameters:
      element - Description of the Parameter
      Returns:
      String the attribute String
    • getAttributeString

      public static String getAttributeString(Attr att)
      returns a string representing a single attribute in a start tag, for example foo=bar plugh"; String starts with space and delimiters are QUOT, not APOS
      Parameters:
      att - Description of the Parameter
      Returns:
      The attributeString value
    • normalizeString

      public static String normalizeString(String s)
      normalizes all whitespace in a string to single spaces
      Parameters:
      s - the string
      Returns:
      normalized string; null if s is null
    • getChildrenWithElementName

      public static Vector<Element> getChildrenWithElementName(Element element, String name)
      gets vector of Element children with given element name (ignored if name is null or bad value of sensitivity).
      Parameters:
      element - Description of the Parameter
      name - Description of the Parameter
      Returns:
      Vector the vector of children (empty if none)
    • getFirstChildWithElementName

      public static Element getFirstChildWithElementName(Element element, String name)
      gets first child with given element name (ignored if name is null).
      Parameters:
      element - Description of the Parameter
      name - Description of the Parameter
      Returns:
      Element the first child or null
    • getPCDATAContent

      public static String getPCDATAContent(Element element)
      if this has a single PCDATA child, returns its string value, else null
      Parameters:
      element - Description of the Parameter
      Returns:
      String String content of single PCDATA child
    • getPCDATAChildNode

      public static Text getPCDATAChildNode(Element element)
      if this has a single PCDATA child, returns it, else null
      Parameters:
      element - Description of the Parameter
      Returns:
      PCDATANode single PCDATA child
    • setPCDATAContent

      public static void setPCDATAContent(Element element, String s)
      Sets a string to be the content of an existing single PCDATANode If there are no children, create a new PCDATANode. If there are non-PCDATA children or more than one PCDATANode child, return without action.
      Parameters:
      element - The new pCDATAContent value
      s - The new pCDATAContent value
    • outputEventStream

      public static String outputEventStream(Node node, Writer w, int type, int level) throws IOException
      Description of the Method
      Parameters:
      node - Description of the Parameter
      w - Description of the Parameter
      type - Description of the Parameter
      level - Description of the Parameter
      Returns:
      Description of the Return Value
      Throws:
      IOException - Description of the Exception
    • debug

      public static String debug(Node node)
      convenience routine to output a Node as a String
      Parameters:
      node - Description of the Parameter
      Returns:
      Description of the Return Value
    • output

      public static String output(Node node)
      convenience routine to output a Node as a String
      Parameters:
      node - Description of the Parameter
      Returns:
      Description of the Return Value
    • output

      public static String output(Document document, Writer w) throws IOException
      convenience routine
      Parameters:
      document - Description of the Parameter
      w - Description of the Parameter
      Returns:
      Description of the Return Value
      Throws:
      IOException - Description of the Exception
    • output

      public static String output(Node node, Writer w) throws IOException
      convenience routine
      Parameters:
      node - Description of the Parameter
      w - Description of the Parameter
      Returns:
      Description of the Return Value
      Throws:
      IOException - Description of the Exception
    • outputEventStream

      public static String outputEventStream(Node node, Writer w, int type, int level, String encoding, boolean xmlDecl, String dtd) throws IOException
      same as above but also outputs XMLDeclaration and DOCTYPE
      Parameters:
      node - Description of the Parameter
      w - Description of the Parameter
      type - Description of the Parameter
      level - Description of the Parameter
      encoding - Description of the Parameter
      xmlDecl - Description of the Parameter
      dtd - Description of the Parameter
      Returns:
      Description of the Return Value
      Throws:
      IOException - Description of the Exception
    • isEmptyElement

      public static boolean isEmptyElement(Element element)
      Gets the emptyElement attribute of the PMRDOMUtils class
      Parameters:
      element - Description of the Parameter
      Returns:
      The emptyElement value
    • outputStartTag

      public static void outputStartTag(Element element, Writer w, int type, int level) throws IOException
      subclassed by specialist classes
      Parameters:
      element - Description of the Parameter
      w - Description of the Parameter
      type - Description of the Parameter
      level - Description of the Parameter
      Throws:
      IOException - Description of the Exception
    • outputEmptyTag

      public static void outputEmptyTag(Element element, Writer w, int type, int level) throws IOException
      subclassed by specialist classes
      Parameters:
      element - Description of the Parameter
      w - Description of the Parameter
      type - Description of the Parameter
      level - Description of the Parameter
      Throws:
      IOException - Description of the Exception
    • outputStartElement

      public static void outputStartElement(Element element, Writer w, Element parentNode, int level) throws IOException
      for building the tree If writer is null, builds a tree by adding this to parentNode, else outputs a start tag.
      Parameters:
      element - Description of the Parameter
      w - Description of the Parameter
      parentNode - Description of the Parameter
      level - Description of the Parameter
      Throws:
      IOException - Description of the Exception
    • outputChildContent

      public static void outputChildContent(Element element, Writer w, int type, int level) throws IOException
      subclassed by specialist classes
      Parameters:
      element - Description of the Parameter
      w - Description of the Parameter
      type - Description of the Parameter
      level - Description of the Parameter
      Throws:
      IOException - Description of the Exception
    • outputEndTag

      public static void outputEndTag(Element element, Writer w, int type, int level) throws IOException
      subclassed by specialist classes
      Parameters:
      element - Description of the Parameter
      w - Description of the Parameter
      type - Description of the Parameter
      level - Description of the Parameter
      Throws:
      IOException - Description of the Exception
    • outputEndElement

      public static void outputEndElement(Element element, Writer w, int level) throws IOException
      for building the tree If writer is null, noop; else outputs an end tag
      Parameters:
      element - Description of the Parameter
      w - Description of the Parameter
      level - Description of the Parameter
      Throws:
      IOException - Description of the Exception
    • deleteWhitespaceDescendants

      public static void deleteWhitespaceDescendants(Document document)
      recursively removes all whitespace nodes
      Parameters:
      document - Description of the Parameter
    • isEmptyAttribute

      public static boolean isEmptyAttribute(String attributeValue)
      Gets the emptyAttribute attribute of the PMRDOMUtils class
      Parameters:
      attributeValue - Description of the Parameter
      Returns:
      The emptyAttribute value
    • cloneNode

      public static Node cloneNode(Node node, Document document)
      clones node in context of new document.

      provides workaraound for setDocument(newDocument) which is DOM3 uses document.createFoo() does not maintain namespaces only does Element, Text, Comment, ProcessingInstruction at present creates W3C classes, not subclasses cannot clone Document node and returns null cloned node will have no parent

      Parameters:
      node - to clone
      document - context document
      Returns:
      new node
    • pickElements

      public static List<Node> pickElements(Element rootElement, Picker p)
      match decendent elements.

      Finds all decendent elements of rootElement that match the criteria defined by p.

      Parameters:
      rootElement - the element who along with its children will be processed.
      p - the Picker used to match the elements
      Returns:
      a List of the matching elements, or null if none matched.
    • copyAttributes

      public static Attr[] copyAttributes(Element e, Picker p)
      copies attributes chosen via a Picker callback.

      Using the Picker, choose a set of Attributes to be copied from the Element.

      Parameters:
      e - the element whose Attributes are to be processed
      p - the picker that determines whether to choose the attribute
      Returns:
      an array of the matching Attributes, or null if none matched.
    • cutAttributes

      public static Attr[] cutAttributes(Element e, Picker p) throws DOMException
      copies and removes attributes chosen via a Picker callback.

      Using the Picker, choose a set of Attributes to be removed from the Element. These Attributes are then returned

      Parameters:
      e - the element whose attributes are to be processed
      p - the Picker that determines whether to choose the attribute
      Returns:
      an array of the matching Attributes, or null if none matched.
      Throws:
      DOMException - propagated from the call to remove the Attribute node from the Element.
    • pasteAttributes

      public static void pasteAttributes(Element e, Attr[] attrs) throws DOMException
      adds Attributes to a given Element.

      Adds a set of Attributes to an Element.

      Parameters:
      e - the element whose attributes are to be processed
      attrs - an array of the Attributes to be added
      Throws:
      DOMException - propagated from the call to add the Attribute node to the Element.