KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > MarkerSnapshotReader_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.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_1 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_1(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*
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      */

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