KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > metadata > HTMLSection


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: HTMLSection.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.metadata;
25
26 import java.util.ArrayList JavaDoc;
27
28 import org.enhydra.xml.io.Encodings;
29 import org.enhydra.xml.xmlc.XMLCException;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32
33 /**
34  * Metadata specific to HTML documents.
35  */

36 public class HTMLSection extends MetaDataElement {
37     /**
38      * Element name.
39      */

40     public static final String JavaDoc TAG_NAME = "html";
41
42     /**
43      * Attribute names.
44      */

45     private static final String JavaDoc ENCODING_ATTR = "encoding";
46
47     /**
48      * The file encoding for this platform.
49      */

50     private String JavaDoc defaultFileEncoding = System.getProperty("file.encoding");
51
52     /**
53      * Constructor.
54      */

55     public HTMLSection(Document JavaDoc ownerDoc) {
56         super(ownerDoc, TAG_NAME);
57     }
58
59     /**
60      * Get the default encoding. This is the default system file encoding
61      * unless its ASCII, in which case ISO_8859-1 is used. The name is
62      * converted to the MIME-preferred name.
63      */

64     private String JavaDoc getDefaultEncoding() {
65         if (defaultFileEncoding == null) {
66             return Encodings.ISO_8859_1;
67         }
68         Encodings encodings = Encodings.getEncodings();
69         String JavaDoc encoding = encodings.getMIMEPreferred(defaultFileEncoding);
70         if (encoding != null) {
71             if (encodings.sameEncodings(encoding, Encodings.US_ASCII)) {
72                 return Encodings.ISO_8859_1;
73             }
74             return encoding;
75         } else {
76             return Encodings.ISO_8859_1;
77         }
78     }
79
80     /**
81      * Get the encoding attribute value, or
82      */

83     public String JavaDoc getEncoding() {
84         String JavaDoc encoding = getAttributeNull(ENCODING_ATTR);
85         if (encoding == null) {
86             encoding = getDefaultEncoding();
87         }
88         return encoding;
89     }
90
91     /**
92      * Set the encoding attribute value.
93      */

94     public void setEncoding(String JavaDoc value) {
95         setRemoveAttribute(ENCODING_ATTR, value);
96     }
97
98     /**
99      * Get the htmlTagSet child elements.
100      */

101     public HTMLTagSet[] getHTMLTagSets() {
102         return (HTMLTagSet[])getChildren(HTMLTagSet.class);
103     }
104
105     /**
106      * Add a htmlTagSet child element.
107      */

108     public void addHTMLTagSet(HTMLTagSet htmlTagSet) {
109         appendChild(htmlTagSet);
110     }
111
112     /**
113      * Create and add a htmlTagSet child element.
114      */

115     public HTMLTagSet addHTMLTagSet() {
116         HTMLTagSet htmlTagSet = new HTMLTagSet(getOwnerDocument());
117         appendChild(htmlTagSet);
118         return htmlTagSet;
119     }
120
121     /**
122      * Delete a htmlTagSet child element.
123      */

124     public void deleteTagSet(HTMLTagSet htmlTagSet) {
125         removeChild(htmlTagSet);
126     }
127
128     /**
129      * Get the htmlTag child elements.
130      */

131     public HTMLTag[] getHTMLTags() {
132         return (HTMLTag[])getChildren(HTMLTag.class);
133     }
134
135     /**
136      * Add a htmlTag child element.
137      */

138     public void addHTMLTag(HTMLTag htmlTag) {
139         appendChild(htmlTag);
140     }
141
142     /**
143      * Create and add a htmlTag child element.
144      */

145     public HTMLTag addHTMLTag() {
146         HTMLTag htmlTag = new HTMLTag(getOwnerDocument());
147         appendChild(htmlTag);
148         return htmlTag;
149     }
150
151     /**
152      * Delete a htmlTag child element.
153      */

154     public void deleteHTMLTag(HTMLTag htmlTag) {
155         removeChild(htmlTag);
156     }
157
158     /**
159      * Get the htmlAttr child elements.
160      */

161     public HTMLAttr[] getHTMLAttr() {
162         return (HTMLAttr[])getChildren(HTMLAttr.class);
163     }
164
165     /**
166      * Add a htmlAttr child element.
167      */

168     public void addHTMLAttr(HTMLAttr htmlAttr) {
169         appendChild(htmlAttr);
170     }
171
172     /**
173      * Create and add a htmlAttr child element.
174      */

175     public HTMLAttr addHTMLAttr() {
176         HTMLAttr htmlAttr = new HTMLAttr(getOwnerDocument());
177         appendChild(htmlAttr);
178         return htmlAttr;
179     }
180
181     /**
182      * Delete a htmlAttr child element.
183      */

184     public void deleteHTMLAttr(HTMLAttr htmlAttr) {
185         removeChild(htmlAttr);
186     }
187
188     /*
189      * Tags from a tag set.
190      */

191     private void addTagSetTagDefs(HTMLTagSetDef tagSet,
192                                   ArrayList JavaDoc tagDefs) {
193         HTMLTagDef[] defs = tagSet.getTagDefs();
194         for (int idx = 0; idx < defs.length; idx++) {
195             tagDefs.add(defs[idx]);
196         }
197     }
198
199     /*
200      * Get a list of the proprietary tags to recognize.
201      */

202     public HTMLTagDef[] getHTMLTagDefs() {
203         ArrayList JavaDoc tagDefs = new ArrayList JavaDoc();
204         
205         HTMLTagSet[] tagSets = getHTMLTagSets();
206         for (int idx = 0; idx < tagSets.length; idx++) {
207             addTagSetTagDefs(tagSets[idx].getTagSetDef(), tagDefs);
208         }
209
210         HTMLTag[] tags = getHTMLTags();
211         for (int idx = 0; idx < tags.length; idx++) {
212             tagDefs.add(tags[idx]);
213         }
214         return (HTMLTagDef[])tagDefs.toArray(new HTMLTagDef[tagDefs.size()]);
215     }
216
217     /*
218      * Add attributes from a tag set.
219      */

220     private void addTagSetAttrDefs(HTMLTagSetDef tagSet,
221                                    ArrayList JavaDoc attrDefs) {
222         HTMLAttrDef[] defs = tagSet.getAttrDefs();
223         for (int idx = 0; idx < defs.length; idx++) {
224             attrDefs.add(defs[idx]);
225         }
226     }
227
228     /*
229      * Get a list of the proprietary attributes to recognize.
230      */

231     public HTMLAttrDef[] getHTMLAttrDefs() {
232         ArrayList JavaDoc attrDefs = new ArrayList JavaDoc();
233
234         HTMLTagSet[] tagSets = getHTMLTagSets();
235         for (int idx = 0; idx < tagSets.length; idx++) {
236             addTagSetAttrDefs(tagSets[idx].getTagSetDef(), attrDefs);
237         }
238
239         HTMLAttr[] attrs = getHTMLAttr();
240         for (int idx = 0; idx < attrs.length; idx++) {
241             attrDefs.add(attrs[idx]);
242         }
243         return (HTMLAttrDef[])attrDefs.toArray(new HTMLAttrDef[attrDefs.size()]);
244     }
245
246     /**
247      * Get the compatibility child element or null if it doesn't exist.
248      */

249     public HTMLCompatibility getCompatibility() {
250         return (HTMLCompatibility)getChild(HTMLCompatibility.class);
251     }
252
253     /**
254      * Get the compatibility child element, creating if it doesn't exist.
255      */

256     public HTMLCompatibility getCreateCompatibility() {
257         return (HTMLCompatibility)getCreateChild(HTMLCompatibility.class);
258     }
259
260     /**
261      * Set the compatibility child element, or delete with null.
262      */

263     public void setCompatibility(HTMLCompatibility compatibility) {
264         setChild(compatibility);
265     }
266
267     /**
268      * Complete modifications to DOM.
269      * @see MetaDataElement#completeModifications
270      */

271     protected void completeModifications() throws XMLCException {
272         super.completeModifications();
273     }
274
275     /**
276      * Merge another element into this element.
277      */

278     protected void mergeElement(MetaDataElement srcElement) {
279         mergeAttributes(srcElement);
280         mergeSingletonChild(HTMLCompatibility.class, srcElement);
281
282         // Import other children
283
Document JavaDoc document = getOwnerDocument();
284         for (Node JavaDoc child = srcElement.getFirstChild();
285              child != null;
286              child = child.getNextSibling()) {
287             if (!(child instanceof HTMLCompatibility)) {
288                 appendChild(document.importNode(child, true));
289             }
290         }
291     }
292 }
293
Popular Tags