| 1 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 35 public static String 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 54 public static void outputContent( List content, Writer out ) 55 throws IOException 56 { 57 59 Iterator it = content.iterator(); 60 while( it.hasNext() ) 61 { 62 Object obj = it.next(); 63 if( obj instanceof Comment ) 65 continue; 66 else if( obj instanceof String ) 68 out.write( ( String ) obj ); 69 else if( obj instanceof Element ) 70 outputElement( ( Element ) obj, out ); 71 else if( obj instanceof EntityRef ) 72 continue; 73 else if( obj instanceof ProcessingInstruction ) 75 continue; 76 else if( obj instanceof CDATA ) 78 continue; 79 } 81 } 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 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 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 if( name.equals( "hr" ) 119 || name.equals( "br" ) ) 120 return; 121 122 out.write( "</" + name + ">" ); 123 } 124 125 } 126 | Popular Tags |