KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > serializers > HTMLSerializer


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
22
23
24 /**
25  *
26  * @author <a HREF="mailto:pier@apache.org">Pier Fumagalli</a>, February 2003
27  * @version CVS $Id: HTMLSerializer.java 30932 2004-07-29 17:35:38Z vgritsenko $
28  */

29 public class HTMLSerializer extends XHTMLSerializer {
30
31     /** A cross-browser compatible very simple document type declaration. */
32     public static final DocType HTML401_DOCTYPE_COMPATIBLE = new SGMLDocType(
33             "HTML", "-//W3C//DTD HTML 4.01 Transitional//EN", null);
34
35     /** A representation of the HTML 4.01 strict document type. */
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     /** A representation of the HTML 4.01 transitional document type. */
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     /** A representation of the HTML 4.01 frameset document type. */
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     /**
55      * Create a new instance of this <code>HTMLSerializer</code>
56      */

57     public HTMLSerializer() {
58         super(HTML_ENCODER);
59     }
60
61     /* ====================================================================== */
62
63     /** Empty namespaces declaration. */
64     private static final String JavaDoc NAMESPACES[][] = new String JavaDoc[0][0];
65
66     /** Check if the URI is allowed by this serializer. */
67     private boolean checkNamespace(String JavaDoc nsuri) {
68         if (nsuri.length() == 0) return(true);
69         if (XHTML1_NAMESPACE.equals(nsuri)) return(true);
70         return(false);
71     }
72
73     /* ====================================================================== */
74
75     /**
76      * Write the XML document header.
77      * <p>
78      * This method overrides the default <code>XMLSerializer</code>.behaviour.
79      * </p>
80      */

81     public void head()
82     throws SAXException JavaDoc {
83         // NO NOTHING!
84
}
85
86     /**
87      * Receive notification of the beginning of the document body.
88      *
89      * @param nsuri The namespace URI of the root element.
90      * @param local The local name of the root element.
91      * @param qual The fully-qualified name of the root element.
92      */

93     public void body(String JavaDoc nsuri, String JavaDoc local, String JavaDoc qual)
94     throws SAXException JavaDoc {
95         String JavaDoc name = local.toUpperCase();
96         if (! this.checkNamespace(nsuri)) {
97             throw new SAXException JavaDoc("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     /**
120      * Receive notification of the beginning of an element.
121      */

122     public void startElementImpl(String JavaDoc nsuri, String JavaDoc local, String JavaDoc qual,
123                              String JavaDoc namespaces[][], String JavaDoc attributes[][])
124     throws SAXException JavaDoc {
125         String JavaDoc name = local.toUpperCase();
126         if (! this.checkNamespace(nsuri)) {
127             throw new SAXException JavaDoc("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 JavaDoc at[][] = new String JavaDoc[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 JavaDoc 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     /**
154      * Receive notification of the end of an element.
155      */

156     public void endElementImpl(String JavaDoc nsuri, String JavaDoc local, String JavaDoc qual)
157     throws SAXException JavaDoc {
158         this.closeElement(false);
159
160         String JavaDoc name = local.toUpperCase();
161         if (! this.checkNamespace(nsuri)) {
162             throw new SAXException JavaDoc("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