KickJava   Java API By Example, From Geeks To Geeks.

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


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 1.
22  */

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

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