Package org.apache.groovy.xml.tools
Class DomToGroovy
java.lang.Object
org.apache.groovy.xml.tools.DomToGroovy
A SAX handler for turning XML into Groovy scripts
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanWhether 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 IndentPrinterDestination printer for the generated Groovy source.protected StringQuote delimiter used for generated Groovy string literals. -
Constructor Summary
ConstructorsConstructorDescriptionDomToGroovy(IndentPrinter out) Creates a converter that writes generated Groovy code with the supplied indent printer.DomToGroovy(PrintWriter out) Creates a converter that writes generated Groovy code to the supplied writer. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancheckEscaping(String localName) Returns whether a generated Groovy name must be quoted.protected voiddefineNamespace(Map namespaces, String prefix, String uri) Records a namespace mapping and emitsmkp.declareNamespacewhen needed.protected MapdefineNamespaces(Element element, Map namespaces) Returns the namespace map extended with any newly encountered element or attribute prefixes.protected StringescapeQuote(String text) Escapes backslashes and the active quote delimiter for generated Groovy strings.protected StringgetAttributeValue(Attr attribute) Returns the attribute value used when emitting the Groovy builder call.protected StringgetLocalName(Node node) Returns the trimmed local name for a DOM node, falling back toNode.getNodeName().protected StringgetTextNodeData(Text node) Returns the trimmed text content used when emitting a DOM text node.static voidCommand-line entry point that converts an XML file into a Groovy builder script.protected booleanmixedContent(NodeList list) Detects whether a node list contains both non-empty text and element children.static DocumentParses the given XML file into a DOM document.static Documentparse(InputStream input) Parses XML read from the supplied stream into a DOM document.static DocumentParses XML read from the supplied reader into a DOM document.protected static DocumentParses the given XML file name into a DOM document.protected voidWrites text through the configured indent printer.voidWrites the Groovy builder form of the supplied DOM document.protected voidDispatches a DOM node to the appropriate Groovy emission helper.protected booleanprintAttributes(Element element) Emits the attributes for the supplied element.protected booleanprintAttributeWithoutPrefix(Attr attribute, boolean hasAttribute) Emits an unprefixed attribute and updates the separator state.protected voidprintAttributeWithPrefix(Attr attribute, StringBuffer buffer) Appends a prefixed attribute representation to the deferred attribute buffer.protected voidprintChildren(Node parent, Map namespaces) Emits all children of the supplied parent node.protected voidprintComment(Comment comment, boolean endWithComma) Emits a Groovy block comment for a non-empty DOM comment node.protected voidprintElement(Element element, Map namespaces, boolean endWithComma) Emits Groovy markup for a DOM element, including namespaces, attributes, and children.protected voidFinishes the current emitted fragment and optionally appends a comma.protected voidWrites the current indentation through the configured indent printer.protected voidWrites a line through the configured indent printer.protected voidprintPI(ProcessingInstruction instruction, boolean endWithComma) Emits a processing instruction as anmkp.picall.protected voidprintQuoted(String text) Writes text as a Groovy string literal, using triple quotes for multi-line content.protected voidEmits a text node, usingmkp.yieldwhen nested in mixed content.
-
Field Details
-
out
Destination printer for the generated Groovy source. -
inMixed
protected boolean inMixedWhether the current element contains mixed text and element content. -
qt
Quote delimiter used for generated Groovy string literals. -
keywords
Groovy keywords that must be quoted when emitted as method-style element names.
-
-
Constructor Details
-
DomToGroovy
Creates a converter that writes generated Groovy code to the supplied writer.- Parameters:
out- destination writer
-
DomToGroovy
Creates a converter that writes generated Groovy code with the supplied indent printer.- Parameters:
out- destination printer
-
-
Method Details
-
print
Writes the Groovy builder form of the supplied DOM document.- Parameters:
document- source DOM document
-
main
Command-line entry point that converts an XML file into a Groovy builder script.- Parameters:
args-infilename [outfilename]
-
parse
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
Parses the given XML file into a DOM document.- Parameters:
file- XML file- Returns:
- parsed DOM document
- Throws:
Exception- if parsing fails
-
parse
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
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
Dispatches a DOM node to the appropriate Groovy emission helper.- Parameters:
node- DOM node to emitnamespaces- namespaces currently in scopeendWithComma- whether to terminate the emitted fragment with a comma
-
printElement
Emits Groovy markup for a DOM element, including namespaces, attributes, and children.- Parameters:
element- element to emitnamespaces- namespaces currently in scopeendWithComma- whether to terminate the emitted fragment with a comma
-
printQuoted
Writes text as a Groovy string literal, using triple quotes for multi-line content.- Parameters:
text- text to quote
-
printPI
Emits a processing instruction as anmkp.picall.- Parameters:
instruction- processing instruction to emitendWithComma- whether to terminate the emitted fragment with a comma
-
printComment
Emits a Groovy block comment for a non-empty DOM comment node.- Parameters:
comment- comment to emitendWithComma- whether to terminate the emitted fragment with a comma
-
printText
Emits a text node, usingmkp.yieldwhen nested in mixed content.- Parameters:
node- text node to emitendWithComma- whether to terminate the emitted fragment with a comma
-
escapeQuote
Escapes backslashes and the active quote delimiter for generated Groovy strings.- Parameters:
text- raw text- Returns:
- escaped text
-
defineNamespaces
Returns the namespace map extended with any newly encountered element or attribute prefixes.- Parameters:
element- element whose namespaces should be inspectednamespaces- namespaces currently in scope- Returns:
- namespace map to use for the element and its children
-
defineNamespace
Records a namespace mapping and emitsmkp.declareNamespacewhen needed.- Parameters:
namespaces- mutable namespace mapprefix- namespace prefixuri- namespace URI
-
printAttributes
Emits the attributes for the supplied element.- Parameters:
element- element whose attributes should be emitted- Returns:
trueif any attributes were written
-
printAttributeWithPrefix
Appends a prefixed attribute representation to the deferred attribute buffer.- Parameters:
attribute- attribute to renderbuffer- buffer collecting prefixed attributes
-
getAttributeValue
Returns the attribute value used when emitting the Groovy builder call.- Parameters:
attribute- attribute to inspect- Returns:
- attribute value
-
printAttributeWithoutPrefix
Emits an unprefixed attribute and updates the separator state.- Parameters:
attribute- attribute to renderhasAttribute- whether a previous attribute has already been written- Returns:
- updated attribute-written flag
-
checkEscaping
Returns whether a generated Groovy name must be quoted.- Parameters:
localName- candidate element or attribute name- Returns:
trueif the name requires quoting
-
getTextNodeData
Returns the trimmed text content used when emitting a DOM text node.- Parameters:
node- text node- Returns:
- trimmed text content
-
mixedContent
Detects whether a node list contains both non-empty text and element children.- Parameters:
list- child nodes to inspect- Returns:
truefor mixed content
-
printChildren
Emits all children of the supplied parent node.- Parameters:
parent- DOM parent nodenamespaces- namespaces currently in scope
-
getLocalName
Returns the trimmed local name for a DOM node, falling back toNode.getNodeName().- Parameters:
node- DOM node- Returns:
- trimmed local name
-
printEnd
Finishes the current emitted fragment and optionally appends a comma.- Parameters:
text- trailing text to emit before line terminationendWithComma- whether to append a comma
-
println
Writes a line through the configured indent printer.- Parameters:
text- line text
-
print
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.
-