1 19 20 package org.netbeans.modules.schema2beansdev; 21 22 import java.util.List ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 26 import org.netbeans.modules.schema2beans.*; 27 28 33 public class GraphLink { 34 38 GraphNode element; 39 40 43 private GraphLink parent; 44 45 55 private GraphLink sibling; 56 57 70 private GraphLink child; 71 72 private transient GraphLink lastChild; 74 99 int propElement; 100 int propChildren; 101 102 106 String name; 107 private String schemaName = null; 108 private String namespace = null; 109 private String defaultValue; 110 private boolean nillable; 111 112 private Object object; 113 114 List extraData = new ArrayList (); 115 116 GraphLink(String name) { 117 this.propElement = Common.SEQUENCE_AND | Common.TYPE_1; 119 this.propChildren = Common.SEQUENCE_AND | Common.TYPE_1; 120 this.name = name; 121 this.schemaName = name; 122 } 123 124 GraphLink(String name, String namespace) { 125 this.propElement = Common.SEQUENCE_AND | Common.TYPE_1; 127 this.propChildren = Common.SEQUENCE_AND | Common.TYPE_1; 128 this.name = name; 129 this.namespace = namespace; 130 this.schemaName = name; 131 } 132 133 public String getNamespace() { 134 return namespace; 135 } 136 137 public boolean isUnion() { 138 return element.isUnion(); 139 } 140 141 public void setUnion(boolean value) { 142 element.setUnion(value); 143 } 144 145 public List getChildren() { List result = new ArrayList (); 147 for (GraphLink l = child; l != null; l = l.sibling) 148 result.add(l); 149 return result; 150 } 151 152 155 public List getSiblings() { List result = new ArrayList (); 157 if (parent == null) { 158 for (GraphLink l = this; l != null; l = l.sibling) 160 result.add(l); 161 return result; 162 } 163 for (GraphLink l = parent.child; l != null; l = l.sibling) { 164 if (l == this) 165 continue; 166 result.add(l); 167 } 168 return result; 169 } 170 171 174 public List getSiblingsAndMe() { if (parent == null) { 176 List result = new ArrayList (); 178 for (GraphLink l = this; l != null; l = l.sibling) 179 result.add(l); 180 return result; 181 } 182 return parent.getChildren(); 183 } 184 185 188 public GraphLink getSibling() { 189 return sibling; 190 } 191 192 public GraphLink getLastSibling() { 193 if (parent != null && parent.lastChild != null) 195 return parent.getLastChild(); 196 if (sibling == null) 197 return this; 198 else 199 return sibling.getLastSibling(); 200 } 201 202 205 public GraphLink getFirstChild() { 206 return child; 207 } 208 209 212 public GraphLink getLastChild() { 213 if (lastChild == null) { 214 if (child != null) 215 lastChild = child.getLastSibling(); 216 } 217 return lastChild; 218 } 219 220 223 public void setChild(GraphLink l) { 224 child = l; 225 child.parent = this; 226 lastChild = null; 227 } 228 229 private void addSibling(GraphLink l) { 230 if (sibling != null) 231 throw new RuntimeException ("I am not the last sibling!"); 232 sibling = l; 233 l.parent = parent; 235 if (parent != null) 236 parent.lastChild = l; 237 } 238 239 242 public void addChild(GraphLink l) { 243 if (child == null) 244 setChild(l); 245 else 246 child.getLastSibling().addSibling(l); 247 } 248 249 252 public GraphLink getParent() { 253 return parent; 254 } 255 256 private void setParent(GraphLink l) { 257 parent = l; 258 } 259 260 boolean isSequenceAnd() { 261 return ((this.propChildren & Common.SEQUENCE_AND) == 263 Common.SEQUENCE_AND); 264 } 265 266 boolean isSequenceOr() { 267 return ((this.propChildren & Common.SEQUENCE_OR) == 269 Common.SEQUENCE_OR); 270 } 271 272 void setSequence(int prop) { 273 this.propChildren = (this.propChildren & Common.MASK_TYPE) | prop; 274 } 275 276 void setElementInstance(int instance) { 277 this.propElement = 278 (this.propElement & Common.MASK_SEQUENCE) | instance; 279 } 280 281 int getElementInstance() { 282 return (this.propElement & Common.MASK_INSTANCE); 283 } 284 285 void setGroupInstance(int instance) { 286 this.propChildren = 287 (this.propChildren & Common.MASK_SEQUENCE) | instance; 288 } 289 290 int getGroupInstance() { 291 return (this.propChildren & Common.MASK_INSTANCE); 292 } 293 294 297 public void getMutuallyExclusiveLinks(List links) { if (parent == null) 299 return; 300 if (parent.isSequenceOr() && 308 (parent.getGroupInstance() == Common.TYPE_1 || 309 parent.getGroupInstance() == Common.TYPE_0_1)) { 310 for (Iterator it = getSiblings().iterator(); it.hasNext(); ) { 311 GraphLink l = (GraphLink) it.next(); 312 l.findAllBelowBranch(links); 313 } 314 } 315 parent.getMutuallyExclusiveLinks(links); 316 } 317 318 322 public void findAllBelowBranch(List links) { links.add(this); 324 if (child != null) 325 child.findAllBelowBranchAndSiblings(links); 326 } 327 328 public void findAllBelowBranchAndSiblings(List links) { GraphLink l = this; 330 for (; l != null; l = l.sibling) { 331 links.add(l); 332 if (l.child != null) 333 l.child.findAllBelowBranchAndSiblings(links); 334 } 335 } 336 337 String getDefaultValue() { 338 return defaultValue; 339 } 340 341 void setDefaultValue(String d) { 342 defaultValue = d; 343 } 344 345 void setObject(Object obj) { 346 this.object = obj; 347 } 348 349 Object getObject() { 350 return this.object; 351 } 352 353 boolean isNillable() { 354 return nillable; 355 } 356 357 void setNillable(boolean value) { 358 nillable = value; 359 } 360 361 String getSchemaName() { 362 return schemaName; 363 } 364 365 public XPathIterator xPathIterator(String xpath) { 366 return new XPathIterator(this, xpath); 367 } 368 369 public static class XPathIterator implements java.util.Iterator { 370 private String xpath; 371 private String step; 372 private GraphLink curLink; 373 private int position; 374 375 public XPathIterator(GraphLink startingLink, String xpath) { 376 this.xpath = xpath; 377 curLink = startingLink; 378 findNextStep(); 379 } 380 381 private void findNextStep() { 383 if (position >= xpath.length()) { 384 step = null; 385 return; 386 } 387 int startingPos = position; 388 for (; position < xpath.length(); ++position) { 389 if (xpath.charAt(position) == '/') { 390 step = xpath.substring(startingPos, position); 391 ++position; 392 if (".".equals(step)) { 393 startingPos = position; 395 } else { 396 return; 397 } 398 } 399 } 400 step = xpath.substring(startingPos, position); 401 if (startingPos > 0 && ".".equals(step)) { 402 step = null; 403 } 404 } 405 406 public boolean hasNext() { 407 if (step == null) 408 return false; 409 return true; 410 } 411 412 415 public Object next() { 416 if (step == null) 417 throw new java.util.NoSuchElementException (); 418 GraphLink result = curLink; 419 while (".".equals(step)) { 420 findNextStep(); 421 if (step == null) 422 return result; 423 } 424 int colonPos = step.indexOf(':'); 426 if (colonPos >= 0) 427 step = step.substring(colonPos+1, step.length()); 428 result = result.lookForChild(step); 431 if (result == null) { 432 step = null; 434 position = xpath.length(); 435 return null; 436 } 437 if (result.element == null) { 438 curLink = null; 439 } else { 440 curLink = result.element.getGraphLink(); 441 } 442 443 findNextStep(); 444 return result; 445 } 446 447 public void remove() { 448 throw new UnsupportedOperationException (); 449 } 450 } 451 452 456 GraphLink lookForChild(String searchName) { 457 464 465 if (searchName.equals(getSchemaName())) 466 return this; 467 if (name == null) { 468 if (child != null) { 470 GraphLink childResult = child.lookForChild(searchName); 471 if (childResult != null) 472 return childResult; 473 } 474 } 475 if (sibling != null) 476 return sibling.lookForChild(searchName); 477 if (child != null) 478 return child.lookForChild(searchName); 479 return null; 480 } 481 } 482 | Popular Tags |