1 11 package org.eclipse.core.internal.resources; 12 13 import java.io.DataOutputStream ; 14 import java.io.IOException ; 15 import java.util.*; 16 import org.eclipse.core.internal.watson.IPathRequestor; 17 18 public class MarkerWriter { 20 21 protected MarkerManager manager; 22 23 public static final int MARKERS_SAVE_VERSION = 3; 25 public static final int MARKERS_SNAP_VERSION = 2; 26 27 public static final byte INDEX = 1; 29 public static final byte QNAME = 2; 30 31 public static final byte ATTRIBUTE_NULL = 0; 33 public static final byte ATTRIBUTE_BOOLEAN = 1; 34 public static final byte ATTRIBUTE_INTEGER = 2; 35 public static final byte ATTRIBUTE_STRING = 3; 36 37 public MarkerWriter(MarkerManager manager) { 38 super(); 39 this.manager = manager; 40 } 41 42 47 private Object [] filterMarkers(IMarkerSetElement[] markers) { 48 Object [] result = new Object [2]; 49 boolean[] isPersistent = new boolean[markers.length]; 50 int count = 0; 51 for (int i = 0; i < markers.length; i++) { 52 MarkerInfo info = (MarkerInfo) markers[i]; 53 if (manager.isPersistent(info)) { 54 isPersistent[i] = true; 55 count++; 56 } 57 } 58 result[0] = new Integer (count); 59 result[1] = isPersistent; 60 return result; 61 } 62 63 85 public void save(ResourceInfo info, IPathRequestor requestor, DataOutputStream output, List writtenTypes) throws IOException { 86 if (info.isSet(ICoreConstants.M_PHANTOM)) 88 return; 89 MarkerSet markers = info.getMarkers(false); 90 if (markers == null) 91 return; 92 IMarkerSetElement[] elements = markers.elements(); 93 Object [] result = filterMarkers(elements); 95 int count = ((Integer ) result[0]).intValue(); 96 if (count == 0) 97 return; 98 if (output.size() == 0) 101 output.writeInt(MARKERS_SAVE_VERSION); 102 boolean[] isPersistent = (boolean[]) result[1]; 103 output.writeUTF(requestor.requestPath().toString()); 104 output.writeInt(count); 105 for (int i = 0; i < elements.length; i++) 106 if (isPersistent[i]) 107 write((MarkerInfo) elements[i], output, writtenTypes); 108 } 109 110 133 public void snap(ResourceInfo info, IPathRequestor requestor, DataOutputStream output) throws IOException { 134 if (info.isSet(ICoreConstants.M_PHANTOM)) 136 return; 137 if (!info.isSet(ICoreConstants.M_MARKERS_SNAP_DIRTY)) 138 return; 139 MarkerSet markers = info.getMarkers(false); 140 if (markers == null) 141 return; 142 IMarkerSetElement[] elements = markers.elements(); 143 Object [] result = filterMarkers(elements); 145 int count = ((Integer ) result[0]).intValue(); 146 output.writeInt(MARKERS_SNAP_VERSION); 148 boolean[] isPersistent = (boolean[]) result[1]; 149 output.writeUTF(requestor.requestPath().toString()); 150 output.writeInt(count); 153 List writtenTypes = new ArrayList(); 154 for (int i = 0; i < elements.length; i++) 155 if (isPersistent[i]) 156 write((MarkerInfo) elements[i], output, writtenTypes); 157 info.clear(ICoreConstants.M_MARKERS_SNAP_DIRTY); 158 } 159 160 163 private void write(Map attributes, DataOutputStream output) throws IOException { 164 output.writeShort(attributes.size()); 165 for (Iterator i = attributes.keySet().iterator(); i.hasNext();) { 166 String key = (String ) i.next(); 167 output.writeUTF(key); 168 Object value = attributes.get(key); 169 if (value instanceof Integer ) { 170 output.writeByte(ATTRIBUTE_INTEGER); 171 output.writeInt(((Integer ) value).intValue()); 172 continue; 173 } 174 if (value instanceof Boolean ) { 175 output.writeByte(ATTRIBUTE_BOOLEAN); 176 output.writeBoolean(((Boolean ) value).booleanValue()); 177 continue; 178 } 179 if (value instanceof String ) { 180 output.writeByte(ATTRIBUTE_STRING); 181 output.writeUTF((String ) value); 182 continue; 183 } 184 output.writeByte(ATTRIBUTE_NULL); 187 } 188 } 189 190 private void write(MarkerInfo info, DataOutputStream output, List writtenTypes) throws IOException { 191 output.writeLong(info.getId()); 192 String type = info.getType(); 195 int index = writtenTypes.indexOf(type); 196 if (index == -1) { 197 output.writeByte(QNAME); 198 output.writeUTF(type); 199 writtenTypes.add(type); 200 } else { 201 output.writeByte(INDEX); 202 output.writeInt(index); 203 } 204 205 if (info.getAttributes(false) == null) { 208 output.writeShort(0); 209 } else 210 write(info.getAttributes(false), output); 211 212 output.writeLong(info.getCreationTime()); 214 } 215 } 216 | Popular Tags |