Example #1
0
 public long getLongContent(long defaultValue) {
   String c = node.getTextContent();
   if (c != null) {
     try {
       return Long.parseLong(c);
     } catch (NumberFormatException nfe) {
     }
   }
   return defaultValue;
 }
Example #2
0
 public double getDoubleContent(double defaultValue) {
   String c = node.getTextContent();
   if (c != null) {
     try {
       return Double.parseDouble(c);
     } catch (NumberFormatException nfe) {
     }
   }
   return defaultValue;
 }
Example #3
0
 private void call(Node operation)
     throws ParserConfigurationException, TransformerConfigurationException {
   String opName = operation.getNodeName();
   if (opName.equals("call")) {
     String script =
         Utils.combine(Utils.getParentDir(this.currentScript), operation.getTextContent());
     call(script);
   } else if (opName.equals("apply")) processApply(operation);
   else if (opName.equals("xml")) processXml(operation);
   else if (opName.equals("txt")) processTxt(operation);
   else if (opName.equals("copy")) processCopy(operation);
   else if (opName.equals("delete")) processDelete(operation);
 }
Example #4
0
 private void processDelete(Node operation) {
   String path = absolutePath(operation.getTextContent());
   File target = new File(path);
   if (!target.exists()) {
     Utils.onError(new Error.FileNotFound(target.getPath()));
     return;
   }
   try {
     if (target.isDirectory()) {
       FileUtils.deleteDirectory(target);
     } else {
       FileUtils.forceDelete(target);
     }
   } catch (IOException ex) {
     Utils.onError(new Error.FileDelete(target.getPath()));
   }
 }
Example #5
0
 /** @param defaultValue the default value of the attribute */
 public float getFloatContent(float defaultValue) {
   return PApplet.parseFloat(node.getTextContent(), defaultValue);
 }
Example #6
0
 /** @param defaultValue the default value of the attribute */
 public int getIntContent(int defaultValue) {
   return PApplet.parseInt(node.getTextContent(), defaultValue);
 }
Example #7
0
 public String getContent(String defaultValue) {
   String s = node.getTextContent();
   return (s != null) ? s : defaultValue;
 }
Example #8
0
 /**
  * Return the #PCDATA content of the element. If the element has a combination of #PCDATA content
  * and child elements, the #PCDATA sections can be retrieved as unnamed child objects. In this
  * case, this method returns null.
  *
  * @webref xml:method
  * @brief Gets the content of an element
  * @return the content.
  * @see XML#getIntContent()
  * @see XML#getFloatContent()
  */
 public String getContent() {
   return node.getTextContent();
 }
Example #9
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()));
      }
    }
  }