1 11 package org.eclipse.core.internal.resources; 12 13 import java.io.UnsupportedEncodingException ; 14 import java.util.Map ; 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 , IStringPoolParticipant { 20 21 protected static final Integer INTEGER_ONE = new Integer (1); 23 protected static final Integer INTEGER_TWO = new Integer (2); 24 protected static final Integer INTEGER_ZERO = new Integer (0); 25 26 protected static final long UNDEFINED_ID = -1; 28 29 protected Map attributes = null; 30 31 32 protected long creationTime = 0; 33 34 35 protected long id = UNDEFINED_ID; 36 37 38 protected String type = null; 39 40 44 protected static Object checkValidAttribute(Object value) { 45 if (value == null) 46 return null; 47 if (value instanceof String ) { 48 String valueString = (String ) value; 50 if (valueString.length() < 21000) 52 return value; 53 byte[] bytes; 54 try { 55 bytes = valueString.getBytes(("UTF-8")); } catch (UnsupportedEncodingException uee) { 57 return value; 59 } 60 if (bytes.length > 65535) { 61 String msg = "Marker property value is too long: " + valueString.substring(0, 10000); Assert.isTrue(false, msg); 63 } 64 return value; 65 } 66 if (value instanceof Boolean ) { 67 return ((Boolean ) value).booleanValue() ? Boolean.TRUE : Boolean.FALSE; 69 } 70 if (value instanceof Integer ) { 71 switch (((Integer ) 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 throw new IllegalArgumentException (); 84 } 85 86 public MarkerInfo() { 87 super(); 88 } 89 90 93 public Object clone() { 94 try { 95 MarkerInfo copy = (MarkerInfo) super.clone(); 96 copy.attributes = getAttributes(true); 98 return copy; 99 } catch (CloneNotSupportedException e) { 100 return null; 102 } 103 } 104 105 public Object getAttribute(String attributeName) { 106 return attributes == null ? null : attributes.get(attributeName); 107 } 108 109 public Map getAttributes() { 110 return getAttributes(true); 111 } 112 113 public Map getAttributes(boolean makeCopy) { 114 if (attributes == null) 115 return null; 116 return makeCopy ? new MarkerAttributeMap(attributes) : attributes; 117 } 118 119 public Object [] getAttributes(String [] attributeNames) { 120 Object [] result = new Object [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 getType() { 135 return type; 136 } 137 138 public void internalSetAttributes(Map map) { 139 attributes = map; 142 } 143 144 public void setAttribute(String attributeName, Object 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 map) { 163 if (map == null) 164 attributes = null; 165 else 166 attributes = new MarkerAttributeMap(map); 167 } 168 169 public void setAttributes(String [] attributeNames, Object [] 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 value) { 184 type = value; 185 } 186 187 190 public void shareStrings(StringPool set) { 191 type = set.add(type); 192 Map map = attributes; 193 if (map instanceof IStringPoolParticipant) 194 ((IStringPoolParticipant) map).shareStrings(set); 195 } 196 } 197 | Popular Tags |