001 package com.hammurapi.review.util;
002
003 import java.io.File;
004 import java.util.ArrayList;
005 import java.util.HashMap;
006 import java.util.Iterator;
007 import java.util.List;
008 import java.util.Map;
009 import java.util.ServiceLoader;
010
011 import javax.xml.parsers.DocumentBuilderFactory;
012 import javax.xml.xpath.XPath;
013 import javax.xml.xpath.XPathConstants;
014 import javax.xml.xpath.XPathFactory;
015
016 import org.eclipse.emf.common.util.URI;
017 import org.eclipse.emf.ecore.EPackage.Registry;
018 import org.eclipse.emf.ecore.resource.Resource;
019 import org.eclipse.emf.ecore.resource.ResourceSet;
020 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
021 import org.eclipse.emf.ecore.xmi.XMLResource;
022 import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
023 import org.w3c.dom.Document;
024 import org.w3c.dom.Element;
025 import org.w3c.dom.Node;
026 import org.w3c.dom.NodeList;
027
028 import com.hammurapi.config.ConfigFactory;
029 import com.hammurapi.config.NamedObjectDefinition;
030 import com.hammurapi.config.runtime.PackageProvider;
031 import com.hammurapi.review.Inspector;
032 import com.hammurapi.review.InspectorCategory;
033 import com.hammurapi.review.InspectorSet;
034 import com.hammurapi.review.ReviewFactory;
035
036 /**
037 * Migrates rule set definitions from 5.x to 6
038 * @author Pavel Vlasov
039 *
040 */
041 public class Migrator {
042
043 private static final String TYPE = "type";
044
045 /**
046 * @param args
047 */
048 public static void main(String[] args) throws Exception {
049 System.out.println("Usage: java [options] "+Migrator.class.getName()+" <source file> <target file>");
050 Document input = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(args[0]));
051
052 ResourceSet resourceSet = new ResourceSetImpl();
053 resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
054 Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
055
056 Registry registry = resourceSet.getPackageRegistry();
057 Iterator<PackageProvider> ppit = ServiceLoader.load(PackageProvider.class, Migrator.class.getClassLoader()).iterator();
058 while (ppit.hasNext()) {
059 ppit.next().registerPackages(registry);
060 }
061
062 InspectorSet inspectorSet = ReviewFactory.eINSTANCE.createInspectorSet();
063 Element root = input.getDocumentElement();
064 XPath xPath = XPathFactory.newInstance().newXPath();
065 inspectorSet.setName(xPath.evaluate("name/text()", root));
066 inspectorSet.setDescription(xPath.evaluate("description/text()", root));
067
068 Map<List<String>, InspectorCategory> categoryMap = new HashMap<List<String>, InspectorCategory>();
069 NodeList inspectors = (NodeList) xPath.evaluate("rules/rule", root, XPathConstants.NODESET);
070 for (int i=0, l=inspectors.getLength(); i<l; ++i) {
071 Inspector inspector = ReviewFactory.eINSTANCE.createInspector();
072 inspectorSet.getInspectors().add(inspector);
073 Node iNode = inspectors.item(i);
074 inspector.setName(xPath.evaluate("name/text()", iNode));
075 inspector.setDescription(xPath.evaluate("description/text()", iNode));
076
077 List<String> categories = new ArrayList<String>();
078 NodeList cnl = (NodeList) xPath.evaluate("category", iNode, XPathConstants.NODESET);
079 for (int j=0, cl=cnl.getLength(); j<cl; ++j) {
080 categories.add(xPath.evaluate("text()", cnl.item(j)));
081 }
082 if (!categories.isEmpty()) {
083 inspector.setCategory(getCategory(categories, categoryMap, inspectorSet));
084 }
085 inspector.setSeverity(Integer.parseInt(xPath.evaluate("severity/text()", iNode)));
086 inspector.setType(((Element) iNode).getAttribute(TYPE));
087 NodeList allChildren=iNode.getChildNodes();
088 for (int j=0, cl=allChildren.getLength(); j<cl; ++j) {
089 Node pNode = allChildren.item(j);
090 if (pNode instanceof Element) {
091 String pName = pNode.getNodeName();
092 if (!"name".equals(pName) && !"severity".equals(pName) && !"description".equals(pName) && !"category".equals(pName)) {
093 NamedObjectDefinition property = ConfigFactory.eINSTANCE.createNamedObjectDefinition();
094 inspector.getProperty().add(property);
095 property.setName(pName);
096 Element pElement = (Element) pNode;
097 if (pElement.hasAttribute(TYPE)) {
098 property.setType(pElement.getAttribute(TYPE));
099 }
100 property.setValue(xPath.evaluate("text()", pNode));
101 }
102 }
103 }
104 }
105
106 Resource inspectorSetResource = resourceSet.createResource(URI.createFileURI(new File(args[1]).getAbsolutePath()));
107 if (inspectorSetResource instanceof XMLResource) {
108 ((XMLResource) inspectorSetResource).setEncoding("UTF-8");
109 }
110 inspectorSetResource.getContents().add(inspectorSet);
111 inspectorSetResource.save(null);
112 }
113
114 private static InspectorCategory getCategory(List<String> categories, Map<List<String>, InspectorCategory> categoryMap, InspectorSet inspectorSet) {
115 InspectorCategory ret = categoryMap.get(categories);
116 if (ret==null) {
117 ret = ReviewFactory.eINSTANCE.createInspectorCategory();
118 ret.setName(categories.get(categories.size()-1));
119 if (categories.size()==1) {
120 inspectorSet.getCategories().add(ret);
121 } else {
122 List<String> pc = new ArrayList<String>(categories);
123 pc.remove(categories.size()-1);
124 getCategory(pc, categoryMap, inspectorSet).getSubCategory().add(ret);
125 }
126 categoryMap.put(categories, ret);
127 }
128 return ret;
129 }
130
131 }