npm.modules.java Source code

Java tutorial

Introduction

Here is the source code for npm.modules.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package npm;

import java.awt.Desktop;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import java.util.Scanner;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Evan
 */
public class modules {

    public boolean showmessage(JFrame frame, String msg, String msgtype) {
        switch (msgtype) {
        case "":
            JOptionPane.showMessageDialog(frame, msg);
            break;
        case "error":
            JOptionPane.showMessageDialog(frame, msg, "Error", JOptionPane.ERROR_MESSAGE);
            break;
        default:
            JOptionPane.showMessageDialog(frame, msg);
            break;
        }
        return true;
    }

    public boolean openafile(String path) throws Exception {
        File file = new File(path);
        Desktop desktop = Desktop.getDesktop();
        desktop.edit(file);
        return true;
    }

    public void addsite(Document doc, String sitename) throws Exception {
        Element site = doc.createElement("site");
        Element name = doc.createElement("name");
        name.setTextContent(sitename);
        Element style = doc.createElement("style");
        style.setTextContent("../sites/styles/" + sitename + "/");
        Element script = doc.createElement("script");
        script.setTextContent("../sites/scripts/" + sitename + "/");
        Node sitesnode = doc.getElementsByTagName("sites").item(0);
        Element sites = (Element) sitesnode;
        site.appendChild(name);
        site.appendChild(style);
        site.appendChild(script);
        sites.appendChild(site);
        savexml(doc);
    }

    public void deletesite(Document doc, int index) throws Exception {
        Node nNode = doc.getElementsByTagName("site").item(index);
        nNode.getParentNode().removeChild(nNode);
        savexml(doc);
    }

    public void savexml(Document doc) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("config.xml"));
        transformer.transform(source, result);
    }

    public Document xmlobject(String path) throws Exception {
        File inputfile = new File(path);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder xmlbuilder = factory.newDocumentBuilder();
        Document doc = xmlbuilder.parse(inputfile);
        doc.getDocumentElement().normalize();
        return doc;
    }

    public String getnodevalue(Document doc, String nodename) throws Exception {
        return doc.getElementsByTagName(nodename).item(0).getTextContent();
    }

    public NodeList getnodevaluelist(Document doc, String nodename, String subnodename) {
        Node nNode = doc.getElementsByTagName(nodename).item(0);
        Element eElement = (Element) nNode;
        return eElement.getElementsByTagName(subnodename);
    }

    public String getnodevaluebyindex(Document doc, String nodename, String[] subnodenames, int index) {
        String results = "";
        Node nNode = doc.getElementsByTagName(nodename).item(index);
        Element eElement = (Element) nNode;
        for (int i = 0; i < subnodenames.length; i++) {
            results = results + "var " + subnodenames[i] + "='"
                    + eElement.getElementsByTagName(subnodenames[i]).item(0).getTextContent() + "';\n";
        }
        return results;
    }

    public void excutecmdwindow(String[] command, String workfolder) throws Exception {
        ProcessBuilder builder = new ProcessBuilder(command);
        builder.directory(new File(workfolder).getAbsoluteFile());
        builder.redirectErrorStream(true);
        builder.start();
    }

    public void excutecmdwindow(String[] command) throws Exception {
        ProcessBuilder builder = new ProcessBuilder(command);
        builder.redirectErrorStream(true);
        builder.start();
    }

    public boolean createfolder(String name) {
        File file = new File(name);
        if (!file.exists()) {
            return file.mkdir();
        }
        return false;
    }

    public void copyfolder(String from, String to) throws Exception {
        File srcDir = new File(from);
        File destDir = new File(to);
        FileUtils.copyDirectory(srcDir, destDir);
    }

    public void deletefolder(String name) throws Exception {
        FileUtils.deleteDirectory(new File(name));
    }

    public boolean appendfront(String inputfile, String content, String outputfile) throws Exception {
        String results = new Scanner(new File(inputfile)).useDelimiter("\\Z").next();
        results = content + results;
        //System.out.println(results);
        File gulpfile = new File(outputfile);
        FileWriter fw = new FileWriter(gulpfile.getAbsoluteFile());
        try (BufferedWriter bw = new BufferedWriter(fw)) {
            bw.write(results);
        }
        return true;
    }

    public boolean listtocombox(Document doc, JComboBox boxname, String parentnodename, String nodename) {
        boxname.removeAllItems();
        NodeList nlist = doc.getElementsByTagName(parentnodename);
        for (int temp = 0; temp < nlist.getLength(); temp++) {
            //System.out.println(nodename +"---"+temp);
            Node nNode = nlist.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                boxname.addItem(eElement.getElementsByTagName(nodename).item(0).getTextContent());
            }
        }
        return true;
    }

}