KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > watson > ElementTreeReader


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.watson;
12
13 import java.io.*;
14 import org.eclipse.core.internal.dtree.DataTreeReader;
15 import org.eclipse.core.internal.dtree.IDataFlattener;
16 import org.eclipse.core.internal.utils.Messages;
17 import org.eclipse.core.runtime.*;
18
19 /** <code>ElementTreeReader</code> is the standard implementation
20  * of an element tree serialization reader.
21  *
22  * <p>Subclasses of this reader read can handle current and various
23  * known old formats of a saved element tree, and dispatch internally
24  * to an appropriate reader.
25  *
26  * <p>The reader has an <code>IElementInfoFactory</code>,
27  * which it consults for the schema and for creating
28  * and reading element infos.
29  *
30  * <p>Element tree readers are thread-safe; several
31  * threads may share a single reader provided, of course,
32  * that the <code>IElementInfoFactory</code> is thread-safe.
33  */

34 public class ElementTreeReader {
35     /** The element info factory.
36      */

37     protected IElementInfoFlattener elementInfoFlattener;
38
39     /**
40      * For reading and writing delta trees
41      */

42     protected DataTreeReader dataTreeReader;
43
44     /**
45      * Constructs a new element tree reader that works for
46      * the given element info flattener.
47      */

48     public ElementTreeReader(final IElementInfoFlattener factory) {
49         Assert.isNotNull(factory);
50         elementInfoFlattener = factory;
51
52         /* wrap the IElementInfoFlattener in an IDataFlattener */
53         IDataFlattener f = new IDataFlattener() {
54             public void writeData(IPath path, Object JavaDoc data, DataOutput output) {
55                 //not needed
56
}
57
58             public Object JavaDoc readData(IPath path, DataInput input) throws IOException {
59                 //never read the root node of an ElementTree
60
//this node is reserved for the parent backpointer
61
if (!Path.ROOT.equals(path))
62                     return factory.readElement(path, input);
63                 return null;
64             }
65         };
66         dataTreeReader = new DataTreeReader(f);
67     }
68
69     /**
70      * Returns the appropriate reader for the given version.
71      */

72     public ElementTreeReader getReader(int formatVersion) throws IOException {
73         if (formatVersion == 1)
74             return new ElementTreeReaderImpl_1(elementInfoFlattener);
75         throw new IOException(Messages.watson_unknown);
76     }
77
78     /**
79      * Reads an element tree delta from the input stream, and
80      * reconstructs it as a delta on the given tree.
81      */

82     public ElementTree readDelta(ElementTree completeTree, DataInput input) throws IOException {
83         /* Dispatch to the appropriate reader. */
84         ElementTreeReader realReader = getReader(readNumber(input));
85         return realReader.readDelta(completeTree, input);
86     }
87
88     /**
89      * Reads a chain of ElementTrees from the given input stream.
90      * @return A chain of ElementTrees, where the first tree in the list is
91      * complete, and all other trees are deltas on the previous tree in the list.
92      */

93     public ElementTree[] readDeltaChain(DataInput input) throws IOException {
94         /* Dispatch to the appropriate reader. */
95         ElementTreeReader realReader = getReader(readNumber(input));
96         return realReader.readDeltaChain(input);
97     }
98
99     /**
100      * Reads an integer stored in compact format. Numbers between
101      * 0 and 254 inclusive occupy 1 byte; other numbers occupy 5 bytes,
102      * the first byte being 0xff and the next 4 bytes being the standard
103      * representation of an int.
104      */

105     protected static int readNumber(DataInput input) throws IOException {
106         byte b = input.readByte();
107         int number = (b & 0xff); // not a no-op! converts unsigned byte to int
108

109         if (number == 0xff) { // magic escape value
110
number = input.readInt();
111         }
112         return number;
113     }
114
115     /**
116      * Reads an element tree from the input stream and returns it.
117      * This method actually just dispatches to the appropriate reader
118      * depending on the stream version id.
119      */

120     public ElementTree readTree(DataInput input) throws IOException {
121         /* Dispatch to the appropriate reader. */
122         ElementTreeReader realReader = getReader(readNumber(input));
123         return realReader.readTree(input);
124     }
125 }
126
Popular Tags