KickJava   Java API By Example, From Geeks To Geeks.

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


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.DataInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.*;
16 import org.eclipse.core.internal.utils.Messages;
17 import org.eclipse.core.resources.IResourceStatus;
18 import org.eclipse.core.runtime.*;
19
20 //
21
/**
22  * Snapshot the markers for the specified resource to the given output stream.
23  */

24 public class MarkerSnapshotReader_2 extends MarkerSnapshotReader {
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 MarkerSnapshotReader_2(Workspace workspace) {
37         super(workspace);
38     }
39
40     /**
41      * SNAP_FILE -> [VERSION_ID RESOURCE]*
42      * VERSION_ID -> int (used for backwards compatibiliy)
43      * RESOURCE -> RESOURCE_PATH MARKER_SIZE MARKER+
44      * RESOURCE_PATH -> String
45      * MARKER_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 -> BOOLEAN_VALUE | INTEGER_VALUE | STRING_VALUE | NULL_VALUE
55      * BOOLEAN_VALUE -> byte boolean
56      * INTEGER_VALUE -> byte int
57      * STRING_VALUE -> byte String
58      * NULL_VALUE -> byte
59      * CREATION_TIME -> long
60      */

61     public void read(DataInputStream JavaDoc input) throws IOException JavaDoc, CoreException {
62         IPath path = new Path(input.readUTF());
63         int markersSize = input.readInt();
64         MarkerSet markers = new MarkerSet(markersSize);
65         ArrayList readTypes = new ArrayList();
66         for (int i = 0; i < markersSize; i++)
67             markers.add(readMarkerInfo(input, readTypes));
68         // we've read all the markers from the file for this snap. if the resource
69
// doesn't exist in the workspace then consider this a delete and return
70
ResourceInfo info = workspace.getResourceInfo(path, false, false);
71         if (info == null)
72             return;
73         info.setMarkers(markers);
74         info.clear(ICoreConstants.M_MARKERS_SNAP_DIRTY);
75     }
76
77     private Map readAttributes(DataInputStream JavaDoc input) throws IOException JavaDoc {
78         short attributesSize = input.readShort();
79         if (attributesSize == 0)
80             return null;
81         Map result = new MarkerAttributeMap(attributesSize);
82         for (int j = 0; j < attributesSize; j++) {
83             String JavaDoc key = input.readUTF();
84             byte type = input.readByte();
85             Object JavaDoc value = null;
86             switch (type) {
87                 case ATTRIBUTE_INTEGER :
88                     int intValue = input.readInt();
89                     switch (intValue) {
90                         case 0:
91                             value = MarkerInfo.INTEGER_ZERO;
92                             break;
93                         case 1:
94                             value = MarkerInfo.INTEGER_ONE;
95                             break;
96                         case 2:
97                             value = MarkerInfo.INTEGER_TWO;
98                             break;
99                         default:
100                             value = new Integer JavaDoc(intValue);
101                     }
102                     break;
103                 case ATTRIBUTE_BOOLEAN :
104                     value = input.readBoolean() ? Boolean.TRUE : Boolean.FALSE;
105                     break;
106                 case ATTRIBUTE_STRING :
107                     value = input.readUTF();
108                     break;
109                 case ATTRIBUTE_NULL :
110                     // do nothing
111
break;
112             }
113             if (value != null)
114                 result.put(key, value);
115         }
116         return result.isEmpty() ? null : result;
117     }
118
119     private MarkerInfo readMarkerInfo(DataInputStream JavaDoc input, List readTypes) throws IOException JavaDoc, CoreException {
120         MarkerInfo info = new MarkerInfo();
121         info.setId(input.readLong());
122         byte constant = input.readByte();
123         switch (constant) {
124             case QNAME :
125                 String JavaDoc type = input.readUTF();
126                 info.setType(type);
127                 readTypes.add(type);
128                 break;
129             case INDEX :
130                 info.setType((String JavaDoc) readTypes.get(input.readInt()));
131                 break;
132             default :
133                 //if we get here the marker file is corrupt
134
String JavaDoc msg = Messages.resources_readMarkers;
135                 throw new ResourceException(IResourceStatus.FAILED_READ_METADATA, null, msg, null);
136         }
137         info.internalSetAttributes(readAttributes(input));
138         info.setCreationTime(input.readLong());
139         return info;
140     }
141 }
142
Popular Tags