001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: HTMLWriter.java,v 1.9 2002/02/14 11:55:46 jstrachan Exp $ 008 */ 009 010package org.dom4j.io; 011 012import java.io.IOException; 013import java.io.OutputStream; 014import java.io.UnsupportedEncodingException; 015import java.io.Writer; 016import java.util.HashSet; 017import java.util.Set; 018 019import org.dom4j.CDATA; 020import org.dom4j.Document; 021import org.dom4j.Element; 022import org.dom4j.Entity; 023import org.dom4j.Node; 024 025import org.xml.sax.SAXException; 026 027/** <p><code>HTMLWriter</code> takes a DOM4J tree and formats it to a 028 * stream as HTML. 029 * This formatter is similar to XMLWriter but outputs the text of CDATA 030 * and Entity sections rather than the serialised format as in XML and 031 * also supports certain element which have no corresponding close tag such 032 * as for <BR> and <P>. 033 * 034 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> (james.strachan@metastuff.com) 035 * @version $Revision: 1.9 $ 036 */ 037public class HTMLWriter extends XMLWriter { 038 039 protected static final OutputFormat defaultHtmlFormat; 040 041 static { 042 defaultHtmlFormat = new OutputFormat( " ", true ); 043 defaultHtmlFormat.setTrimText( true ); 044 defaultHtmlFormat.setSuppressDeclaration( true ); 045 } 046 047 /** Used to store the qualified element names which 048 * should have no close element tag 049 */ 050 private Set omitElementCloseSet; 051 052 053 public HTMLWriter(Writer writer) { 054 super( writer, defaultHtmlFormat ); 055 } 056 057 public HTMLWriter(Writer writer, OutputFormat format) { 058 super( writer, format ); 059 } 060 061 public HTMLWriter() throws UnsupportedEncodingException { 062 super( defaultHtmlFormat ); 063 } 064 065 public HTMLWriter(OutputFormat format) throws UnsupportedEncodingException { 066 super( format ); 067 } 068 069 public HTMLWriter(OutputStream out) throws UnsupportedEncodingException { 070 super( out, defaultHtmlFormat ); 071 } 072 073 public HTMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException { 074 super( out, format ); 075 } 076 077 078 public void startCDATA() throws SAXException { 079 } 080 081 public void endCDATA() throws SAXException { 082 } 083 084 085 // Overloaded methods 086 087 protected void writeCDATA(String text) throws IOException { 088 // XXX: Should we escape entities? 089 // writer.write( escapeElementEntities( text ) ); 090 writer.write( text ); 091 lastOutputNodeType = Node.CDATA_SECTION_NODE; 092 } 093 094 protected void writeEntity(Entity entity) throws IOException { 095 writer.write(entity.getText()); 096 lastOutputNodeType = Node.ENTITY_REFERENCE_NODE; 097 } 098 099 protected void writeDeclaration() throws IOException { 100 } 101 102 103 104 /** Overriden method to not close certain element names to avoid 105 * wierd behaviour from browsers for versions up to 5.x 106 */ 107 protected void writeClose(String qualifiedName) throws IOException { 108 if ( ! omitElementClose( qualifiedName ) ) { 109 super.writeClose(qualifiedName); 110 } 111 } 112 113 protected void writeEmptyElementClose(String qualifiedName) throws IOException { 114 if ( ! getOmitElementCloseSet().contains( qualifiedName.toUpperCase() ) ) { 115 super.writeEmptyElementClose(qualifiedName); 116 } 117 else { 118 writer.write(">"); 119 } 120 } 121 122 protected boolean omitElementClose( String qualifiedName ) { 123 return getOmitElementCloseSet().contains( qualifiedName.toUpperCase() ); 124 } 125 126 protected Set getOmitElementCloseSet() { 127 if (omitElementCloseSet == null) { 128 omitElementCloseSet = new HashSet(); 129 loadOmitElementCloseSet(omitElementCloseSet); 130 } 131 return omitElementCloseSet; 132 } 133 134 protected void loadOmitElementCloseSet(Set set) { 135 set.add( "AREA" ); 136 set.add( "BASE" ); 137 set.add( "BR" ); 138 set.add( "COL" ); 139 set.add( "HR" ); 140 set.add( "IMG" ); 141 set.add( "INPUT" ); 142 set.add( "LINK" ); 143 set.add( "META" ); 144 set.add( "P" ); 145 set.add( "PARAM" ); 146 } 147 148 protected String getPadText() { 149 return " "; 150 } 151 152 protected boolean isExpandEmptyElements() { 153 return true; 154 } 155 156} 157 158 159 160 161/* 162 * Redistribution and use of this software and associated documentation 163 * ("Software"), with or without modification, are permitted provided 164 * that the following conditions are met: 165 * 166 * 1. Redistributions of source code must retain copyright 167 * statements and notices. Redistributions must also contain a 168 * copy of this document. 169 * 170 * 2. Redistributions in binary form must reproduce the 171 * above copyright notice, this list of conditions and the 172 * following disclaimer in the documentation and/or other 173 * materials provided with the distribution. 174 * 175 * 3. The name "DOM4J" must not be used to endorse or promote 176 * products derived from this Software without prior written 177 * permission of MetaStuff, Ltd. For written permission, 178 * please contact dom4j-info@metastuff.com. 179 * 180 * 4. Products derived from this Software may not be called "DOM4J" 181 * nor may "DOM4J" appear in their names without prior written 182 * permission of MetaStuff, Ltd. DOM4J is a registered 183 * trademark of MetaStuff, Ltd. 184 * 185 * 5. Due credit should be given to the DOM4J Project 186 * (http://dom4j.org/). 187 * 188 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 189 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 190 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 191 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 192 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 193 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 194 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 195 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 196 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 197 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 198 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 199 * OF THE POSSIBILITY OF SUCH DAMAGE. 200 * 201 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 202 * 203 * $Id: HTMLWriter.java,v 1.9 2002/02/14 11:55:46 jstrachan Exp $ 204 */