Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Format.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*--
  2. $Id: Format.java,v 1.13 2007/11/10 05:29:01 jhunter Exp $
  3. Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions, and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions, and the disclaimer that follows
  12. these conditions in the documentation and/or other materials
  13. provided with the distribution.
  14. 3. The name "JDOM" must not be used to endorse or promote products
  15. derived from this software without prior written permission. For
  16. written permission, please contact <request_AT_jdom_DOT_org>.
  17. 4. Products derived from this software may not be called "JDOM", nor
  18. may "JDOM" appear in their name, without prior written permission
  19. from the JDOM Project Management <request_AT_jdom_DOT_org>.
  20. In addition, we request (but do not require) that you include in the
  21. end-user documentation provided with the redistribution and/or in the
  22. software itself an acknowledgement equivalent to the following:
  23. "This product includes software developed by the
  24. JDOM Project (http://www.jdom.org/)."
  25. Alternatively, the acknowledgment may be graphical using the logos
  26. available at http://www.jdom.org/images/logos.
  27. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  34. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. SUCH DAMAGE.
  39. This software consists of voluntary contributions made by many
  40. individuals on behalf of the JDOM Project and was originally
  41. created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
  42. Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
  43. on the JDOM Project, please see <http://www.jdom.org/>.
  44. */
  45. package org.jdom.output;
  46. import java.lang.reflect.Method;
  47. /**
  48. * Class to encapsulate XMLOutputter format options.
  49. * Typical users can use the standard format configurations obtained by
  50. * {@link #getRawFormat} (no whitespace changes),
  51. * {@link #getPrettyFormat} (whitespace beautification), and
  52. * {@link #getCompactFormat} (whitespace normalization).
  53. * <p>
  54. * Several modes are available to effect the way textual content is printed.
  55. * See the documentation for {@link TextMode} for details.
  56. *
  57. * @version $Revision: 1.13 $, $Date: 2007/11/10 05:29:01 $
  58. * @author Jason Hunter
  59. */
  60. public class Format implements Cloneable {
  61. private static final String CVS_ID =
  62. "@(#) $RCSfile: Format.java,v $ $Revision: 1.13 $ $Date: 2007/11/10 05:29:01 $ $Name: jdom_1_1 $";
  63. /**
  64. * Returns a new Format object that performs no whitespace changes, uses
  65. * the UTF-8 encoding, doesn't expand empty elements, includes the
  66. * declaration and encoding, and uses the default entity escape strategy.
  67. * Tweaks can be made to the returned Format instance without affecting
  68. * other instances.
  69. * @return a Format with no whitespace changes
  70. */
  71. public static Format getRawFormat() {
  72. return new Format();
  73. }
  74. /**
  75. * Returns a new Format object that performs whitespace beautification with
  76. * 2-space indents, uses the UTF-8 encoding, doesn't expand empty elements,
  77. * includes the declaration and encoding, and uses the default entity
  78. * escape strategy.
  79. * Tweaks can be made to the returned Format instance without affecting
  80. * other instances.
  81. *
  82. * @return a Format with whitespace beautification
  83. */
  84. public static Format getPrettyFormat() {
  85. Format f = new Format();
  86. f.setIndent(STANDARD_INDENT);
  87. f.setTextMode(TextMode.TRIM);
  88. return f;
  89. }
  90. /**
  91. * Returns a new Format object that performs whitespace normalization, uses
  92. * the UTF-8 encoding, doesn't expand empty elements, includes the
  93. * declaration and encoding, and uses the default entity escape strategy.
  94. * Tweaks can be made to the returned Format instance without affecting
  95. * other instances.
  96. *
  97. * @return a Format with whitespace normalization
  98. */
  99. public static Format getCompactFormat() {
  100. Format f = new Format();
  101. f.setTextMode(TextMode.NORMALIZE);
  102. return f;
  103. }
  104. /** standard value to indent by, if we are indenting */
  105. private static final String STANDARD_INDENT = " ";
  106. /** standard string with which to end a line */
  107. private static final String STANDARD_LINE_SEPARATOR = "\r\n";
  108. /** standard encoding */
  109. private static final String STANDARD_ENCODING = "UTF-8";
  110. /** The default indent is no spaces (as original document) */
  111. String indent = null;
  112. /** New line separator */
  113. String lineSeparator = STANDARD_LINE_SEPARATOR;
  114. /** The encoding format */
  115. String encoding = STANDARD_ENCODING;
  116. /** Whether or not to output the XML declaration
  117. * - default is <code>false</code> */
  118. boolean omitDeclaration = false;
  119. /** Whether or not to output the encoding in the XML declaration
  120. * - default is <code>false</code> */
  121. boolean omitEncoding = false;
  122. /** Whether or not to expand empty elements to
  123. * &lt;tagName&gt;&lt;/tagName&gt; - default is <code>false</code> */
  124. boolean expandEmptyElements = false;
  125. /** Whether TrAX output escaping disabling/enabling PIs are ignored
  126. * or processed - default is <code>false</code> */
  127. boolean ignoreTrAXEscapingPIs = false;
  128. /** text handling mode */
  129. TextMode mode = TextMode.PRESERVE;
  130. /** entity escape logic */
  131. EscapeStrategy escapeStrategy = new DefaultEscapeStrategy(encoding);
  132. /**
  133. * Creates a new Format instance with default (raw) behavior.
  134. */
  135. private Format() { }
  136. /**
  137. * Sets the {@link EscapeStrategy} to use for character escaping.
  138. *
  139. * @param strategy the EscapeStrategy to use
  140. * @return a pointer to this Format for chaining
  141. */
  142. public Format setEscapeStrategy(EscapeStrategy strategy) {
  143. escapeStrategy = strategy;
  144. return this;
  145. }
  146. /**
  147. * Returns the current escape strategy
  148. *
  149. * @return the current escape strategy
  150. */
  151. public EscapeStrategy getEscapeStrategy() {
  152. return escapeStrategy;
  153. }
  154. /**
  155. * This will set the newline separator (<code>lineSeparator</code>).
  156. * The default is <code>\r\n</code>. To make it output
  157. * the system default line ending string, call
  158. * <code>setLineSeparator(System.getProperty("line.separator"))</code>.
  159. *
  160. * <p>
  161. * To output "UNIX-style" documents, call
  162. * <code>setLineSeparator("\n")</code>. To output "Mac-style"
  163. * documents, call <code>setLineSeparator("\r")</code>. DOS-style
  164. * documents use CR-LF ("\r\n"), which is the default.
  165. * </p>
  166. *
  167. * <p>
  168. * Note that this only applies to newlines generated by the
  169. * outputter. If you parse an XML document that contains newlines
  170. * embedded inside a text node, and you do not set TextMode.NORMALIZE,
  171. * then the newlines will be output
  172. * verbatim, as "\n" which is how parsers normalize them.
  173. * </p>
  174. *
  175. * <p>
  176. * If the format's "indent" property is null (as is the default
  177. * for the Raw and Compact formats), then this value only effects the
  178. * newlines written after the declaration and doctype.
  179. * </p>
  180. *
  181. * @see #setTextMode
  182. *
  183. * @param separator <code>String</code> line separator to use.
  184. * @return a pointer to this Format for chaining
  185. */
  186. public Format setLineSeparator(String separator) {
  187. this.lineSeparator = separator;
  188. return this;
  189. }
  190. /**
  191. * Returns the current line separator.
  192. *
  193. * @return the current line separator
  194. */
  195. public String getLineSeparator() {
  196. return lineSeparator;
  197. }
  198. /**
  199. * This will set whether the XML declaration
  200. * (<code>&lt;&#063;xml version="1&#046;0"
  201. * encoding="UTF-8"&#063;&gt;</code>)
  202. * includes the encoding of the document. It is common to omit
  203. * this in uses such as WML and other wireless device protocols.
  204. *
  205. * @param omitEncoding <code>boolean</code> indicating whether or not
  206. * the XML declaration should indicate the document encoding.
  207. * @return a pointer to this Format for chaining
  208. */
  209. public Format setOmitEncoding(boolean omitEncoding) {
  210. this.omitEncoding = omitEncoding;
  211. return this;
  212. }
  213. /**
  214. * Returns whether the XML declaration encoding will be omitted.
  215. *
  216. * @return whether the XML declaration encoding will be omitted
  217. */
  218. public boolean getOmitEncoding() {
  219. return omitEncoding;
  220. }
  221. /**
  222. * This will set whether the XML declaration
  223. * (<code>&lt;&#063;xml version="1&#046;0"&#063;gt;</code>)
  224. * will be omitted or not. It is common to omit this in uses such
  225. * as SOAP and XML-RPC calls.
  226. *
  227. * @param omitDeclaration <code>boolean</code> indicating whether or not
  228. * the XML declaration should be omitted.
  229. * @return a pointer to this Format for chaining
  230. */
  231. public Format setOmitDeclaration(boolean omitDeclaration) {
  232. this.omitDeclaration = omitDeclaration;
  233. return this;
  234. }
  235. /**
  236. * Returns whether the XML declaration will be omitted.
  237. *
  238. * @return whether the XML declaration will be omitted
  239. */
  240. public boolean getOmitDeclaration() {
  241. return omitDeclaration;
  242. }
  243. /**
  244. * This will set whether empty elements are expanded from
  245. * <code>&lt;tagName/&gt;</code> to
  246. * <code>&lt;tagName&gt;&lt;/tagName&gt;</code>.
  247. *
  248. * @param expandEmptyElements <code>boolean</code> indicating whether or not
  249. * empty elements should be expanded.
  250. * @return a pointer to this Format for chaining
  251. */
  252. public Format setExpandEmptyElements(boolean expandEmptyElements) {
  253. this.expandEmptyElements = expandEmptyElements;
  254. return this;
  255. }
  256. /**
  257. * Returns whether empty elements are expanded.
  258. *
  259. * @return whether empty elements are expanded
  260. */
  261. public boolean getExpandEmptyElements() {
  262. return expandEmptyElements;
  263. }
  264. /**
  265. * This will set whether JAXP TrAX processing instructions for
  266. * disabling/enabling output escaping are ignored. Disabling
  267. * output escaping allows using XML text as element content and
  268. * outputing it verbatim, i&#46;e&#46; as element children would be.
  269. * <p>
  270. * When processed, these processing instructions are removed from
  271. * the generated XML text and control whether the element text
  272. * content is output verbatim or with escaping of the pre-defined
  273. * entities in XML 1.0. The text to be output verbatim shall be
  274. * surrounded by the
  275. * <code>&lt;?javax.xml.transform.disable-output-escaping ?&gt;</code>
  276. * and <code>&lt;?javax.xml.transform.enable-output-escaping ?&gt;</code>
  277. * PIs.</p>
  278. * <p>
  279. * When ignored, the processing instructions are present in the
  280. * generated XML text and the pre-defined entities in XML 1.0 are
  281. * escaped.
  282. * <p>
  283. * Default: <code>false</code>.</p>
  284. *
  285. * @param ignoreTrAXEscapingPIs <code>boolean</code> indicating
  286. * whether or not TrAX ouput escaping PIs are ignored.
  287. *
  288. * @see javax.xml.transform.Result#PI_ENABLE_OUTPUT_ESCAPING
  289. * @see javax.xml.transform.Result#PI_DISABLE_OUTPUT_ESCAPING
  290. */
  291. public void setIgnoreTrAXEscapingPIs(boolean ignoreTrAXEscapingPIs) {
  292. this.ignoreTrAXEscapingPIs = ignoreTrAXEscapingPIs;
  293. }
  294. /**
  295. * Returns whether JAXP TrAX processing instructions for
  296. * disabling/enabling output escaping are ignored.
  297. *
  298. * @return whether or not TrAX ouput escaping PIs are ignored.
  299. */
  300. public boolean getIgnoreTrAXEscapingPIs() {
  301. return ignoreTrAXEscapingPIs;
  302. }
  303. /**
  304. * This sets the text output style. Options are available as static
  305. * {@link TextMode} instances. The default is {@link TextMode#PRESERVE}.
  306. *
  307. * @return a pointer to this Format for chaining
  308. */
  309. public Format setTextMode(Format.TextMode mode) {
  310. this.mode = mode;
  311. return this;
  312. }
  313. /**
  314. * Returns the current text output style.
  315. *
  316. * @return the current text output style
  317. */
  318. public Format.TextMode getTextMode() {
  319. return mode;
  320. }
  321. /**
  322. * This will set the indent <code>String</code> to use; this
  323. * is usually a <code>String</code> of empty spaces. If you pass
  324. * the empty string (""), then no indentation will happen but newlines
  325. * will still be generated. Passing null will result in no indentation
  326. * and no newlines generated. Default: none (null)
  327. *
  328. * @param indent <code>String</code> to use for indentation.
  329. * @return a pointer to this Format for chaining
  330. */
  331. public Format setIndent(String indent) {
  332. this.indent = indent;
  333. return this;
  334. }
  335. /**
  336. * Returns the indent string in use.
  337. *
  338. * @return the indent string in use
  339. */
  340. public String getIndent() {
  341. return indent;
  342. }
  343. /**
  344. * Sets the output encoding. The name should be an accepted XML
  345. * encoding.
  346. *
  347. * @param encoding the encoding format. Use XML-style names like
  348. * "UTF-8" or "ISO-8859-1" or "US-ASCII"
  349. * @return a pointer to this Format for chaining
  350. */
  351. public Format setEncoding(String encoding) {
  352. this.encoding = encoding;
  353. escapeStrategy = new DefaultEscapeStrategy(encoding);
  354. return this;
  355. }
  356. /**
  357. * Returns the configured output encoding.
  358. *
  359. * @return the output encoding
  360. */
  361. public String getEncoding() {
  362. return encoding;
  363. }
  364. protected Object clone() {
  365. Format format = null;
  366. try {
  367. format = (Format) super.clone();
  368. }
  369. catch (CloneNotSupportedException ce) {
  370. }
  371. return format;
  372. }
  373. /**
  374. * Handle common charsets quickly and easily. Use reflection
  375. * to query the JDK 1.4 CharsetEncoder class for unknown charsets.
  376. * If JDK 1.4 isn't around, default to no special encoding.
  377. */
  378. class DefaultEscapeStrategy implements EscapeStrategy {
  379. private int bits;
  380. Object encoder;
  381. Method canEncode;
  382. public DefaultEscapeStrategy(String encoding) {
  383. if ("UTF-8".equalsIgnoreCase(encoding) ||
  384. "UTF-16".equalsIgnoreCase(encoding)) {
  385. bits = 16;
  386. }
  387. else if ("ISO-8859-1".equalsIgnoreCase(encoding) ||
  388. "Latin1".equalsIgnoreCase(encoding)) {
  389. bits = 8;
  390. }
  391. else if ("US-ASCII".equalsIgnoreCase(encoding) ||
  392. "ASCII".equalsIgnoreCase(encoding)) {
  393. bits = 7;
  394. }
  395. else {
  396. bits = 0;
  397. //encoder = Charset.forName(encoding).newEncoder();
  398. try {
  399. Class charsetClass = Class.forName("java.nio.charset.Charset");
  400. Class encoderClass = Class.forName("java.nio.charset.CharsetEncoder");
  401. Method forName = charsetClass.getMethod("forName", new Class[]{String.class});
  402. Object charsetObj = forName.invoke(null, new Object[]{encoding});
  403. Method newEncoder = charsetClass.getMethod("newEncoder", null);
  404. encoder = newEncoder.invoke(charsetObj, null);
  405. canEncode = encoderClass.getMethod("canEncode", new Class[]{char.class});
  406. }
  407. catch (Exception ignored) {
  408. }
  409. }
  410. }
  411. public boolean shouldEscape(char ch) {
  412. if (bits == 16) {
  413. return false;
  414. }
  415. if (bits == 8) {
  416. if ((int) ch > 255)
  417. return true;
  418. else
  419. return false;
  420. }
  421. if (bits == 7) {
  422. if ((int) ch > 127)
  423. return true;
  424. else
  425. return false;
  426. }
  427. else {
  428. if (canEncode != null && encoder != null) {
  429. try {
  430. Boolean val = (Boolean) canEncode.invoke(encoder, new Object[]{new Character(ch)});
  431. return !val.booleanValue();
  432. }
  433. catch (Exception ignored) {
  434. }
  435. }
  436. // Return false if we don't know. This risks not escaping
  437. // things which should be escaped, but also means people won't
  438. // start getting loads of unnecessary escapes.
  439. return false;
  440. }
  441. }
  442. }
  443. /**
  444. * Class to signify how text should be handled on output. The following
  445. * table provides details.
  446. *
  447. * <table>
  448. * <tr>
  449. * <th align="left">
  450. * Text Mode
  451. * </th>
  452. * <th>
  453. * Resulting behavior.
  454. * </th>
  455. * </tr>
  456. *
  457. * <tr valign="top">
  458. * <td>
  459. * <i>PRESERVE (Default)</i>
  460. * </td>
  461. * <td>
  462. * All content is printed in the format it was created, no whitespace
  463. * or line separators are are added or removed.
  464. * </td>
  465. * </tr>
  466. *
  467. * <tr valign="top">
  468. * <td>
  469. * TRIM_FULL_WHITE
  470. * </td>
  471. * <td>
  472. * Content between tags consisting of all whitespace is not printed.
  473. * If the content contains even one non-whitespace character, it is
  474. * printed verbatim, whitespace and all.
  475. * </td>
  476. * </tr>
  477. *
  478. * <tr valign="top">
  479. * <td>
  480. * TRIM
  481. * </td>
  482. * <td>
  483. * Same as TrimAllWhite, plus leading/trailing whitespace are
  484. * trimmed.
  485. * </td>
  486. * </tr>
  487. *
  488. * <tr valign="top">
  489. * <td>
  490. * NORMALIZE
  491. * </td>
  492. * <td>
  493. * Same as TextTrim, plus addition interior whitespace is compressed
  494. * to a single space.
  495. * </td>
  496. * </tr>
  497. * </table>
  498. *
  499. * In most cases textual content is aligned with the surrounding tags
  500. * (after the appropriate text mode is applied). In the case where the only
  501. * content between the start and end tags is textual, the start tag, text,
  502. * and end tag are all printed on the same line. If the document being
  503. * output already has whitespace, it's wise to turn on TRIM mode so the
  504. * pre-existing whitespace can be trimmed before adding new whitespace.
  505. * <p>
  506. * When a element has a xml:space attribute with the value of "preserve",
  507. * all formating is turned off and reverts back to the default until the
  508. * element and its contents have been printed. If a nested element contains
  509. * another xml:space with the value "default" formatting is turned back on
  510. * for the child element and then off for the remainder of the parent
  511. * element.
  512. */
  513. public static class TextMode {
  514. /**
  515. * Mode for literal text preservation.
  516. */
  517. public static final TextMode PRESERVE = new TextMode("PRESERVE");
  518. /**
  519. * Mode for text trimming (left and right trim).
  520. */
  521. public static final TextMode TRIM = new TextMode("TRIM");
  522. /**
  523. * Mode for text normalization (left and right trim plus internal
  524. * whitespace is normalized to a single space.
  525. * @see org.jdom.Element#getTextNormalize
  526. */
  527. public static final TextMode NORMALIZE = new TextMode("NORMALIZE");
  528. /**
  529. * Mode for text trimming of content consisting of nothing but
  530. * whitespace but otherwise not changing output.
  531. */
  532. public static final TextMode TRIM_FULL_WHITE =
  533. new TextMode("TRIM_FULL_WHITE");
  534. private final String name;
  535. private TextMode(String name) {
  536. this.name = name;
  537. }
  538. public String toString() {
  539. return name;
  540. }
  541. }
  542. }