1. /*
  2. * $Id: XmlWriteContext.java,v 1.1.1.1 2000/11/23 01:53:34 edwingo Exp $
  3. *
  4. * The Apache Software License, Version 1.1
  5. *
  6. *
  7. * Copyright (c) 2000 The Apache Software Foundation. All rights
  8. * reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. The end-user documentation included with the redistribution,
  23. * if any, must include the following acknowledgment:
  24. * "This product includes software developed by the
  25. * Apache Software Foundation (http://www.apache.org/)."
  26. * Alternately, this acknowledgment may appear in the software itself,
  27. * if and wherever such third-party acknowledgments normally appear.
  28. *
  29. * 4. The names "Crimson" and "Apache Software Foundation" must
  30. * not be used to endorse or promote products derived from this
  31. * software without prior written permission. For written
  32. * permission, please contact apache@apache.org.
  33. *
  34. * 5. Products derived from this software may not be called "Apache",
  35. * nor may "Apache" appear in their name, without prior written
  36. * permission of the Apache Software Foundation.
  37. *
  38. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  39. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  40. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  45. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  46. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  47. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  48. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  49. * SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This software consists of voluntary contributions made by many
  53. * individuals on behalf of the Apache Software Foundation and was
  54. * originally based on software copyright (c) 1999, Sun Microsystems, Inc.,
  55. * http://www.sun.com. For more information on the Apache Software
  56. * Foundation, please see <http://www.apache.org/>.
  57. */
  58. package org.apache.crimson.tree;
  59. import java.io.IOException;
  60. import java.io.Writer;
  61. /**
  62. * This captures context used when writing XML text, such as state
  63. * used to "pretty print" output or to identify entities which are
  64. * defined. Pretty printing is useful when displaying structure in
  65. * XML documents that need to be read or edited by people (rather
  66. * than only by machines).
  67. *
  68. * @see XmlWritable
  69. * @see XmlDocument#createWriteContext
  70. *
  71. * @author David Brownell
  72. * @version $Revision: 1.1.1.1 $
  73. */
  74. public class XmlWriteContext
  75. {
  76. private Writer writer;
  77. private int indentLevel;
  78. private boolean prettyOutput;
  79. /**
  80. * Constructs a write context that doesn't pretty-print output.
  81. */
  82. public XmlWriteContext (Writer out)
  83. {
  84. writer = out;
  85. }
  86. /**
  87. * Constructs a write context that supports pretty-printing
  88. * output starting at the specified number of spaces.
  89. */
  90. public XmlWriteContext (Writer out, int level)
  91. {
  92. writer = out;
  93. prettyOutput = true;
  94. indentLevel = level;
  95. }
  96. /**
  97. * Returns the writer to which output should be written.
  98. */
  99. public Writer getWriter ()
  100. {
  101. return writer;
  102. }
  103. /**
  104. * Returns true if the specified entity was already declared
  105. * in this output context, so that entity references may be
  106. * written rather than their expanded values. The predefined
  107. * XML entities are always declared.
  108. */
  109. public boolean isEntityDeclared (String name)
  110. {
  111. // for contexts tied to documents with DTDs,
  112. // ask that DTD if it knows that entity...
  113. return ("amp".equals (name)
  114. || "lt".equals (name) || "gt".equals (name)
  115. || "quot".equals (name) || "apos".equals (name));
  116. }
  117. /**
  118. * Returns the current indent level, in terms of spaces, for
  119. * use in pretty printing XML text.
  120. */
  121. public int getIndentLevel ()
  122. {
  123. return indentLevel;
  124. }
  125. /**
  126. * Assigns the current indent level, in terms of spaces, for
  127. * use in pretty printing XML text.
  128. */
  129. public void setIndentLevel (int level)
  130. {
  131. indentLevel = level;
  132. }
  133. /**
  134. * If pretty printing is enabled, this writes a newline followed by
  135. * <em>indentLevel</em> spaces. At the beginning of a line, groups
  136. * of eight consecutive spaces are replaced by tab characters, for
  137. * storage efficiency.
  138. *
  139. * <P> Note that this method should not be used except in cases
  140. * where the additional whitespace is guaranteed to be semantically
  141. * meaningless. This is the default, and is controlled through the
  142. * <em>xml:space</em> attribute, inherited from parent elements.
  143. * When this attribute value is <em>preserve</em>, this method should
  144. * not be used. Otherwise, text normalization is expected to remove
  145. * excess whitespace such as that added by this call.
  146. */
  147. public void printIndent () throws IOException
  148. {
  149. int temp = indentLevel;
  150. if (!prettyOutput)
  151. return;
  152. writer.write(XmlDocument.eol);
  153. while (temp-- > 0) {
  154. writer.write (' ');
  155. }
  156. }
  157. /**
  158. * Returns true if writes using the context should "pretty print",
  159. * displaying structure through indentation as appropriate.
  160. */
  161. public boolean isPrettyOutput ()
  162. {
  163. return prettyOutput;
  164. }
  165. }