| //------------------------------------------------------------------------------ |
| // Copyright (c) 2005, 2006 IBM Corporation and others. |
| // All rights reserved. This program and the accompanying materials |
| // are made available under the terms of the Eclipse Public License v1.0 |
| // which accompanies this distribution, and is available at |
| // http://www.eclipse.org/legal/epl-v10.html |
| // |
| // Contributors: |
| // IBM Corporation - initial implementation |
| //------------------------------------------------------------------------------ |
| package org.eclipse.epf.library.tester.impl.testcommands; |
| |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.Iterator; |
| import java.util.LinkedHashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.eclipse.emf.ecore.EAttribute; |
| import org.eclipse.emf.ecore.EClass; |
| import org.eclipse.emf.ecore.EDataType; |
| import org.eclipse.emf.ecore.EStructuralFeature; |
| import org.eclipse.epf.library.tester.TesterOutputUtil; |
| import org.eclipse.epf.library.tester.iface.TCExeReply; |
| import org.eclipse.epf.library.tester.impl.TestCommandImpl; |
| import org.eclipse.epf.library.tester.impl.TestCommandMgr; |
| import org.eclipse.epf.uma.ContentDescription; |
| import org.eclipse.epf.uma.ContentElement; |
| import org.eclipse.epf.uma.MethodElement; |
| import org.eclipse.epf.uma.MethodLibrary; |
| import org.w3c.dom.Element; |
| import org.w3c.dom.NodeList; |
| |
| /** |
| * A test command class |
| * |
| * @author Weiping Lu |
| * @since 1.0 |
| * |
| */ |
| public class TCEditContentElement extends TestCommandImpl { |
| |
| static class TagData { |
| public Map attMap = new LinkedHashMap(); |
| public Map refMap = new LinkedHashMap(); |
| } |
| |
| private static boolean localDebug = true; |
| private EClass eclass; |
| private TagData tagData; |
| private Map refValueMap = new HashMap(); |
| private static Map eclassDataMap = new HashMap(); |
| |
| public TCEditContentElement(EClass ecls, TestCommandMgr owner) { |
| setOwner(owner); |
| eclass = ecls; |
| tagData = (TagData) eclassDataMap.get(eclass); |
| if (tagData != null) { |
| return; |
| } |
| tagData = new TagData(); |
| eclassDataMap.put(eclass, tagData); |
| |
| TesterOutputUtil.show("eclass.getEAllAttributes()", eclass.getEAllAttributes()); |
| |
| addToEclassDataMap(eclass.getEAllAttributes(), tagData.attMap); |
| addToEclassDataMap(eclass.getEAllReferences(), tagData.refMap); |
| |
| } |
| |
| private void addToEclassDataMap(List features, Map feaMap) { |
| for (int i=0; i<features.size(); i++) { |
| EStructuralFeature feature = (EStructuralFeature) features.get(i); |
| feaMap.put(feature.getName(), feature); |
| } |
| } |
| |
| public void parse(Element element) { |
| setAttribute(AT_Type, element.getAttribute(AT_Type)); |
| setAttribute(AT_Path, element.getAttribute(AT_Path)); |
| |
| for (Iterator it = tagData.attMap.entrySet().iterator(); it.hasNext();) { |
| Map.Entry entry = (Map.Entry) it.next(); |
| String at_tag = (String) entry.getKey(); |
| setAttribute(at_tag, element.getAttribute(at_tag)); |
| } |
| |
| parseChildren(element, tagData.refMap); |
| } |
| |
| protected void parseChildren(Element element, Map refMap) { |
| for (Iterator it = refMap.entrySet().iterator(); it.hasNext();) { |
| Map.Entry entry = (Map.Entry) it.next(); |
| String vt_tag = (String) entry.getKey(); |
| setAttribute(vt_tag, element.getAttribute(vt_tag)); |
| |
| NodeList cnodes = element.getElementsByTagName(vt_tag); |
| if (cnodes == null || cnodes.getLength() == 0) { |
| continue; |
| } |
| Element cElement = (Element) cnodes.item(0); |
| cnodes = cElement.getElementsByTagName(VT_Value); |
| if (cnodes == null || cnodes.getLength() == 0) { |
| return; |
| } |
| List valueList = new ArrayList(); |
| for (int i=0; i<cnodes.getLength(); i++) { |
| Element vElem = (Element) cnodes.item(i); |
| String value = vElem.getFirstChild().getNodeValue(); |
| valueList.add(value); |
| } |
| refValueMap.put(vt_tag, valueList); |
| } |
| } |
| |
| public TCExeReply execute() { |
| MethodLibrary currLib = getOwner().getCurrentBaseLib(); |
| |
| String path = getAttribute(AT_Path); |
| MethodElement me = (MethodElement) TesterOutputUtil.getMethodElement(currLib, path); |
| if (me == null) { |
| return null; |
| } |
| |
| Map attMap = tagData.attMap; |
| Map attributeMap = getAttributeMap(); |
| for (Iterator it = attMap.entrySet().iterator(); it.hasNext();) { |
| Map.Entry entry = (Map.Entry) it.next(); |
| String at_tag = (String) entry.getKey(); |
| EAttribute eatt = (EAttribute) entry.getValue(); |
| EDataType dataType = eatt.getEAttributeType(); |
| String at_val = (String) attributeMap.get(at_tag); |
| if (localDebug) { |
| System.out.println("LD> at_tag: " + at_tag); |
| System.out.println("LD> at_val: " + at_val); |
| System.out.println("LD> eatt: " + eatt); |
| System.out.println("LD> dataType: " + dataType); |
| System.out.println(""); |
| } |
| if (at_val == null || at_val.length() == 0) { |
| continue; |
| } |
| if (dataType.getName().equals("String")) { |
| me.eSet(eatt, at_val); |
| } |
| } |
| |
| save((MethodElement) me.eContainer()); |
| |
| return null; |
| } |
| |
| |
| |
| } |
| |
| |