| /***************************************************************************** |
| * Copyright (c) 2017 CEA LIST 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: |
| * Francois LE FEVRE (CEA) francois.le-fevre@cea.fr - Initial API and Implementation |
| * |
| *****************************************************************************/ |
| package org.eclipse.papyrus.developer.migration.elementtypereference.expansionmodel; |
| |
| import java.io.File; |
| import java.util.List; |
| |
| import javax.xml.parsers.DocumentBuilder; |
| import javax.xml.parsers.DocumentBuilderFactory; |
| |
| import org.apache.commons.io.FileUtils; |
| import org.apache.log4j.Logger; |
| import org.eclipse.papyrus.developer.migration.elementtypereference.common.AbstractElementTypeMigrationModel; |
| import org.w3c.dom.Document; |
| import org.w3c.dom.Element; |
| import org.w3c.dom.Node; |
| import org.w3c.dom.NodeList; |
| |
| |
| /** |
| * |
| * Dedicated to migrate |
| * |
| */ |
| public class ElementTypeMigrationExpansionModel extends AbstractElementTypeMigrationModel{ |
| |
| static Logger LOG4J = Logger.getLogger("org.eclipse.papyrus.developer.migration.elementtypereference.expansion.ElementTypeMigrationExpansionModel"); |
| |
| static String ELEMENTTYPESCONFIGURATIONS = new String("elementtypesconfigurations"); |
| static String EXPANSIONMODEL = new String("expansionmodel"); |
| |
| |
| public ElementTypeMigrationExpansionModel(String folderRootGit4ElementtypeData) { |
| super(folderRootGit4ElementtypeData); |
| } |
| |
| public void migrateModelDocuments(String rootFolder, String outputRootFolder){ |
| Document doc = null; |
| try { |
| String[] extensions = new String[] {EXPANSIONMODEL}; |
| |
| File folder = new File(rootFolder); |
| |
| List<File> files = (List<File>) FileUtils.listFiles(folder, extensions, true ); |
| boolean migrated; |
| for (File file : files) { |
| migrated = false; |
| if (file.isFile()) { |
| DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| doc = dBuilder.parse(file); |
| |
| //optional, but recommended |
| //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work |
| doc.getDocumentElement().normalize(); |
| |
| NodeList nList= doc.getElementsByTagName("expansionmodel:DiagramExpansion"); |
| if(nList!=null && nList.getLength()!=0) { |
| LOG4J.debug("Dealing with expansionmodel: "+ file.getAbsolutePath()); |
| |
| Element root = doc.getDocumentElement(); |
| if(root.getAttribute("xmlns:elementtypesconfigurations").length()==0) { |
| root.setAttribute("xmlns:elementtypesconfigurations", "http://www.eclipse.org/papyrus/infra/elementtypesconfigurations/1.2"); |
| } |
| |
| nList = doc.getElementsByTagName("representations"); |
| for (int temp = 0; temp < nList.getLength(); temp++) { |
| doc.getElementsByTagName("ElementCreationMenuModel:Folder"); |
| //xmlns:elementtypesconfigurations="http://www.eclipse.org/papyrus/infra/elementtypesconfigurations/1.2" |
| |
| Node nNode = nList.item(temp); |
| Element eElement = (Element) nNode; |
| if(eElement.getAttribute("graphicalElementType")!=null) { |
| if(identifier2Id.get(eElement.getAttribute("graphicalElementType"))!=null) { |
| Element newChild = doc.createElement("graphicalElementTypeRef"); |
| newChild.setAttribute("xsi:type", identifier2Id.get(eElement.getAttribute("graphicalElementType")).get(2)); |
| newChild.setAttribute("href", "platform:/plugin/"+identifier2Id.get(eElement.getAttribute("graphicalElementType")).get(1)+"#"+identifier2Id.get(eElement.getAttribute("graphicalElementType")).get(0)); |
| eElement.appendChild(newChild); |
| eElement.removeAttribute("graphicalElementType"); |
| migrated=true; |
| |
| } |
| else { |
| LOG4J.warn(eElement.getAttribute("graphicalElementType")+"\t@"+identifier2Id.get(eElement.getAttribute("graphicalElementType"))); |
| } |
| } |
| } |
| if(migrated) { |
| exportDocument2File(doc, file, outputRootFolder) ; |
| } |
| |
| |
| } |
| } |
| } |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| } |