📄 format.java
字号:
}
/**
* This will set whether JAXP TrAX processing instructions for
* disabling/enabling output escaping are ignored. Disabling
* output escaping allows using XML text as element content and
* outputing it verbatim, i.e. as element children would be.
* <p>
* When processed, these processing instructions are removed from
* the generated XML text and control whether the element text
* content is output verbatim or with escaping of the pre-defined
* entities in XML 1.0. The text to be output verbatim shall be
* surrounded by the
* <code><?javax.xml.transform.disable-output-escaping ?></code>
* and <code><?javax.xml.transform.enable-output-escaping ?></code>
* PIs.</p>
* <p>
* When ignored, the processing instructions are present in the
* generated XML text and the pre-defined entities in XML 1.0 are
* escaped.
* <p>
* Default: <code>false</code>.</p>
*
* @param ignoreTrAXEscapingPIs <code>boolean</code> indicating
* whether or not TrAX ouput escaping PIs are ignored.
*
* @see javax.xml.transform.Result#PI_ENABLE_OUTPUT_ESCAPING
* @see javax.xml.transform.Result#PI_DISABLE_OUTPUT_ESCAPING
*/
public void setIgnoreTrAXEscapingPIs(boolean ignoreTrAXEscapingPIs) {
this.ignoreTrAXEscapingPIs = ignoreTrAXEscapingPIs;
}
/**
* Returns whether JAXP TrAX processing instructions for
* disabling/enabling output escaping are ignored.
*
* @return whether or not TrAX ouput escaping PIs are ignored.
*/
public boolean getIgnoreTrAXEscapingPIs() {
return ignoreTrAXEscapingPIs;
}
/**
* This sets the text output style. Options are available as static
* {@link TextMode} instances. The default is {@link TextMode#PRESERVE}.
*
* @return a pointer to this Format for chaining
*/
public Format setTextMode(Format.TextMode mode) {
this.mode = mode;
return this;
}
/**
* Returns the current text output style.
*
* @return the current text output style
*/
public Format.TextMode getTextMode() {
return mode;
}
/**
* This will set the indent <code>String</code> to use; this
* is usually a <code>String</code> of empty spaces. If you pass
* the empty string (""), then no indentation will happen but newlines
* will still be generated. Passing null will result in no indentation
* and no newlines generated. Default: none (null)
*
* @param indent <code>String</code> to use for indentation.
* @return a pointer to this Format for chaining
*/
public Format setIndent(String indent) {
this.indent = indent;
return this;
}
/**
* Returns the indent string in use.
*
* @return the indent string in use
*/
public String getIndent() {
return indent;
}
/**
* Sets the output encoding. The name should be an accepted XML
* encoding.
*
* @param encoding the encoding format. Use XML-style names like
* "UTF-8" or "ISO-8859-1" or "US-ASCII"
* @return a pointer to this Format for chaining
*/
public Format setEncoding(String encoding) {
this.encoding = encoding;
escapeStrategy = new DefaultEscapeStrategy(encoding);
return this;
}
/**
* Returns the configured output encoding.
*
* @return the output encoding
*/
public String getEncoding() {
return encoding;
}
protected Object clone() {
Format format = null;
try {
format = (Format) super.clone();
}
catch (CloneNotSupportedException ce) {
}
return format;
}
/**
* Handle common charsets quickly and easily. Use reflection
* to query the JDK 1.4 CharsetEncoder class for unknown charsets.
* If JDK 1.4 isn't around, default to no special encoding.
*/
class DefaultEscapeStrategy implements EscapeStrategy {
private int bits;
Object encoder;
Method canEncode;
public DefaultEscapeStrategy(String encoding) {
if ("UTF-8".equalsIgnoreCase(encoding) ||
"UTF-16".equalsIgnoreCase(encoding)) {
bits = 16;
}
else if ("ISO-8859-1".equalsIgnoreCase(encoding) ||
"Latin1".equalsIgnoreCase(encoding)) {
bits = 8;
}
else if ("US-ASCII".equalsIgnoreCase(encoding) ||
"ASCII".equalsIgnoreCase(encoding)) {
bits = 7;
}
else {
bits = 0;
//encoder = Charset.forName(encoding).newEncoder();
try {
Class charsetClass = Class.forName("java.nio.charset.Charset");
Class encoderClass = Class.forName("java.nio.charset.CharsetEncoder");
Method forName = charsetClass.getMethod("forName", new Class[]{String.class});
Object charsetObj = forName.invoke(null, new Object[]{encoding});
Method newEncoder = charsetClass.getMethod("newEncoder", null);
encoder = newEncoder.invoke(charsetObj, null);
canEncode = encoderClass.getMethod("canEncode", new Class[]{char.class});
}
catch (Exception ignored) {
}
}
}
public boolean shouldEscape(char ch) {
if (bits == 16) {
return false;
}
if (bits == 8) {
if ((int) ch > 255)
return true;
else
return false;
}
if (bits == 7) {
if ((int) ch > 127)
return true;
else
return false;
}
else {
if (canEncode != null && encoder != null) {
try {
Boolean val = (Boolean) canEncode.invoke(encoder, new Object[]{new Character(ch)});
return !val.booleanValue();
}
catch (Exception ignored) {
}
}
// Return false if we don't know. This risks not escaping
// things which should be escaped, but also means people won't
// start getting loads of unnecessary escapes.
return false;
}
}
}
/**
* Class to signify how text should be handled on output. The following
* table provides details.
*
* <table>
* <tr>
* <th align="left">
* Text Mode
* </th>
* <th>
* Resulting behavior.
* </th>
* </tr>
*
* <tr valign="top">
* <td>
* <i>PRESERVE (Default)</i>
* </td>
* <td>
* All content is printed in the format it was created, no whitespace
* or line separators are are added or removed.
* </td>
* </tr>
*
* <tr valign="top">
* <td>
* TRIM_FULL_WHITE
* </td>
* <td>
* Content between tags consisting of all whitespace is not printed.
* If the content contains even one non-whitespace character, it is
* printed verbatim, whitespace and all.
* </td>
* </tr>
*
* <tr valign="top">
* <td>
* TRIM
* </td>
* <td>
* Same as TrimAllWhite, plus leading/trailing whitespace are
* trimmed.
* </td>
* </tr>
*
* <tr valign="top">
* <td>
* NORMALIZE
* </td>
* <td>
* Same as TextTrim, plus addition interior whitespace is compressed
* to a single space.
* </td>
* </tr>
* </table>
*
* In most cases textual content is aligned with the surrounding tags
* (after the appropriate text mode is applied). In the case where the only
* content between the start and end tags is textual, the start tag, text,
* and end tag are all printed on the same line. If the document being
* output already has whitespace, it's wise to turn on TRIM mode so the
* pre-existing whitespace can be trimmed before adding new whitespace.
* <p>
* When a element has a xml:space attribute with the value of "preserve",
* all formating is turned off and reverts back to the default until the
* element and its contents have been printed. If a nested element contains
* another xml:space with the value "default" formatting is turned back on
* for the child element and then off for the remainder of the parent
* element.
*/
public static class TextMode {
/**
* Mode for literal text preservation.
*/
public static final TextMode PRESERVE = new TextMode("PRESERVE");
/**
* Mode for text trimming (left and right trim).
*/
public static final TextMode TRIM = new TextMode("TRIM");
/**
* Mode for text normalization (left and right trim plus internal
* whitespace is normalized to a single space.
* @see org.jdom.Element#getTextNormalize
*/
public static final TextMode NORMALIZE = new TextMode("NORMALIZE");
/**
* Mode for text trimming of content consisting of nothing but
* whitespace but otherwise not changing output.
*/
public static final TextMode TRIM_FULL_WHITE =
new TextMode("TRIM_FULL_WHITE");
private final String name;
private TextMode(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -