KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webrender > output > CssStyle


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webrender.output;
31
32 /**
33  * A renderable representation of a single CSS style.
34  */

35 public class CssStyle {
36     
37     // Note that this class uses a proprietary associative array implementation
38
// in the interest of performance/memory allocation during rendering. The
39
// implementation is tuned for very low numbers of keys/value which will be
40
// the typical case in describing a CSS style.
41

42     private static final int GROW_RATE = 5 * 2; // Must be a multiple of 2.
43
private static final String JavaDoc[] EMPTY = new String JavaDoc[0];
44     
45     private String JavaDoc[] data = EMPTY;
46     int length = 0; // Number of items * 2;
47

48     /**
49      * Retrieves a style attribute value.
50      *
51      * @param attributeName the name of the attribute
52      * @return the value of the attribute (null if it is not set)
53      */

54     public String JavaDoc getAttribute(String JavaDoc attributeName) {
55         for (int i = 0; i < length; i += 2) {
56             if (data[i].equals(attributeName)) {
57                 return data[i + 1];
58             }
59         }
60         return null;
61     }
62     
63     /**
64      * Determines if any attributes are set.
65      *
66      * @return true if any attributes are set.
67      */

68     public boolean hasAttributes() {
69         return length > 0;
70     }
71     
72     /**
73      * Sets a style attribute value.
74      *
75      * @param attributeName the name of the attribute.
76      * @param attributeValue the value of the attribute.
77      */

78     public void setAttribute(String JavaDoc attributeName, String JavaDoc attributeValue) {
79         if (data == EMPTY) {
80             data = new String JavaDoc[GROW_RATE];
81         }
82
83         int propertyNameHashCode = attributeName.hashCode();
84         for (int i = 0; i < data.length; i += 2) {
85             if (data[i] == null) {
86                 // Property is not set, space remains to set property.
87
// Add property at end.
88
data[i] = attributeName;
89                 data[i + 1] = attributeValue;
90                 length += 2;
91                 return;
92             }
93             if (propertyNameHashCode == data[i].hashCode() && attributeName.equals(data[i])) {
94                 // Found property, overwrite.
95
data[i + 1] = attributeValue;
96                 return;
97             }
98         }
99         
100         // Array is full: grow array.
101
String JavaDoc[] newData = new String JavaDoc[data.length + GROW_RATE];
102         System.arraycopy(data, 0, newData, 0, data.length);
103         
104         newData[data.length] = attributeName;
105         newData[data.length + 1] = attributeValue;
106         length += 2;
107         data = newData;
108     }
109     
110     /**
111      * Renders the style inline. The returned value is suitable as the value
112      * of the "style" attribute of an HTML element.
113      *
114      * @return the inline representation
115      */

116     public String JavaDoc renderInline() {
117         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
118         for (int i = 0; i < length; i += 2) {
119             out.append(data[i]);
120             out.append(":");
121             out.append(data[i + 1]);
122             out.append(";");
123         }
124         return out.toString();
125     }
126     
127     /**
128      * Renders a debug representation of the object.
129      *
130      * @see java.lang.Object#toString()
131      */

132     public String JavaDoc toString() {
133         return CssStyle.class.getName() + " {" + renderInline() + "}";
134     }
135 }
136
Popular Tags