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.

EchoXML.java 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.io.File;
  20. import java.io.OutputStream;
  21. import java.io.FileOutputStream;
  22. import org.apache.tools.ant.Project;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.types.EnumeratedAttribute;
  25. import org.apache.tools.ant.util.XMLFragment;
  26. import org.apache.tools.ant.util.DOMElementWriter;
  27. import org.apache.tools.ant.util.FileUtils;
  28. import org.w3c.dom.Node;
  29. import org.w3c.dom.Element;
  30. /**
  31. * Echo XML.
  32. *
  33. * Known limitations:
  34. * <ol>
  35. * <li>Processing Instructions get ignored</li>
  36. * <li>Encoding is always UTF-8</li>
  37. * </ol>
  38. *
  39. * @since Ant 1.7
  40. */
  41. public class EchoXML extends XMLFragment {
  42. private File file;
  43. private boolean append;
  44. private NamespacePolicy namespacePolicy = NamespacePolicy.DEFAULT;
  45. private static final String ERROR_NO_XML = "No nested XML specified";
  46. /**
  47. * Set the output file.
  48. * @param f the output file.
  49. */
  50. public void setFile(File f) {
  51. file = f;
  52. }
  53. /**
  54. * Set the namespace policy for the xml file
  55. * @param s namespace policy: "ignore," "elementsOnly," or "all"
  56. * @see
  57. * org.apache.tools.ant.util.DOMElementWriter.XmlNamespacePolicy
  58. */
  59. public void setNamespacePolicy(NamespacePolicy n) {
  60. namespacePolicy = n;
  61. }
  62. /**
  63. * Set whether to append the output file.
  64. * @param b boolean append flag.
  65. */
  66. public void setAppend(boolean b) {
  67. append = b;
  68. }
  69. /**
  70. * Execute the task.
  71. */
  72. public void execute() {
  73. DOMElementWriter writer =
  74. new DOMElementWriter(!append, namespacePolicy.getPolicy());
  75. OutputStream os = null;
  76. try {
  77. if (file != null) {
  78. os = new FileOutputStream(file.getAbsolutePath(), append);
  79. } else {
  80. os = new LogOutputStream(this, Project.MSG_INFO);
  81. }
  82. Node n = getFragment().getFirstChild();
  83. if (n == null) {
  84. throw new BuildException(ERROR_NO_XML);
  85. }
  86. writer.write((Element) n, os);
  87. } catch (BuildException e) {
  88. throw e;
  89. } catch (Exception e) {
  90. throw new BuildException(e);
  91. } finally {
  92. FileUtils.close(os);
  93. }
  94. }
  95. public static class NamespacePolicy extends EnumeratedAttribute {
  96. private static final String IGNORE = "ignore";
  97. private static final String ELEMENTS = "elementsOnly";
  98. private static final String ALL = "all";
  99. public static final NamespacePolicy DEFAULT
  100. = new NamespacePolicy(IGNORE);
  101. public NamespacePolicy() {}
  102. public NamespacePolicy(String s) {
  103. setValue(s);
  104. }
  105. /** {@inheritDoc}. */
  106. public String[] getValues() {
  107. return new String[] {IGNORE, ELEMENTS, ALL};
  108. }
  109. public DOMElementWriter.XmlNamespacePolicy getPolicy() {
  110. String s = getValue();
  111. if (IGNORE.equalsIgnoreCase(s)) {
  112. return DOMElementWriter.XmlNamespacePolicy.IGNORE;
  113. } else if (ELEMENTS.equalsIgnoreCase(s)) {
  114. return
  115. DOMElementWriter.XmlNamespacePolicy.ONLY_QUALIFY_ELEMENTS;
  116. } else if (ALL.equalsIgnoreCase(s)) {
  117. return DOMElementWriter.XmlNamespacePolicy.QUALIFY_ALL;
  118. } else {
  119. throw new BuildException("Invalid namespace policy: " + s);
  120. }
  121. }
  122. }
  123. }