1 11 package org.eclipse.core.internal.content; 12 13 import org.eclipse.core.runtime.QualifiedName; 14 import org.eclipse.core.runtime.content.IContentDescription; 15 16 public final class ContentDescription extends BasicDescription { 17 18 private static final String CHARSET_UTF_16 = "UTF-16"; private static final String CHARSET_UTF_8 = "UTF-8"; private static final byte FLAG_ALL_OPTIONS = 0x01; 21 private static final byte FLAG_IMMUTABLE = 0x02; 22 private byte flags; 23 private Object keys; 24 private Object values; 25 26 public ContentDescription(QualifiedName[] requested, IContentTypeInfo contentTypeInfo) { 27 super(contentTypeInfo); 28 if (requested == IContentDescription.ALL) { 29 flags |= FLAG_ALL_OPTIONS; 30 return; 31 } 32 if (requested.length > 1) { 33 keys = requested; 34 values = new Object [requested.length]; 35 } else if (requested.length == 1) 36 keys = requested[0]; 37 } 39 40 private void assertMutable() { 41 if ((flags & FLAG_IMMUTABLE) != 0) 42 throw new IllegalStateException ("Content description is immutable"); } 44 45 48 public String getCharset() { 49 byte[] bom = (byte[]) getProperty(BYTE_ORDER_MARK); 50 if (bom == BOM_UTF_8) 51 return CHARSET_UTF_8; 52 else if (bom == BOM_UTF_16BE || bom == BOM_UTF_16LE) 53 return CHARSET_UTF_16; 55 return (String ) getProperty(CHARSET); 56 } 57 58 private Object getDescribedProperty(QualifiedName key) { 59 if (values == null) 61 return null; 62 if (keys instanceof QualifiedName) 64 return keys.equals(key) ? values : null; 65 QualifiedName[] tmpKeys = (QualifiedName[]) this.keys; 67 for (int i = 0; i < tmpKeys.length; i++) 68 if (tmpKeys[i].equals(key)) 69 return ((Object []) values)[i]; 70 return null; 71 } 72 73 76 public Object getProperty(QualifiedName key) { 77 Object describedProperty = getDescribedProperty(key); 78 if (describedProperty != null) 79 return describedProperty; 80 return contentTypeInfo.getDefaultProperty(key); 81 } 82 83 86 public boolean isRequested(QualifiedName propertyKey) { 87 if ((flags & FLAG_ALL_OPTIONS) != 0) 89 return true; 90 if (keys == null) 92 return false; 93 if (keys instanceof QualifiedName) 95 return keys.equals(propertyKey); 96 QualifiedName[] tmpKeys = (QualifiedName[]) this.keys; 98 for (int i = 0; i < tmpKeys.length; i++) 99 if (tmpKeys[i].equals(propertyKey)) 100 return true; 101 return false; 102 } 103 104 boolean isSet() { 105 if (keys == null || values == null) 106 return false; 107 if (keys instanceof QualifiedName) 108 return true; 109 Object [] tmpValues = (Object []) this.values; 110 for (int i = 0; i < tmpValues.length; i++) 111 if (tmpValues[i] != null) 112 return true; 113 return false; 114 } 115 116 public void markImmutable() { 117 assertMutable(); 118 flags |= FLAG_IMMUTABLE; 119 } 120 121 122 void setContentTypeInfo(IContentTypeInfo info) { 123 this.contentTypeInfo = info; 124 } 125 126 129 public void setProperty(QualifiedName newKey, Object newValue) { 130 assertMutable(); 131 if (keys == null) { 132 if ((flags & FLAG_ALL_OPTIONS) != 0) { 133 keys = newKey; 134 values = newValue; 135 } 136 return; 137 } 138 if (keys.equals(newKey)) { 139 values = newValue; 140 return; 141 } 142 if (keys instanceof QualifiedName) { 143 if ((flags & FLAG_ALL_OPTIONS) != 0) { 144 keys = new QualifiedName[] {(QualifiedName) keys, newKey}; 145 values = new Object [] {values, newValue}; 146 } 147 return; 148 } 149 QualifiedName[] tmpKeys = (QualifiedName[]) this.keys; 150 for (int i = 0; i < tmpKeys.length; i++) 151 if (tmpKeys[i].equals(newKey)) { 152 ((Object []) values)[i] = newValue; 153 return; 154 } 155 if ((flags & FLAG_ALL_OPTIONS) == 0) 156 return; 157 int currentSize = tmpKeys.length; 159 tmpKeys = new QualifiedName[currentSize + 1]; 160 System.arraycopy(keys, 0, tmpKeys, 0, currentSize); 161 Object [] tmpValues = new Object [currentSize + 1]; 162 System.arraycopy(values, 0, tmpValues, 0, currentSize); 163 tmpKeys[tmpKeys.length - 1] = newKey; 164 tmpValues[tmpValues.length - 1] = newValue; 165 keys = tmpKeys; 166 values = tmpValues; 167 } 168 169 public String toString() { 170 StringBuffer result = new StringBuffer ("{"); if (keys != null) 172 if (keys instanceof QualifiedName) { 173 if (values != null) 174 result.append(keys + "=" + values); } else { 176 QualifiedName[] tmpKeys = (QualifiedName[]) keys; 177 Object [] tmpValues = (Object []) values; 178 boolean any = false; 179 for (int i = 0; i < tmpKeys.length; i++) 180 if (tmpValues[i] != null) { 181 result.append(tmpKeys[i] + "=" + tmpValues[i] + ","); any = true; 183 } 184 if (any) 185 result.deleteCharAt(result.length() - 1); 186 } 187 result.append("} : "); result.append(contentTypeInfo.getContentType()); 189 return result.toString(); 190 } 191 } 192 | Popular Tags |