Пример #1
0
 private NodeList getValueNodes(Node operation) {
   List<Node> valueNL = getChildNodes(operation, "value");
   if (valueNL.isEmpty()) {
     return null;
   }
   return valueNL.get(0).getChildNodes();
 }
Пример #2
0
  private List<Node> getChildNodes(Node parentNode, String tagName) {
    List<Node> nodeList = new ArrayList<Node>();
    for (Node child = parentNode.getFirstChild(); child != null; child = child.getNextSibling()) {
      if (child.getNodeType() == Node.ELEMENT_NODE && tagName.equals(child.getNodeName())) {
        nodeList.add(child);
      }
    }

    return nodeList;
  }
Пример #3
0
 private NodeList findNodes(Document doc, Node operation) {
   List<Node> xpaths = getChildNodes(operation, "xpath");
   if (xpaths.isEmpty()) {
     return null;
   }
   String xpathExpression = xpaths.get(0).getTextContent();
   if (xpathExpression == null) {
     return null;
   }
   XPathFactory xPathfactory = XPathFactory.newInstance();
   XPath xpath = xPathfactory.newXPath();
   NodeList nl = null;
   try {
     XPathExpression expr = xpath.compile(xpathExpression);
     nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
   } catch (XPathExpressionException ex) {
     Utils.onError(new Error.WrongXpathExpression(xpathExpression));
   }
   return nl;
 }
Пример #4
0
 private void processApply(Node operation)
     throws ParserConfigurationException, TransformerConfigurationException {
   List<Node> targets = getChildNodes(operation, "target");
   List<Node> scripts = getChildNodes(operation, "script");
   for (int t = 0; t < targets.size(); t++) {
     String path = absolutePath(targets.get(t).getTextContent());
     NodeList[] operations = new NodeList[scripts.size()];
     for (int s = 0; s < scripts.size(); s++) {
       operations[s] = scripts.get(s).getChildNodes();
     }
     apply(path, operations);
   }
 }
Пример #5
0
 private void processCopy(Node operation) {
   List<Node> fromNode = getChildNodes(operation, "from");
   List<Node> toNode = getChildNodes(operation, "to");
   if (fromNode.isEmpty() || toNode.isEmpty()) {
     return;
   }
   String path = fromNode.get(0).getTextContent();
   String fromPath = (new File(path).isAbsolute()) ? path : absolutePath(path);
   String toPath = absolutePath(toNode.get(0).getTextContent());
   Boolean copyContents = fromPath.endsWith(File.separator + "*");
   File from = new File((copyContents) ? fromPath.substring(0, fromPath.length() - 2) : fromPath);
   File to = new File(toPath);
   if (!from.exists()) {
     Utils.onError(new Error.FileNotFound(from.getPath()));
     return;
   }
   try {
     if (copyContents) {
       FileUtils.forceMkdir(to);
       FileUtils.copyDirectory(from, to);
     } else if (from.isDirectory()) {
       FileUtils.forceMkdir(to);
       FileUtils.copyDirectoryToDirectory(from, to);
     } else {
       if (to.isDirectory()) {
         FileUtils.forceMkdir(to);
         FileUtils.copyFileToDirectory(from, to);
       } else {
         File toDir = new File(Utils.getParentDir(to));
         FileUtils.forceMkdir(toDir);
         FileUtils.copyFile(from, to);
       }
     }
   } catch (IOException ex) {
     Utils.onError(new Error.FileCopy(from.getPath(), to.getPath()));
   }
 }
Пример #6
0
  private void processXml(Node operation)
      throws ParserConfigurationException, TransformerConfigurationException {
    List<Node> targets = getChildNodes(operation, "target");
    List<Node> appendOpNodes = getChildNodes(operation, "append");
    List<Node> setOpNodes = getChildNodes(operation, "set");
    List<Node> replaceOpNodes = getChildNodes(operation, "replace");
    List<Node> removeOpNodes = getChildNodes(operation, "remove");
    if (targets.isEmpty()) {
      return;
    }
    for (int t = 0; t < targets.size(); t++) {
      File target = new File(absolutePath(targets.get(t).getTextContent()));
      if (!target.exists()) {
        Utils.onError(new Error.FileNotFound(target.getPath()));
        return;
      }
      DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document doc = null;
      try {
        doc = db.parse(target);
      } catch (Exception ex) {
        Utils.onError(new Error.FileParse(target.getPath()));
        return;
      }
      for (int i = 0; i < removeOpNodes.size(); i++) removeXmlEntry(doc, removeOpNodes.get(i));
      for (int i = 0; i < replaceOpNodes.size(); i++) replaceXmlEntry(doc, replaceOpNodes.get(i));
      for (int i = 0; i < setOpNodes.size(); i++) setXmlEntry(doc, setOpNodes.get(i));
      for (int i = 0; i < appendOpNodes.size(); i++) appendXmlEntry(doc, appendOpNodes.get(i));

      try {
        OutputFormat format = new OutputFormat(doc);
        format.setOmitXMLDeclaration(true);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setIndent(4);
        Writer out = new FileWriter(target);
        XMLSerializer serializer = new XMLSerializer(out, format);
        serializer.serialize(doc);
      } catch (IOException e) {
        Utils.onError(new Error.WriteXmlConfig(target.getPath()));
      }
    }
  }
Пример #7
0
  private void processTxt(Node operation) {
    List<Node> targets = getChildNodes(operation, "target");
    List<Node> optionNodes = getChildNodes(operation, "opt");
    List<Node> separatorNode = getChildNodes(operation, "separator");
    if (targets.isEmpty() || optionNodes.isEmpty()) {
      return;
    }
    String defaultSeparator = "=";
    String globalSeparator = defaultSeparator;
    if (!separatorNode.isEmpty()) {
      globalSeparator = separatorNode.get(0).getTextContent();
      if (globalSeparator.length() != 1) {
        globalSeparator = defaultSeparator;
      }
    }
    Map<String, String> options = new HashMap<String, String>();
    Map<String, String> processedOptions = new HashMap<String, String>();
    for (int i = 0; i < optionNodes.size(); i++) {
      Node option = optionNodes.get(i);
      String name = option.getAttributes().getNamedItem("name").getNodeValue();
      String value = option.getTextContent();
      if (options.containsKey(name)) {
        options.remove(name);
      }
      options.put(name, value);
    }
    for (int t = 0; t < targets.size(); t++) {
      File target = new File(absolutePath(targets.get(t).getTextContent()));
      File tmpFile = new File(Utils.timestamp());
      BufferedWriter bw = null;
      BufferedReader br = null;
      try {
        Node separatorAttr = targets.get(t).getAttributes().getNamedItem("separator");
        String separator = (separatorAttr == null) ? globalSeparator : separatorAttr.getNodeValue();
        if (separator.length() != 1) {
          separator = globalSeparator;
        }
        bw = new BufferedWriter(new FileWriter(tmpFile));
        if (target.exists()) {
          br = new BufferedReader(new FileReader(target));
          for (String line; (line = br.readLine()) != null; ) {
            String[] parts = line.split(separator);
            if (parts.length < 2) {
              bw.write(line);
              bw.newLine();
              continue;
            }

            String optName = parts[0].trim();
            if (options.containsKey(optName)) {
              String optValue = options.get(optName);
              bw.write(optName + " " + separator + " " + optValue);
              bw.newLine();
              processedOptions.put(optName, optValue);
              options.remove(optName);
            } else if (processedOptions.containsKey(optName)) {
              bw.write(optName + " " + separator + " " + processedOptions.get(optName));
              bw.newLine();
            } else {
              bw.write(line);
              bw.newLine();
            }
          }
          br.close();
        }
        for (Map.Entry<String, String> entry : options.entrySet()) {
          bw.write(entry.getKey() + " " + separator + " " + entry.getValue());
          bw.newLine();
        }
        bw.close();
        FileUtils.copyFile(tmpFile, target);
        FileUtils.forceDelete(tmpFile);
      } catch (IOException ex) {
        Utils.onError(new Error.WriteTxtConfig(target.getPath()));
      }
    }
  }