KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > content > ContentDescription


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc CHARSET_UTF_16 = "UTF-16"; //$NON-NLS-1$
19
private static final String JavaDoc CHARSET_UTF_8 = "UTF-8"; //$NON-NLS-1$
20
private static final byte FLAG_ALL_OPTIONS = 0x01;
21     private static final byte FLAG_IMMUTABLE = 0x02;
22     private byte flags;
23     private Object JavaDoc keys;
24     private Object JavaDoc 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 JavaDoc[requested.length];
35         } else if (requested.length == 1)
36             keys = requested[0];
37         // if requested.length == 0 then keys == null (no options to describe!)
38
}
39
40     private void assertMutable() {
41         if ((flags & FLAG_IMMUTABLE) != 0)
42             throw new IllegalStateException JavaDoc("Content description is immutable"); //$NON-NLS-1$
43
}
44
45     /**
46      * @see IContentDescription
47      */

48     public String JavaDoc 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             // UTF-16 will properly recognize the BOM
54
return CHARSET_UTF_16;
55         return (String JavaDoc) getProperty(CHARSET);
56     }
57
58     private Object JavaDoc getDescribedProperty(QualifiedName key) {
59         // no values have been set
60
if (values == null)
61             return null;
62         // a single property may have been set
63
if (keys instanceof QualifiedName)
64             return keys.equals(key) ? values : null;
65         // multiple properties may have been set
66
QualifiedName[] tmpKeys = (QualifiedName[]) this.keys;
67         for (int i = 0; i < tmpKeys.length; i++)
68             if (tmpKeys[i].equals(key))
69                 return ((Object JavaDoc[]) values)[i];
70         return null;
71     }
72
73     /**
74      * @see IContentDescription
75      */

76     public Object JavaDoc getProperty(QualifiedName key) {
77         Object JavaDoc describedProperty = getDescribedProperty(key);
78         if (describedProperty != null)
79             return describedProperty;
80         return contentTypeInfo.getDefaultProperty(key);
81     }
82
83     /**
84      * @see IContentDescription
85      */

86     public boolean isRequested(QualifiedName propertyKey) {
87         // all options requested
88
if ((flags & FLAG_ALL_OPTIONS) != 0)
89             return true;
90         // no options requested
91
if (keys == null)
92             return false;
93         // a single option requested
94
if (keys instanceof QualifiedName)
95             return keys.equals(propertyKey);
96         // some (but not all) options requested
97
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 JavaDoc[] tmpValues = (Object JavaDoc[]) 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     /** Overrides content type info object. */
122     void setContentTypeInfo(IContentTypeInfo info) {
123         this.contentTypeInfo = info;
124     }
125
126     /**
127      * @see IContentDescription
128      */

129     public void setProperty(QualifiedName newKey, Object JavaDoc 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 JavaDoc[] {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 JavaDoc[]) values)[i] = newValue;
153                 return;
154             }
155         if ((flags & FLAG_ALL_OPTIONS) == 0)
156             return;
157         // need to resize arrays
158
int currentSize = tmpKeys.length;
159         tmpKeys = new QualifiedName[currentSize + 1];
160         System.arraycopy(keys, 0, tmpKeys, 0, currentSize);
161         Object JavaDoc[] tmpValues = new Object JavaDoc[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 JavaDoc toString() {
170         StringBuffer JavaDoc result = new StringBuffer JavaDoc("{"); //$NON-NLS-1$
171
if (keys != null)
172             if (keys instanceof QualifiedName) {
173                 if (values != null)
174                     result.append(keys + "=" + values); //$NON-NLS-1$
175
} else {
176                 QualifiedName[] tmpKeys = (QualifiedName[]) keys;
177                 Object JavaDoc[] tmpValues = (Object JavaDoc[]) 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] + ","); //$NON-NLS-1$ //$NON-NLS-2$
182
any = true;
183                     }
184                 if (any)
185                     result.deleteCharAt(result.length() - 1);
186             }
187         result.append("} : "); //$NON-NLS-1$
188
result.append(contentTypeInfo.getContentType());
189         return result.toString();
190     }
191 }
192
Popular Tags