KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > caramel > util > HtmlOutputter


1 /*
2 ** Caramel - Non-GUI Java Addons
3 ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** Lesser General Public License as published by the Free Software Foundation.
9 ** Version 2.1 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package caramel.util;
24
25 import java.io.*;
26 import java.util.*;
27 import org.jdom.*;
28 import houston.*;
29
30 public class HtmlOutputter
31 {
32    /**
33     * convenience method for outputContent; returns String directly
34     */

35    public static String JavaDoc getContent( List content )
36    {
37       try
38       {
39          StringWriter buf = new StringWriter();
40          outputContent( content, buf );
41          return buf.toString();
42       }
43       catch( IOException ioex )
44       {
45          Status.error( "*** failed to get element's content: " + ioex.toString() );
46          return "";
47       }
48    }
49
50    /**
51     * print out an element's content only, not including its tag and
52     * attributes.
53     */

54    public static void outputContent( List content, Writer out )
55           throws IOException
56    {
57       // Loop over content/children
58

59       Iterator it = content.iterator();
60       while( it.hasNext() )
61       {
62          Object JavaDoc obj = it.next();
63          // See if text, an element, a PI or a comment
64
if( obj instanceof Comment )
65             continue;
66          // ignore comments
67
else if( obj instanceof String JavaDoc )
68             out.write( ( String JavaDoc ) obj );
69          else if( obj instanceof Element )
70             outputElement( ( Element ) obj, out );
71          else if( obj instanceof EntityRef )
72             continue;
73          // not supported yet
74
else if( obj instanceof ProcessingInstruction )
75             continue;
76          // not supported yet
77
else if( obj instanceof CDATA )
78             continue;
79          // not supported yet
80
}
81       // Unsupported types are *not* printed, nor should they exist
82
}
83
84    private static void outputAttributes( List attributes, Writer out )
85           throws IOException
86    {
87       Iterator it = attributes.iterator();
88       while( it.hasNext() )
89       {
90          Attribute attribute = ( Attribute ) it.next();
91
92          out.write( " " );
93          out.write( attribute.getName() );
94          out.write( "=" );
95
96          // fix: add support for entities (aka escapce sequences)
97
out.write( "\"" );
98          out.write( attribute.getValue() );
99          out.write( "\"" );
100       }
101    }
102
103    private static void outputElement( Element element, Writer out )
104           throws IOException
105    {
106       String JavaDoc name = element.getName();
107
108       out.write( "<" + name );
109
110       outputAttributes( element.getAttributes(), out );
111
112       out.write( ">" );
113
114       outputContent( element.getContent(), out );
115
116       // convert xhtml to html
117
// - we don't want a closing tag for hr, br, etc.
118
if( name.equals( "hr" )
119              || name.equals( "br" ) )
120          return;
121
122       out.write( "</" + name + ">" );
123    }
124
125 }
126
Popular Tags