KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ecs > ElementAttributes


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  */

58 package org.apache.ecs;
59
60 import java.util.Enumeration;
61 import java.io.OutputStream;
62 import java.io.PrintWriter;
63 import java.io.IOException;
64 import java.io.ByteArrayOutputStream;
65 import java.io.BufferedOutputStream;
66
67 /**
68     This class provides a common set of attributes set* methods for all classes.
69     It is abstract to prevent direct instantiation.
70     @version $Id: ElementAttributes.java,v 1.15 2003/05/02 09:53:58 rdonkin Exp $
71     @author <a HREF="mailto:snagy@servletapi.com">Stephan Nagy</a>
72     @author <a HREF="mailto:jon@clearink.com">Jon S. Stevens</a>
73 */

74
75 public abstract class ElementAttributes extends GenericElement implements Attributes
76 {
77     /**
78         Basic Constructor.
79     */

80     public ElementAttributes()
81     {
82     }
83
84     /**
85         Filter to use to escape attribute input
86         @serial attribute_filter attribute_filter
87     */

88     private Filter attribute_filter = getFilter(); // By default the attribute filter and the element filter are the same.
89

90     /**
91         Should we filter the value of the element attributes
92         @serial filter_attribute_state filter_attribute_state
93     */

94     private boolean filter_attribute_state = ECSDefaults.getDefaultFilterAttributeState();
95
96     /**
97         What is the equality character for an attribute.
98         @serial attribute_equality_sign attribute_equality_sign
99     */

100     private char attribute_equality_sign = ECSDefaults.getDefaultAttributeEqualitySign();
101
102     /**
103         What character should we use for quoting attributes.
104         @serial attribute_quote_char attribute_quote_char
105     */

106     private char attribute_quote_char = ECSDefaults.getDefaultAttributeQuoteChar();
107
108     /**
109         Should we wrap quotes around an attribute?
110         @serial attribute_quote attribute_quote
111     */

112     private boolean attribute_quote = ECSDefaults.getDefaultAttributeQuote();
113
114     /**
115         Set the character used to quote attributes.
116         @param quote_char character used to quote attributes
117     */

118     public Element setAttributeQuoteChar(char quote_char)
119     {
120         attribute_quote_char = quote_char;
121         return(this);
122     }
123
124     /**
125         Get the character used to quote attributes.
126     */

127     public char getAttributeQuoteChar()
128     {
129         return(attribute_quote_char);
130     }
131
132     /**
133         Set the equality sign for an attribute.
134         @param equality_sign The equality sign used for attributes.
135     */

136     public Element setAttributeEqualitySign(char equality_sign)
137     {
138         attribute_equality_sign = equality_sign;
139         return(this);
140     }
141
142     /**
143         Get the equality sign for an attribute.
144     */

145     public char getAttributeEqualitySign()
146     {
147         return(attribute_equality_sign);
148     }
149
150     /*
151         Do we surround attributes with qoutes?
152     */

153     public boolean getAttributeQuote()
154     {
155         return(attribute_quote);
156     }
157
158     /**
159         Set wether or not we surround the attributes with quotes.
160     */

161     public Element setAttributeQuote(boolean attribute_quote)
162     {
163         this.attribute_quote = attribute_quote;
164         return(this);
165     }
166
167     /**
168         Set the element id for Cascading Style Sheets.
169     */

170     public Element setID(String id)
171     {
172         addAttribute("id",id);
173         return(this);
174     }
175
176     /**
177         Set the element class for Cascading Style Sheets.
178     */

179     public Element setClass(String element_class)
180     {
181         addAttribute("class",element_class);
182         return(this);
183     }
184
185     /**
186         Sets the LANG="" attribute
187         @param lang the LANG="" attribute
188     */

189     public Element setLang(String lang)
190     {
191         addAttribute("lang",lang);
192         return(this);
193     }
194
195     /**
196         Sets the STYLE="" attribute
197         @param style the STYLE="" attribute
198     */

199     public Element setStyle(String style)
200     {
201         addAttribute("style",style);
202         return(this);
203     }
204
205     /**
206         Sets the DIR="" attribute
207         @param dir the DIR="" attribute
208     */

209     public Element setDir(String dir)
210     {
211         addAttribute("dir",dir);
212         return(this);
213     }
214     /**
215         Sets the TITLE="" attribute
216         @param title the TITLE="" attribute
217     */

218     public Element setTitle(String title)
219     {
220         addAttribute("title",title);
221         return(this);
222     }
223
224     /**
225         Find out if we want to filter the elements attributes or not.
226     */

227     public boolean getAttributeFilterState()
228     {
229         return(filter_attribute_state);
230     }
231
232     /**
233         Tell the element if we want to filter its attriubtes.
234         @param filter_attribute_state do we want to filter the attributes of this element?
235     */

236     public Element setAttributeFilterState(boolean filter_attribute_state)
237     {
238         this.filter_attribute_state = filter_attribute_state;
239         return(this);
240     }
241
242     /**
243         Set up a new filter for all element attributes.
244         @param Filter the filter we want to use for element attributes. By <br>
245         default it is the same as is used for the value of the tag. It is assumed<br>
246         that if you create a new filter you must want to use it.
247     */

248     public Element setAttributeFilter(Filter attribute_filter)
249     {
250         filter_attribute_state = true; // If your setting up a filter you must want to filter.
251
this.attribute_filter = attribute_filter;
252         return(this);
253     }
254
255     /**
256         Get the filter for all element attributes.
257         @param Filter the filter we want to use for element attributes. By <br>
258         default it is the same as is used for the value of the tag. It is assumed<br>
259         that if you create a new filter you must want to use it.
260     */

261     public Filter getAttributeFilter()
262     {
263         return(this.attribute_filter);
264     }
265
266
267     /** Add an attribute to the element. */
268     public Element addAttribute(String attribute_name, Object attribute_value)
269     {
270         getElementHashEntry().put(attribute_name, attribute_value);
271         return(this);
272     }
273
274     /** Add an attribute to the element. */
275     public Element addAttribute(String attribute_name, int attribute_value)
276     {
277         getElementHashEntry().put(attribute_name, new Integer(attribute_value));
278         return(this);
279     }
280
281     /** Add an attribute to the element. */
282     public Element addAttribute(String attribute_name, String attribute_value)
283     {
284         getElementHashEntry().put(attribute_name, attribute_value);
285         return(this);
286     }
287
288     /** Add an attribute to the element. */
289     public Element addAttribute(String attribute_name, Integer attribute_value)
290     {
291         getElementHashEntry().put(attribute_name, attribute_value);
292         return(this);
293     }
294
295     /** remove an attribute from the element */
296     public Element removeAttribute(String attribute_name)
297     {
298         try
299         {
300             getElementHashEntry().remove(attribute_name);
301         }
302         catch ( Exception e )
303         {
304         }
305         return(this);
306     }
307
308     /** does the element have a particular attribute? */
309     public boolean hasAttribute(String attribute)
310     {
311         return(getElementHashEntry().containsKey(attribute));
312     }
313
314     /** Return a list of the attributes associated with this element. */
315     public Enumeration attributes()
316     {
317         return getElementHashEntry().keys();
318     }
319
320     /**
321      * Return the specified attribute.
322      * @param attribute The name of the attribute to fetch
323      */

324     public String getAttribute(String attribute)
325     {
326         return (String)getElementHashEntry().get(attribute);
327     }
328
329     /**
330         This method overrides createStartTag() in Generic Element.
331         It provides a way to print out the attributes of an element.
332     */

333     public String createStartTag()
334     {
335         StringBuffer out = new StringBuffer();
336
337         out.append(getStartTagChar());
338
339         if(getBeginStartModifierDefined())
340         {
341             out.append(getBeginStartModifier());
342         }
343         out.append(getElementType());
344
345         Enumeration enum = getElementHashEntry().keys();
346         String value = null; // avoid creating a new string object on each pass through the loop
347

348         while (enum.hasMoreElements())
349         {
350             String attr = (String) enum.nextElement();
351             if(getAttributeFilterState())
352             {
353                 value = getAttributeFilter().process(getElementHashEntry().get(attr).toString());
354             }
355             else
356             {
357                 Object valueObj = getElementHashEntry().get(attr);
358                 if (valueObj == null) {
359                     return null;
360                 }
361                 value = valueObj.toString();
362             }
363             out.append(' ');
364             out.append(alterCase(attr));
365             int iStartPos = out.length();
366             if ( !value.equalsIgnoreCase(NO_ATTRIBUTE_VALUE) )
367             {
368                 // we have a value
369
// we might still enclose in quotes
370
boolean quoteThisAttribute = attribute_quote;
371                 int singleQuoteFound = 0;
372                 int doubleQuoteFound = 0;
373                 if ( value.length() == 0 )
374                 { // quote a null string
375
quoteThisAttribute = true;
376                 }
377                 for ( int ii = 0; ii < value.length(); ii++ )
378                 {
379                     char c = value.charAt( ii );
380                     if ( 'a' <= c && c <= 'z' )
381                     {
382                         continue;
383                     }
384                     if ( 'A' <= c && c <= 'Z' )
385                     {
386                         continue;
387                     }
388                     if ( '0' <= c && c <= '9' )
389                     {
390                         continue;
391                     }
392                     if ( c == ':' ||
393                          c == '-' ||
394                          c == '_' ||
395                          c == '.' )
396                     {
397                         continue;
398                     }
399                     if ( c == '\'' )
400                     {
401                         singleQuoteFound++;
402                     }
403                     if ( c == '"' )
404                     {
405                         doubleQuoteFound++;
406                     }
407                     quoteThisAttribute = true;
408                 }
409                 out.append( getAttributeEqualitySign() );
410                 if ( !quoteThisAttribute )
411                 { // no need to append quotes
412
out.append( value );
413                 }
414                 else
415                 { // use single quotes if possible
416
if ( singleQuoteFound == 0 )
417                     { // no single quotes, safe to use them to quote
418
out.append( '\'' );
419                         out.append( value );
420                         out.append( '\'' );
421                     }
422                     else
423                     if ( doubleQuoteFound == 0 )
424                     { // no double quotes, safe to use them to quote
425
out.append( '"' );
426                         out.append( value );
427                         out.append( '"' );
428                     }
429                     else if ( singleQuoteFound <= doubleQuoteFound )
430                     { // use single quotes, convert embedded single quotes
431
out.append( '\'' );
432                         int startPos = out.length();
433                         out.append( value );
434                         for ( int ii = startPos; ii < out.length(); ii++ )
435                         { // note out.length() may change during processing
436
if ( out.charAt( ii ) == '\'' )
437                             {
438                                 out.setCharAt( ii, '&');
439                                 out.insert( ii+1, "#39;" );
440                                 ii++;
441                             }
442                         }
443                         out.append( '\'' );
444                     }
445                     else
446                     { // use double quotes, convert embedded double quotes
447
out.append( '"' );
448                         int startPos = out.length();
449                         out.append( value );
450                         for ( int ii = startPos; ii < out.length(); ii++ )
451                         { // note out.length() may change during processing
452
if ( out.charAt( ii ) == '"' )
453                             {
454                                 out.setCharAt( ii, '&');
455                                 out.insert( ii+1, "#34;" );
456                                 ii++;
457                             }
458                         }
459                         out.append( '"' );
460                     }
461                 }
462             }
463         }
464         if(getBeginEndModifierDefined())
465         {
466             out.append(getBeginEndModifier());
467         }
468         out.append(getEndTagChar());
469
470         return(out.toString());
471     }
472 }
473
Free Books   Free Magazines  
Popular Tags