KickJava   Java API By Example, From Geeks To Geeks.

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


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.DataOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.*;
16 import org.eclipse.core.internal.watson.IPathRequestor;
17
18 //
19
public class MarkerWriter {
20
21     protected MarkerManager manager;
22
23     // version numbers
24
public static final int MARKERS_SAVE_VERSION = 3;
25     public static final int MARKERS_SNAP_VERSION = 2;
26
27     // type constants
28
public static final byte INDEX = 1;
29     public static final byte QNAME = 2;
30
31     // marker attribute types
32
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     /**
43      * Returns an Object array of length 2. The first element is an Integer which is the number
44      * of persistent markers found. The second element is an array of boolean values, with a
45      * value of true meaning that the marker at that index is to be persisted.
46      */

47     private Object JavaDoc[] filterMarkers(IMarkerSetElement[] markers) {
48         Object JavaDoc[] result = new Object JavaDoc[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 JavaDoc(count);
59         result[1] = isPersistent;
60         return result;
61     }
62
63     /**
64      * SAVE_FILE -> VERSION_ID RESOURCE+
65      * VERSION_ID -> int
66      * RESOURCE -> RESOURCE_PATH MARKERS_SIZE MARKER+
67      * RESOURCE_PATH -> String
68      * MARKERS_SIZE -> int
69      * MARKER -> MARKER_ID TYPE ATTRIBUTES_SIZE ATTRIBUTE* CREATION_TIME
70      * MARKER_ID -> long
71      * TYPE -> INDEX | QNAME
72      * INDEX -> byte int
73      * QNAME -> byte String
74      * ATTRIBUTES_SIZE -> short
75      * ATTRIBUTE -> ATTRIBUTE_KEY ATTRIBUTE_VALUE
76      * ATTRIBUTE_KEY -> String
77      * ATTRIBUTE_VALUE -> INTEGER_VALUE | BOOLEAN_VALUE | STRING_VALUE | NULL_VALUE
78      * INTEGER_VALUE -> byte int
79      * BOOLEAN_VALUE -> byte boolean
80      * STRING_VALUE -> byte String
81      * NULL_VALUE -> byte
82      * CREATION_TIME -> long
83      *
84      */

85     public void save(ResourceInfo info, IPathRequestor requestor, DataOutputStream JavaDoc output, List writtenTypes) throws IOException JavaDoc {
86         // phantom resources don't have markers
87
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         // filter out the markers...determine if there are any persistent ones
94
Object JavaDoc[] result = filterMarkers(elements);
95         int count = ((Integer JavaDoc) result[0]).intValue();
96         if (count == 0)
97             return;
98         // if this is the first set of markers that we have written, then
99
// write the version id for the file.
100
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     /**
111      * Snapshot the markers for the specified resource to the given output stream.
112      *
113      * SNAP_FILE -> [VERSION_ID RESOURCE]*
114      * VERSION_ID -> int (used for backwards compatibiliy)
115      * RESOURCE -> RESOURCE_PATH MARKER_SIZE MARKER+
116      * RESOURCE_PATH -> String
117      * MARKER_SIZE -> int
118      * MARKER -> MARKER_ID TYPE ATTRIBUTES_SIZE ATTRIBUTE* CREATION_TIME
119      * MARKER_ID -> long
120      * TYPE -> INDEX | QNAME
121      * INDEX -> byte int
122      * QNAME -> byte String
123      * ATTRIBUTES_SIZE -> short
124      * ATTRIBUTE -> ATTRIBUTE_KEY ATTRIBUTE_VALUE
125      * ATTRIBUTE_KEY -> String
126      * ATTRIBUTE_VALUE -> BOOLEAN_VALUE | INTEGER_VALUE | STRING_VALUE | NULL_VALUE
127      * BOOLEAN_VALUE -> byte boolean
128      * INTEGER_VALUE -> byte int
129      * STRING_VALUE -> byte String
130      * NULL_VALUE -> byte
131      * CREATION_TIME -> long
132      */

133     public void snap(ResourceInfo info, IPathRequestor requestor, DataOutputStream JavaDoc output) throws IOException JavaDoc {
134         // phantom resources don't have markers
135
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         // filter out the markers...determine if there are any persistent ones
144
Object JavaDoc[] result = filterMarkers(elements);
145         int count = ((Integer JavaDoc) result[0]).intValue();
146         // write the version id for the snapshot.
147
output.writeInt(MARKERS_SNAP_VERSION);
148         boolean[] isPersistent = (boolean[]) result[1];
149         output.writeUTF(requestor.requestPath().toString());
150         // always write out the count...even if its zero. this will help
151
// use pick up marker deletions from our snapshot.
152
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     /*
161      * Write out the given marker attributes to the given output stream.
162      */

163     private void write(Map attributes, DataOutputStream JavaDoc output) throws IOException JavaDoc {
164         output.writeShort(attributes.size());
165         for (Iterator i = attributes.keySet().iterator(); i.hasNext();) {
166             String JavaDoc key = (String JavaDoc) i.next();
167             output.writeUTF(key);
168             Object JavaDoc value = attributes.get(key);
169             if (value instanceof Integer JavaDoc) {
170                 output.writeByte(ATTRIBUTE_INTEGER);
171                 output.writeInt(((Integer JavaDoc) value).intValue());
172                 continue;
173             }
174             if (value instanceof Boolean JavaDoc) {
175                 output.writeByte(ATTRIBUTE_BOOLEAN);
176                 output.writeBoolean(((Boolean JavaDoc) value).booleanValue());
177                 continue;
178             }
179             if (value instanceof String JavaDoc) {
180                 output.writeByte(ATTRIBUTE_STRING);
181                 output.writeUTF((String JavaDoc) value);
182                 continue;
183             }
184             // otherwise we came across an attribute of an unknown type
185
// so just write out null since we don't know how to marshal it.
186
output.writeByte(ATTRIBUTE_NULL);
187         }
188     }
189
190     private void write(MarkerInfo info, DataOutputStream JavaDoc output, List writtenTypes) throws IOException JavaDoc {
191         output.writeLong(info.getId());
192         // if we have already written the type once, then write an integer
193
// constant to represent it instead to remove duplication
194
String JavaDoc 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         // write out the size of the attribute table and
206
// then each attribute.
207
if (info.getAttributes(false) == null) {
208             output.writeShort(0);
209         } else
210             write(info.getAttributes(false), output);
211
212         // write out the creation time
213
output.writeLong(info.getCreationTime());
214     }
215 }
216
Popular Tags