KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > io > HTMLElements


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: HTMLElements.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.io;
25
26 import java.util.HashMap JavaDoc;
27
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  * Information about HTML elements and attributes for use HTML formatter.
32  */

33 final public class HTMLElements {
34     /**
35      * Format element as a block.
36      */

37     public static final Integer JavaDoc BLOCK_FORMATTING = new Integer JavaDoc(1);
38
39     /**
40      * Format element as a header.
41      */

42     public static final Integer JavaDoc HEADER_FORMATTING = new Integer JavaDoc(2);
43
44     /**
45      * Format element as a inline.
46      */

47     public static final Integer JavaDoc INLINE_FORMATTING = new Integer JavaDoc(3);
48
49     /**
50      * Block-formatted elements.
51      */

52     private static final String JavaDoc[] BLOCK_FORMATTED_ELEMENTS = {
53         "HTML", "HEAD", "BODY", "SCRIPT", "P", "UL", "OL", "DL", "DIR",
54         "MENU", "PRE", "LISTING", "XMP", "PLAINTEXT", "ADDRESS", "BLOCKQUOTE",
55         "FORM", "ISINDEX", "FIELDSET", "TABLE", "HR", "DIV", "NOSAVE", "LAYER",
56         "NOLAYER", "ALIGN", "CENTER", "INS", "DEL", "NOFRAMES", "NOSCRIPT",
57         "AREA"
58     };
59
60     /**
61      * Header-formatted elements.
62      */

63     private static final String JavaDoc[] HEADER_FORMATTED_ELEMENTS = {
64         "TITLE", "H1", "H2", "H3", "H4", "H5", "H6"
65     };
66
67     /**
68      * Tag name to formatting. All tags not found are assumed
69      * to be inline.
70      */

71     private static final HashMap JavaDoc fFormattingMap = new HashMap JavaDoc();
72
73     /**
74      * List of attributes that do not have values. If the attribute
75      * exists and is empty, we output it without a value.
76      * Note: HTML attribute names are normalized to lowercase in the DOM.
77      */

78     private static final String JavaDoc[] NO_VALUE_ATTRS = {
79         "checked", "compact", "declare", "defer", "disabled", "ismap",
80         "nohref", "noresize", "noshade", "nowrap", "readonly", "selected"
81     };
82     private static final HashMap JavaDoc fNoValueAttrs = new HashMap JavaDoc();
83
84     /**
85      * List and table of element classes where closing elements are forbidden
86      * (or in the <P> case, just not desired).
87      */

88     private static final String JavaDoc[] NO_CLOSE_TAGS = {
89         "AREA", "BASE", "BASEFONT", "BR", "COL", "FRAME", "HR", "IMG",
90         "INPUT", "ISINDEX", "LINK", "META", "PARAM", "P"
91     };
92     private static final HashMap JavaDoc fNoCloseTags = new HashMap JavaDoc();
93
94     /**
95      * Static constructor.
96      */

97     static {
98         for (int idx = 0; idx < NO_CLOSE_TAGS.length; idx++) {
99             fNoCloseTags.put(NO_CLOSE_TAGS[idx], NO_CLOSE_TAGS[idx]);
100         }
101         for (int idx = 0; idx < NO_VALUE_ATTRS.length; idx++) {
102             fNoValueAttrs.put(NO_VALUE_ATTRS[idx], NO_VALUE_ATTRS[idx]);
103         }
104         for (int idx = 0; idx < BLOCK_FORMATTED_ELEMENTS.length; idx++) {
105             fFormattingMap.put(BLOCK_FORMATTED_ELEMENTS[idx],
106                                BLOCK_FORMATTING);
107         }
108         for (int idx = 0; idx < HEADER_FORMATTED_ELEMENTS.length; idx++) {
109             fFormattingMap.put(HEADER_FORMATTED_ELEMENTS[idx],
110                                HEADER_FORMATTING);
111         }
112     }
113
114     /**
115      * Prevent instanciation.
116      */

117     private HTMLElements() {
118     }
119
120     /**
121      * Check if an attribute does not have a value (is boolean).
122      * @param attrName Nme of attribute, must be lower-case.
123      */

124     public static boolean isBooleanAttr(String JavaDoc attrName) {
125         return fNoValueAttrs.containsKey(attrName);
126     }
127
128     /**
129      * Determine the formatting to use for an element. Unknown
130      * elements are returned as INLINE_FORMATTING.
131      * @param tagName Name of element, must be upper-case.
132      * @return One of the *_FORMATTING constants; references can
133      * be compared.
134      */

135     public Integer JavaDoc getElementFormatting(String JavaDoc tagName) {
136         Integer JavaDoc formatting = (Integer JavaDoc)fFormattingMap.get(tagName);
137         if (formatting == null) {
138             return INLINE_FORMATTING;
139         } else {
140             return formatting;
141         }
142     }
143
144     /**
145      * Check if an element is may have a close tag.
146      * @param tagName Name of element, must be upper-case.
147      */

148     public static boolean hasCloseTag(String JavaDoc tagName) {
149         return !fNoCloseTags.containsKey(tagName);
150     }
151
152     /**
153      * Test if an Element is a script or style element, which have special
154      * content handling.
155      */

156     public static boolean isScriptStyle(Element JavaDoc element) {
157         String JavaDoc tagName = element.getTagName();
158         return (tagName.equals("SCRIPT") || tagName.equals("STYLE"));
159     }
160
161     // FIXME: Add factory methods for all HTML elements.
162
}
163
Popular Tags