Java Code Examples for org.openide.xml.XMLUtil#write()

The following examples show how to use org.openide.xml.XMLUtil#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaActions.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Write a script with a new or modified document.
 * @param scriptPath e.g. {@link #FILE_SCRIPT_PATH} or {@link #GENERAL_SCRIPT_PATH}
 */
void writeCustomScript(Document doc, String scriptPath) throws IOException {
    FileObject script = helper.getProjectDirectory().getFileObject(scriptPath);
    if (script == null) {
        script = FileUtil.createData(helper.getProjectDirectory(), scriptPath);
    }
    FileLock lock = script.lock();
    try {
        OutputStream os = script.getOutputStream(lock);
        try {
            XMLUtil.write(doc, os, "UTF-8"); // NOI18N
        } finally {
            os.close();
        }
    } finally {
        lock.releaseLock();
    }
}
 
Example 2
Source File: WebFreeFormActionProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Write a script with a new or modified document.
 * @param script Document written to the script path.
 * @param scriptPath e.g. {@link #FILE_SCRIPT_PATH} or {@link #GENERAL_SCRIPT_PATH}
 */
void writeCustomScript(Document script, String scriptPath) throws IOException {
    FileObject scriptFile = helper.getProjectDirectory().getFileObject(scriptPath);
    if (scriptFile == null) {
        scriptFile = FileUtil.createData(helper.getProjectDirectory(), scriptPath);
    }
    FileLock lock = scriptFile.lock();
    try {
        OutputStream os = scriptFile.getOutputStream(lock);
        try {
            XMLUtil.write(script, os, "UTF-8"); // NOI18N
        } finally {
            os.close();
        }
    } finally {
        lock.releaseLock();
    }
}
 
Example 3
Source File: AssetPackProject.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void saveSettings() {
    FileLock lock = null;
    try {
        FileObject file = getConfigFile();
        lock = file.lock();
        OutputStream out = file.getOutputStream(lock);
        XMLUtil.write(configuration, out, "UTF-8");
        if (out != null) {
            out.close();

        }
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    } finally {
        if (lock != null) {
            lock.releaseLock();
        }
    }
}
 
Example 4
Source File: CreateProjectWizardIterator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            FileUtil.copy(str, baos);
            Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
            doc.getDocumentElement().setAttribute("name", (String) wiz.getProperty("name"));
            doc.getDocumentElement().setAttribute("version", (String) wiz.getProperty("version"));
            doc.getDocumentElement().setAttribute("distributor", (String) wiz.getProperty("distributor"));
//            doc.getDocumentElement().setAttribute("filename", (String) wiz.getProperty("filename"));
            doc.getDocumentElement().setAttribute("name", (String) wiz.getProperty("name"));
            XMLUtil.findElement(doc.getDocumentElement(), "description", null).setTextContent((String) wiz.getProperty("description"));
            XMLUtil.findElement(doc.getDocumentElement(), "license", null).setTextContent((String) wiz.getProperty("license"));
            OutputStream out = fo.getOutputStream();
            try {
                XMLUtil.write(doc, out, "UTF-8");
            } finally {
                out.close();
            }
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
            writeFile(str, fo);
        }

    }
 
Example 5
Source File: PHPSamplesWizardIterator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileUtil.copy(str, baos);
        Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
        NodeList nl = doc.getDocumentElement().getElementsByTagName("name");
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                Element el = (Element) nl.item(i);
                if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) {
                    NodeList nl2 = el.getChildNodes();
                    if (nl2.getLength() > 0) {
                        nl2.item(0).setNodeValue(name);
                    }
                    break;
                }
            }
        }
        OutputStream out = fo.getOutputStream();
        try {
            XMLUtil.write(doc, out, "UTF-8");
        } finally {
            out.close();
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        writeFile(str, fo);
    }

}
 
Example 6
Source File: Populate.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void create(File root, int val, final BoundedRangeModel progress) throws IOException {
    for (int i = 0; i < val; i++) {
        final int progval = i;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                progress.setValue(progval);
            }
        });
        boolean xml = i % 5 > 2;
        String fname = "file" + i + (xml ? ".xml" : ".txt");
        int bit = 0;
        int x = i;
        while (x > 0) {
            if (x % 3 == 0) {
                fname = "dir" + bit + File.separatorChar + fname;
            }
            bit++;
            x /= 3;
        }
        File tomake = new File(root, "test" + File.separatorChar + fname);
        tomake.getParentFile().mkdirs();
        if (tomake.createNewFile()) {
            OutputStream os = new FileOutputStream(tomake);
            try {
                if (xml) {
                    Document doc = createXML(i);
                    XMLUtil.write(doc, os, "UTF-8");
                } else {
                    PrintStream ps = new PrintStream(os);
                    ps.println("Sample data for file #" + i);
                    ps.close();
                }
            } finally {
                os.close();
            }
        }
    }
}
 
Example 7
Source File: HudsonSubversionSCMTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testSVNDir() throws Exception {
    HudsonSCM scm = new HudsonSubversionSCM();
    File dir = getWorkDir();
    File dotSvn = new File(dir, ".svn");
    dotSvn.mkdir();
    OutputStream os = new FileOutputStream(new File(dotSvn, "entries"));
    InputStream is = HudsonSubversionSCMTest.class.getResourceAsStream("sample-entries-file");
    int c;
    while ((c = is.read()) != -1) {
        os.write(c);
    }
    is.close();
    os.close();
    HudsonSCM.Configuration cfg = scm.forFolder(dir);
    assertNotNull(cfg);
    Document doc = XMLUtil.createDocument("root", null, null, null);
    cfg.configure(doc);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLUtil.write(doc, baos, "UTF-8");
    assertEquals("<?xml version='1.0' encoding='UTF-8'?>" +
            "<root>" +
            "<scm class='hudson.scm.SubversionSCM'>" +
            "<locations>" +
            "<hudson.scm.SubversionSCM_-ModuleLocation>" +
            "<remote>https://sezpoz.dev.java.net/svn/sezpoz/trunk</remote>" +
            "<local>.</local>" +
            "</hudson.scm.SubversionSCM_-ModuleLocation>" +
            "</locations>" +
            "<useUpdate>false</useUpdate>" +
            "</scm>" +
            "<triggers>" +
            "<hudson.triggers.SCMTrigger>" +
            "<spec>@hourly</spec>" +
            "</hudson.triggers.SCMTrigger>" +
            "</triggers>" +
            "</root>",
            baos.toString("UTF-8").replace('"', '\'').replaceAll("\n *", "").replaceAll("\r|\n", ""));
}
 
Example 8
Source File: J2SESampleProjectGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Save an XML config file to a named path.
 * If the file does not yet exist, it is created.
 */
private static void saveXml(Document doc, FileObject dir, String path) throws IOException {
    FileObject xml = FileUtil.createData(dir, path);
    FileLock lock = xml.lock();
    try {
        OutputStream os = xml.getOutputStream(lock);
        try {
            XMLUtil.write(doc, os, "UTF-8"); // NOI18N
        } finally {
            os.close();
        }
    } finally {
        lock.releaseLock();
    }
}
 
Example 9
Source File: AndroidProjectTemplateWizardIterator.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileUtil.copy(str, baos);
        Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
        NodeList nl = doc.getDocumentElement().getElementsByTagName("name");
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                Element el = (Element) nl.item(i);
                if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) {
                    NodeList nl2 = el.getChildNodes();
                    if (nl2.getLength() > 0) {
                        nl2.item(0).setNodeValue(name);
                    }
                    break;
                }
            }
        }
        try (OutputStream out = fo.getOutputStream()) {
            XMLUtil.write(doc, out, "UTF-8");
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        writeFile(str, fo);
    }

}
 
Example 10
Source File: pluggableViewDemoSuiteWizardIterator.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileUtil.copy(str, baos);
        Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
        NodeList nl = doc.getDocumentElement().getElementsByTagName("name");
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                Element el = (Element) nl.item(i);
                if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) {
                    NodeList nl2 = el.getChildNodes();
                    if (nl2.getLength() > 0) {
                        nl2.item(0).setNodeValue(name);
                    }
                    break;
                }
            }
        }
        OutputStream out = fo.getOutputStream();
        try {
            XMLUtil.write(doc, out, "UTF-8");
        } finally {
            out.close();
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        writeFile(str, fo);
    }

}
 
Example 11
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void writeXMLDocumentToFile (Document doc, File dest) {
    doc.getDocumentElement ().normalize ();

    dest.getParentFile ().mkdirs ();
    InputStream is = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream ();
    OutputStream fos = null;
    try {
        try {
            XMLUtil.write (doc, bos, "UTF-8"); // NOI18N
            bos.close ();
            fos = new FileOutputStream (dest);
            is = new ByteArrayInputStream (bos.toByteArray ());
            FileUtil.copy (is, fos);
        } finally {
            if (is != null) {
                is.close ();
            }
            if (fos != null) {
                fos.close ();
            }
            bos.close ();
        }
    } catch (java.io.FileNotFoundException fnfe) {
        Exceptions.printStackTrace (fnfe);
    } catch (java.io.IOException ioe) {
        Exceptions.printStackTrace (ioe);
    } finally {
        try {
            bos.close ();
        } catch (IOException x) {
            Exceptions.printStackTrace (x);
        }
    }
}
 
Example 12
Source File: InitializrProjectWizardIterator.java    From nb-springboot with Apache License 2.0 5 votes vote down vote up
private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileUtil.copy(str, baos);
        Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
        NodeList nl = doc.getDocumentElement().getElementsByTagName("name");
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                Element el = (Element) nl.item(i);
                if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) {
                    NodeList nl2 = el.getChildNodes();
                    if (nl2.getLength() > 0) {
                        nl2.item(0).setNodeValue(name);
                    }
                    break;
                }
            }
        }
        try (OutputStream out = fo.getOutputStream()) {
            XMLUtil.write(doc, out, "UTF-8");
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        writeFile(str, fo);
    }

}
 
Example 13
Source File: ImportWorldForgeAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void replaceMeshMatName(FileObject file) {
    InputStream stream = null;
    try {
        stream = file.getInputStream();
        Document doc = XMLUtil.parse(new InputSource(stream), false, false, null, null);
        stream.close();
        Element elem = doc.getDocumentElement();
        if (elem == null) {
            throw new IllegalStateException("Cannot find root mesh element");
        }
        Element submeshes = XmlHelper.findChildElement(elem, "submeshes");
        if (submeshes == null) {
            throw new IllegalStateException("Cannot find submeshes element");
        }
        Element submesh = XmlHelper.findChildElement(submeshes, "submesh");
        boolean changed = false;
        while (submesh != null) {
            String matName = submesh.getAttribute("material");
            String newName = removePastUnderScore(matName);
            if (!matName.equals(newName)) {
                Logger.getLogger(ImportWorldForgeAction.class.getName()).log(Level.INFO, "Change material name for {0}", file);
                submesh.setAttribute("material", newName);
                submesh = XmlHelper.findNextSiblingElement(submesh);
                changed = true;
            }
        }
        if (changed) {
            OutputStream out = file.getOutputStream();
            XMLUtil.write(doc, out, "UTF-8");
            out.close();
        }

    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    } finally {
    }
}
 
Example 14
Source File: ProjectXMLManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void safelyWrite(FileObject projectXml, Document prjDoc) throws IOException {
    OutputStream os = projectXml.getOutputStream();
    try {
        XMLUtil.write(prjDoc, os, "UTF-8"); // NOI18N
    } finally {
        os.close();
    }
}
 
Example 15
Source File: datasourceWizardIterator.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileUtil.copy(str, baos);
        Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
        NodeList nl = doc.getDocumentElement().getElementsByTagName("name");
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                Element el = (Element) nl.item(i);
                if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) {
                    NodeList nl2 = el.getChildNodes();
                    if (nl2.getLength() > 0) {
                        nl2.item(0).setNodeValue(name);
                    }
                    break;
                }
            }
        }
        OutputStream out = fo.getOutputStream();
        try {
            XMLUtil.write(doc, out, "UTF-8");
        } finally {
            out.close();
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        writeFile(str, fo);
    }

}
 
Example 16
Source File: GroovyJavaDemoWizardIterator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileUtil.copy(str, baos);
        Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
        NodeList nl = doc.getDocumentElement().getElementsByTagName("name");
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                Element el = (Element) nl.item(i);
                if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) {
                    NodeList nl2 = el.getChildNodes();
                    if (nl2.getLength() > 0) {
                        nl2.item(0).setNodeValue(name);
                    }
                    break;
                }
            }
        }
        OutputStream out = fo.getOutputStream();
        try {
            XMLUtil.write(doc, out, "UTF-8");
        } finally {
            out.close();
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        writeFile(str, fo);
    }

}
 
Example 17
Source File: NBProjectGeneratorsWizardIterator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileUtil.copy(str, baos);
        Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
        NodeList nl = doc.getDocumentElement().getElementsByTagName("name");
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                Element el = (Element) nl.item(i);
                if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) {
                    NodeList nl2 = el.getChildNodes();
                    if (nl2.getLength() > 0) {
                        nl2.item(0).setNodeValue(name);
                    }
                    break;
                }
            }
        }
        OutputStream out = fo.getOutputStream();
        try {
            XMLUtil.write(doc, out, "UTF-8");
        } finally {
            out.close();
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        writeFile(str, fo);
    }

}
 
Example 18
Source File: JdkConfiguration.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void writeXML(Document doc, String path) throws IOException {
    FileObject fo = FileUtil.createData(project.getProjectDirectory(), path);
    FileLock lock = fo.lock();
    try {
        OutputStream os = fo.getOutputStream(lock);
        try {
            XMLUtil.write(doc, os, "UTF-8"); // NOI18N
        } finally {
            os.close();
        }
    } finally {
        lock.releaseLock();
    }
}
 
Example 19
Source File: LayerBuilderTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private String dump() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLUtil.write(doc, baos, "UTF-8");
    return clean(baos.toString("UTF-8"));
}
 
Example 20
Source File: TomcatUsers.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an user with the specified username and password and gives him
 * the "manager" and the "admin" roles, if the user already exists the two
 * roles are added to him and the password is changed to the new one.
 * 
 * @param tomcatUsersFile tomcat-users.xml file.
 * @param username username.
 * @param password password.
 * 
 * @throws IOException if the file does not exist or an error occurs during 
 *         parsing it.
 */
public static void createUser(File tomcatUsersFile, String username, String password,
        TomcatVersion version) throws IOException {
    Document doc = getDocument(tomcatUsersFile);
    Element root = doc.getDocumentElement();
    Element userElement = findUserByName(root, username);
    if (userElement == null) {
        userElement = doc.createElement("user"); // NOI18N
        userElement.setAttribute("username", username); // NOI18N
    }
    // add roles
    String roles = userElement.getAttribute("roles"); // NOI18N
    if (roles == null) {
        roles = ""; // NOI18N
    }
    StringBuilder newRoles = new StringBuilder(roles.trim());
    if (TomcatVersion.TOMCAT_70.equals(version)
            || TomcatVersion.TOMCAT_80.equals(version)
            || TomcatVersion.TOMCAT_90.equals(version)) {
        if (!hasRole(roles, "manager-script")) { // NOI18N
            if (newRoles.length() > 0 && !newRoles.toString().endsWith(",")) { // NOI18N
                newRoles.append(',');
            }
            newRoles.append("manager-script"); // NOI18N
        }
    } else {
        if (!hasRole(roles, "manager")) { // NOI18N
            if (newRoles.length() > 0 && !newRoles.toString().endsWith(",")) { // NOI18N
                newRoles.append(',');
            }
            newRoles.append("manager"); // NOI18N
        }
    }
    if (!hasRole(roles, "admin")) { // NOI18N
        if (!newRoles.toString().endsWith(",")) { // NOI18N
            newRoles.append(',');
        }
        newRoles.append("admin"); // NOI18N
    }
    userElement.setAttribute("roles", newRoles.toString()); // NOI18N
    userElement.setAttribute("password", password); // NOI18N
    root.appendChild(userElement);
    FileObject fo = FileUtil.toFileObject(tomcatUsersFile);
    if (fo == null) {
        throw new IOException(NbBundle.getMessage(TomcatUsers.class, "MSG_FileNotFound", tomcatUsersFile.getPath()));
    }
    OutputStream os = fo.getOutputStream();
    try {
        XMLUtil.write(doc, os, "UTF-8"); // NOI18N
    } finally {
        os.close();
    }
}