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.

XMLFragment.java 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2003-2004 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 org.w3c.dom.Document;
  19. import org.w3c.dom.DocumentFragment;
  20. import org.w3c.dom.Element;
  21. import org.w3c.dom.Node;
  22. import org.w3c.dom.Text;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.DynamicConfiguratorNS;
  25. import org.apache.tools.ant.ProjectHelper;
  26. /**
  27. * Use this class as a nested element if you want to get a literal DOM
  28. * fragment of something nested into your task/type.
  29. *
  30. * <p>This is useful for tasks that want to deal with the "real" XML
  31. * from the build file instead of objects.</p>
  32. *
  33. * <p>Code heavily influenced by code written by Dominique Devienne.</p>
  34. *
  35. * @since Ant 1.7
  36. */
  37. public class XMLFragment implements DynamicConfiguratorNS {
  38. private Document doc;
  39. private DocumentFragment fragment;
  40. public XMLFragment() {
  41. doc = JAXPUtils.getDocumentBuilder().newDocument();
  42. fragment = doc.createDocumentFragment();
  43. }
  44. /**
  45. * Return the DocumentFragment that corresponds to the nested
  46. * structure.
  47. */
  48. public DocumentFragment getFragment() {
  49. return fragment;
  50. }
  51. /**
  52. * Add nested text.
  53. */
  54. public void addText(String s) {
  55. addText(fragment, s);
  56. }
  57. /**
  58. * No attributes for the wrapping element.
  59. */
  60. public void setDynamicAttribute(String uri, String name, String qName, String value)
  61. throws BuildException {
  62. throw new BuildException("Attribute " + name + " is not supported.");
  63. }
  64. /**
  65. * Creates a nested element.
  66. */
  67. public Object createDynamicElement(String uri, String name, String qName) {
  68. Element e = doc.createElementNS(uri, qName);
  69. fragment.appendChild(e);
  70. return new Child(e);
  71. }
  72. private void addText(Node n, String s) {
  73. if (s != null && !s.trim().equals("")) {
  74. Text t = doc.createTextNode(s);
  75. n.appendChild(t);
  76. }
  77. }
  78. public class Child implements DynamicConfiguratorNS {
  79. private Element e;
  80. Child(Element e) {
  81. this.e = e;
  82. }
  83. /**
  84. * Add nested text.
  85. */
  86. public void addText(String s) {
  87. XMLFragment.this.addText(e, s);
  88. }
  89. /**
  90. * Sets the attribute
  91. */
  92. public void setDynamicAttribute(
  93. String uri, String name, String qName, String value) {
  94. if (uri.equals("")) {
  95. e.setAttribute(name, value);
  96. } else {
  97. e.setAttributeNS(uri, qName, value);
  98. }
  99. }
  100. /**
  101. * Creates a nested element.
  102. */
  103. public Object createDynamicElement(String uri, String name, String qName) {
  104. Element e2 = null;
  105. if (uri.equals("")) {
  106. e2 = doc.createElement(name);
  107. } else {
  108. e2 = doc.createElementNS(uri, qName);
  109. }
  110. e.appendChild(e2);
  111. return new Child(e2);
  112. }
  113. }
  114. }