KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > watson > ElementTreeReaderImpl_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.watson;
12
13 import java.io.DataInput JavaDoc;
14 import java.io.IOException JavaDoc;
15 import org.eclipse.core.internal.dtree.DeltaDataTree;
16
17 /** <code>ElementTreeReader_1</code> is an implementation
18  * of the <code>ElementTreeReader</code> for format version 1.
19  *
20  * <p>Instances of this reader read only format 1
21  * of a saved element tree (they do not deal with
22  * compatibility issues).
23  *
24  * @see ElementTreeReader
25  */

26 /* package */class ElementTreeReaderImpl_1 extends ElementTreeReader {
27
28     /**
29      * Constructs a new element tree reader that works for
30      * the given element info factory.
31      */

32     ElementTreeReaderImpl_1(IElementInfoFlattener factory) {
33         super(factory);
34     }
35
36     /**
37      * Reads an element tree delta from the input stream, and
38      * reconstructs it as a delta on the given tree.
39      */

40     public ElementTree readDelta(ElementTree parentTree, DataInput JavaDoc input) throws IOException JavaDoc {
41         DeltaDataTree complete = parentTree.getDataTree();
42         DeltaDataTree delta = dataTreeReader.readTree(complete, input);
43
44         //if the delta is empty, just return the parent
45
if (delta.isEmptyDelta())
46             return parentTree;
47
48         ElementTree tree = new ElementTree(delta);
49
50         //copy the user data forward
51
IElementTreeData data = parentTree.getTreeData();
52         if (data != null) {
53             tree.setTreeData((IElementTreeData) data.clone());
54         }
55
56         //make the underlying data tree immutable
57
//can't call immutable() on the ElementTree because
58
//this would attempt to reroot.
59
delta.immutable();
60         return tree;
61     }
62
63     /**
64      * Reads a chain of ElementTrees from the given input stream.
65      * @return A chain of ElementTrees, where the first tree in the list is
66      * complete, and all other trees are deltas on the previous tree in the list.
67      */

68     public ElementTree[] readDeltaChain(DataInput JavaDoc input) throws IOException JavaDoc {
69         /* read the number of trees */
70         int treeCount = readNumber(input);
71         ElementTree[] results = new ElementTree[treeCount];
72
73         if (treeCount <= 0) {
74             return results;
75         }
76
77         /* read the sort order */
78         int[] order = new int[treeCount];
79         for (int i = 0; i < treeCount; i++) {
80             order[i] = readNumber(input);
81         }
82
83         /* read the complete tree */
84         results[order[0]] = super.readTree(input);
85
86         /* reconstitute each of the remaining trees from their written deltas */
87         for (int i = 1; i < treeCount; i++) {
88             results[order[i]] = super.readDelta(results[order[i - 1]], input);
89         }
90
91         return results;
92     }
93
94     /** Part of <code>ElementTreeReader</code> interface.
95      * @see ElementTreeReader
96      */

97     public ElementTree readTree(DataInput JavaDoc input) throws IOException JavaDoc {
98
99         /* The format version number has already been consumed
100          * by ElementTreeReader#readFrom.
101          */

102         ElementTree result = new ElementTree(dataTreeReader.readTree(null, input));
103         return result;
104     }
105 }
106
Popular Tags