1 17 package org.eclipse.emf.importer.rose.parser; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.StringTokenizer ; 23 import java.util.regex.Matcher ; 24 import java.util.regex.Pattern ; 25 26 import org.eclipse.emf.importer.rose.builder.RoseStrings; 27 28 29 32 public class RoseNode 33 { 34 public final static int STRING = 0; 35 public final static int STRING_SEQ = 1; 36 public final static int OBJECT = 2; 37 public final static int LIST = 3; 38 public final static int VALUE = 4; 39 40 protected String key; 41 protected String value; 42 protected RoseNode parent; 43 protected List nodes = new ArrayList (); 44 protected int type; 45 protected String id; 46 protected boolean commit = true; 47 protected Object node; 48 49 public RoseNode(String key, String value, int type) 50 { 51 this.key = key; 52 this.value = value; 53 this.type = type; 54 } 55 56 public void setParent(RoseNode parent) 57 { 58 this.parent = parent; 59 } 60 61 public RoseNode getParent() 62 { 63 return parent; 64 } 65 66 public RoseNode getRoot() 67 { 68 RoseNode result = this; 69 for (RoseNode root = this.getParent(); root != null; root = root.getParent()) 70 { 71 result = root; 72 } 73 return result; 74 } 75 76 public String getKey() 77 { 78 return key; 79 } 80 81 public String getValue() 82 { 83 return value; 84 } 85 86 public String getAllValues() 87 { 88 return getAllValues(false); 89 } 90 91 public String getAllValues(boolean preserveSpace) 92 { 93 if (type == STRING_SEQ) 94 { 95 StringBuffer temp = new StringBuffer (); 96 for (int i = 0; i < nodes.size(); i++) 97 { 98 RoseNode n = (RoseNode)nodes.get(i); 99 temp.append(n.getValue()); 100 if (preserveSpace && i < nodes.size() - 1) 101 { 102 temp.append('\n'); 103 } 104 } 105 return temp.toString(); 106 } 107 else 108 { 109 return preserveSpace ? Util.trimQuotes(value) : value; 110 } 111 } 112 113 public String getFilteredValues(String filter) 114 { 115 if (type == STRING_SEQ) 116 { 117 StringBuffer temp = new StringBuffer (); 118 for (int i = 0; i < nodes.size(); i++) 119 { 120 RoseNode n = (RoseNode)nodes.get(i); 121 if (i == 0 && Util.getWord(n.getValue(), 1).equals(filter)) 122 { 123 continue; 124 } 125 temp.append(n.getValue()); 126 if (i < nodes.size() - 1) 127 { 128 temp.append('\n'); 129 } 130 } 131 return temp.toString(); 132 } 133 else 134 { 135 String val = Util.trimQuotes(value); 136 return Util.getWord(val, 1).equals(filter) ? "" : val; 137 } 138 } 139 140 public void setValue(String value) 141 { 142 this.value = value; 143 } 144 145 public boolean getCommit() 146 { 147 return commit; 148 } 149 150 public void setCommit(boolean b) 151 { 152 commit = b; 153 } 154 155 public String getId() 156 { 157 return id; 158 } 159 160 public String getAtId() 161 { 162 int ind = value.lastIndexOf("@"); 163 return ind != -1 ? value.substring(ind + 1) : null; 164 } 165 166 public void setId(String id) 167 { 168 this.id = id; 169 } 170 171 public int getRoseNodeType() 172 { 173 return type; 174 } 175 176 public List getNodes() 177 { 178 return nodes; 179 } 180 181 public void addNode(RoseNode n) 182 { 183 n.parent = this; 184 nodes.add(n); 185 } 186 187 public void deleteNode(RoseNode n) 188 { 189 if (n != null) 190 { 191 nodes.remove(n); 192 } 193 } 194 195 public RoseNode findNodeWithKey(String key) 196 { 197 for (int i = 0; i < nodes.size(); i++) 198 { 199 RoseNode node = (RoseNode)nodes.get(i); 200 if (key.equals(node.getKey())) 201 { 202 return node; 203 } 204 } 205 return null; 206 } 207 208 public RoseNode findNodeWithValue(String value) 209 { 210 for (int i = 0; i < nodes.size(); i++) 211 { 212 RoseNode node = (RoseNode)nodes.get(i); 213 if (value.equals(node.getValue())) 214 { 215 return node; 216 } 217 } 218 return null; 219 } 220 221 public RoseNode findNodeWithWord(String w) 222 { 223 for (int i = 0; i < nodes.size(); i++) 224 { 225 RoseNode node = (RoseNode)nodes.get(i); 226 String value = node.getValue(); 227 StringTokenizer st = new StringTokenizer (value); 228 while (st.hasMoreTokens()) 229 { 230 String tok = st.nextToken(); 231 if (tok.equals(w)) 232 { 233 return node; 234 } 235 } 236 } 237 return null; 238 } 239 240 public String getRoseId() 241 { 242 RoseNode node = findNodeWithKey("quid"); 243 return node != null ? node.getValue() : null; 244 } 245 246 public String getRoseRefId() 247 { 248 RoseNode node = findNodeWithKey("quidu"); 249 return node != null ? node.getValue() : null; 250 } 251 252 public String getRoseSupplier() 253 { 254 RoseNode node = findNodeWithKey("supplier"); 255 return node != null ? Util.trimQuotes(node.getValue()) : null; 256 } 257 258 public void setNode(Object node) 259 { 260 this.node = node; 261 } 262 263 public Object getNode() 264 { 265 return node; 266 } 267 268 public String getType() 269 { 270 String attributeValue = getAttributeValue(RoseStrings.TYPE); 271 return attributeValue; 272 } 273 274 public String getStereotype() 275 { 276 String attributeValue = getAttributeValue(RoseStrings.STEREOTYPE); 277 return attributeValue; 278 } 279 280 public boolean isDerived() 281 { 282 String attributeValue = getAttributeValue(RoseStrings.DERIVED); 283 return "true".equalsIgnoreCase(attributeValue); 284 } 285 286 public boolean isAbstract() 287 { 288 String attributeValue = getAttributeValue(RoseStrings.ABSTRACT); 289 return "true".equalsIgnoreCase(attributeValue); 290 } 291 292 public boolean isNavigable() 293 { 294 String attributeValue = getAttributeValue(RoseStrings.IS_NAVIGABLE); 295 return "true".equalsIgnoreCase(attributeValue); 296 } 297 298 public boolean isAggregate() 299 { 300 String attributeValue = getAttributeValue(RoseStrings.IS_AGGREGATE); 301 return "true".equalsIgnoreCase(attributeValue); 302 } 303 304 public String getContainment() 305 { 306 String attributeValue = getAttributeValue(RoseStrings.CONTAINMENT); 307 return attributeValue; 308 } 309 310 public String getConstraints() 311 { 312 String attributeValue = getAttributeValue(RoseStrings.CONSTRAINTS); 313 return attributeValue; 314 } 315 316 public String getResult() 317 { 318 String attributeValue = getAttributeValue(RoseStrings.RESULT); 319 return attributeValue; 320 } 321 322 public String getExceptions() 323 { 324 String attributeValue = getAttributeValue(RoseStrings.EXCEPTIONS); 325 return attributeValue; 326 } 327 328 public String getSemantics() 329 { 330 RoseNode semantics = findNodeWithKey(RoseStrings.SEMANTICS); 331 return semantics == null ? null : semantics.getAttributeValue(RoseStrings.PDL); 332 } 333 334 public String getInitV() 335 { 336 String attributeValue = getAttributeValue(RoseStrings.INITV); 337 return attributeValue; 338 } 339 340 public String getDocumentation() 341 { 342 String attributeValue = getAttributeValue(RoseStrings.DOCUMENTATION); 343 return attributeValue; 344 } 345 346 public boolean isLoaded() 347 { 348 String attributeValue = getAttributeValue(RoseStrings.IS_LOADED); 349 return attributeValue == null || "true".equalsIgnoreCase(attributeValue); 350 } 351 352 protected String getAttributeValue(String key) 353 { 354 for (Iterator i = getNodes().iterator(); i.hasNext();) 355 { 356 RoseNode roseNode = (RoseNode)i.next(); 357 if (roseNode.getRoseNodeType() == RoseNode.STRING) 358 { 359 String nodeKey = roseNode.getKey(); 360 String nodeValue = roseNode.getValue(); 361 if (nodeKey.equals(key)) 362 { 363 nodeValue = dequote(nodeValue); 364 return nodeValue; 365 } 366 } 367 else if (roseNode.getRoseNodeType() == RoseNode.STRING_SEQ && roseNode.getKey().equals(key)) 368 { 369 String separator = System.getProperty("line.separator"); 370 List subNodes = roseNode.getNodes(); 371 StringBuffer result = new StringBuffer (); 372 for (Iterator j = subNodes.iterator(); j.hasNext();) 373 { 374 RoseNode subNode = (RoseNode)j.next(); 375 if (subNode.getRoseNodeType() == RoseNode.STRING) 376 { 377 if (subNode.getValue().equals("")) 378 { 379 result.append(separator); 380 } 381 else 382 { 383 if (result.length() != 0) 384 { 385 result.append(separator); 386 } 387 result.append(subNode.getValue()); 388 } 389 } 390 } 391 return result.toString(); 392 } 393 } 394 395 return null; 396 } 397 398 public String getUML2MOFCorbaType() 399 { 400 String attributeValue = getAttributeValue("MOF", "uml2mof.corbaType"); 401 return attributeValue; 402 } 403 404 public boolean isTransient() 405 { 406 String attributeValue = getAttributeValue("Ecore", "isTransient"); 407 return "true".equalsIgnoreCase(attributeValue); 408 } 409 410 public boolean isVolatile() 411 { 412 String attributeValue = getAttributeValue("Ecore", "isVolatile"); 413 return "true".equalsIgnoreCase(attributeValue); 414 } 415 416 public boolean isChangeable() 417 { 418 String attributeValue = getAttributeValue("Ecore", "isChangeable"); 419 return !"false".equalsIgnoreCase(attributeValue); 420 } 421 422 public boolean isResolveProxies() 423 { 424 String attributeValue = getAttributeValue("Ecore", "isResolveProxies"); 425 return !"false".equalsIgnoreCase(attributeValue); 426 } 427 428 public boolean isUnsettable() 429 { 430 String attributeValue = getAttributeValue("Ecore", "isUnsettable"); 431 return "true".equalsIgnoreCase(attributeValue); 432 } 433 434 public boolean isID() 435 { 436 String attributeValue = getAttributeValue("Ecore", "isID"); 437 return "true".equalsIgnoreCase(attributeValue); 438 } 439 440 public boolean isUnique() 441 { 442 String attributeValue = getAttributeValue("Ecore", "isUnique"); 443 return !"false".equalsIgnoreCase(attributeValue); 444 } 445 446 public String getBasePackage() 447 { 448 String attributeValue = getAttributeValue("Ecore", "basePackage"); 449 return attributeValue; 450 } 451 452 public String getPrefix() 453 { 454 String attributeValue = getAttributeValue("Ecore", "prefix"); 455 return attributeValue; 456 } 457 458 public String getPackageName() 459 { 460 String attributeValue = getAttributeValue("Ecore", "packageName"); 461 return attributeValue; 462 } 463 464 public String getClassifierName() 465 { 466 String attributeValue = getAttributeValue("Ecore", "classifierName"); 467 return attributeValue; 468 } 469 470 public String getEcoreConstraints() 471 { 472 String attributeValue = getAttributeValue("Ecore", "constraints"); 473 return attributeValue; 474 } 475 476 public String getReferenceName() 477 { 478 String attributeValue = getAttributeValue("Ecore", "referenceName"); 479 return attributeValue; 480 } 481 482 public String getAttributeName() 483 { 484 String attributeValue = getAttributeValue("Ecore", "attributeName"); 485 return attributeValue; 486 } 487 488 public String getOperationName() 489 { 490 String attributeValue = getAttributeValue("Ecore", "operationName"); 491 return attributeValue; 492 } 493 494 public String getNsPrefix() 495 { 496 String attributeValue = getAttributeValue("Ecore", "nsPrefix"); 497 if (attributeValue == null || attributeValue.length() == 0) 498 { 499 attributeValue = getAttributeValue("Ecore", "nsName"); 500 } 501 return attributeValue; 502 } 503 504 public String getNsURI() 505 { 506 String attributeValue = getAttributeValue("Ecore", "nsURI"); 507 return attributeValue; 508 } 509 510 public String getXMLName() 511 { 512 String attributeValue = getAttributeValue("Ecore", "xmlName"); 513 return attributeValue; 514 } 515 516 public String getXMLNamespace() 517 { 518 String attributeValue = getAttributeValue("Ecore", "xmlNamespace"); 519 return attributeValue; 520 } 521 522 protected static final Pattern FEATURE_VALUE_PATTERN = Pattern.compile("\\s*\\(\\s*\"FeatureKind\"\\s+([0-9]+)\\s*\\)"); 523 524 public int getXMLFeatureKind() 525 { 526 String attributeValue = getAttributeValue("Ecore", "xmlFeatureKind"); 527 if (attributeValue != null) 528 { 529 Matcher matcher = FEATURE_VALUE_PATTERN.matcher(attributeValue); 530 if (matcher.matches()) 531 { 532 return Integer.parseInt(matcher.group(1)); 533 } 534 } 535 return 0; 536 } 537 538 protected static final Pattern CONTENT_VALUE_PATTERN = Pattern.compile("\\s*\\(\\s*\"ContentKind\"\\s+([0-9]+)\\s*\\)"); 539 540 public int getXMLContentKind() 541 { 542 String attributeValue = getAttributeValue("Ecore", "xmlContentKind"); 543 if (attributeValue != null) 544 { 545 Matcher matcher = CONTENT_VALUE_PATTERN.matcher(attributeValue); 546 if (matcher.matches()) 547 { 548 return Integer.parseInt(matcher.group(1)); 549 } 550 } 551 return 0; 552 } 553 554 public static final int VISIBILITY_UNSPECIFIED = 0; 555 public static final int VISIBILITY_NONE = 1; 556 public static final int VISIBILITY_READ_ONLY = 2; 557 public static final int VISIBILITY_READ_WRITE = 3; 558 public static final int VISIBILITY_READ_ONLY_UNSETTABLE = 4; 559 public static final int VISIBILITY_READ_WRITE_UNSETTABLE = 5; 560 561 protected static final Pattern VISIBILITY_VALUE_PATTERN = Pattern.compile("\\s*\\(\\s*\"VisibilityKind\"\\s+([0-9]+)\\s*\\)"); 562 563 public int getVisibility() 564 { 565 String attributeValue = getAttributeValue("Ecore", "visibility"); 566 if (attributeValue != null) 567 { 568 Matcher matcher = VISIBILITY_VALUE_PATTERN.matcher(attributeValue); 569 if (matcher.matches()) 570 { 571 return Integer.parseInt(matcher.group(1)); 572 } 573 } 574 return VISIBILITY_UNSPECIFIED; 575 } 576 577 public String getAnnotation() 578 { 579 String attributeValue = getAttributeValue("Ecore", "annotation"); 580 if ("".equals(attributeValue)) 581 { 582 attributeValue = null; 583 } 584 return attributeValue; 585 } 586 587 591 protected String getAttributeValue(String tool, String name) 592 { 593 String result = basicGetAttributeValue(tool, name); 594 if (result == null && "Ecore".equals(tool)) 595 { 596 result = basicGetAttributeValue("eCore", name); 597 } 598 return result; 599 } 600 601 protected String basicGetAttributeValue(String tool, String name) 602 { 603 RoseNode attributeListNode = findNodeWithKey(RoseStrings.ATTRIBUTES); 604 605 String value = getAttributeValueInAttributeList(attributeListNode, tool, name); 606 607 if (value == null) 608 { 609 value = getAttributeValueInAttributeList(getDefaultAttributeList(tool), tool, name); 610 } 611 612 return value; 613 } 614 615 private String getAttributeValueInAttributeList(RoseNode attributeListNode, String tool, String name) 616 { 617 if (attributeListNode != null) 618 { 619 List attributeNodes = attributeListNode.getNodes(); 620 if (attributeNodes != null) 621 { 622 for (Iterator i = attributeNodes.iterator(); i.hasNext();) 623 { 624 RoseNode attributeNode = (RoseNode)i.next(); 625 List nodes = attributeNode.getNodes(); 626 if (nodes != null) 627 { 628 String setName = ""; 629 String tagName = ""; 630 String valueName = ""; 631 for (Iterator j = nodes.iterator(); j.hasNext();) 632 { 633 RoseNode node = (RoseNode)j.next(); 634 if (node.getRoseNodeType() == RoseNode.STRING) 635 { 636 String stringV = dequote(node.getValue()); 637 if (node.getKey().equals(RoseStrings.TOOL)) 638 { 639 setName = stringV; 640 } 641 else if (node.getKey().equals(RoseStrings.NAME)) 642 { 643 tagName = stringV; 644 } 645 else if (node.getKey().equals(RoseStrings.VALUE)) 646 { 647 valueName = stringV; 648 } 649 } 650 else if (node.getRoseNodeType() == RoseNode.STRING_SEQ) 651 { 652 List subSubNodes = node.getNodes(); 653 String stringV = ""; 654 for (Iterator k = subSubNodes.iterator(); k.hasNext();) 655 { 656 RoseNode subSubNode = (RoseNode)k.next(); 657 if (subSubNode.getRoseNodeType() == RoseNode.STRING) 658 { 659 if (stringV.length() > 0) 660 { 661 stringV += " "; 662 } 663 stringV += subSubNode.getValue(); 664 } 665 } 666 if (node.getKey().equals(RoseStrings.TOOL)) 667 { 668 setName = stringV; 669 } 670 else if (node.getKey().equals(RoseStrings.NAME)) 671 { 672 tagName = stringV; 673 } 674 else if (node.getKey().equals(RoseStrings.VALUE)) 675 { 676 valueName = stringV; 677 } 678 } 679 else if (node.getRoseNodeType() == RoseNode.VALUE) 680 { 681 List stringNodes = node.getNodes(); 682 if (stringNodes != null && stringNodes.size() == 1) 683 { 684 RoseNode stringNode = (RoseNode)stringNodes.get(0); 685 String stringV = ""; 686 if (stringNode.getRoseNodeType() == RoseNode.STRING) 687 { 688 stringV = stringNode.getValue(); 689 } 690 else if (stringNode.getRoseNodeType() == RoseNode.STRING_SEQ) 691 { 692 for (Iterator k = stringNode.getNodes().iterator(); k.hasNext();) 693 { 694 RoseNode subSubNode = (RoseNode)k.next(); 695 if (subSubNode.getRoseNodeType() == RoseNode.STRING) 696 { 697 if (stringV.length() > 0) 698 { 699 stringV += " "; 700 } 701 stringV += subSubNode.getValue(); 702 } 703 } 704 } 705 stringV = dequote(stringV); 706 if (node.getKey().equals(RoseStrings.TOOL)) 707 { 708 setName = stringV; 709 } 710 else if (node.getKey().equals(RoseStrings.NAME)) 711 { 712 tagName = stringV; 713 } 714 else if (node.getKey().equals(RoseStrings.VALUE)) 715 { 716 valueName = stringV; 717 } 718 } 719 } 720 } 721 if (setName.equals(tool) && tagName.equals(name)) 722 { 723 return valueName; 724 } 725 } 726 } 727 } 728 } 729 return null; 730 } 731 732 private RoseNode getDefaultAttributeList(String tool) 733 { 734 RoseNode attributeListNode = null; 735 RoseNode rootNode = getRoot(); 736 String rootNodeValue = rootNode.getValue(); 737 String rootObjectType = Util.getType(rootNodeValue); 738 739 if (rootObjectType.equals(RoseStrings.DESIGN)) 740 { 741 String objectType = Util.getType(getValue()); 742 RoseNode defaultProperties = rootNode.findNodeWithKey(RoseStrings.PROPERTIES); 743 if (defaultProperties != null) 744 { 745 RoseNode attributes = defaultProperties.findNodeWithKey(RoseStrings.ATTRIBUTES); 746 for (Iterator i = attributes.getNodes().iterator(); i.hasNext();) 747 { 748 RoseNode attribute = (RoseNode)i.next(); 749 RoseNode toolNode = attribute.findNodeWithKey(RoseStrings.TOOL); 750 if (toolNode != null && Util.trimQuotes(toolNode.getValue()).equals(tool)) 751 { 752 RoseNode nameNode = attribute.findNodeWithKey(RoseStrings.NAME); 753 String theName = Util.trimQuotes(nameNode.getValue()); 754 if (objectType.equals(RoseStrings.CLASSATTRIBUTE) && theName.equals(RoseStrings.DEFAULT_ATTRIBUTE)) 755 { 756 attributeListNode = attribute.findNodeWithKey(RoseStrings.VALUE); 757 break; 758 } 759 else if (objectType.equals(RoseStrings.CLASS) && theName.equals(RoseStrings.DEFAULT_CLASS)) 760 { 761 attributeListNode = attribute.findNodeWithKey(RoseStrings.VALUE); 762 break; 763 } 764 else if (objectType.equals(RoseStrings.CLASS_CATEGORY) && theName.equals(RoseStrings.DEFAULT_CATEGORY)) 765 { 766 attributeListNode = attribute.findNodeWithKey(RoseStrings.VALUE); 767 break; 768 } 769 else if (objectType.equals(RoseStrings.OPERATION) && theName.equals(RoseStrings.DEFAULT_OPERATION)) 770 { 771 attributeListNode = attribute.findNodeWithKey(RoseStrings.VALUE); 772 break; 773 } 774 else if (objectType.equals(RoseStrings.ROLE) && theName.equals(RoseStrings.DEFAULT_ROLE)) 775 { 776 attributeListNode = attribute.findNodeWithKey(RoseStrings.VALUE); 777 break; 778 } 779 } 780 } 781 } 782 } 783 return attributeListNode; 784 } 785 786 public String getRoleMultiplicity() 787 { 788 List nodes = getNodes(); 789 for (int i = 0; i < nodes.size(); i++) 790 { 791 RoseNode node = (RoseNode)nodes.get(i); 792 if (node.getRoseNodeType() == RoseNode.VALUE) 793 { 794 String objKey = node.getKey(); 795 String objType = Util.getType(node.getValue()); 796 if (objKey.equals(RoseStrings.CLIENT_CARDINALITY) && objType.equals(RoseStrings.CARDINALITY)) 797 { 798 List subNodes = node.getNodes(); 799 String value = ""; 800 for (int j = 0; j < subNodes.size(); j++) 801 { 802 RoseNode subNode = (RoseNode)subNodes.get(j); 804 if (subNode.getRoseNodeType() == RoseNode.STRING) 805 { 806 value = subNode.getValue(); 807 value = value.substring(1, value.length() - 1); 808 } 809 else if (subNode.getRoseNodeType() == RoseNode.STRING_SEQ) 810 { 811 List subSubNodes = subNode.getNodes(); 813 for (int k = 0; k < subSubNodes.size(); k++) 814 { 815 RoseNode subSubNode = (RoseNode)subSubNodes.get(k); 816 if (subSubNode.getRoseNodeType() == RoseNode.STRING) 817 value = subSubNode.getValue() + "|#" + value; 818 } 819 } 820 } 821 return value; 822 } 823 } 824 } 825 return null; 826 } 827 828 protected static String dequote(String s) 829 { 830 if (s != null && s.length() >= 2 && s.charAt(0) == '\"' && s.charAt(s.length() - 1) == '\"') 831 { 832 s = s.substring(1, s.length() - 1); 833 for (int i = s.indexOf("\\\\"); i != -1; i = s.indexOf("\\\\")) 834 { 835 s = s.substring(0, i) + s.substring(i + 1, s.length()); 836 } 837 } 838 return s; 839 } 840 } 841 | Popular Tags |