KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
14 import java.util.*;
15 import org.eclipse.core.internal.utils.Messages;
16 import org.eclipse.core.resources.IResourceDelta;
17 import org.eclipse.core.resources.IResourceStatus;
18 import org.eclipse.core.runtime.*;
19
20 /**
21  * This class is used to read markers from disk. This is for version 2. Here
22  * is the file format:
23  */

24 public class MarkerReader_3 extends MarkerReader {
25
26     // type constants
27
public static final byte INDEX = 1;
28     public static final byte QNAME = 2;
29
30     // marker attribute types
31
public static final byte ATTRIBUTE_NULL = 0;
32     public static final byte ATTRIBUTE_BOOLEAN = 1;
33     public static final byte ATTRIBUTE_INTEGER = 2;
34     public static final byte ATTRIBUTE_STRING = 3;
35
36     public MarkerReader_3(Workspace workspace) {
37         super(workspace);
38     }
39
40     /**
41      * SAVE_FILE -> VERSION_ID RESOURCE+
42      * VERSION_ID -> int
43      * RESOURCE -> RESOURCE_PATH MARKERS_SIZE MARKER+
44      * RESOURCE_PATH -> String
45      * MARKERS_SIZE -> int
46      * MARKER -> MARKER_ID TYPE ATTRIBUTES_SIZE ATTRIBUTE* CREATION_TIME
47      * MARKER_ID -> long
48      * TYPE -> INDEX | QNAME
49      * INDEX -> byte int
50      * QNAME -> byte String
51      * ATTRIBUTES_SIZE -> short
52      * ATTRIBUTE -> ATTRIBUTE_KEY ATTRIBUTE_VALUE
53      * ATTRIBUTE_KEY -> String
54      * ATTRIBUTE_VALUE -> INTEGER_VALUE | BOOLEAN_VALUE | STRING_VALUE | NULL_VALUE
55      * INTEGER_VALUE -> byte int
56      * BOOLEAN_VALUE -> byte boolean
57      * STRING_VALUE -> byte String
58      * NULL_VALUE -> byte
59      * CREATION_TIME -> long
60      */

61     public void read(DataInputStream input, boolean generateDeltas) throws IOException, CoreException {
62         try {
63             List readTypes = new ArrayList(5);
64             while (true) {
65                 IPath path = new Path(input.readUTF());
66                 int markersSize = input.readInt();
67                 MarkerSet markers = new MarkerSet(markersSize);
68                 for (int i = 0; i < markersSize; i++)
69                     markers.add(readMarkerInfo(input, readTypes));
70                 // if the resource doesn't exist then return. ensure we do this after
71
// reading the markers from the file so we don't get into an
72
// inconsistent state.
73
ResourceInfo info = workspace.getResourceInfo(path, false, false);
74                 if (info == null)
75                     continue;
76                 info.setMarkers(markers);
77                 if (generateDeltas) {
78                     // Iterate over all elements and add not null ones. This saves us from copying
79
// and shrinking the array.
80
Resource resource = workspace.newResource(path, info.getType());
81                     IMarkerSetElement[] infos = markers.elements;
82                     ArrayList deltas = new ArrayList(infos.length);
83                     for (int i = 0; i < infos.length; i++)
84                         if (infos[i] != null)
85                             deltas.add(new MarkerDelta(IResourceDelta.ADDED, resource, (MarkerInfo) infos[i]));
86                     workspace.getMarkerManager().changedMarkers(resource, (IMarkerSetElement[]) deltas.toArray(new IMarkerSetElement[deltas.size()]));
87                 }
88             }
89         } catch (EOFException e) {
90             // ignore end of file
91
}
92     }
93
94     private Map readAttributes(DataInputStream input) throws IOException {
95         int attributesSize = input.readShort();
96         if (attributesSize == 0)
97             return null;
98         Map result = new MarkerAttributeMap(attributesSize);
99         for (int j = 0; j < attributesSize; j++) {
100             String JavaDoc key = input.readUTF();
101             byte type = input.readByte();
102             Object JavaDoc value = null;
103             switch (type) {
104                 case ATTRIBUTE_INTEGER :
105                     int intValue = input.readInt();
106                     //canonicalize well known values (marker severity, task priority)
107
switch (intValue) {
108                         case 0:
109                             value = MarkerInfo.INTEGER_ZERO;
110                             break;
111                         case 1:
112                             value = MarkerInfo.INTEGER_ONE;
113                             break;
114                         case 2:
115                             value = MarkerInfo.INTEGER_TWO;
116                             break;
117                         default:
118                             value = new Integer JavaDoc(intValue);
119                     }
120                     break;
121                 case ATTRIBUTE_BOOLEAN :
122                     value = input.readBoolean() ? Boolean.TRUE : Boolean.FALSE;
123                     break;
124                 case ATTRIBUTE_STRING :
125                     value = input.readUTF();
126                     break;
127                 case ATTRIBUTE_NULL :
128                     // do nothing
129
break;
130             }
131             if (value != null)
132                 result.put(key, value);
133         }
134         return result.isEmpty() ? null : result;
135     }
136
137     private MarkerInfo readMarkerInfo(DataInputStream input, List readTypes) throws IOException, CoreException {
138         MarkerInfo info = new MarkerInfo();
139         info.setId(input.readLong());
140         byte constant = input.readByte();
141         switch (constant) {
142             case QNAME :
143                 String JavaDoc type = input.readUTF();
144                 info.setType(type);
145                 readTypes.add(type);
146                 break;
147             case INDEX :
148                 info.setType((String JavaDoc) readTypes.get(input.readInt()));
149                 break;
150             default :
151                 //if we get here the marker file is corrupt
152
String JavaDoc msg = Messages.resources_readMarkers;
153                 throw new ResourceException(IResourceStatus.FAILED_READ_METADATA, null, msg, null);
154         }
155         info.internalSetAttributes(readAttributes(input));
156         info.setCreationTime(input.readLong());
157         return info;
158     }
159 }
160
Popular Tags