You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DOMElementWriter.java 18 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright 2000-2005 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.util;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.io.OutputStreamWriter;
  21. import java.io.Writer;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.Iterator;
  25. import org.w3c.dom.Attr;
  26. import org.w3c.dom.Element;
  27. import org.w3c.dom.NamedNodeMap;
  28. import org.w3c.dom.Node;
  29. import org.w3c.dom.NodeList;
  30. import org.w3c.dom.Text;
  31. /**
  32. * Writes a DOM tree to a given Writer.
  33. * <p>Utility class used by {@link org.apache.tools.ant.XmlLogger
  34. * XmlLogger} and
  35. * org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter
  36. * XMLJUnitResultFormatter}.</p>
  37. *
  38. */
  39. public class DOMElementWriter {
  40. /** prefix for genefrated prefixes */
  41. private static final String NS = "ns";
  42. /** xml declaration is on by default */
  43. private boolean xmlDeclaration=true;
  44. /**
  45. * XML Namespaces are ignored by default.
  46. */
  47. private XmlNamespacePolicy namespacePolicy = XmlNamespacePolicy.IGNORE;
  48. /**
  49. * Map (URI to prefix) of known namespaces.
  50. */
  51. private HashMap nsPrefixMap = new HashMap();
  52. /**
  53. * Number of generated prefix to use next.
  54. */
  55. private int nextPrefix = 0;
  56. /**
  57. * Map (Element to URI) of namespaces defined on a given element.
  58. */
  59. private HashMap nsURIByElement = new HashMap();
  60. /**
  61. * Whether namespaces should be ignored for elements and attributes.
  62. *
  63. * @since Ant 1.7
  64. */
  65. public static class XmlNamespacePolicy {
  66. private boolean qualifyElements;
  67. private boolean qualifyAttributes;
  68. /**
  69. * Ignores namespaces for elements and attributes, the default.
  70. */
  71. public static final XmlNamespacePolicy IGNORE =
  72. new XmlNamespacePolicy(false, false);
  73. /**
  74. * Ignores namespaces for attributes.
  75. */
  76. public static final XmlNamespacePolicy ONLY_QUALIFY_ELEMENTS =
  77. new XmlNamespacePolicy(true, false);
  78. /**
  79. * Qualifies namespaces for elements and attributes.
  80. */
  81. public static final XmlNamespacePolicy QUALIFY_ALL =
  82. new XmlNamespacePolicy(true, true);
  83. /**
  84. * @param qualifyElements whether to qualify elements
  85. * @param qualifyAttributes whether to qualify elements
  86. */
  87. public XmlNamespacePolicy(boolean qualifyElements,
  88. boolean qualifyAttributes) {
  89. this.qualifyElements = qualifyElements;
  90. this.qualifyAttributes = qualifyAttributes;
  91. }
  92. }
  93. /**
  94. * Create an element writer.
  95. * The ?xml? declaration will be included, namespaces ignored.
  96. */
  97. public DOMElementWriter() {
  98. }
  99. /**
  100. * Create an element writer
  101. * XML namespaces will be ignored.
  102. * @param xmlDeclaration flag to indicate whether the ?xml? declaration
  103. * should be included.
  104. * @since Ant1.7
  105. */
  106. public DOMElementWriter(boolean xmlDeclaration) {
  107. this(xmlDeclaration, XmlNamespacePolicy.IGNORE);
  108. }
  109. /**
  110. * Create an element writer
  111. * XML namespaces will be ignored.
  112. * @param xmlDeclaration flag to indicate whether the ?xml? declaration
  113. * should be included.
  114. * @since Ant1.7
  115. */
  116. public DOMElementWriter(boolean xmlDeclaration,
  117. XmlNamespacePolicy namespacePolicy) {
  118. this.xmlDeclaration = xmlDeclaration;
  119. this.namespacePolicy = namespacePolicy;
  120. }
  121. private static String lSep = System.getProperty("line.separator");
  122. /**
  123. * Don't try to be too smart but at least recognize the predefined
  124. * entities.
  125. */
  126. protected String[] knownEntities = {"gt", "amp", "lt", "apos", "quot"};
  127. /**
  128. * Writes a DOM tree to a stream in UTF8 encoding. Note that
  129. * it prepends the &lt;?xml version='1.0' encoding='UTF-8'?&gt; if
  130. * the xmlDeclaration field is true.
  131. * The indent number is set to 0 and a 2-space indent.
  132. * @param root the root element of the DOM tree.
  133. * @param out the outputstream to write to.
  134. * @throws IOException if an error happens while writing to the stream.
  135. */
  136. public void write(Element root, OutputStream out) throws IOException {
  137. Writer wri = new OutputStreamWriter(out, "UTF8");
  138. if(xmlDeclaration) {
  139. wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  140. }
  141. write(root, wri, 0, " ");
  142. wri.flush();
  143. }
  144. /**
  145. * Writes a DOM tree to a stream.
  146. *
  147. * @param element the Root DOM element of the tree
  148. * @param out where to send the output
  149. * @param indent number of
  150. * @param indentWith string that should be used to indent the
  151. * corresponding tag.
  152. * @throws IOException if an error happens while writing to the stream.
  153. */
  154. public void write(Element element, Writer out, int indent,
  155. String indentWith)
  156. throws IOException {
  157. // Write child elements and text
  158. NodeList children = element.getChildNodes();
  159. boolean hasChildren = (children.getLength() > 0);
  160. boolean hasChildElements = false;
  161. openElement(element, out, indent, indentWith, hasChildren);
  162. if (hasChildren) {
  163. for (int i = 0; i < children.getLength(); i++) {
  164. Node child = children.item(i);
  165. switch (child.getNodeType()) {
  166. case Node.ELEMENT_NODE:
  167. hasChildElements = true;
  168. if (i == 0) {
  169. out.write(lSep);
  170. }
  171. write((Element) child, out, indent + 1, indentWith);
  172. break;
  173. case Node.TEXT_NODE:
  174. out.write(encode(child.getNodeValue()));
  175. break;
  176. case Node.COMMENT_NODE:
  177. out.write("<!--");
  178. out.write(encode(child.getNodeValue()));
  179. out.write("-->");
  180. break;
  181. case Node.CDATA_SECTION_NODE:
  182. out.write("<![CDATA[");
  183. out.write(encodedata(((Text) child).getData()));
  184. out.write("]]>");
  185. break;
  186. case Node.ENTITY_REFERENCE_NODE:
  187. out.write('&');
  188. out.write(child.getNodeName());
  189. out.write(';');
  190. break;
  191. case Node.PROCESSING_INSTRUCTION_NODE:
  192. out.write("<?");
  193. out.write(child.getNodeName());
  194. String data = child.getNodeValue();
  195. if (data != null && data.length() > 0) {
  196. out.write(' ');
  197. out.write(data);
  198. }
  199. out.write("?>");
  200. break;
  201. default:
  202. // Do nothing
  203. }
  204. }
  205. closeElement(element, out, indent, indentWith, hasChildElements);
  206. }
  207. }
  208. /**
  209. * Writes the opening tag - including all attributes -
  210. * corresponding to a DOM element.
  211. *
  212. * @param element the DOM element to write
  213. * @param out where to send the output
  214. * @param indent number of
  215. * @param indentWith string that should be used to indent the
  216. * corresponding tag.
  217. * @throws IOException if an error happens while writing to the stream.
  218. */
  219. public void openElement(Element element, Writer out, int indent,
  220. String indentWith)
  221. throws IOException {
  222. openElement(element, out, indent, indentWith, true);
  223. }
  224. /**
  225. * Writes the opening tag - including all attributes -
  226. * corresponding to a DOM element.
  227. *
  228. * @param element the DOM element to write
  229. * @param out where to send the output
  230. * @param indent number of
  231. * @param indentWith string that should be used to indent the
  232. * corresponding tag.
  233. * @param hasChildren whether this element has children.
  234. * @throws IOException if an error happens while writing to the stream.
  235. * @since Ant 1.7
  236. */
  237. public void openElement(Element element, Writer out, int indent,
  238. String indentWith, boolean hasChildren)
  239. throws IOException {
  240. // Write indent characters
  241. for (int i = 0; i < indent; i++) {
  242. out.write(indentWith);
  243. }
  244. // Write element
  245. out.write("<");
  246. if (namespacePolicy.qualifyElements) {
  247. String prefix = (String) nsPrefixMap.get(element.getNamespaceURI());
  248. if (prefix == null) {
  249. if (nsPrefixMap.isEmpty()) {
  250. // steal default namespace
  251. prefix = "";
  252. } else {
  253. prefix = NS + (nextPrefix++);
  254. }
  255. nsPrefixMap.put(element.getNamespaceURI(), prefix);
  256. addNSDefinition(element, element.getNamespaceURI());
  257. }
  258. if (!"".equals(prefix)) {
  259. out.write(prefix);
  260. out.write(":");
  261. }
  262. }
  263. out.write(element.getTagName());
  264. // Write attributes
  265. NamedNodeMap attrs = element.getAttributes();
  266. for (int i = 0; i < attrs.getLength(); i++) {
  267. Attr attr = (Attr) attrs.item(i);
  268. out.write(" ");
  269. if (namespacePolicy.qualifyAttributes) {
  270. String prefix =
  271. (String) nsPrefixMap.get(attr.getNamespaceURI());
  272. if (prefix == null) {
  273. prefix = NS + (nextPrefix++);
  274. nsPrefixMap.put(attr.getNamespaceURI(), prefix);
  275. addNSDefinition(element, attr.getNamespaceURI());
  276. }
  277. out.write(prefix);
  278. out.write(":");
  279. }
  280. out.write(attr.getName());
  281. out.write("=\"");
  282. out.write(encode(attr.getValue()));
  283. out.write("\"");
  284. }
  285. // write namespace declarations
  286. ArrayList al = (ArrayList) nsURIByElement.get(element);
  287. if (al != null) {
  288. Iterator iter = al.iterator();
  289. while (iter.hasNext()) {
  290. String uri = (String) iter.next();
  291. String prefix = (String) nsPrefixMap.get(uri);
  292. out.write(" xmlns");
  293. if (!"".equals(prefix)) {
  294. out.write(":");
  295. out.write(prefix);
  296. }
  297. out.write("=\"");
  298. out.write(uri);
  299. out.write("\"");
  300. }
  301. }
  302. if (hasChildren) {
  303. out.write(">");
  304. } else {
  305. removeNSDefinitions(element);
  306. out.write(" />");
  307. out.write(lSep);
  308. out.flush();
  309. }
  310. }
  311. /**
  312. * Writes a DOM tree to a stream.
  313. *
  314. * @param element the Root DOM element of the tree
  315. * @param out where to send the output
  316. * @param indent number of
  317. * @param indentWith string that should be used to indent the
  318. * corresponding tag.
  319. * @throws IOException if an error happens while writing to the stream.
  320. */
  321. public void closeElement(Element element, Writer out, int indent,
  322. String indentWith, boolean hasChildren)
  323. throws IOException {
  324. // If we had child elements, we need to indent before we close
  325. // the element, otherwise we're on the same line and don't need
  326. // to indent
  327. if (hasChildren) {
  328. for (int i = 0; i < indent; i++) {
  329. out.write(indentWith);
  330. }
  331. }
  332. // Write element close
  333. out.write("</");
  334. if (namespacePolicy.qualifyElements
  335. || namespacePolicy.qualifyAttributes) {
  336. String prefix =
  337. (String) nsPrefixMap.get(element.getNamespaceURI());
  338. if (prefix != null && !"".equals(prefix)) {
  339. out.write(prefix);
  340. out.write(":");
  341. }
  342. removeNSDefinitions(element);
  343. }
  344. out.write(element.getTagName());
  345. out.write(">");
  346. out.write(lSep);
  347. out.flush();
  348. }
  349. /**
  350. * Escape &lt;, &gt; &amp; &apos;, &quot; as their entities and
  351. * drop characters that are illegal in XML documents.
  352. * @param value the string to encode.
  353. * @return the encoded string.
  354. */
  355. public String encode(String value) {
  356. StringBuffer sb = new StringBuffer();
  357. int len = value.length();
  358. for (int i = 0; i < len; i++) {
  359. char c = value.charAt(i);
  360. switch (c) {
  361. case '<':
  362. sb.append("&lt;");
  363. break;
  364. case '>':
  365. sb.append("&gt;");
  366. break;
  367. case '\'':
  368. sb.append("&apos;");
  369. break;
  370. case '\"':
  371. sb.append("&quot;");
  372. break;
  373. case '&':
  374. int nextSemi = value.indexOf(";", i);
  375. if (nextSemi < 0
  376. || !isReference(value.substring(i, nextSemi + 1))) {
  377. sb.append("&amp;");
  378. } else {
  379. sb.append('&');
  380. }
  381. break;
  382. default:
  383. if (isLegalCharacter(c)) {
  384. sb.append(c);
  385. }
  386. break;
  387. }
  388. }
  389. return sb.substring(0);
  390. }
  391. /**
  392. * Drop characters that are illegal in XML documents.
  393. *
  394. * <p>Also ensure that we are not including an <code>]]&gt;</code>
  395. * marker by replacing that sequence with
  396. * <code>&amp;#x5d;&amp;#x5d;&amp;gt;</code>.</p>
  397. *
  398. * <p>See XML 1.0 2.2 <a
  399. * href="http://www.w3.org/TR/1998/REC-xml-19980210#charsets">http://www.w3.org/TR/1998/REC-xml-19980210#charsets</a> and
  400. * 2.7 <a
  401. * href="http://www.w3.org/TR/1998/REC-xml-19980210#sec-cdata-sect">http://www.w3.org/TR/1998/REC-xml-19980210#sec-cdata-sect</a>.</p>
  402. * @param value the value to be encoded.
  403. * @return the encoded value.
  404. */
  405. public String encodedata(final String value) {
  406. StringBuffer sb = new StringBuffer();
  407. int len = value.length();
  408. for (int i = 0; i < len; ++i) {
  409. char c = value.charAt(i);
  410. if (isLegalCharacter(c)) {
  411. sb.append(c);
  412. }
  413. }
  414. String result = sb.substring(0);
  415. int cdEnd = result.indexOf("]]>");
  416. while (cdEnd != -1) {
  417. sb.setLength(cdEnd);
  418. sb.append("&#x5d;&#x5d;&gt;")
  419. .append(result.substring(cdEnd + 3));
  420. result = sb.substring(0);
  421. cdEnd = result.indexOf("]]>");
  422. }
  423. return result;
  424. }
  425. /**
  426. * Is the given argument a character or entity reference?
  427. * @param ent the value to be checked.
  428. * @return true if it is an entity.
  429. */
  430. public boolean isReference(String ent) {
  431. if (!(ent.charAt(0) == '&') || !ent.endsWith(";")) {
  432. return false;
  433. }
  434. if (ent.charAt(1) == '#') {
  435. if (ent.charAt(2) == 'x') {
  436. try {
  437. Integer.parseInt(ent.substring(3, ent.length() - 1), 16);
  438. return true;
  439. } catch (NumberFormatException nfe) {
  440. return false;
  441. }
  442. } else {
  443. try {
  444. Integer.parseInt(ent.substring(2, ent.length() - 1));
  445. return true;
  446. } catch (NumberFormatException nfe) {
  447. return false;
  448. }
  449. }
  450. }
  451. String name = ent.substring(1, ent.length() - 1);
  452. for (int i = 0; i < knownEntities.length; i++) {
  453. if (name.equals(knownEntities[i])) {
  454. return true;
  455. }
  456. }
  457. return false;
  458. }
  459. /**
  460. * Is the given character allowed inside an XML document?
  461. *
  462. * <p>See XML 1.0 2.2 <a
  463. * href="http://www.w3.org/TR/1998/REC-xml-19980210#charsets">
  464. * http://www.w3.org/TR/1998/REC-xml-19980210#charsets</a>.</p>
  465. * @param c the character to test.
  466. * @return true if the character is allowed.
  467. * @since 1.10, Ant 1.5
  468. */
  469. public boolean isLegalCharacter(char c) {
  470. if (c == 0x9 || c == 0xA || c == 0xD) {
  471. return true;
  472. } else if (c < 0x20) {
  473. return false;
  474. } else if (c <= 0xD7FF) {
  475. return true;
  476. } else if (c < 0xE000) {
  477. return false;
  478. } else if (c <= 0xFFFD) {
  479. return true;
  480. }
  481. return false;
  482. }
  483. private void removeNSDefinitions(Element element) {
  484. ArrayList al = (ArrayList) nsURIByElement.get(element);
  485. if (al != null) {
  486. Iterator iter = al.iterator();
  487. while (iter.hasNext()) {
  488. nsPrefixMap.remove(iter.next());
  489. }
  490. nsURIByElement.remove(element);
  491. }
  492. }
  493. private void addNSDefinition(Element element, String uri) {
  494. ArrayList al = (ArrayList) nsURIByElement.get(element);
  495. if (al == null) {
  496. al = new ArrayList();
  497. nsURIByElement.put(element, al);
  498. }
  499. al.add(uri);
  500. }
  501. }