XMLOutputFactoryImpl.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* XMLOutputFactoryImpl.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.stream;
  32. import java.io.OutputStream;
  33. import java.io.OutputStreamWriter;
  34. import java.io.Writer;
  35. import java.io.UnsupportedEncodingException;
  36. import javax.xml.transform.Result;
  37. import javax.xml.transform.stream.StreamResult;
  38. import javax.xml.stream.XMLEventWriter;
  39. import javax.xml.stream.XMLOutputFactory;
  40. import javax.xml.stream.XMLStreamException;
  41. import javax.xml.stream.XMLStreamWriter;
  42. /**
  43. * Standard output factory.
  44. *
  45. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  46. */
  47. public class XMLOutputFactoryImpl
  48. extends XMLOutputFactory
  49. {
  50. protected boolean prefixDefaulting = false;
  51. public XMLOutputFactoryImpl()
  52. {
  53. }
  54. public XMLStreamWriter createXMLStreamWriter(Writer stream)
  55. throws XMLStreamException
  56. {
  57. // XXX try to determine character encoding of writer?
  58. return new XMLStreamWriterImpl(stream, null, prefixDefaulting);
  59. }
  60. public XMLStreamWriter createXMLStreamWriter(OutputStream stream)
  61. throws XMLStreamException
  62. {
  63. return createXMLStreamWriter(stream, "UTF-8");
  64. }
  65. public XMLStreamWriter createXMLStreamWriter(OutputStream stream,
  66. String encoding)
  67. throws XMLStreamException
  68. {
  69. if (encoding == null)
  70. encoding = "UTF-8";
  71. try
  72. {
  73. Writer writer = new OutputStreamWriter(stream, encoding);
  74. return new XMLStreamWriterImpl(writer, encoding, prefixDefaulting);
  75. }
  76. catch (UnsupportedEncodingException e)
  77. {
  78. XMLStreamException e2 = new XMLStreamException(e);
  79. e2.initCause(e);
  80. throw e2;
  81. }
  82. }
  83. public XMLStreamWriter createXMLStreamWriter(Result result)
  84. throws XMLStreamException
  85. {
  86. if (result instanceof StreamResult)
  87. {
  88. StreamResult sr = (StreamResult) result;
  89. OutputStream out = sr.getOutputStream();
  90. if (out != null)
  91. return createXMLStreamWriter(out);
  92. Writer writer = sr.getWriter();
  93. if (writer != null)
  94. return createXMLStreamWriter(writer);
  95. }
  96. throw new UnsupportedOperationException();
  97. }
  98. public XMLEventWriter createXMLEventWriter(OutputStream stream)
  99. throws XMLStreamException
  100. {
  101. XMLStreamWriter writer = createXMLStreamWriter(stream);
  102. return new XMLEventWriterImpl(writer);
  103. }
  104. public XMLEventWriter createXMLEventWriter(OutputStream stream,
  105. String encoding)
  106. throws XMLStreamException
  107. {
  108. XMLStreamWriter writer = createXMLStreamWriter(stream, encoding);
  109. return new XMLEventWriterImpl(writer);
  110. }
  111. public XMLEventWriter createXMLEventWriter(Writer stream)
  112. throws XMLStreamException
  113. {
  114. XMLStreamWriter writer = createXMLStreamWriter(stream);
  115. return new XMLEventWriterImpl(writer);
  116. }
  117. public XMLEventWriter createXMLEventWriter(Result result)
  118. throws XMLStreamException
  119. {
  120. if (result instanceof StreamResult)
  121. {
  122. StreamResult sr = (StreamResult) result;
  123. OutputStream out = sr.getOutputStream();
  124. if (out != null)
  125. return createXMLEventWriter(out);
  126. Writer writer = sr.getWriter();
  127. if (writer != null)
  128. return createXMLEventWriter(writer);
  129. }
  130. throw new UnsupportedOperationException();
  131. }
  132. public void setProperty(String name, Object value)
  133. throws IllegalArgumentException
  134. {
  135. if (IS_REPAIRING_NAMESPACES.equals(name))
  136. prefixDefaulting = ((Boolean) value).booleanValue();
  137. else
  138. throw new IllegalArgumentException(name);
  139. }
  140. public Object getProperty(String name)
  141. throws IllegalArgumentException
  142. {
  143. if (IS_REPAIRING_NAMESPACES.equals(name))
  144. return new Boolean(prefixDefaulting);
  145. throw new IllegalArgumentException(name);
  146. }
  147. public boolean isPropertySupported(String name)
  148. {
  149. if (IS_REPAIRING_NAMESPACES.equals(name))
  150. return true;
  151. return false;
  152. }
  153. public boolean isPrefixDefaulting()
  154. {
  155. return prefixDefaulting;
  156. }
  157. public void setPrefixDefaulting(boolean value)
  158. {
  159. prefixDefaulting = value;
  160. }
  161. }