1 11 package org.eclipse.core.internal.dtree; 12 13 import java.io.DataInput ; 14 import java.io.IOException ; 15 import org.eclipse.core.internal.utils.Messages; 16 import org.eclipse.core.runtime.*; 17 18 21 public class DataTreeReader { 22 25 protected IDataFlattener flatener; 26 27 30 protected DataInput input; 31 32 35 public DataTreeReader(IDataFlattener f) { 36 flatener = f; 37 } 38 39 42 protected boolean hasData(int nodeType) { 43 switch (nodeType) { 44 case AbstractDataTreeNode.T_COMPLETE_NODE : 45 case AbstractDataTreeNode.T_DELTA_NODE : 46 return true; 47 case AbstractDataTreeNode.T_DELETED_NODE : 48 case AbstractDataTreeNode.T_NO_DATA_DELTA_NODE : 49 default : 50 return false; 51 } 52 } 53 54 57 protected AbstractDataTreeNode readNode(IPath parentPath) throws IOException { 58 59 String name = input.readUTF(); 60 61 62 int nodeType = readNumber(); 63 64 65 IPath path; 66 67 68 if (parentPath != null) { 69 path = parentPath.append(name); 70 } else { 71 path = Path.ROOT; 72 } 73 74 Object data = null; 75 if (hasData(nodeType)) { 76 77 78 int dataFlag = readNumber(); 79 if (dataFlag != 0) { 80 data = flatener.readData(path, input); 81 } 82 } 83 84 85 int childCount = readNumber(); 86 87 88 AbstractDataTreeNode[] children; 89 if (childCount == 0) { 90 children = AbstractDataTreeNode.NO_CHILDREN; 91 } else { 92 children = new AbstractDataTreeNode[childCount]; 93 for (int i = 0; i < childCount; i++) { 94 children[i] = readNode(path); 95 } 96 } 97 98 99 switch (nodeType) { 100 case AbstractDataTreeNode.T_COMPLETE_NODE : 101 return new DataTreeNode(name, data, children); 102 case AbstractDataTreeNode.T_DELTA_NODE : 103 return new DataDeltaNode(name, data, children); 104 case AbstractDataTreeNode.T_DELETED_NODE : 105 return new DeletedNode(name); 106 case AbstractDataTreeNode.T_NO_DATA_DELTA_NODE : 107 return new NoDataDeltaNode(name, children); 108 default : 109 Assert.isTrue(false, Messages.dtree_switchError); 110 return null; 111 } 112 } 113 114 120 protected int readNumber() throws IOException { 121 byte b = input.readByte(); 122 int number = (b & 0xff); 124 if (number == 0xff) { number = input.readInt(); 126 } 127 return number; 128 } 129 130 133 public DeltaDataTree readTree(DeltaDataTree parent, DataInput input) throws IOException { 134 this.input = input; 135 AbstractDataTreeNode root = readNode(Path.ROOT); 136 return new DeltaDataTree(root, parent); 137 } 138 } 139 | Popular Tags |