|
HtmlOutputter |
|
/*
** Luxor - XML User Interface Language (XUL) Toolkit
** Copyright (c) 2001, 2002 by Gerald Bauer
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/
package luxor.util;
import java.io.*;
import java.util.*;
import org.jdom.*;
import luxor.core.*;
public class HtmlOutputter
{
/**
* convenience method for outputContent; returns String directly
*/
public static String getContent( List content )
{
try
{
StringWriter buf = new StringWriter();
outputContent( content, buf );
return buf.toString();
}
catch( IOException ioex )
{
Xul.error( "failed to get element's content: " + ioex.toString() );
return "";
}
}
/**
* print out an element's content only, not including its tag and
* attributes.
*/
public static void outputContent( List content, Writer out )
throws IOException
{
// Loop over content/children
Iterator it = content.iterator();
while( it.hasNext() )
{
Object obj = it.next();
// See if text, an element, a PI or a comment
if( obj instanceof Comment )
continue;
// ignore comments
else if( obj instanceof String )
out.write( ( String ) obj );
else if( obj instanceof Element )
outputElement( ( Element ) obj, out );
else if( obj instanceof EntityRef )
continue;
// not supported yet
else if( obj instanceof ProcessingInstruction )
continue;
// not supported yet
else if( obj instanceof CDATA )
continue;
// not supported yet
}
// Unsupported types are *not* printed, nor should they exist
}
private static void outputAttributes( List attributes, Writer out )
throws IOException
{
Iterator it = attributes.iterator();
while( it.hasNext() )
{
Attribute attribute = ( Attribute ) it.next();
out.write( " " );
out.write( attribute.getName() );
out.write( "=" );
// fix: add support for entities (aka escapce sequences)
out.write( "\"" );
out.write( attribute.getValue() );
out.write( "\"" );
}
}
private static void outputElement( Element element, Writer out )
throws IOException
{
String name = element.getName();
out.write( "<" + name );
outputAttributes( element.getAttributes(), out );
out.write( ">" );
outputContent( element.getContent(), out );
// convert xhtml to html
// - we don't want a closing tag for hr, br, etc.
if( name.equals( "hr" )
|| name.equals( "br" ) )
return;
out.write( "</" + name + ">" );
}
}
|
HtmlOutputter |
|