KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ecs > xml > XML


1 /*
2  * ====================================================================
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Jakarta Element Construction Set",
29  * "Jakarta ECS" , and "Apache Software Foundation" must not be used
30  * to endorse or promote products derived
31  * from this software without prior written permission. For written
32  * permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache",
35  * "Jakarta Element Construction Set" nor "Jakarta ECS" nor may "Apache"
36  * appear in their names without prior written permission of the Apache Group.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  *
57  */
package org.apache.ecs.xml;
58
59 import org.apache.ecs.*;
60
61 /**
62     This class creates a generic &lt;&gt; tag.
63
64     @version $Id: XML.java,v 1.4 2003/04/27 09:28:14 rdonkin Exp $
65     @author <a HREF="mailto:snagy@servletapi.com">Stephan Nagy</a>
66     @author <a HREF="mailto:jon@clearink.com">Jon S. Stevens</a>
67 */

68 public class XML extends MultiPartElement implements Printable
69 {
70
71     /**
72         Default constructor use set* Methods. With this name.
73         @param element_type The name of this element.
74     */

75     public XML(String element_type)
76     {
77         setElementType(element_type);
78     }
79
80     /**
81         Construct a new XML element with this name, <br>and specify if it needs
82         the element tag closed.
83         @param element_type The name of this element.
84         @param close Should it have a closing tag
85     */

86     public XML(String element_type,boolean close)
87     {
88         setElementType(element_type);
89         setNeedClosingTag(close);
90     }
91
92     /**
93         Construct a new XML element with this name, and specify a filter for it.
94         @param element_type The name of this element.
95         @param filter a new Filter for this element override the default.
96     */

97     public XML(String element_type,Filter filter)
98     {
99         setElementType(element_type);
100         setFilter(filter);
101     }
102     
103     /**
104         Construct a new XML element with this name, and specify a filter for it.
105         @param element_type The name of this element.
106         @param close Should it have a closing tag
107         @param filter Should this element be filtered?
108     */

109     public XML(String element_type,boolean close,boolean filter)
110     {
111         setElementType(element_type);
112         setNeedClosingTag(close);
113         setFilterState(filter);
114     }
115
116     /**
117         Construct a new XML element with this name, and specify a filter for it.
118         @param element_type The name of this element.
119         @param close Should it have a closing tag
120         @param filter a new Filter for this element override the default.
121     */

122     public XML(String element_type,boolean close,Filter filter)
123     {
124         setElementType(element_type);
125         setNeedClosingTag(close);
126         setFilter(filter);
127     }
128
129     /**
130         Add a new attribute to this XML tag.
131         @param attribute the attribute name
132         @param attribute_value the value of the attribute set this to<BR>
133         <code>"ECS_NO_ATTRIBUTE_VALUE"</code> if this attribute <BR>
134         doesn't take a value.
135     */

136     public XML addXMLAttribute(String attribute, String attribute_value)
137     {
138         addAttribute(attribute,attribute_value);
139         return(this);
140     }
141
142     /**
143         Add an element to the valuie of &lt;&gt;VALUE&lt;/&gt;
144         @param element the value of &lt;&gt;VALUE&lt;/&gt;
145     */

146     public XML addElement(String element)
147     {
148         addElementToRegistry(element);
149         return(this);
150     }
151
152     /**
153         Adds an Element to the element.
154         @param hashcode name of element for hash table
155         @param element Adds an Element to the element.
156      */

157     public XML addElement(String hashcode,Element element)
158     {
159         addElementToRegistry(hashcode,element);
160         return(this);
161     }
162
163     /**
164         Adds an Element to the element.
165         @param hashcode name of element for hash table
166         @param element Adds an Element to the element.
167      */

168     public XML addElement(String hashcode,String element)
169     {
170         addElementToRegistry(hashcode,element);
171         return(this);
172     }
173
174     /**
175         Add an element to the valuie of &lt;&gt;VALUE&lt;/&gt;
176         @param element the value of &lt;&gt;VALUE&lt;/&gt;
177     */

178     public XML addElement(Element element)
179     {
180         addElementToRegistry(element);
181         return(this);
182     }
183     /**
184         Removes an Element from the element.
185         @param hashcode the name of the element to be removed.
186     */

187     public XML removeElement(String hashcode)
188     {
189         removeElementFromRegistry(hashcode);
190         return(this);
191     }
192
193     public boolean getNeedLineBreak() {
194         boolean linebreak = true;
195
196         java.util.Enumeration enum = elements();
197
198         // if this tag has one child, and it's a String, then don't
199
// do any linebreaks to preserve whitespace
200

201         while (enum.hasMoreElements()) {
202             Object obj = enum.nextElement();
203             if (obj instanceof StringElement) {
204                  linebreak = false;
205                  break;
206             }
207
208         }
209
210         return linebreak;
211     }
212
213     public boolean getBeginEndModifierDefined() {
214         boolean answer = false;
215
216         if (! this.getNeedClosingTag())
217             answer = true;
218
219         return answer;
220     }
221
222     public char getBeginEndModifier() {
223         return '/';
224     }
225 }
226
Popular Tags