KickJava   Java API By Example, From Geeks To Geeks.

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


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.XHTMLEncoder;
19 import org.apache.cocoon.components.serializers.util.DocType;
20 import org.xml.sax.SAXException JavaDoc;
21
22
23 /**
24  *
25  *
26  * @author <a HREF="mailto:pier@apache.org">Pier Fumagalli</a>, February 2003
27  * @version CVS $Id: XHTMLSerializer.java 30932 2004-07-29 17:35:38Z vgritsenko $
28  */

29 public class XHTMLSerializer extends XMLSerializer {
30
31     /** The namespace URI for XHTML 1.0. */
32     public static final String JavaDoc XHTML1_NAMESPACE =
33             "http://www.w3.org/1999/xhtml";
34
35     /** A representation of the XHTML 1.0 strict document type. */
36     public static final DocType XHTML1_DOCTYPE_STRICT = new DocType(
37             "html", "-//W3C//DTD XHTML 1.0 Strict//EN",
38             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
39
40     /** A representation of the XHTML 1.0 transitional document type. */
41     public static final DocType XHTML1_DOCTYPE_TRANSITIONAL = new DocType(
42             "html", "-//W3C//DTD XHTML 1.0 Transitional//EN",
43             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");
44
45     /** A representation of the XHTML 1.0 frameset document type. */
46     public static final DocType XHTML1_DOCTYPE_FRAMESET = new DocType(
47             "html", "-//W3C//DTD XHTML 1.0 Frameset//EN",
48             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd");
49
50     /* ====================================================================== */
51
52     private static final XHTMLEncoder XHTML_ENCODER = new XHTMLEncoder();
53
54     /**
55      * Create a new instance of this <code>XHTMLSerializer</code>
56      */

57     public XHTMLSerializer() {
58         super(XHTML_ENCODER);
59     }
60
61     /**
62      * Create a new instance of this <code>XHTMLSerializer</code>
63      */

64     protected XHTMLSerializer(XHTMLEncoder encoder) {
65         super(encoder);
66     }
67     
68     /**
69      * Return the MIME Content-Type produced by this serializer.
70      */

71     public String JavaDoc getMimeType() {
72         if (this.charset == null) return("text/html");
73         return("text/html; charset=" + this.charset.getName());
74     }
75
76     /* ====================================================================== */
77
78     /**
79      * Receive notification of the beginning of the document body.
80      *
81      * @param uri The namespace URI of the root element.
82      * @param local The local name of the root element.
83      * @param qual The fully-qualified name of the root element.
84      */

85     public void body(String JavaDoc uri, String JavaDoc local, String JavaDoc qual)
86     throws SAXException JavaDoc {
87         if (this.doctype == null) this.doctype = XHTML1_DOCTYPE_TRANSITIONAL;
88         if (this.namespaces.getUri("").length() == 0) {
89             this.namespaces.push("", XHTML1_NAMESPACE);
90         }
91         super.body(uri, local, qual);
92     }
93
94     /**
95      * Receive notification of the beginning of an element.
96      *
97      * @param uri The namespace URI of the root element.
98      * @param local The local name of the root element.
99      * @param qual The fully-qualified name of the root element.
100      * @param namespaces An array of <code>String</code> objects containing
101      * the namespaces to be declared by this element.
102      * @param attributes An array of <code>String</code> objects containing
103      * all attributes of this element.
104      */

105     public void startElementImpl(String JavaDoc uri, String JavaDoc local, String JavaDoc qual,
106                                  String JavaDoc namespaces[][], String JavaDoc attributes[][])
107     throws SAXException JavaDoc {
108         if (uri.length() == 0) uri = XHTML1_NAMESPACE;
109         super.startElementImpl(uri, local, qual, namespaces, attributes);
110     }
111
112     /**
113      * Receive notification of the end of an element.
114      *
115      * @param uri The namespace URI of the root element.
116      * @param local The local name of the root element.
117      * @param qual The fully-qualified name of the root element.
118      */

119     public void endElementImpl(String JavaDoc uri, String JavaDoc local, String JavaDoc qual)
120     throws SAXException JavaDoc {
121         if (uri.length() == 0) uri = XHTML1_NAMESPACE;
122
123         if (XHTML1_NAMESPACE.equals(uri)) {
124             if ((local.equalsIgnoreCase("textarea")) ||
125                 (local.equalsIgnoreCase("script")) ||
126                 (local.equalsIgnoreCase("style"))) {
127                 this.closeElement(false);
128             } else if (local.equalsIgnoreCase("head")) {
129                 String JavaDoc loc = "meta";
130                 String JavaDoc qua = namespaces.qualify(XHTML1_NAMESPACE, loc, "meta");
131                 String JavaDoc nsp[][] = new String JavaDoc[0][0];
132                 String JavaDoc att[][] = new String JavaDoc[2][ATTRIBUTE_LENGTH];
133
134                 att[0][ATTRIBUTE_NSURI] = att[1][ATTRIBUTE_NSURI] = "";
135                 att[0][ATTRIBUTE_LOCAL] = att[0][ATTRIBUTE_QNAME] = "http-equiv";
136                 att[1][ATTRIBUTE_LOCAL] = att[1][ATTRIBUTE_QNAME] = "content";
137                 att[0][ATTRIBUTE_VALUE] = "Content-Type";
138                 att[1][ATTRIBUTE_VALUE] = this.getMimeType();
139
140                 this.closeElement(false);
141                 this.startElementImpl(XHTML1_NAMESPACE, loc, qua, nsp, att);
142                 this.endElementImpl(XHTML1_NAMESPACE, loc, qua);
143             }
144         }
145         super.endElementImpl(uri, local, qual);
146     }
147 }
148
Popular Tags