1 29 30 package nextapp.echo2.webrender.output; 31 32 35 public class CssStyle { 36 37 42 private static final int GROW_RATE = 5 * 2; private static final String [] EMPTY = new String [0]; 44 45 private String [] data = EMPTY; 46 int length = 0; 48 54 public String getAttribute(String 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 68 public boolean hasAttributes() { 69 return length > 0; 70 } 71 72 78 public void setAttribute(String attributeName, String attributeValue) { 79 if (data == EMPTY) { 80 data = new String [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 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 data[i + 1] = attributeValue; 96 return; 97 } 98 } 99 100 String [] newData = new String [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 116 public String renderInline() { 117 StringBuffer out = new StringBuffer (); 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 132 public String toString() { 133 return CssStyle.class.getName() + " {" + renderInline() + "}"; 134 } 135 } 136 | Popular Tags |