KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > MarkerInfo


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.resources;
12
13 import java.io.UnsupportedEncodingException JavaDoc;
14 import java.util.Map JavaDoc;
15 import org.eclipse.core.internal.utils.IStringPoolParticipant;
16 import org.eclipse.core.internal.utils.StringPool;
17 import org.eclipse.core.runtime.Assert;
18
19 public class MarkerInfo implements IMarkerSetElement, Cloneable JavaDoc, IStringPoolParticipant {
20
21     // well known Integer values
22
protected static final Integer JavaDoc INTEGER_ONE = new Integer JavaDoc(1);
23     protected static final Integer JavaDoc INTEGER_TWO = new Integer JavaDoc(2);
24     protected static final Integer JavaDoc INTEGER_ZERO = new Integer JavaDoc(0);
25     
26     //
27
protected static final long UNDEFINED_ID = -1;
28     /** The store of attributes for this marker. */
29     protected Map JavaDoc attributes = null;
30
31     /** The creation time for this marker. */
32     protected long creationTime = 0;
33
34     /** Marker identifier. */
35     protected long id = UNDEFINED_ID;
36
37     /** The type of this marker. */
38     protected String JavaDoc type = null;
39
40     /**
41      * Returns whether the given object is a valid attribute value. Returns
42      * either the attribute or an equal canonical substitute.
43      */

44     protected static Object JavaDoc checkValidAttribute(Object JavaDoc value) {
45         if (value == null)
46             return null;
47         if (value instanceof String JavaDoc) {
48             //we cannot write attributes whose UTF encoding exceeds 65535 bytes.
49
String JavaDoc valueString = (String JavaDoc) value;
50             //optimized test based on maximum 3 bytes per character
51
if (valueString.length() < 21000)
52                 return value;
53             byte[] bytes;
54             try {
55                 bytes = valueString.getBytes(("UTF-8"));//$NON-NLS-1$
56
} catch (UnsupportedEncodingException JavaDoc uee) {
57                 //cannot validate further
58
return value;
59             }
60             if (bytes.length > 65535) {
61                 String JavaDoc msg = "Marker property value is too long: " + valueString.substring(0, 10000); //$NON-NLS-1$
62
Assert.isTrue(false, msg);
63             }
64             return value;
65         }
66         if (value instanceof Boolean JavaDoc) {
67             //return canonical boolean
68
return ((Boolean JavaDoc) value).booleanValue() ? Boolean.TRUE : Boolean.FALSE;
69         }
70         if (value instanceof Integer JavaDoc) {
71             //replace common integers with canonical values
72
switch (((Integer JavaDoc) value).intValue()) {
73                 case 0 :
74                     return INTEGER_ZERO;
75                 case 1 :
76                     return INTEGER_ONE;
77                 case 2 :
78                     return INTEGER_TWO;
79             }
80             return value;
81         }
82         //if we got here, it's an invalid attribute value type
83
throw new IllegalArgumentException JavaDoc();
84     }
85
86     public MarkerInfo() {
87         super();
88     }
89
90     /**
91      * See Object#clone.
92      */

93     public Object JavaDoc clone() {
94         try {
95             MarkerInfo copy = (MarkerInfo) super.clone();
96             //copy the attribute table contents
97
copy.attributes = getAttributes(true);
98             return copy;
99         } catch (CloneNotSupportedException JavaDoc e) {
100             //cannot happen because this class implements Cloneable
101
return null;
102         }
103     }
104
105     public Object JavaDoc getAttribute(String JavaDoc attributeName) {
106         return attributes == null ? null : attributes.get(attributeName);
107     }
108
109     public Map JavaDoc getAttributes() {
110         return getAttributes(true);
111     }
112
113     public Map JavaDoc getAttributes(boolean makeCopy) {
114         if (attributes == null)
115             return null;
116         return makeCopy ? new MarkerAttributeMap(attributes) : attributes;
117     }
118
119     public Object JavaDoc[] getAttributes(String JavaDoc[] attributeNames) {
120         Object JavaDoc[] result = new Object JavaDoc[attributeNames.length];
121         for (int i = 0; i < attributeNames.length; i++)
122             result[i] = getAttribute(attributeNames[i]);
123         return result;
124     }
125
126     public long getCreationTime() {
127         return creationTime;
128     }
129
130     public long getId() {
131         return id;
132     }
133
134     public String JavaDoc getType() {
135         return type;
136     }
137
138     public void internalSetAttributes(Map JavaDoc map) {
139         //the cast effectively acts as an assertion to make sure
140
//the right kind of map is being used
141
attributes = map;
142     }
143
144     public void setAttribute(String JavaDoc attributeName, Object JavaDoc value) {
145         value = checkValidAttribute(value);
146         if (attributes == null) {
147             if (value == null)
148                 return;
149             attributes = new MarkerAttributeMap();
150             attributes.put(attributeName, value);
151         } else {
152             if (value == null) {
153                 attributes.remove(attributeName);
154                 if (attributes.isEmpty())
155                     attributes = null;
156             } else {
157                 attributes.put(attributeName, value);
158             }
159         }
160     }
161
162     public void setAttributes(Map JavaDoc map) {
163         if (map == null)
164             attributes = null;
165         else
166             attributes = new MarkerAttributeMap(map);
167     }
168
169     public void setAttributes(String JavaDoc[] attributeNames, Object JavaDoc[] values) {
170         Assert.isTrue(attributeNames.length == values.length);
171         for (int i = 0; i < attributeNames.length; i++)
172             setAttribute(attributeNames[i], values[i]);
173     }
174
175     public void setCreationTime(long value) {
176         creationTime = value;
177     }
178
179     public void setId(long value) {
180         id = value;
181     }
182
183     public void setType(String JavaDoc value) {
184         type = value;
185     }
186
187     /* (non-Javadoc
188      * Method declared on IStringPoolParticipant
189      */

190     public void shareStrings(StringPool set) {
191         type = set.add(type);
192         Map JavaDoc map = attributes;
193         if (map instanceof IStringPoolParticipant)
194             ((IStringPoolParticipant) map).shareStrings(set);
195     }
196 }
197
Popular Tags