KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > command > NameMatchMappingCommand


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: NameMatchMappingCommand.java,v 1.2 2005/06/08 06:21:43 nickb Exp $
16  */

17 package org.eclipse.emf.mapping.command;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.eclipse.emf.common.command.Command;
26 import org.eclipse.emf.edit.command.CommandParameter;
27 import org.eclipse.emf.mapping.Mapping;
28 import org.eclipse.emf.mapping.domain.MappingDomain;
29
30
31 public class NameMatchMappingCommand extends MatchMappingCommand
32 {
33   /**
34    * This creates a command that creates a new child mappings for the given mapping
35    * by attempting to match by name input children with output children.
36    */

37   public static Command create(MappingDomain domain, Mapping mapping)
38   {
39     return domain.createCommand(NameMatchMappingCommand.class, new CommandParameter(mapping));
40   }
41
42   public NameMatchMappingCommand(MappingDomain domain, Mapping mapping)
43   {
44     super(domain, mapping);
45   }
46
47   protected boolean match(Object JavaDoc inputObject, Object JavaDoc outputObject, Collection JavaDoc mappedObjects)
48   {
49     String JavaDoc inputName = domain.getName(inputObject);
50     String JavaDoc outputName = domain.getName(outputObject);
51
52     if (inputName != null && outputName != null)
53     {
54       List JavaDoc parsedInputName = domain.parseInputName(inputName);
55       List JavaDoc parsedOutputName = domain.parseOutputName(outputName);
56
57       if (concatName(parsedInputName).equalsIgnoreCase(concatName(parsedOutputName)))
58       {
59         mappedObjects.add(inputObject);
60       }
61 // int parsedInputSize = parsedInputName.size();
62
// if (parsedInputSize > 0 && parsedInputSize == parsedOutputName.size())
63
// {
64
// boolean match = true;
65
// for (int i=0; i < parsedInputSize; i++)
66
// {
67
// String inputWord = (String)parsedInputName.get(i);
68
// String outputWord = (String)parsedOutputName.get(i);
69
// if (!inputWord.equalsIgnoreCase(outputWord))
70
// {
71
// match = false;
72
// break;
73
// }
74
// }
75
// if (match)
76
// {
77
// mappedObjects.add(inputObject);
78
// }
79
// }
80
}
81
82     boolean multipleMatchesAllowed = (domain.getMappingEnablementFlags() & MappingDomain.ENABLE_MULTIPLE_INPUTS) != 0;
83     return !multipleMatchesAllowed; // return false if iteration should continue.
84
}
85
86   protected String JavaDoc concatName(List JavaDoc parsedName)
87   {
88     String JavaDoc result = "";
89     for (Iterator JavaDoc nameIter = parsedName.iterator(); nameIter.hasNext(); )
90     {
91       result += nameIter.next();
92     }
93     return result;
94   }
95
96   public void execute()
97   {
98     super.execute();
99
100     // Now we'll do recursive MatchMapping.
101
// (We need to iterate over a copy, since we modify the underlying list in the loop.)
102
for (Iterator JavaDoc commands = new ArrayList JavaDoc(commandList).iterator(); commands.hasNext(); )
103     {
104       Command command = (Command)commands.next();
105       appendAndExecute(NameMatchMappingCommand.create(domain, (Mapping)command.getResult().iterator().next()));
106     }
107   }
108
109 }
110
Popular Tags