1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: HTMLSerializingContentHandler.java 2050 2010-12-09 15:31:45Z dmsmith $
21   */
22  package org.crosswire.common.xml;
23  
24  import java.io.Writer;
25  
26  /**
27   * This class provides for the formatted and syntax highlighted serialization of
28   * a SAX stream to a <code>Writer</code>.
29   * 
30   * @see gnu.lgpl.License for license details.<br>
31   *      The copyright to this program is held by it's authors.
32   * @author DM Smith [ dmsmith555 at yahoo dot com]
33   */
34  public class HTMLSerializingContentHandler extends PrettySerializingContentHandler {
35      /**
36       * A formatting serializer that does not add whitespace to the document.
37       * This uses a StringWriter and the toString method will return its content.
38       */
39      public HTMLSerializingContentHandler() {
40          super();
41      }
42  
43      /**
44       * A formatting serializer that adds whitespace to the document according to
45       * the specified <code>FormatType</code>. This uses a StringWriter and the
46       * toString method will return its content.
47       * 
48       * @param theFormat
49       *            the formatting to use
50       */
51      public HTMLSerializingContentHandler(FormatType theFormat) {
52          super(theFormat);
53      }
54  
55      /**
56       * A formatting serializer that adds whitespace to the document according to
57       * the specified <code>FormatType</code>. As the document is serialized it
58       * is written to the provided <code>Writer</code>.
59       * 
60       * @param theFormat
61       *            the formatting to use
62       * @param theWriter
63       *            the writer to use
64       */
65      public HTMLSerializingContentHandler(FormatType theFormat, Writer theWriter) {
66          super(theFormat, theWriter);
67      }
68  
69      @Override
70      protected String decorateTagName(String tagName) {
71          StringBuilder buf = new StringBuilder(50);
72          buf.append("<font class='tag'>");
73          buf.append(super.decorateTagName(tagName));
74          buf.append("</font>");
75          return buf.toString();
76      }
77  
78      /*
79       * (non-Javadoc)
80       * 
81       * @seeorg.crosswire.common.xml.PrettySerializingContentHandler#
82       * decorateAttributeName(java.lang.String)
83       */
84      @Override
85      protected String decorateAttributeName(String attrName) {
86          StringBuilder buf = new StringBuilder(50);
87          buf.append("<font class='attr'>");
88          buf.append(super.decorateAttributeName(attrName));
89          buf.append("</font>");
90          return buf.toString();
91      }
92  
93      /*
94       * (non-Javadoc)
95       * 
96       * @seeorg.crosswire.common.xml.PrettySerializingContentHandler#
97       * decorateAttributeValue(java.lang.String)
98       */
99      @Override
100     protected String decorateAttributeValue(String attrValue) {
101         StringBuilder buf = new StringBuilder(50);
102         buf.append("<font class='value'>");
103         buf.append(super.decorateAttributeValue(attrValue));
104         buf.append("</font>");
105         return buf.toString();
106     }
107 
108     /*
109      * (non-Javadoc)
110      * 
111      * @see
112      * org.crosswire.common.xml.PrettySerializingContentHandler#decorateCharacters
113      * (java.lang.String)
114      */
115     @Override
116     protected String decorateCharacters(String characters) {
117         StringBuilder buf = new StringBuilder(50);
118         buf.append("<font class='text'>");
119         buf.append(XMLUtil.escape(super.decorateCharacters(characters)).replaceAll("\n", "<br>"));
120         buf.append("</font>");
121         return buf.toString();
122     }
123 
124     /*
125      * (non-Javadoc)
126      * 
127      * @see
128      * org.crosswire.common.xml.PrettySerializingContentHandler#decorateIndent
129      * (int)
130      */
131     @Override
132     protected String decorateIndent(int indentLevel) {
133         StringBuilder buf = new StringBuilder(100);
134         buf.append("<font class='indent'>");
135         buf.append(super.decorateIndent(indentLevel).replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;"));
136         buf.append("</font>");
137         return buf.toString();
138     }
139 
140     /*
141      * (non-Javadoc)
142      * 
143      * @see org.xml.sax.ContentHandler#startDocument()
144      */
145     @Override
146     public void startDocument() {
147         // Note: we should be using SPAN here but Sun's Java does not support
148         // styling it.
149         // Also, it introduces whitespace between the span and the text.
150         write("<html><head><style type='text/css'>\nFONT.tag    { font-family:courier new, monospaced; color:#666699; font-weight:bold; }\nFONT.attr   { font-family:courier new, monospaced; color:#669966; font-weight:bold; }\nFONT.value  { font-family:courier new, monospaced; color:#669966; font-style:italic; }\nFONT.indent { }\nFONT.text   { font-family:courier new, monospaced; background:#FFFF99; }\n</style></head><body>\n");
151     }
152 
153     /*
154      * (non-Javadoc)
155      * 
156      * @see org.xml.sax.ContentHandler#endDocument()
157      */
158     @Override
159     public void endDocument() {
160         write("</body></head>");
161     }
162 
163     /*
164      * (non-Javadoc)
165      * 
166      * @see
167      * org.crosswire.common.xml.PrettySerializingContentHandler#getEmptyTagEnd()
168      */
169     @Override
170     protected String getEmptyTagEnd() {
171         return XMLUtil.escape(super.getEmptyTagEnd());
172     }
173 
174     /*
175      * (non-Javadoc)
176      * 
177      * @see
178      * org.crosswire.common.xml.PrettySerializingContentHandler#getEndTagStart()
179      */
180     @Override
181     protected String getEndTagStart() {
182         return XMLUtil.escape(super.getEndTagStart());
183     }
184 
185     /*
186      * (non-Javadoc)
187      * 
188      * @see org.crosswire.common.xml.PrettySerializingContentHandler#getPIEnd()
189      */
190     @Override
191     protected String getPIEnd() {
192         return XMLUtil.escape(super.getPIEnd());
193     }
194 
195     /*
196      * (non-Javadoc)
197      * 
198      * @see
199      * org.crosswire.common.xml.PrettySerializingContentHandler#getPIStart()
200      */
201     @Override
202     protected String getPIStart() {
203         return XMLUtil.escape(super.getPIStart());
204     }
205 
206     /*
207      * (non-Javadoc)
208      * 
209      * @see org.crosswire.common.xml.PrettySerializingContentHandler#getTagEnd()
210      */
211     @Override
212     protected String getTagEnd() {
213         return XMLUtil.escape(super.getTagEnd());
214     }
215 
216     /*
217      * (non-Javadoc)
218      * 
219      * @see
220      * org.crosswire.common.xml.PrettySerializingContentHandler#getTagStart()
221      */
222     @Override
223     protected String getTagStart() {
224         return XMLUtil.escape(super.getTagStart());
225     }
226 
227     /*
228      * (non-Javadoc)
229      * 
230      * @see
231      * org.crosswire.common.xml.PrettySerializingContentHandler#getNewline()
232      */
233     @Override
234     protected String getNewline() {
235         return "<br>";
236     }
237 }
238