Java tutorial
/* * Copyright 2013 Christoph Brill <egore911@gmail.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package de.egore911.versioning.deployer.performer; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map.Entry; import java.util.Properties; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.IOFileFilter; import org.apache.commons.io.filefilter.TrueFileFilter; import org.apache.commons.io.filefilter.WildcardFileFilter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import de.egore911.versioning.deployer.util.ReplacementPair; /** * @author Christoph Brill <egore911@gmail.com> */ public class PerformReplacement { private static final Logger LOG = LoggerFactory.getLogger(PerformReplacement.class); private final XPathExpression replaceXpath; private final XPathExpression basepathXpath; private final XPathExpression wildcardsWildcardXpath; private final XPathExpression replacementsReplacementXpath; private final XPathExpression variableXpath; private final XPathExpression valueXpath; private final XPathExpression replacementfileReplacementfileXpath; public PerformReplacement(XPath xPath) throws XPathExpressionException { replaceXpath = xPath.compile("replace"); basepathXpath = xPath.compile("basepath/text()"); wildcardsWildcardXpath = xPath.compile("wildcards/wildcard/text()"); replacementsReplacementXpath = xPath.compile("replacements/replacement"); variableXpath = xPath.compile("variable/text()"); valueXpath = xPath.compile("value/text()"); replacementfileReplacementfileXpath = xPath.compile("replacementfiles/replacementfile/text()"); } public void perform(Node serverDeploymentsDeploymentNode) throws XPathExpressionException { NodeList replacementOperations = (NodeList) replaceXpath.evaluate(serverDeploymentsDeploymentNode, XPathConstants.NODESET); for (int j = 0; j < replacementOperations.getLength(); j++) { Node replacementOperation = replacementOperations.item(j); String basepath = (String) basepathXpath.evaluate(replacementOperation, XPathConstants.STRING); NodeList wildcardNodes = (NodeList) wildcardsWildcardXpath.evaluate(replacementOperation, XPathConstants.NODESET); List<String> wildcards = new ArrayList<>(); for (int k = 0; k < wildcardNodes.getLength(); k++) { Node wildcard = wildcardNodes.item(k); wildcards.add(wildcard.getNodeValue()); } NodeList replacementsNodes = (NodeList) replacementsReplacementXpath.evaluate(replacementOperation, XPathConstants.NODESET); List<ReplacementPair> replacements = new ArrayList<>(); for (int k = 0; k < replacementsNodes.getLength(); k++) { Node replacement = replacementsNodes.item(k); String variable = (String) variableXpath.evaluate(replacement, XPathConstants.STRING); String value = (String) valueXpath.evaluate(replacement, XPathConstants.STRING); replacements.add(new ReplacementPair(variable, value)); } NodeList replacementfilesNodes = (NodeList) replacementfileReplacementfileXpath .evaluate(replacementOperation, XPathConstants.NODESET); List<File> replacementfiles = new ArrayList<>(); for (int k = 0; k < replacementfilesNodes.getLength(); k++) { Node replacementfile = replacementfilesNodes.item(k); File file = new File(replacementfile.getNodeValue()); if (!file.exists()) { LOG.error("Could not find file {}, replacements will be likely incomplete", file); } replacementfiles.add(file); } perform(new File(basepath), wildcards, replacements, replacementfiles); } } static void perform(File directory, List<String> wildcards, List<ReplacementPair> replacementsIn, List<File> replacementfiles) { List<ReplacementPair> replacements = new ArrayList<>(replacementsIn); for (File replacementfile : replacementfiles) { if (replacementfile.exists()) { Properties properties = new Properties(); try { properties.load(new FileInputStream(replacementfile)); } catch (IOException e) { LOG.error(e.getMessage(), e); return; } for (Entry<Object, Object> x : properties.entrySet()) { replacements .add(new ReplacementPair(((String) x.getKey()).trim(), ((String) x.getValue()).trim())); } } else { LOG.warn("Replacement file {} not found", replacementfile); } } perform(directory, wildcards, replacements); } private static void perform(File directory, List<String> wildcards, List<ReplacementPair> replacements) { if (!directory.exists()) { LOG.error("Directory {} does not exist", directory); return; } if (!directory.isDirectory()) { LOG.error("{} is not a directory", directory); return; } IOFileFilter fileFilter = new WildcardFileFilter(wildcards); Collection<File> files = FileUtils.listFiles(directory, fileFilter, TrueFileFilter.INSTANCE); for (File file : files) { try { String content = FileUtils.readFileToString(file); for (ReplacementPair replacement : replacements) { content = content.replace("${versioning:" + replacement.variable + "}", replacement.value); } FileUtils.write(file, content); } catch (IOException e) { LOG.error(e.getMessage(), e); } } } }