Last active
February 7, 2019 16:16
-
-
Save Stwissel/aeff1c523983ad7a89b33a7d2d67de43 to your computer and use it in GitHub Desktop.
Extract validation relevant files from object meta data files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** ========================================================================= * | |
* Copyright (C) 2019 Salesforce Inc ( http://www.salesforce.com/ . * | |
* All rights reserved. * | |
* * | |
* @author Stephan H. Wissel (stw) <swissel@salesforce.com> * | |
* @notessensei * | |
* @version 1.0 * | |
* ========================================================================== * | |
* * | |
* Licensed under the Apache License, Version 2.0 (the "License"). You may * | |
* not use this file except in compliance with the License. You may obtain a * | |
* copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>. * | |
* * | |
* Unless required by applicable law or agreed to in writing, software * | |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * | |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * | |
* License for the specific language governing permissions and limitations * | |
* under the License. * | |
* * | |
* ========================================================================== * | |
*/ | |
package net.wissel.sfdc; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.FilenameFilter; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.util.HashMap; | |
import java.util.Map; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import javax.xml.transform.TransformerConfigurationException; | |
import javax.xml.xpath.XPath; | |
import javax.xml.xpath.XPathConstants; | |
import javax.xml.xpath.XPathExpressionException; | |
import javax.xml.xpath.XPathFactory; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
import org.xml.sax.SAXException; | |
import com.google.common.io.Files; | |
/** | |
* @author swissel | |
* | |
*/ | |
public class ValidationReportGenerator { | |
/** | |
* @param args | |
* @throws Exception | |
*/ | |
public static void main(final String[] args) throws Exception { | |
if (args.length < 2) { | |
System.err.println("Usage ValidationReportGenerator sourceDir outputFile.xml"); | |
System.exit(1); | |
} | |
final String sourceDirName = args[0]; | |
final String outputFileName = args[1]; | |
final ValidationReportGenerator vrg = new ValidationReportGenerator(); | |
vrg.extractValidations(sourceDirName, outputFileName); | |
System.out.println("Done"); | |
} | |
private void addElementToOutput(final Element element, final SimpleXMLDoc output) | |
throws TransformerConfigurationException, SAXException { | |
output.openTag("Rule"); | |
final NodeList children = element.getChildNodes(); | |
for (int i = 0; i < children.getLength(); i++) { | |
Node n = children.item(i); | |
if (n instanceof Element) { | |
final Element childElement = (Element) n; | |
output.addTag(childElement.getTagName(), childElement.getTextContent()); | |
} | |
} | |
output.closeTag(1); | |
} | |
public void extractValidations(final String sourceDirName, final String outputFileName) throws Exception { | |
final File sourceDir = new File(sourceDirName); | |
if (!sourceDir.exists() || !sourceDir.isDirectory()) { | |
System.err.println("Source doesn't exist or isn't a directory"); | |
return; | |
} | |
final File targetFile = new File(outputFileName); | |
Files.createParentDirs(targetFile); | |
final SimpleXMLDoc output = new SimpleXMLDoc(); | |
output.openTag("Validations"); | |
final FilenameFilter filter = (dir, name) -> name.endsWith(".object"); | |
for (final String curFileName : sourceDir.list(filter)) { | |
this.processOneObjectDefinition(sourceDir.getAbsolutePath() + "/" + curFileName, output); | |
} | |
// Closure | |
output.closeDocument(); | |
final OutputStream out = new FileOutputStream(targetFile); | |
output.printToStream(out); | |
out.close(); | |
} | |
private NodeList getValidationList(final String curFileName) | |
throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { | |
final File inFile = new File(curFileName); | |
final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |
final Document doc = dBuilder.parse(inFile); | |
final XPath xPath = XPathFactory.newInstance().newXPath(); | |
final String expression = "/CustomObject/validationRules"; | |
final NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET); | |
return nodeList; | |
} | |
private void processOneObjectDefinition(final String curFileName, final SimpleXMLDoc output) | |
throws TransformerConfigurationException, SAXException, ParserConfigurationException, IOException, | |
XPathExpressionException { | |
final String objectName = curFileName.substring(curFileName.lastIndexOf("/")+1, curFileName.lastIndexOf(".")); | |
final NodeList nodeList = this.getValidationList(curFileName); | |
if ((nodeList != null) && (nodeList.getLength() > 0)) { | |
final Map<String, String> attributes = new HashMap<>(); | |
attributes.put("objectName", objectName); | |
output.openTag("Validation", attributes); | |
for (int i = 0; i < nodeList.getLength(); i++) { | |
final Element element = (Element) nodeList.item(i); | |
this.addElementToOutput(element, output); | |
} | |
output.closeTag(1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment