PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/src/org/apache/webdav/lib/util/XMLPrinter.java

http://exchangeit.googlecode.com/
Java | 279 lines | 115 code | 27 blank | 137 comment | 27 complexity | 75ff86a35731dc7ca3f4e9196b36ccab MD5 | raw file
  1. /*
  2. * $Header$
  3. * $Revision:207563 $
  4. * $Date:2004-08-03 01:45:51 +1000 (Tue, 03 Aug 2004) $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 1999-2002 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. */
  23. package org.apache.webdav.lib.util;
  24. import java.io.IOException;
  25. import java.io.Writer;
  26. /**
  27. * XMLPrinter helper class.
  28. */
  29. public class XMLPrinter {
  30. // -------------------------------------------------------------- Constants
  31. /**
  32. * Opening tag.
  33. */
  34. public static final int OPENING = 0;
  35. /**
  36. * Closing tag.
  37. */
  38. public static final int CLOSING = 1;
  39. /**
  40. * Element with no content.
  41. */
  42. public static final int NO_CONTENT = 2;
  43. // ----------------------------------------------------- Instance Variables
  44. /**
  45. * Buffer.
  46. */
  47. protected StringBuffer buffer = new StringBuffer();
  48. /**
  49. * Writer.
  50. */
  51. protected Writer writer = null;
  52. // ----------------------------------------------------------- Constructors
  53. /**
  54. * Constructor.
  55. */
  56. public XMLPrinter() {
  57. }
  58. /**
  59. * Constructor.
  60. */
  61. public XMLPrinter(Writer writer) {
  62. this.writer = writer;
  63. }
  64. // --------------------------------------------------------- Public Methods
  65. /**
  66. * Retrieve generated XML.
  67. *
  68. * @return String containing the generated XML
  69. */
  70. public String toString() {
  71. return buffer.toString();
  72. }
  73. /**
  74. * Write property to the XML.
  75. *
  76. * @param namespace
  77. * Namespace
  78. * @param namespaceInfo
  79. * Namespace info
  80. * @param name
  81. * Property name
  82. * @param value
  83. * Property value
  84. */
  85. public void writeProperty(String namespace, String namespaceInfo,
  86. String name, String value, boolean cdata) {
  87. writeElement(namespace, namespaceInfo, name, OPENING);
  88. if (cdata)
  89. writeData(value);
  90. else
  91. writeText(value);
  92. writeElement(namespace, namespaceInfo, name, CLOSING);
  93. }
  94. /**
  95. * Write property to the XML.
  96. *
  97. * @param namespace
  98. * Namespace
  99. * @param namespaceInfo
  100. * Namespace info
  101. * @param name
  102. * Property name
  103. * @param value
  104. * Property value
  105. */
  106. public void writeProperty(String namespace, String namespaceInfo,
  107. String name, String value) {
  108. writeProperty(namespace, namespaceInfo, name, value, false);
  109. }
  110. /**
  111. * Write property to the XML.
  112. *
  113. * @param namespace
  114. * Namespace
  115. * @param name
  116. * Property name
  117. * @param value
  118. * Property value
  119. */
  120. public void writeProperty(String namespace, String name, String value) {
  121. writeProperty(namespace, null, name, value);
  122. }
  123. /**
  124. * Write property to the XML.
  125. *
  126. * @param namespace
  127. * Namespace
  128. * @param name
  129. * Property name
  130. */
  131. public void writeProperty(String namespace, String name) {
  132. writeElement(namespace, name, NO_CONTENT);
  133. }
  134. /**
  135. * Write an element.
  136. *
  137. * @param name
  138. * Element name
  139. * @param namespace
  140. * Namespace abbreviation
  141. * @param type
  142. * Element type
  143. */
  144. public void writeElement(String namespace, String name, int type) {
  145. writeElement(namespace, null, name, type);
  146. }
  147. public void writeElement(String namespace, String namespaceInfo,
  148. String name, int type) {
  149. writeElement(namespace, namespaceInfo, name, type, null);
  150. }
  151. /**
  152. * Write an element.
  153. *
  154. * @param namespace
  155. * Namespace abbreviation
  156. * @param namespaceInfo
  157. * Namespace info
  158. * @param name
  159. * Element name
  160. * @param type
  161. * Element type
  162. */
  163. public void writeElement(String namespace, String namespaceInfo,
  164. String name, int type, String definitionNamespace) {
  165. if (definitionNamespace == null)
  166. definitionNamespace = namespace;
  167. if ((namespace != null) && (namespace.length() > 0)) {
  168. switch (type) {
  169. case OPENING:
  170. if ((namespaceInfo != null) && (namespaceInfo.length() > 0)) {
  171. buffer.append("<" + namespace + ":" + name + " xmlns:"
  172. + definitionNamespace + "=\"" + namespaceInfo
  173. + "\">");
  174. } else {
  175. buffer.append("<" + namespace + ":" + name + ">");
  176. }
  177. break;
  178. case CLOSING:
  179. buffer.append("</" + namespace + ":" + name + ">");
  180. break;
  181. case NO_CONTENT:
  182. default:
  183. if ((namespaceInfo != null) && (namespaceInfo.length() > 0)) {
  184. buffer.append("<" + namespace + ":" + name + " xmlns:"
  185. + definitionNamespace + "=\"" + namespaceInfo
  186. + "\"/>");
  187. } else {
  188. buffer.append("<" + namespace + ":" + name + "/>");
  189. }
  190. break;
  191. }
  192. } else {
  193. switch (type) {
  194. case OPENING:
  195. if ((namespaceInfo != null) && (namespaceInfo.length() > 0)) {
  196. buffer.append("<" + name + " xmlns=\"" + namespaceInfo
  197. + "\">");
  198. } else {
  199. buffer.append("<" + name + ">");
  200. }
  201. break;
  202. case CLOSING:
  203. buffer.append("</" + name + ">");
  204. break;
  205. case NO_CONTENT:
  206. default:
  207. if ((namespaceInfo != null) && (namespaceInfo.length() > 0)) {
  208. buffer.append("<" + name + " xmlns=\"" + namespaceInfo
  209. + "\"/>");
  210. } else {
  211. buffer.append("<" + name + "/>");
  212. }
  213. break;
  214. }
  215. }
  216. }
  217. /**
  218. * Write text.
  219. *
  220. * @param text
  221. * Text to append
  222. */
  223. public void writeText(String text) {
  224. buffer.append(text);
  225. }
  226. /**
  227. * Write data.
  228. *
  229. * @param data
  230. * Data to append
  231. */
  232. public void writeData(String data) {
  233. buffer.append("<![CDATA[");
  234. buffer.append(data);
  235. buffer.append("]]>");
  236. }
  237. /**
  238. * Write XML Header.
  239. */
  240. public void writeXMLHeader() {
  241. buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
  242. }
  243. /**
  244. * Send data and reinitializes buffer.
  245. */
  246. public void sendData() throws IOException {
  247. if (writer != null) {
  248. writer.write(buffer.toString());
  249. buffer = new StringBuffer();
  250. }
  251. }
  252. }