Class DomToGroovy

java.lang.Object
org.apache.groovy.xml.tools.DomToGroovy

public class DomToGroovy extends Object
A SAX handler for turning XML into Groovy scripts
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
    Whether the current element contains mixed text and element content.
    protected Collection<String>
    Groovy keywords that must be quoted when emitted as method-style element names.
    protected IndentPrinter
    Destination printer for the generated Groovy source.
    protected String
    Quote delimiter used for generated Groovy string literals.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a converter that writes generated Groovy code with the supplied indent printer.
    Creates a converter that writes generated Groovy code to the supplied writer.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected boolean
    checkEscaping(String localName)
    Returns whether a generated Groovy name must be quoted.
    protected void
    defineNamespace(Map namespaces, String prefix, String uri)
    Records a namespace mapping and emits mkp.declareNamespace when needed.
    protected Map
    defineNamespaces(Element element, Map namespaces)
    Returns the namespace map extended with any newly encountered element or attribute prefixes.
    protected String
    Escapes backslashes and the active quote delimiter for generated Groovy strings.
    protected String
    Returns the attribute value used when emitting the Groovy builder call.
    protected String
    Returns the trimmed local name for a DOM node, falling back to Node.getNodeName().
    protected String
    Returns the trimmed text content used when emitting a DOM text node.
    static void
    main(String[] args)
    Command-line entry point that converts an XML file into a Groovy builder script.
    protected boolean
    Detects whether a node list contains both non-empty text and element children.
    static Document
    parse(File file)
    Parses the given XML file into a DOM document.
    static Document
    Parses XML read from the supplied stream into a DOM document.
    static Document
    parse(Reader input)
    Parses XML read from the supplied reader into a DOM document.
    protected static Document
    parse(String fileName)
    Parses the given XML file name into a DOM document.
    protected void
    print(String text)
    Writes text through the configured indent printer.
    void
    print(Document document)
    Writes the Groovy builder form of the supplied DOM document.
    protected void
    print(Node node, Map namespaces, boolean endWithComma)
    Dispatches a DOM node to the appropriate Groovy emission helper.
    protected boolean
    Emits the attributes for the supplied element.
    protected boolean
    printAttributeWithoutPrefix(Attr attribute, boolean hasAttribute)
    Emits an unprefixed attribute and updates the separator state.
    protected void
    Appends a prefixed attribute representation to the deferred attribute buffer.
    protected void
    printChildren(Node parent, Map namespaces)
    Emits all children of the supplied parent node.
    protected void
    printComment(Comment comment, boolean endWithComma)
    Emits a Groovy block comment for a non-empty DOM comment node.
    protected void
    printElement(Element element, Map namespaces, boolean endWithComma)
    Emits Groovy markup for a DOM element, including namespaces, attributes, and children.
    protected void
    printEnd(String text, boolean endWithComma)
    Finishes the current emitted fragment and optionally appends a comma.
    protected void
    Writes the current indentation through the configured indent printer.
    protected void
    Writes a line through the configured indent printer.
    protected void
    printPI(ProcessingInstruction instruction, boolean endWithComma)
    Emits a processing instruction as an mkp.pi call.
    protected void
    Writes text as a Groovy string literal, using triple quotes for multi-line content.
    protected void
    printText(Text node, boolean endWithComma)
    Emits a text node, using mkp.yield when nested in mixed content.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • out

      protected IndentPrinter out
      Destination printer for the generated Groovy source.
    • inMixed

      protected boolean inMixed
      Whether the current element contains mixed text and element content.
    • qt

      protected String qt
      Quote delimiter used for generated Groovy string literals.
    • keywords

      protected Collection<String> keywords
      Groovy keywords that must be quoted when emitted as method-style element names.
  • Constructor Details

    • DomToGroovy

      public DomToGroovy(PrintWriter out)
      Creates a converter that writes generated Groovy code to the supplied writer.
      Parameters:
      out - destination writer
    • DomToGroovy

      public DomToGroovy(IndentPrinter out)
      Creates a converter that writes generated Groovy code with the supplied indent printer.
      Parameters:
      out - destination printer
  • Method Details

    • print

      public void print(Document document)
      Writes the Groovy builder form of the supplied DOM document.
      Parameters:
      document - source DOM document
    • main

      public static void main(String[] args)
      Command-line entry point that converts an XML file into a Groovy builder script.
      Parameters:
      args - infilename [outfilename]
    • parse

      protected static Document parse(String fileName) throws Exception
      Parses the given XML file name into a DOM document.
      Parameters:
      fileName - XML file path
      Returns:
      parsed DOM document
      Throws:
      Exception - if parsing fails
    • parse

      public static Document parse(File file) throws Exception
      Parses the given XML file into a DOM document.
      Parameters:
      file - XML file
      Returns:
      parsed DOM document
      Throws:
      Exception - if parsing fails
    • parse

      public static Document parse(Reader input) throws Exception
      Parses XML read from the supplied reader into a DOM document.
      Parameters:
      input - XML reader
      Returns:
      parsed DOM document
      Throws:
      Exception - if parsing fails
    • parse

      public static Document parse(InputStream input) throws Exception
      Parses XML read from the supplied stream into a DOM document.
      Parameters:
      input - XML stream
      Returns:
      parsed DOM document
      Throws:
      Exception - if parsing fails
    • print

      protected void print(Node node, Map namespaces, boolean endWithComma)
      Dispatches a DOM node to the appropriate Groovy emission helper.
      Parameters:
      node - DOM node to emit
      namespaces - namespaces currently in scope
      endWithComma - whether to terminate the emitted fragment with a comma
    • printElement

      protected void printElement(Element element, Map namespaces, boolean endWithComma)
      Emits Groovy markup for a DOM element, including namespaces, attributes, and children.
      Parameters:
      element - element to emit
      namespaces - namespaces currently in scope
      endWithComma - whether to terminate the emitted fragment with a comma
    • printQuoted

      protected void printQuoted(String text)
      Writes text as a Groovy string literal, using triple quotes for multi-line content.
      Parameters:
      text - text to quote
    • printPI

      protected void printPI(ProcessingInstruction instruction, boolean endWithComma)
      Emits a processing instruction as an mkp.pi call.
      Parameters:
      instruction - processing instruction to emit
      endWithComma - whether to terminate the emitted fragment with a comma
    • printComment

      protected void printComment(Comment comment, boolean endWithComma)
      Emits a Groovy block comment for a non-empty DOM comment node.
      Parameters:
      comment - comment to emit
      endWithComma - whether to terminate the emitted fragment with a comma
    • printText

      protected void printText(Text node, boolean endWithComma)
      Emits a text node, using mkp.yield when nested in mixed content.
      Parameters:
      node - text node to emit
      endWithComma - whether to terminate the emitted fragment with a comma
    • escapeQuote

      protected String escapeQuote(String text)
      Escapes backslashes and the active quote delimiter for generated Groovy strings.
      Parameters:
      text - raw text
      Returns:
      escaped text
    • defineNamespaces

      protected Map defineNamespaces(Element element, Map namespaces)
      Returns the namespace map extended with any newly encountered element or attribute prefixes.
      Parameters:
      element - element whose namespaces should be inspected
      namespaces - namespaces currently in scope
      Returns:
      namespace map to use for the element and its children
    • defineNamespace

      protected void defineNamespace(Map namespaces, String prefix, String uri)
      Records a namespace mapping and emits mkp.declareNamespace when needed.
      Parameters:
      namespaces - mutable namespace map
      prefix - namespace prefix
      uri - namespace URI
    • printAttributes

      protected boolean printAttributes(Element element)
      Emits the attributes for the supplied element.
      Parameters:
      element - element whose attributes should be emitted
      Returns:
      true if any attributes were written
    • printAttributeWithPrefix

      protected void printAttributeWithPrefix(Attr attribute, StringBuffer buffer)
      Appends a prefixed attribute representation to the deferred attribute buffer.
      Parameters:
      attribute - attribute to render
      buffer - buffer collecting prefixed attributes
    • getAttributeValue

      protected String getAttributeValue(Attr attribute)
      Returns the attribute value used when emitting the Groovy builder call.
      Parameters:
      attribute - attribute to inspect
      Returns:
      attribute value
    • printAttributeWithoutPrefix

      protected boolean printAttributeWithoutPrefix(Attr attribute, boolean hasAttribute)
      Emits an unprefixed attribute and updates the separator state.
      Parameters:
      attribute - attribute to render
      hasAttribute - whether a previous attribute has already been written
      Returns:
      updated attribute-written flag
    • checkEscaping

      protected boolean checkEscaping(String localName)
      Returns whether a generated Groovy name must be quoted.
      Parameters:
      localName - candidate element or attribute name
      Returns:
      true if the name requires quoting
    • getTextNodeData

      protected String getTextNodeData(Text node)
      Returns the trimmed text content used when emitting a DOM text node.
      Parameters:
      node - text node
      Returns:
      trimmed text content
    • mixedContent

      protected boolean mixedContent(NodeList list)
      Detects whether a node list contains both non-empty text and element children.
      Parameters:
      list - child nodes to inspect
      Returns:
      true for mixed content
    • printChildren

      protected void printChildren(Node parent, Map namespaces)
      Emits all children of the supplied parent node.
      Parameters:
      parent - DOM parent node
      namespaces - namespaces currently in scope
    • getLocalName

      protected String getLocalName(Node node)
      Returns the trimmed local name for a DOM node, falling back to Node.getNodeName().
      Parameters:
      node - DOM node
      Returns:
      trimmed local name
    • printEnd

      protected void printEnd(String text, boolean endWithComma)
      Finishes the current emitted fragment and optionally appends a comma.
      Parameters:
      text - trailing text to emit before line termination
      endWithComma - whether to append a comma
    • println

      protected void println(String text)
      Writes a line through the configured indent printer.
      Parameters:
      text - line text
    • print

      protected void print(String text)
      Writes text through the configured indent printer.
      Parameters:
      text - text to print
    • printIndent

      protected void printIndent()
      Writes the current indentation through the configured indent printer.