Search in sources :

Example 1 with DOM4JSettingsNode

use of org.gradle.gradleplugin.foundation.settings.DOM4JSettingsNode in project gradle by gradle.

the class DOM4JSettingsNodeTest method testAddChild.

/**
     * This tests that addChild actually works. We'll verify a child isn't present, then add one.
     */
public void testAddChild() {
    //make sure we have no child named 'fred' at the DOM4J level
    assertNull(rootElement.element(SAMPLE_NAME_1));
    //as such, we shouldn't have one at the DOM4JSettingsNode level either.
    assertNull(rootNode.getChildNode(SAMPLE_NAME_1));
    SettingsNode settingsNode = rootNode.addChild(SAMPLE_NAME_1);
    assertNotNull(settingsNode);
    //and now it should be present under both.
    assertNotNull(Dom4JUtility.getChild(rootElement, DOM4JSettingsNode.TAG_NAME, DOM4JSettingsNode.NAME_ATTRIBUTE, SAMPLE_NAME_1));
    assertNotNull(rootNode.getChildNode(SAMPLE_NAME_1));
}
Also used : SettingsNode(org.gradle.gradleplugin.foundation.settings.SettingsNode) DOM4JSettingsNode(org.gradle.gradleplugin.foundation.settings.DOM4JSettingsNode)

Example 2 with DOM4JSettingsNode

use of org.gradle.gradleplugin.foundation.settings.DOM4JSettingsNode in project gradle by gradle.

the class DOM4JSettingsNodeTest method testAddingChildrenIfNotPresent.

/**
     * This tests that if you call addChildIfNotPresent, it actually adds a child that is not present. We'll verify a
     * child isn't present, then add one.
     */
public void testAddingChildrenIfNotPresent() {
    //make sure we have no child named 'fred' at the DOM4J level
    assertNull(rootElement.element(SAMPLE_NAME_1));
    //as such, we shouldn't have one at the DOM4JSettingsNode level either.
    assertNull(rootNode.getChildNode(SAMPLE_NAME_1));
    SettingsNode settingsNode = rootNode.addChildIfNotPresent(SAMPLE_NAME_1);
    assertNotNull(settingsNode);
    //and now it should be present under both.
    assertNotNull(Dom4JUtility.getChild(rootElement, DOM4JSettingsNode.TAG_NAME, DOM4JSettingsNode.NAME_ATTRIBUTE, SAMPLE_NAME_1));
    assertNotNull(rootNode.getChildNode(SAMPLE_NAME_1));
}
Also used : SettingsNode(org.gradle.gradleplugin.foundation.settings.SettingsNode) DOM4JSettingsNode(org.gradle.gradleplugin.foundation.settings.DOM4JSettingsNode)

Example 3 with DOM4JSettingsNode

use of org.gradle.gradleplugin.foundation.settings.DOM4JSettingsNode in project gradle by gradle.

the class DOM4JSerializer method readSettingsFile.

public static DOM4JSettingsNode readSettingsFile(ImportInteraction importInteraction, FileFilter fileFilter) {
    File file = importInteraction.promptForFile(fileFilter);
    if (file == null) {
        return null;
    }
    if (!file.exists()) {
        importInteraction.reportError("File does not exist: " + file.getAbsolutePath());
        //we should really sit in a loop until they cancel or give us a valid file.
        return null;
    }
    try {
        SAXReader reader = new SAXReader();
        Document document = reader.read(file);
        return new DOM4JSettingsNode(document.getRootElement());
    } catch (Throwable t) {
        LOGGER.error("Unable to read file: " + file.getAbsolutePath(), t);
        importInteraction.reportError("Unable to read file: " + file.getAbsolutePath());
        return null;
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Document(org.dom4j.Document) DOM4JSettingsNode(org.gradle.gradleplugin.foundation.settings.DOM4JSettingsNode)

Example 4 with DOM4JSettingsNode

use of org.gradle.gradleplugin.foundation.settings.DOM4JSettingsNode in project gradle by gradle.

the class DOM4JSerializer method createBlankSettings.

public static DOM4JSettingsNode createBlankSettings() {
    Document document = DocumentHelper.createDocument();
    Element rootElement = document.addElement("root");
    DOM4JSettingsNode settings = new DOM4JSettingsNode(rootElement);
    return settings;
}
Also used : Element(org.dom4j.Element) Document(org.dom4j.Document) DOM4JSettingsNode(org.gradle.gradleplugin.foundation.settings.DOM4JSettingsNode)

Example 5 with DOM4JSettingsNode

use of org.gradle.gradleplugin.foundation.settings.DOM4JSettingsNode in project gradle by gradle.

the class DOM4JSerializer method exportToFile.

/**
     * Call this to save the JDOMSerializable to a file. This handles confirming overwriting an existing file as well as ensuring the extension is correct based on the passed in fileFilter.
     */
public static void exportToFile(String rootElementTag, ExportInteraction exportInteraction, ExtensionFileFilter fileFilter, SettingsSerializable... serializables) {
    File file = promptForFile(exportInteraction, fileFilter);
    if (file == null) {
        //the user canceled.
        return;
    }
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        LOGGER.error("Could not write to file: " + file.getAbsolutePath(), e);
        exportInteraction.reportError("Could not write to file: " + file.getAbsolutePath());
        return;
    }
    try {
        XMLWriter xmlWriter = new XMLWriter(fileOutputStream, OutputFormat.createPrettyPrint());
        Document document = DocumentHelper.createDocument();
        Element rootElement = document.addElement(rootElementTag);
        DOM4JSettingsNode settingsNode = new DOM4JSettingsNode(rootElement);
        for (int index = 0; index < serializables.length; index++) {
            SettingsSerializable serializable = serializables[index];
            try {
                //don't let a single serializer stop the entire thing from being written in.
                serializable.serializeOut(settingsNode);
            } catch (Exception e) {
                LOGGER.error("serializing", e);
            }
        }
        xmlWriter.write(document);
    } catch (Throwable t) {
        LOGGER.error("Failed to save", t);
        exportInteraction.reportError("Internal error. Failed to save.");
    } finally {
        closeQuietly(fileOutputStream);
    }
}
Also used : Element(org.dom4j.Element) SettingsSerializable(org.gradle.gradleplugin.foundation.settings.SettingsSerializable) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) DOM4JSettingsNode(org.gradle.gradleplugin.foundation.settings.DOM4JSettingsNode)

Aggregations

DOM4JSettingsNode (org.gradle.gradleplugin.foundation.settings.DOM4JSettingsNode)6 Document (org.dom4j.Document)4 Element (org.dom4j.Element)2 SettingsNode (org.gradle.gradleplugin.foundation.settings.SettingsNode)2 SAXReader (org.dom4j.io.SAXReader)1 XMLWriter (org.dom4j.io.XMLWriter)1 SettingsSerializable (org.gradle.gradleplugin.foundation.settings.SettingsSerializable)1