1 16 package org.apache.cocoon.components.serializers; 17 18 import org.apache.cocoon.components.serializers.encoding.HTMLEncoder; 19 import org.apache.cocoon.components.serializers.util.DocType; 20 import org.apache.cocoon.components.serializers.util.SGMLDocType; 21 import org.xml.sax.SAXException ; 22 23 24 29 public class HTMLSerializer extends XHTMLSerializer { 30 31 32 public static final DocType HTML401_DOCTYPE_COMPATIBLE = new SGMLDocType( 33 "HTML", "-//W3C//DTD HTML 4.01 Transitional//EN", null); 34 35 36 public static final DocType HTML401_DOCTYPE_STRICT = new SGMLDocType( 37 "HTML", "-//W3C//DTD HTML 4.01//EN", 38 "http://www.w3.org/TR/html4/strict.dtd"); 39 40 41 public static final DocType HTML401_DOCTYPE_TRANSITIONAL = new SGMLDocType( 42 "HTML", "-//W3C//DTD HTML 4.01 Transitional//EN", 43 "http://www.w3.org/TR/html4/loose.dtd"); 44 45 46 public static final DocType HTML401_DOCTYPE_FRAMESET = new SGMLDocType( 47 "HTML", "-//W3C//DTD HTML 4.01 Frameset//EN", 48 "http://www.w3.org/TR/html4/frameset.dtd"); 49 50 51 52 private static final HTMLEncoder HTML_ENCODER = new HTMLEncoder(); 53 54 57 public HTMLSerializer() { 58 super(HTML_ENCODER); 59 } 60 61 62 63 64 private static final String NAMESPACES[][] = new String [0][0]; 65 66 67 private boolean checkNamespace(String nsuri) { 68 if (nsuri.length() == 0) return(true); 69 if (XHTML1_NAMESPACE.equals(nsuri)) return(true); 70 return(false); 71 } 72 73 74 75 81 public void head() 82 throws SAXException { 83 } 85 86 93 public void body(String nsuri, String local, String qual) 94 throws SAXException { 95 String name = local.toUpperCase(); 96 if (! this.checkNamespace(nsuri)) { 97 throw new SAXException ("Unsupported namespace \"" + nsuri + "\" " 98 + "for HTML root element \"" + qual + "\"" 99 + this.getLocation()); 100 } 101 102 if (this.doctype == null) { 103 this.doctype = HTML401_DOCTYPE_COMPATIBLE; 104 } else if (XHTML1_DOCTYPE_STRICT.equals(this.doctype)) { 105 this.doctype = HTML401_DOCTYPE_STRICT; 106 } else if (XHTML1_DOCTYPE_TRANSITIONAL.equals(this.doctype)) { 107 this.doctype = HTML401_DOCTYPE_TRANSITIONAL; 108 } else if (XHTML1_DOCTYPE_FRAMESET.equals(this.doctype)) { 109 this.doctype = HTML401_DOCTYPE_FRAMESET; 110 } else { 111 this.doctype = new DocType(this.doctype.getName().toUpperCase(), 112 this.doctype.getPublicId(), 113 this.doctype.getSystemId()); 114 } 115 super.body(XHTML1_NAMESPACE, name, name); 116 } 117 118 119 122 public void startElementImpl(String nsuri, String local, String qual, 123 String namespaces[][], String attributes[][]) 124 throws SAXException { 125 String name = local.toUpperCase(); 126 if (! this.checkNamespace(nsuri)) { 127 throw new SAXException ("Unsupported namespace \"" + nsuri + "\" " 128 + "for HTML element \"" + qual + "\"" 129 + this.getLocation()); 130 } 131 132 int length = 0; 133 for (int x = 0; x < attributes.length; x ++) { 134 if (checkNamespace(attributes[x][ATTRIBUTE_NSURI])) length ++; 135 } 136 137 String at[][] = new String [length][ATTRIBUTE_LENGTH]; 138 length = 0; 139 for (int x = 0; x < attributes.length; x ++) { 140 if (!checkNamespace(attributes[x][ATTRIBUTE_NSURI])) continue; 141 142 String at_name = attributes[x][ATTRIBUTE_LOCAL].toLowerCase(); 143 at[length][ATTRIBUTE_NSURI] = XHTML1_NAMESPACE; 144 at[length][ATTRIBUTE_LOCAL] = at_name; 145 at[length][ATTRIBUTE_QNAME] = at_name; 146 at[length][ATTRIBUTE_VALUE] = attributes[x][ATTRIBUTE_VALUE]; 147 length++; 148 } 149 150 super.startElementImpl(XHTML1_NAMESPACE, name, name, NAMESPACES, at); 151 } 152 153 156 public void endElementImpl(String nsuri, String local, String qual) 157 throws SAXException { 158 this.closeElement(false); 159 160 String name = local.toUpperCase(); 161 if (! this.checkNamespace(nsuri)) { 162 throw new SAXException ("Unsupported namespace \"" + nsuri + "\" " 163 + "for HTML element \"" + qual + "\"" 164 + this.getLocation()); 165 } 166 167 if (name.equals("AREA")) return; 168 if (name.equals("BASE")) return; 169 if (name.equals("BASEFONT")) return; 170 if (name.equals("BR")) return; 171 if (name.equals("COL")) return; 172 if (name.equals("FRAME")) return; 173 if (name.equals("HR")) return; 174 if (name.equals("IMG")) return; 175 if (name.equals("INPUT")) return; 176 if (name.equals("ISINDEX")) return; 177 if (name.equals("LINK")) return; 178 if (name.equals("META")) return; 179 if (name.equals("PARAM")) return; 180 181 super.endElementImpl(XHTML1_NAMESPACE, name, name); 182 } 183 } 184 | Popular Tags |