1 17 package org.eclipse.emf.mapping.domain; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.eclipse.emf.common.command.Command; 27 import org.eclipse.emf.common.command.CommandStack; 28 import org.eclipse.emf.common.command.UnexecutableCommand; 29 import org.eclipse.emf.common.notify.AdapterFactory; 30 import org.eclipse.emf.common.notify.Notification; 31 import org.eclipse.emf.common.notify.NotificationWrapper; 32 import org.eclipse.emf.ecore.EClassifier; 33 import org.eclipse.emf.ecore.ENamedElement; 34 import org.eclipse.emf.ecore.EObject; 35 import org.eclipse.emf.ecore.EPackage; 36 import org.eclipse.emf.ecore.EReference; 37 import org.eclipse.emf.ecore.EStructuralFeature; 38 import org.eclipse.emf.ecore.ETypedElement; 39 import org.eclipse.emf.ecore.resource.ResourceSet; 40 import org.eclipse.emf.edit.command.AddCommand; 41 import org.eclipse.emf.edit.command.CommandParameter; 42 import org.eclipse.emf.edit.command.CopyToClipboardCommand; 43 import org.eclipse.emf.edit.command.CreateCopyCommand; 44 import org.eclipse.emf.edit.command.DragAndDropCommand; 45 import org.eclipse.emf.edit.command.InitializeCopyCommand; 46 import org.eclipse.emf.edit.command.MoveCommand; 47 import org.eclipse.emf.edit.command.OverrideableCommand; 48 import org.eclipse.emf.edit.command.PasteFromClipboardCommand; 49 import org.eclipse.emf.edit.command.RemoveCommand; 50 import org.eclipse.emf.edit.command.ReplaceCommand; 51 import org.eclipse.emf.edit.command.SetCommand; 52 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; 53 import org.eclipse.emf.edit.domain.EditingDomain; 54 import org.eclipse.emf.edit.domain.IEditingDomainProvider; 55 import org.eclipse.emf.edit.provider.ComposedAdapterFactory; 56 import org.eclipse.emf.edit.provider.IItemLabelProvider; 57 import org.eclipse.emf.edit.provider.ITreeItemContentProvider; 58 import org.eclipse.emf.mapping.Mapping; 59 import org.eclipse.emf.mapping.MappingPlugin; 60 import org.eclipse.emf.mapping.MappingRoot; 61 import org.eclipse.emf.mapping.command.AddOverrideCommand; 62 import org.eclipse.emf.mapping.command.CopyToClipboardOverrideCommand; 63 import org.eclipse.emf.mapping.command.CreateCopyOverrideCommand; 64 import org.eclipse.emf.mapping.command.CreateMappingCommand; 65 import org.eclipse.emf.mapping.command.DragAndDropOverrideCommand; 66 import org.eclipse.emf.mapping.command.InitializeCopyOverrideCommand; 67 import org.eclipse.emf.mapping.command.PasteFromClipboardOverrideCommand; 68 import org.eclipse.emf.mapping.command.PersistentCommandStack; 69 import org.eclipse.emf.mapping.command.RemoveOverrideCommand; 70 import org.eclipse.emf.mapping.command.SetOverrideCommand; 71 import org.eclipse.emf.mapping.provider.MappedObjectItemProvider; 72 73 74 84 public class AdapterFactoryMappingDomain extends AdapterFactoryEditingDomain implements MappingDomain 85 { 86 public static final int LABEL_UPPER = 1; 87 public static final int LABEL_LOWER = 2; 88 public static final int LABEL_MIXED = 3; 89 public static final char LABEL_NO_SEPARATOR = '\0'; 90 91 protected HashMap topToBottomTypeTable = new HashMap (); 92 protected HashMap bottomToTopTypeTable = new HashMap (); 93 94 protected char topLabelSeparator = LABEL_NO_SEPARATOR; 95 protected char bottomLabelSeparator = LABEL_NO_SEPARATOR; 96 97 protected int topLabelCase = LABEL_MIXED; 98 protected int bottomLabelCase = LABEL_MIXED; 99 100 protected List topLabelLongForms = new ArrayList (); 101 protected List bottomLabelLongForms = new ArrayList (); 102 103 protected List topLabelShortForms = new ArrayList (); 104 protected List bottomLabelShortForms = new ArrayList (); 105 106 protected char packageTypeSeparator = '|'; 107 108 protected MappingRoot mappingRoot; 109 protected boolean isSameEditingDomainAdapterFactory; 110 protected PersistentCommandStack persistentCommandStack; 111 112 protected int mappingEnablementFlags = 0; 113 114 public AdapterFactoryMappingDomain 115 (AdapterFactory mappingDomainAdapterFactory, 116 AdapterFactory editingDomainAdapterFactory, 117 CommandStack commandStack) 118 { 119 this(mappingDomainAdapterFactory, editingDomainAdapterFactory, editingDomainAdapterFactory, commandStack); 120 } 121 122 public AdapterFactoryMappingDomain 123 (AdapterFactory mappingDomainAdapterFactory, 124 AdapterFactory editingDomainAdapterFactory, 125 CommandStack commandStack, 126 ResourceSet resourceSet) 127 { 128 this(mappingDomainAdapterFactory, editingDomainAdapterFactory, editingDomainAdapterFactory, commandStack, resourceSet); 129 } 130 131 public AdapterFactoryMappingDomain 132 (AdapterFactory mappingDomainAdapterFactory, 133 AdapterFactory topDomainAdapterFactory, 134 AdapterFactory bottomDomainAdapterFactory, 135 CommandStack commandStack) 136 { 137 super(null, commandStack); 138 139 adapterFactory = createComposedAdapterFactory(mappingDomainAdapterFactory, topDomainAdapterFactory, bottomDomainAdapterFactory); 140 141 isSameEditingDomainAdapterFactory = (topDomainAdapterFactory == bottomDomainAdapterFactory); 142 143 if (commandStack instanceof PersistentCommandStack) 144 { 145 persistentCommandStack = (PersistentCommandStack)commandStack; 146 } 147 } 148 149 public AdapterFactoryMappingDomain 150 (AdapterFactory mappingDomainAdapterFactory, 151 AdapterFactory topDomainAdapterFactory, 152 AdapterFactory bottomDomainAdapterFactory, 153 CommandStack commandStack, 154 ResourceSet resourceSet) 155 { 156 super(null, commandStack, resourceSet); 157 158 adapterFactory = createComposedAdapterFactory(mappingDomainAdapterFactory, topDomainAdapterFactory, bottomDomainAdapterFactory); 159 160 isSameEditingDomainAdapterFactory = (topDomainAdapterFactory == bottomDomainAdapterFactory); 161 162 if (commandStack instanceof PersistentCommandStack) 163 { 164 persistentCommandStack = (PersistentCommandStack)commandStack; 165 } 166 } 167 168 protected class LabelUpdatePropagatingComposedAdapterFactory extends ComposedAdapterFactory 169 { 170 public LabelUpdatePropagatingComposedAdapterFactory(AdapterFactory adapterFactory) 171 { 172 super(adapterFactory); 173 } 174 175 public void fireNotifyChanged(Notification note) 177 { 178 super.fireNotifyChanged(note); 179 { 181 if (parentAdapterFactory != null && getMappingRoot() != null && note.getNotifier() != null) 182 { 183 for (Iterator mappings = getMappingRoot().getMappings(note.getNotifier()).iterator(); mappings.hasNext(); ) 184 { 185 Mapping mapping = (Mapping)mappings.next(); 186 parentAdapterFactory.fireNotifyChanged(new NotificationWrapper(mapping, note)); 187 188 ITreeItemContentProvider treeItemContentProvider = 191 (ITreeItemContentProvider)getAdapterFactory().adapt(mapping, ITreeItemContentProvider.class); 192 193 if (treeItemContentProvider != null) 194 { 195 for (Iterator children = treeItemContentProvider.getChildren(mapping).iterator(); children.hasNext(); ) 196 { 197 Object child = children.next(); 198 if (child instanceof MappedObjectItemProvider && 199 ((MappedObjectItemProvider)child).getMappedObject() == note.getNotifier()) 200 { 201 parentAdapterFactory.fireNotifyChanged(new NotificationWrapper(child, note)); 202 } 203 } 204 } 205 } 206 } 207 } 208 } 209 } 210 211 public static class EditingDomainProvidingComposedAdapterFactory extends ComposedAdapterFactory implements IEditingDomainProvider 212 { 213 protected EditingDomain editingDomain; 214 215 217 public EditingDomainProvidingComposedAdapterFactory(AdapterFactory adapterFactory, EditingDomain editingDomain) 218 { 219 super(adapterFactory); 220 this.editingDomain = editingDomain; 221 addAdapterFactory(adapterFactory); 222 } 223 224 public EditingDomainProvidingComposedAdapterFactory(AdapterFactory [] adapterFactories, EditingDomain editingDomain) 225 { 226 super(adapterFactories); 227 this.editingDomain = editingDomain; 228 } 229 230 public EditingDomainProvidingComposedAdapterFactory(Collection adapterFactories, EditingDomain editingDomain) 231 { 232 super(adapterFactories); 233 this.editingDomain = editingDomain; 234 } 235 236 public EditingDomain getEditingDomain() 237 { 238 return editingDomain; 239 } 240 } 241 242 protected ComposedAdapterFactory createComposedAdapterFactory 243 (AdapterFactory mappingDomainAdapterFactory, 244 AdapterFactory topDomainAdapterFactory, 245 AdapterFactory bottomDomainAdapterFactory) 246 { 247 AdapterFactory[] adapterFactories; 248 249 if (topDomainAdapterFactory == bottomDomainAdapterFactory) 250 { 251 adapterFactories = 252 new AdapterFactory[] 253 { 254 mappingDomainAdapterFactory, 255 new LabelUpdatePropagatingComposedAdapterFactory(topDomainAdapterFactory) 256 }; 257 } 258 else 259 { 260 adapterFactories = 261 new AdapterFactory[] 262 { 263 mappingDomainAdapterFactory, 264 new LabelUpdatePropagatingComposedAdapterFactory(topDomainAdapterFactory), 265 new LabelUpdatePropagatingComposedAdapterFactory(bottomDomainAdapterFactory) 266 }; 267 } 268 269 return createComposedAdapterFactory(adapterFactories); 270 } 271 272 protected ComposedAdapterFactory createComposedAdapterFactory(AdapterFactory[] adapterFactories) 273 { 274 return new EditingDomainProvidingComposedAdapterFactory(adapterFactories, this); 275 } 276 277 public String getOutputName(String inputName) 278 { 279 if (isSameEditingDomainAdapterFactory) 280 { 281 return inputName; 282 } 283 else if (mappingRoot.isTopToBottom()) 284 { 285 return 286 convertName 287 (inputName, 288 topLabelSeparator, 289 topLabelCase, 290 topLabelShortForms, 291 topLabelLongForms, 292 bottomLabelSeparator, 293 bottomLabelCase, 294 bottomLabelShortForms, 295 bottomLabelLongForms); 296 } 297 else 298 { 299 return 300 convertName 301 (inputName, 302 bottomLabelSeparator, 303 bottomLabelCase, 304 bottomLabelShortForms, 305 bottomLabelLongForms, 306 topLabelSeparator, 307 topLabelCase, 308 topLabelShortForms, 309 topLabelLongForms); 310 } 311 } 312 313 public List parseOutputName(String outputName) 314 { 315 if (mappingRoot.isTopToBottom()) 316 { 317 return parseName(outputName, bottomLabelSeparator); 318 } 319 else 320 { 321 return parseName(outputName, topLabelSeparator); 322 } 323 } 324 325 public List parseInputName(String inputName) 326 { 327 if (mappingRoot.isTopToBottom()) 328 { 329 return parseName(inputName, topLabelSeparator); 330 } 331 else 332 { 333 return parseName(inputName, bottomLabelSeparator); 334 } 335 } 336 337 public String getName(Object object) 338 { 339 if (object instanceof ENamedElement) 342 { 343 return ((ENamedElement)object).getName(); 344 } 345 346 IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(object, IItemLabelProvider.class); 349 return 350 itemLabelProvider != null ? 351 itemLabelProvider.getText(object) : 352 null; 353 } 354 355 public void setName(Object object, String name) 356 { 357 if (object instanceof ENamedElement) 360 { 361 ((ENamedElement)object).setName(name); 362 } 363 } 364 365 public EObject getOutputMetaObject(EObject inputMetaObject) 366 { 367 if (isSameEditingDomainAdapterFactory) 368 { 369 return inputMetaObject; 370 } 371 else 372 { 373 return getCorrespondingType(inputMetaObject, mappingRoot.isTopToBottom() ? topToBottomTypeTable : bottomToTopTypeTable); 374 } 375 } 376 377 public MappingRoot getMappingRoot() 378 { 379 return mappingRoot; 380 } 381 382 public void setMappingRoot(MappingRoot mappingRoot) 383 { 384 if (this.mappingRoot != mappingRoot) 385 { 386 this.mappingRoot = mappingRoot; 387 } 388 } 389 390 393 public Object getTypeClassifier(Object mappedObject) 394 { 395 if (mappedObject instanceof ETypedElement) 396 { 397 return ((ETypedElement)mappedObject).getEType(); 398 } 399 return null; 400 } 401 402 405 public void setTypeClassifier(Object mappedObject, Object typeClassifier) 406 { 407 if (mappedObject instanceof ETypedElement) 408 { 409 ((ETypedElement)mappedObject).setEType((EClassifier)typeClassifier); 410 } 411 } 412 413 public Object getOutputTypeClassifier(Object inputType) 414 { 415 Object outputType = null; 416 MappingRoot typeMappingRoot = mappingRoot.getTypeMappingRoot(); 417 if (typeMappingRoot != null) 418 { 419 Collection mappings = typeMappingRoot.getMappings(inputType); 420 if (mappings.size() == 1) 421 { 422 Mapping typeMapping = (Mapping)mappings.iterator().next(); 423 outputType = typeMapping.getOutputs().iterator().next(); 424 } 425 } 426 return outputType; 427 } 428 429 public int getMappingEnablementFlags() 430 { 431 return mappingEnablementFlags; 432 } 433 434 public void setMappingEnablementFlags(int mappingEnablementFlags) 435 { 436 this.mappingEnablementFlags = mappingEnablementFlags; 437 } 438 439 442 public void handleCreateCommand(Class commandClass, CommandParameter commandParameter, Command command) 443 { 444 if (persistentCommandStack != null) 445 { 446 persistentCommandStack.handleCreateCommand(commandClass, commandParameter, command); 447 } 448 } 449 450 public Command createCommand(Class commandClass, CommandParameter commandParameter) 451 { 452 if (commandClass == SetCommand.class && 456 !(commandParameter.getOwner() instanceof Mapping) && 457 !(commandParameter.getOwner() instanceof MappedObjectItemProvider) && 458 commandParameter.getFeature() == null && 459 commandParameter.getValue() instanceof Collection ) 460 { 461 boolean inputToOutput = mappingRoot.isOutputObject(commandParameter.getOwner()); 462 boolean okay = true; 463 for (Iterator objects = ((Collection )commandParameter.getValue()).iterator(); objects.hasNext(); ) 464 { 465 Object object = objects.next(); 466 if (mappingRoot.isInputObject(object) != inputToOutput) 467 { 468 okay = false; 469 } 470 } 471 if (okay) 472 { 473 Collection mappedObjects = new ArrayList ((Collection )commandParameter.getValue()); 474 mappedObjects.add(commandParameter.getOwner()); 475 return CreateMappingCommand.create(this, mappedObjects); 476 } 477 } 478 else if (commandClass == RemoveCommand.class && commandParameter.getOwner() == null) 479 { 480 Collection collection = commandParameter.getCollection(); 483 if (mappingRoot.getMappedObjects().containsAll(collection)) 484 { 485 commandParameter.setOwner(mappingRoot); 486 } 487 } 488 489 if ((mappingRoot.isInputObject(commandParameter.getOwner()) || 490 mappingRoot.isOutputReadOnly() && mappingRoot.isOutputObject(commandParameter.getOwner())) && 491 (commandClass == AddCommand.class || 492 commandClass == MoveCommand.class || 493 commandClass == RemoveCommand.class || 494 commandClass == ReplaceCommand.class || 495 commandClass == SetCommand.class)) 496 { 497 return UnexecutableCommand.INSTANCE; 498 } 499 else 500 { 501 Command result = super.createCommand(commandClass, commandParameter); 502 handleCreateCommand(commandClass, commandParameter, result); 503 return result; 504 } 505 } 506 507 public Command createOverrideCommand(OverrideableCommand command) 508 { 509 if (command instanceof AddCommand) 510 { 511 AddCommand addCommand = (AddCommand)command; 512 return createAddOverrideCommand(addCommand); 513 } 514 else if (command instanceof RemoveCommand) 515 { 516 RemoveCommand removeCommand = (RemoveCommand)command; 517 return createRemoveOverrideCommand(removeCommand); 518 } 519 else if (command instanceof SetCommand) 520 { 521 SetCommand setCommand = (SetCommand)command; 522 return createSetOverrideCommand(setCommand); 523 } 524 else if (command instanceof ReplaceCommand) 525 { 526 ReplaceCommand replaceCommand = (ReplaceCommand)command; 527 return createReplaceOverrideCommand(replaceCommand); 528 } 529 else if (command instanceof MoveCommand) 530 { 531 MoveCommand moveCommand = (MoveCommand)command; 532 return createMoveOverrideCommand(moveCommand); 533 } 534 else if (command instanceof CreateCopyCommand) 535 { 536 CreateCopyCommand createCopyCommand = (CreateCopyCommand)command; 537 return createCreateCopyOverrideCommand(createCopyCommand); 538 } 539 else if (command instanceof InitializeCopyCommand) 540 { 541 InitializeCopyCommand initializeCopyCommand = (InitializeCopyCommand)command; 542 return createInitializeCopyOverrideCommand(initializeCopyCommand); 543 } 544 else if (command instanceof CopyToClipboardCommand) 545 { 546 CopyToClipboardCommand copyToClipboardCommand = (CopyToClipboardCommand)command; 547 return createCopyToClipboardOverrideCommand(copyToClipboardCommand); 548 } 549 else if (command instanceof PasteFromClipboardCommand) 550 { 551 PasteFromClipboardCommand pasteFromClipboardCommand = (PasteFromClipboardCommand)command; 552 return createPasteFromClipboardOverrideCommand(pasteFromClipboardCommand); 553 } 554 else if (command instanceof DragAndDropCommand) 555 { 556 DragAndDropCommand dragAndDropCommand = (DragAndDropCommand)command; 557 return createDragAndDropOverrideCommand(dragAndDropCommand); 558 } 559 else 560 { 561 return null; 562 } 563 } 564 565 protected Command createAddOverrideCommand(AddCommand addCommand) 566 { 567 if (!(addCommand.getOwner() instanceof Mapping)) 568 { 569 return new AddOverrideCommand(this, addCommand); 570 } 571 572 return null; 573 } 574 575 protected Command createRemoveOverrideCommand(RemoveCommand removeCommand) 576 { 577 if (!(removeCommand.getOwner() instanceof Mapping)) 578 { 579 return new RemoveOverrideCommand(this, removeCommand); 580 } 581 582 return null; 583 } 584 585 protected Command createSetOverrideCommand(SetCommand setCommand) 586 { 587 EStructuralFeature feature = setCommand.getFeature(); 588 return 589 feature instanceof EReference && ((EReference)feature).isContainment() && mappingRoot.isOutputObject(setCommand.getOwner()) ? 590 new SetOverrideCommand(this, setCommand) : 591 null; 592 } 593 594 protected Command createMoveOverrideCommand(MoveCommand moveCommand) 595 { 596 return null; 597 } 598 599 protected Command createReplaceOverrideCommand(ReplaceCommand replaceCommand) 600 { 601 return null; 602 } 603 604 protected Command createCreateCopyOverrideCommand(CreateCopyCommand createCopyCommand) 605 { 606 if (mappingRoot.isInputObject(createCopyCommand.getOwner())) 607 { 608 return new CreateCopyOverrideCommand(this, createCopyCommand); 609 } 610 611 return null; 612 } 613 614 protected Command createInitializeCopyOverrideCommand(InitializeCopyCommand initializeCopyCommand) 615 { 616 if (!isSameEditingDomainAdapterFactory) 620 { 621 return new InitializeCopyOverrideCommand(this, initializeCopyCommand); 622 } 623 624 return null; 625 } 626 627 protected Command createCopyToClipboardOverrideCommand(CopyToClipboardCommand copyToClipboardCommand) 628 { 629 if (!(copyToClipboardCommand instanceof CopyToClipboardOverrideCommand)) 630 { 631 Collection inputObjects = new ArrayList (); 632 Collection nonInputObjects = new ArrayList (); 633 634 for (Iterator i = copyToClipboardCommand.getSourceObjects().iterator(); i.hasNext(); ) 635 { 636 Object object = i.next(); 637 if (mappingRoot.isInputObject(object)) 638 { 639 inputObjects.add(object); 640 } 641 else 642 { 643 nonInputObjects.add(object); 644 } 645 } 646 647 if (inputObjects.size() > 0) 648 { 649 return new CopyToClipboardOverrideCommand(this, nonInputObjects, inputObjects); 650 } 651 } 652 653 return null; 654 } 655 656 protected Command createPasteFromClipboardOverrideCommand(PasteFromClipboardCommand pasteFromClipboardCommand) 657 { 658 if (!isSameEditingDomainAdapterFactory && optimizeCopy && !(pasteFromClipboardCommand instanceof PasteFromClipboardOverrideCommand)) 659 { 660 return new PasteFromClipboardOverrideCommand(this, pasteFromClipboardCommand); 661 } 662 return null; 663 } 664 665 protected Command createDragAndDropOverrideCommand(DragAndDropCommand dragAndDropCommand) 666 { 667 if (!isSameEditingDomainAdapterFactory && optimizeCopy && !(dragAndDropCommand instanceof DragAndDropOverrideCommand)) 668 { 669 return new DragAndDropOverrideCommand(this, dragAndDropCommand); 670 } 671 return null; 672 } 673 674 protected EObject getCorrespondingType(EObject sourceType, HashMap typeTable) 675 { 676 EObject result = null; 677 EClassifier sourceClassifier = (EClassifier)sourceType; 678 String sourceTypeName = sourceClassifier.getEPackage().getNsURI() + packageTypeSeparator + sourceClassifier.getName(); 679 String targetTypeName = (String )typeTable.get(sourceTypeName); 680 681 if (targetTypeName != null) 682 { 683 int pos = targetTypeName.indexOf(packageTypeSeparator); 684 if (pos != -1) 685 { 686 String pkgName = targetTypeName.substring(0, pos); 687 EPackage targetPkg = EPackage.Registry.INSTANCE.getEPackage(pkgName); 688 targetTypeName = targetTypeName.substring(pos + 1, targetTypeName.length()); 689 result = targetPkg.getEClassifier(targetTypeName); 690 } 691 else 692 { 693 throw 694 new RuntimeException 695 (MappingPlugin.INSTANCE.getString 696 ("_EXC_AdapterFactoryMappingDomain_getCorrespondingType_failed", 697 new Object [] { targetTypeName, String.valueOf(packageTypeSeparator) })); 698 } 699 } 700 701 return result; 702 } 703 704 protected List parseName(String sourceName, char sourceSeparator) 705 { 706 List result = new ArrayList (); 707 708 StringBuffer currentWord = new StringBuffer (); 709 int length = sourceName.length(); 710 boolean lastIsLower = false; 711 712 for (int index=0; index<length; index++) 713 { 714 char curChar = sourceName.charAt(index); 715 if (Character.isUpperCase(curChar) || curChar == sourceSeparator) 716 { 717 if (lastIsLower || curChar == sourceSeparator) 718 { 719 result.add(currentWord.toString()); 720 currentWord = new StringBuffer (); 721 } 722 lastIsLower = false; 723 } 724 else 725 { 726 if (!lastIsLower) 727 { 728 int currentWordLength = currentWord.length(); 729 if (currentWordLength > 1) 730 { 731 char lastChar = currentWord.charAt(--currentWordLength); 732 currentWord.setLength(currentWordLength); 733 result.add(currentWord.toString()); 734 currentWord = new StringBuffer (); 735 currentWord.append(lastChar); 736 } 737 } 738 lastIsLower = true; 739 } 740 if (curChar != sourceSeparator) 741 { 742 currentWord.append(curChar); 743 } 744 } 745 746 result.add(currentWord.toString()); 747 return result; 748 } 749 750 protected String convertName 751 (String sourceName, 752 char sourceSeparator, 753 int sourceCase, 754 List sourceShortForms, 755 List sourceLongForms, 756 char targetSeparator, 757 int targetCase, 758 List targetShortForms, 759 List targetLongForms) 760 { 761 String result = convertNameForm(sourceName, sourceShortForms, sourceLongForms); 762 763 if (targetSeparator == LABEL_NO_SEPARATOR) 764 { 765 result = convertCase(result, sourceCase, targetCase, sourceSeparator); 766 result = convertSeparator(result, sourceSeparator, targetSeparator, sourceCase); 767 } 768 else 769 { 770 result = convertSeparator(result, sourceSeparator, targetSeparator, sourceCase); 771 result = convertCase(result, sourceCase, targetCase, sourceSeparator); 772 } 773 774 result = convertNameForm(result, targetLongForms, targetShortForms); 775 776 return result; 777 } 778 779 protected String convertCase(String sourceName, int sourceCase, int targetCase, char sourceSeparator) 780 { 781 if (targetCase == sourceCase) 782 { 783 return sourceName; 784 } 785 786 switch (targetCase) 787 { 788 case LABEL_UPPER: 789 { 790 return sourceName.toUpperCase(); 791 } 792 case LABEL_LOWER: 793 { 794 return sourceName.toLowerCase(); 795 } 796 case LABEL_MIXED: 797 { 798 StringBuffer newString = new StringBuffer (); 799 int lastIndex = 0; 800 int newIndex = 0; 801 for(;;) 802 { 803 newIndex = sourceName.indexOf(sourceSeparator, lastIndex); 804 if (newIndex != -1 && ++newIndex < sourceName.length()) 805 { 806 newString.append(sourceName.substring(lastIndex, newIndex).toLowerCase()); 807 newString.append(Character.toUpperCase(sourceName.charAt(newIndex))); 808 lastIndex = newIndex + 1; 809 } 810 else 811 { 812 newString.append(sourceName.substring(lastIndex).toLowerCase()); 813 break; 814 } 815 } 816 return newString.toString(); 817 } 818 default: 819 { 820 return null; 821 } 822 } 823 } 824 825 protected String convertSeparator(String sourceName, char sourceSeparator, char targetSeparator, int sourceCase) 826 { 827 if (targetSeparator == sourceSeparator) 828 { 829 return sourceName; 830 } 831 832 if (sourceSeparator != LABEL_NO_SEPARATOR && targetSeparator != LABEL_NO_SEPARATOR) 833 { 834 return sourceName.replace(sourceSeparator, targetSeparator); 835 } 836 837 if (targetSeparator == LABEL_NO_SEPARATOR) 838 { 839 StringBuffer newString = new StringBuffer (); 841 int lastIndex = 0; 842 int newIndex = 0; 843 for(;;) 844 { 845 newIndex = sourceName.indexOf(sourceSeparator, lastIndex); 846 if (newIndex != -1) 847 { 848 newString.append(sourceName.substring(lastIndex, newIndex)); 849 lastIndex = newIndex + 1; 850 } 851 else 852 { 853 newString.append(sourceName.substring(lastIndex)); 854 break; 855 } 856 } 857 return newString.toString(); 858 } 859 else 860 { 861 if (sourceCase == LABEL_MIXED) 863 { 864 StringBuffer newString = new StringBuffer (); 866 int length = sourceName.length(); 867 for (int index=0; index<length; index++) 868 { 869 char curChar = sourceName.charAt(index); 870 if (Character.isUpperCase(curChar) && index != 0) 871 newString.append(targetSeparator); 872 newString.append(curChar); 873 } 874 return newString.toString(); 875 } 876 } 877 878 return sourceName; 879 } 880 881 protected String convertNameForm(String name, List fromStrings, List toStrings) 882 { 883 String newName = name; 884 885 for (int i=0; i<fromStrings.size(); i++) 886 { 887 String fromString = (String )fromStrings.get(i); 888 if (name.indexOf(fromString) != -1) 889 { 890 String toString = (String )toStrings.get(i); 891 newName = change(newName, fromString, toString); 892 } 893 } 894 895 return newName; 896 } 897 898 protected String change(String in, String oldPat, String newPat) 904 { 905 if (oldPat.length() == 0) 906 return in; 907 if (oldPat.length() == 1 && newPat.length() == 1) 908 return in.replace(oldPat.charAt(0), newPat.charAt(0)); 909 910 int lastIndex = 0; 911 int newIndex = 0; 912 StringBuffer newString = new StringBuffer (); 913 for(;;) 914 { 915 newIndex = in.indexOf(oldPat, lastIndex); 916 if (newIndex != -1) 917 { 918 newString.append(in.substring(lastIndex, newIndex) + newPat); 919 lastIndex = newIndex + oldPat.length(); 920 } 921 else 922 { 923 newString.append(in.substring(lastIndex)); 924 break; 925 } 926 } 927 return newString.toString(); 928 } 929 } 930 | Popular Tags |