001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.webrender.output;
031:
032: /**
033: * A renderable representation of a single CSS style.
034: */
035: public class CssStyle {
036:
037: // Note that this class uses a proprietary associative array implementation
038: // in the interest of performance/memory allocation during rendering. The
039: // implementation is tuned for very low numbers of keys/value which will be
040: // the typical case in describing a CSS style.
041:
042: private static final int GROW_RATE = 5 * 2; // Must be a multiple of 2.
043: private static final String[] EMPTY = new String[0];
044:
045: private String[] data = EMPTY;
046: int length = 0; // Number of items * 2;
047:
048: /**
049: * Retrieves a style attribute value.
050: *
051: * @param attributeName the name of the attribute
052: * @return the value of the attribute (null if it is not set)
053: */
054: public String getAttribute(String attributeName) {
055: for (int i = 0; i < length; i += 2) {
056: if (data[i].equals(attributeName)) {
057: return data[i + 1];
058: }
059: }
060: return null;
061: }
062:
063: /**
064: * Determines if any attributes are set.
065: *
066: * @return true if any attributes are set.
067: */
068: public boolean hasAttributes() {
069: return length > 0;
070: }
071:
072: /**
073: * Sets a style attribute value.
074: *
075: * @param attributeName the name of the attribute.
076: * @param attributeValue the value of the attribute.
077: */
078: public void setAttribute(String attributeName, String attributeValue) {
079: if (data == EMPTY) {
080: data = new String[GROW_RATE];
081: }
082:
083: int propertyNameHashCode = attributeName.hashCode();
084: for (int i = 0; i < data.length; i += 2) {
085: if (data[i] == null) {
086: // Property is not set, space remains to set property.
087: // Add property at end.
088: data[i] = attributeName;
089: data[i + 1] = attributeValue;
090: length += 2;
091: return;
092: }
093: if (propertyNameHashCode == data[i].hashCode()
094: && attributeName.equals(data[i])) {
095: // Found property, overwrite.
096: data[i + 1] = attributeValue;
097: return;
098: }
099: }
100:
101: // Array is full: grow array.
102: String[] newData = new String[data.length + GROW_RATE];
103: System.arraycopy(data, 0, newData, 0, data.length);
104:
105: newData[data.length] = attributeName;
106: newData[data.length + 1] = attributeValue;
107: length += 2;
108: data = newData;
109: }
110:
111: /**
112: * Renders the style inline. The returned value is suitable as the value
113: * of the "style" attribute of an HTML element.
114: *
115: * @return the inline representation
116: */
117: public String renderInline() {
118: StringBuffer out = new StringBuffer();
119: for (int i = 0; i < length; i += 2) {
120: out.append(data[i]);
121: out.append(":");
122: out.append(data[i + 1]);
123: out.append(";");
124: }
125: return out.toString();
126: }
127:
128: /**
129: * Renders a debug representation of the object.
130: *
131: * @see java.lang.Object#toString()
132: */
133: public String toString() {
134: return CssStyle.class.getName() + " {" + renderInline() + "}";
135: }
136: }
|