1 11 package org.eclipse.core.internal.dtree; 12 13 import java.io.DataOutput ; 14 import java.io.IOException ; 15 import org.eclipse.core.runtime.*; 16 17 20 public class DataTreeWriter { 21 24 protected IDataFlattener flatener; 25 26 29 protected DataOutput output; 30 31 34 public static final int D_INFINITE = -1; 35 36 39 public DataTreeWriter(IDataFlattener f) { 40 flatener = f; 41 } 42 43 49 protected void writeNode(AbstractDataTreeNode node, IPath path, int depth) throws IOException { 50 int type = node.type(); 51 52 53 String name = node.getName(); 54 if (name == null) { 55 name = ""; } 57 output.writeUTF(name); 58 59 60 writeNumber(type); 61 62 63 if (node.hasData()) { 64 Object data = node.getData(); 65 66 70 if (data == null) { 71 writeNumber(0); 72 } else { 73 writeNumber(1); 74 flatener.writeData(path, node.getData(), output); 75 } 76 77 } 78 79 80 if (depth > 0 || depth == D_INFINITE) { 81 AbstractDataTreeNode[] children = node.getChildren(); 82 83 84 writeNumber(children.length); 85 86 87 int newDepth = (depth == D_INFINITE) ? D_INFINITE : depth - 1; 88 for (int i = 0, imax = children.length; i < imax; i++) { 89 writeNode(children[i], path.append(children[i].getName()), newDepth); 90 } 91 } else { 92 93 writeNumber(0); 94 } 95 } 96 97 102 protected void writeNumber(int number) throws IOException { 103 if (number >= 0 && number < 0xff) { 104 output.writeByte(number); 105 } else { 106 output.writeByte(0xff); 107 output.writeInt(number); 108 } 109 } 110 111 115 protected void writeSingleNode(AbstractDataTreeNode node, IPath path) throws IOException { 116 117 String name = node.getName(); 118 if (name == null) { 119 name = ""; } 121 output.writeUTF(name); 122 123 124 writeNumber(node.type()); 125 126 127 if (node.hasData()) { 128 Object data = node.getData(); 129 130 134 if (data == null) { 135 writeNumber(0); 136 } else { 137 writeNumber(1); 138 flatener.writeData(path, node.getData(), output); 139 } 140 } 141 } 142 143 154 public void writeTree(AbstractDataTree tree, IPath path, int depth, DataOutput output) throws IOException { 155 this.output = output; 156 157 AbstractDataTreeNode node = tree.getRootNode(); 158 IPath currentPath = Path.ROOT; 159 String [] segments = path.segments(); 160 for (int i = 0; i < segments.length; i++) { 161 String nextSegment = segments[i]; 162 163 164 writeSingleNode(node, currentPath); 165 166 currentPath = currentPath.append(nextSegment); 167 node = node.childAtOrNull(nextSegment); 168 169 170 if (node != null) { 171 writeNumber(1); 172 } else { 173 174 writeNumber(0); 175 return; 176 } 177 } 178 179 Assert.isTrue(currentPath.equals(path), "dtree.navigationError"); 181 182 writeNode(node, path, depth); 183 } 184 } 185 | Popular Tags |