KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > XHTMLEmitter


1 package net.sf.saxon.event;
2
3 import net.sf.saxon.trans.XPathException;
4 import net.sf.saxon.om.NamePool;
5 import net.sf.saxon.om.NamespaceConstant;
6
7 import java.util.HashSet JavaDoc;
8
9
10 /**
11   * XHTMLEmitter is an Emitter that generates XHTML output.
12   * It is the same as XMLEmitter except that it follows the legacy HTML browser
13   * compatibility rules: for example, generating empty elements such as [BR /], and
14   * using [p][/p] for empty paragraphs rather than [p/]
15   */

16
17 public class XHTMLEmitter extends XMLEmitter {
18
19     /**
20     * Table of XHTML tags that have no closing tag
21     */

22
23     HashSet JavaDoc emptyTags = new HashSet JavaDoc(31);
24
25     private static String JavaDoc[] emptyTagNames = {
26         "area", "base", "basefont", "br", "col", "frame", "hr", "img", "input", "isindex", "link", "meta", "param"
27     };
28
29     /**
30      * Do the real work of starting the document. This happens when the first
31      * content is written.
32      *
33      * @throws net.sf.saxon.trans.XPathException
34      *
35      */

36
37     protected void openDocument() throws XPathException {
38         NamePool pool = getPipelineConfiguration().getConfiguration().getNamePool();
39         for (int i=0; i<emptyTagNames.length; i++) {
40             emptyTags.add(new Integer JavaDoc(
41                     pool.allocate("", NamespaceConstant.XHTML, emptyTagNames[i]) & NamePool.FP_MASK));
42         }
43         super.openDocument();
44     }
45
46     /**
47     * Close an empty element tag.
48     */

49
50     protected String JavaDoc emptyElementTagCloser(String JavaDoc displayName, int nameCode) {
51         if (emptyTags.contains(new Integer JavaDoc(nameCode & NamePool.FP_MASK))) {
52             return " />";
53         } else {
54             return "></" + displayName + '>';
55         }
56     }
57
58 }
59
60 //
61
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
62
// you may not use this file except in compliance with the License. You may obtain a copy of the
63
// License at http://www.mozilla.org/MPL/
64
//
65
// Software distributed under the License is distributed on an "AS IS" basis,
66
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
67
// See the License for the specific language governing rights and limitations under the License.
68
//
69
// The Original Code is: all this file.
70
//
71
// The Initial Developer of the Original Code is Michael H. Kay.
72
//
73
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
74
//
75
// Contributor(s): none.
76
//
77
Popular Tags