com.android.ide.common.xml
Class XmlPrettyPrinter

java.lang.Object
  extended by com.android.ide.common.xml.XmlPrettyPrinter

public class XmlPrettyPrinter
extends java.lang.Object

Visitor which walks over the subtree of the DOM to be formatted and pretty prints the DOM into the given StringBuilder


Constructor Summary
XmlPrettyPrinter(XmlFormatPreferences prefs, XmlFormatStyle style, java.lang.String lineSeparator)
          Creates a new XmlPrettyPrinter
 
Method Summary
protected  java.lang.String getSource(org.w3c.dom.Node node)
           
protected  boolean isEmptyTag(org.w3c.dom.Element element)
          Returns true if the given element should be an empty tag
static void main(java.lang.String[] args)
          Command line driver
 void prettyPrint(int rootDepth, org.w3c.dom.Node root, org.w3c.dom.Node startNode, org.w3c.dom.Node endNode, java.lang.StringBuilder out, boolean openTagOnly)
          Start pretty-printing at the given node, which must either be the startNode or contain it as a descendant.
static java.lang.String prettyPrint(org.w3c.dom.Node node)
          Deprecated. Use prettyPrint(org.w3c.dom.Node, boolean) instead
static java.lang.String prettyPrint(org.w3c.dom.Node node, boolean endWithNewline)
          Pretty prints the given node using default styles
static java.lang.String prettyPrint(org.w3c.dom.Node node, XmlFormatPreferences prefs, XmlFormatStyle style, java.lang.String lineSeparator)
          Deprecated. Use prettyPrint(org.w3c.dom.Node, XmlFormatPreferences, XmlFormatStyle, String, boolean) instead
static java.lang.String prettyPrint(org.w3c.dom.Node node, XmlFormatPreferences prefs, XmlFormatStyle style, java.lang.String lineSeparator, boolean endWithNewline)
          Pretty prints the given node
static java.lang.String prettyPrint(java.lang.String xml, XmlFormatPreferences prefs, XmlFormatStyle style, java.lang.String lineSeparator)
          Pretty-prints the given XML document, which must be well-formed.
 XmlPrettyPrinter setEndWithNewline(boolean endWithNewline)
          Sets whether the document should end with a newline/ line separator
 void setIndentationLevels(java.lang.String[] indentationLevels)
          Sets the indentation levels to use (indentation string to use for each depth, indexed by depth
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlPrettyPrinter

public XmlPrettyPrinter(XmlFormatPreferences prefs,
                        XmlFormatStyle style,
                        java.lang.String lineSeparator)
Creates a new XmlPrettyPrinter

Parameters:
prefs - the preferences to format with
style - the style to format with
lineSeparator - the line separator to use, such as "\n" (can be null, in which case the system default is looked up via the line.separator property)
Method Detail

setEndWithNewline

public XmlPrettyPrinter setEndWithNewline(boolean endWithNewline)
Sets whether the document should end with a newline/ line separator

Parameters:
endWithNewline - if true, ensure that the document ends with a newline
Returns:
this, for constructor chaining

setIndentationLevels

public void setIndentationLevels(java.lang.String[] indentationLevels)
Sets the indentation levels to use (indentation string to use for each depth, indexed by depth

Parameters:
indentationLevels - an array of strings to use for the various indentation levels

prettyPrint

@NonNull
public static java.lang.String prettyPrint(@NonNull
                                                   java.lang.String xml,
                                                   @NonNull
                                                   XmlFormatPreferences prefs,
                                                   @NonNull
                                                   XmlFormatStyle style,
                                                   @Nullable
                                                   java.lang.String lineSeparator)
Pretty-prints the given XML document, which must be well-formed. If it is not, the original unformatted XML document is returned

Parameters:
xml - the XML content to format
prefs - the preferences to format with
style - the style to format with
lineSeparator - the line separator to use, such as "\n" (can be null, in which case the system default is looked up via the line.separator property)
Returns:
the formatted document (or if a parsing error occurred, returns the unformatted document)

prettyPrint

@NonNull
@Deprecated
public static java.lang.String prettyPrint(@NonNull
                                                              org.w3c.dom.Node node,
                                                              @NonNull
                                                              XmlFormatPreferences prefs,
                                                              @NonNull
                                                              XmlFormatStyle style,
                                                              @Nullable
                                                              java.lang.String lineSeparator)
Deprecated. Use prettyPrint(org.w3c.dom.Node, XmlFormatPreferences, XmlFormatStyle, String, boolean) instead

Pretty prints the given node

Parameters:
node - the node, usually a document, to be printed
prefs - the formatting preferences
style - the formatting style to use
lineSeparator - the line separator to use, or null to use the default
Returns:
a formatted string

prettyPrint

@NonNull
public static java.lang.String prettyPrint(@NonNull
                                                   org.w3c.dom.Node node,
                                                   @NonNull
                                                   XmlFormatPreferences prefs,
                                                   @NonNull
                                                   XmlFormatStyle style,
                                                   @Nullable
                                                   java.lang.String lineSeparator,
                                                   boolean endWithNewline)
Pretty prints the given node

Parameters:
node - the node, usually a document, to be printed
prefs - the formatting preferences
style - the formatting style to use
lineSeparator - the line separator to use, or null to use the default
endWithNewline - if true, ensure that the printed output ends with a newline
Returns:
a formatted string

prettyPrint

@NonNull
@Deprecated
public static java.lang.String prettyPrint(@NonNull
                                                              org.w3c.dom.Node node)
Deprecated. Use prettyPrint(org.w3c.dom.Node, boolean) instead

Pretty prints the given node using default styles

Parameters:
node - the node, usually a document, to be printed
Returns:
the resulting formatted string

prettyPrint

@NonNull
public static java.lang.String prettyPrint(@NonNull
                                                   org.w3c.dom.Node node,
                                                   boolean endWithNewline)
Pretty prints the given node using default styles

Parameters:
node - the node, usually a document, to be printed
endWithNewline - if true, ensure that the printed output ends with a newline
Returns:
the resulting formatted string

prettyPrint

public void prettyPrint(int rootDepth,
                        org.w3c.dom.Node root,
                        org.w3c.dom.Node startNode,
                        org.w3c.dom.Node endNode,
                        java.lang.StringBuilder out,
                        boolean openTagOnly)
Start pretty-printing at the given node, which must either be the startNode or contain it as a descendant.

Parameters:
rootDepth - the depth of the given node, used to determine indentation
root - the node to start pretty printing from (which may not itself be included in the start to end node range but should contain it)
startNode - the node to start formatting at
endNode - the node to end formatting at
out - the StringBuilder to pretty print into
openTagOnly - if true, only format the open tag of the startNode (and nothing else)

getSource

@Nullable
protected java.lang.String getSource(@NonNull
                                              org.w3c.dom.Node node)

isEmptyTag

protected boolean isEmptyTag(org.w3c.dom.Element element)
Returns true if the given element should be an empty tag

Parameters:
element - the element to test
Returns:
true if this element should be an empty tag

main

public static void main(java.lang.String[] args)
Command line driver