1 22 23 package org.xquark.xpath; 24 25 import java.util.Iterator ; 26 27 38 public abstract class XTreeBuilder 39 { 40 private static final String RCSRevision = "$Revision: 1.1 $"; 41 private static final String RCSName = "$Name: $"; 42 43 protected XTree tree = null; 44 45 protected XNode pattern = new XNode(); 46 47 50 public XTreeBuilder() 51 {} 52 53 57 public XTreeBuilder(XTree tree) 58 { 59 this.tree = tree; 60 } 61 62 66 public XTree createTree() 67 { 68 if (tree == null) 69 { 70 this.tree = allocateTree(); 71 tree.setRoot(createNode(null, NodeKind.NONE)); 72 } 73 return tree; 74 } 75 76 80 public XTree getTree() 81 { 82 return tree; 83 } 84 85 93 public synchronized XTreeNode createNamedNode( 94 XTreeNode parent, 95 String namespace, 96 String localName, 97 byte type) 98 { 99 return tree.register(allocateNode(parent, namespace, localName, type)); 100 } 101 102 109 public synchronized XTreeNode createNamedNode( 110 XTreeNode parent, 111 String localName, 112 byte type) 113 { 114 return createNamedNode(parent, null, localName, type); 115 } 116 117 125 public synchronized XTreeNode createNamedNodeIfNotExist( 126 XTreeNode parent, 127 String localName, 128 byte type) 129 { 130 pattern.set(null, localName, type); 131 XTreeNode node = parent.getChild(pattern); 132 if (node == null) 133 return createNamedNode(parent, localName, type); 134 else 135 return node; 136 } 137 138 147 public synchronized XTreeNode createNamedNodeIfNotExist( 148 XTreeNode parent, 149 String namespace, 150 String localName, 151 byte type) 152 { 153 pattern.set(namespace, localName, type); 154 XTreeNode node = parent.getChild(pattern); 155 if (node == null) 156 return createNamedNode(parent, namespace, localName, type); 157 else 158 return node; 159 } 160 161 167 public synchronized XTreeNode createNode(XTreeNode parent, byte type) 168 { 169 return tree.register(allocateNode(parent, null, null, type)); 170 } 171 172 178 public synchronized XTreeNode createPI(XTreeNode parent, String target) 179 { 180 return tree.register(allocateNode(parent, target, null, NodeKind.PI)); 181 } 182 183 190 public synchronized XTreeNode createPIIfNotExist( 191 XTreeNode parent, 192 String target) 193 { 194 pattern.set(target); 195 XTreeNode node = parent.getChild(pattern); 196 if (node == null) 197 return createPI(parent, target); 198 else 199 return node; 200 } 201 202 208 public abstract XTree allocateTree(); 209 210 218 public abstract XTreeNode allocateNode( 219 XTreeNode parent, 220 String namespace, 221 String localName, 222 byte type 223 ); 224 225 230 public void add(PathExpr location) 231 { 232 int index = 0; 233 StepExpr step; 234 XTreeNode parent = tree.getRoot(); 235 Iterator it = location.getSteps().iterator(); 237 238 while (it.hasNext()) 239 { 240 step = (StepExpr) it.next(); 241 parent = 242 createNamedNodeIfNotExist( 243 parent, 244 step.getNameSpace(), 245 step.getLocalName(), 246 step.getTypeTest()); 247 } 248 } 249 250 254 public void duplicate(XTreeBuilder builder) 255 { 256 tree.getRoot().duplicate( 257 builder.getTree().getRoot().customizeDuplicate(tree.getRoot()), 258 builder); 259 } 260 261 } 262 | Popular Tags |