Search in sources :

Example 11 with Document

use of de.pdark.decentxml.Document in project sling by apache.

the class JcrNode method createVaultFileWithContent.

private void createVaultFileWithContent(IFolder parent, String filename, String childNodeType, Element content) {
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<jcr:root \n    xmlns:sling=\"http://sling.apache.org/jcr/sling/1.0\"\n    xmlns:jcr=\"http://www.jcp.org/jcr/1.0\"\n    jcr:primaryType=\"" + childNodeType + "\"/>";
    final IFile file = parent.getFile(filename);
    try {
        if (content != null) {
            Document document = TolerantXMLParser.parse(xml, file.getFullPath().toOSString());
            // add the attributes of content
            List<Attribute> attributes = content.getAttributes();
            for (Attribute attribute : attributes) {
                if (attribute.getName().equals("jcr:primaryType")) {
                    // skip this
                    continue;
                }
                document.getRootElement().addAttribute(attribute);
            }
            // then copy all the children
            document.getRootElement().addNodes(content.getChildren());
            // then save the document
            xml = document.toXML();
        }
        if (file.exists()) {
            file.setContents(new ByteArrayInputStream(xml.getBytes()), true, true, new NullProgressMonitor());
        } else {
            file.create(new ByteArrayInputStream(xml.getBytes()), true, new NullProgressMonitor());
        }
    } catch (Exception e) {
        //TODO proper logging
        e.printStackTrace();
        MessageDialog.openInformation(Display.getDefault().getActiveShell(), "Cannot create JCR node on a File", "Following Exception encountered: " + e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) Attribute(de.pdark.decentxml.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) Document(de.pdark.decentxml.Document) CoreException(org.eclipse.core.runtime.CoreException) XMLParseException(de.pdark.decentxml.XMLParseException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) RepositoryException(org.apache.sling.ide.transport.RepositoryException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 12 with Document

use of de.pdark.decentxml.Document in project tycho by eclipse.

the class EclipseModelTest method testDefaultXmlEncoding.

public void testDefaultXmlEncoding() throws Exception {
    // Run the test with -Dfile.encoding=Cp1252 to be sure
    Feature feature = Feature.read(new File("src/test/resources/modelio/feature-default-encoding.xml"));
    Feature.write(feature, new File("target/feature-default-encoding.xml"));
    Document document = XMLParser.parse(new File("target/feature-default-encoding.xml"));
    Element child = document.getChild("/feature/license");
    assertEquals("\u201cI AGREE\u201d", child.getText().trim());
}
Also used : Element(de.pdark.decentxml.Element) Document(de.pdark.decentxml.Document) Feature(org.eclipse.tycho.model.Feature) File(java.io.File)

Example 13 with Document

use of de.pdark.decentxml.Document in project tycho by eclipse.

the class Category method write.

public static void write(Category category, File file) throws IOException {
    OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
    Document document = category.document;
    try {
        String enc = document.getEncoding() != null ? document.getEncoding() : "UTF-8";
        Writer w = new OutputStreamWriter(os, enc);
        XMLWriter xw = new XMLWriter(w);
        try {
            document.toXML(xw);
        } finally {
            xw.flush();
        }
    } finally {
        IOUtil.close(os);
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Document(de.pdark.decentxml.Document) BufferedOutputStream(java.io.BufferedOutputStream) XMLWriter(de.pdark.decentxml.XMLWriter) Writer(java.io.Writer) XMLWriter(de.pdark.decentxml.XMLWriter) OutputStreamWriter(java.io.OutputStreamWriter)

Example 14 with Document

use of de.pdark.decentxml.Document in project tycho by eclipse.

the class IU method read.

public static IU read(File file) throws IOException {
    FileInputStream is = new FileInputStream(file);
    try {
        Document iuDocument = parser.parse(new XMLIOSource(is));
        Element root = iuDocument.getChild(UNIT);
        if (root == null)
            throw new RuntimeException("No iu found.");
        IU result = new IU(iuDocument, root);
        if (result.getId() == null)
            throw new RuntimeException(String.format("The IU defined in %s is missing an id.", file.getAbsolutePath()));
        if (result.getVersion() == null)
            throw new RuntimeException(String.format("The IU defined in %s is missing a version.", file.getAbsolutePath()));
        return result;
    } finally {
        IOUtil.close(is);
    }
}
Also used : XMLIOSource(de.pdark.decentxml.XMLIOSource) Element(de.pdark.decentxml.Element) Document(de.pdark.decentxml.Document) FileInputStream(java.io.FileInputStream)

Example 15 with Document

use of de.pdark.decentxml.Document in project tycho by eclipse.

the class Tycho465RootFilesTest method testProductBuild.

@Test
public void testProductBuild() throws Exception {
    Verifier verifier = getVerifier("product.rootFiles", false);
    verifier.getSystemProperties().setProperty("forceContextQualifier", Tycho465RootFilesTest.QUALIFIER.toString());
    verifier.getSystemProperties().setProperty("p2.repo", P2Repositories.ECLIPSE_342.toString());
    verifier.executeGoal("install");
    verifier.verifyErrorFreeLog();
    File targetDir = new File(verifier.getBasedir(), Tycho465RootFilesTest.MODULE + "/target");
    File repositoryTargetDirectory = new File(targetDir, "repository");
    Document contentXml = openMetadataRepositoryDocument(repositoryTargetDirectory);
    assertBuildProductAndRepository(targetDir, repositoryTargetDirectory, contentXml);
    // clean the local build results
    verifier.executeGoal("clean");
    verifier.verifyErrorFreeLog();
    // re-build the repository project only (incl. products) to ensure that the created root file zips were attached
    // to the project and are available from the local repository
    final boolean ignoreLocallyInstalledArtifacts = false;
    Verifier eclipseRepoProjectVerifier = getVerifier("product.rootFiles/eclipse-repository", false, ignoreLocallyInstalledArtifacts);
    eclipseRepoProjectVerifier.getSystemProperties().setProperty("forceContextQualifier", Tycho465RootFilesTest.QUALIFIER.toString());
    eclipseRepoProjectVerifier.getSystemProperties().setProperty("p2.repo", P2Repositories.ECLIPSE_342.toString());
    eclipseRepoProjectVerifier.executeGoal("verify");
    eclipseRepoProjectVerifier.verifyErrorFreeLog();
    Document updatedContentXml = openMetadataRepositoryDocument(repositoryTargetDirectory);
    assertBuildProductAndRepository(targetDir, repositoryTargetDirectory, updatedContentXml);
}
Also used : Verifier(org.apache.maven.it.Verifier) Document(de.pdark.decentxml.Document) File(java.io.File) Test(org.junit.Test) AbstractTychoIntegrationTest(org.eclipse.tycho.test.AbstractTychoIntegrationTest)

Aggregations

Document (de.pdark.decentxml.Document)15 Element (de.pdark.decentxml.Element)6 XMLWriter (de.pdark.decentxml.XMLWriter)6 BufferedOutputStream (java.io.BufferedOutputStream)6 FileOutputStream (java.io.FileOutputStream)6 OutputStream (java.io.OutputStream)6 OutputStreamWriter (java.io.OutputStreamWriter)6 Writer (java.io.Writer)6 XMLIOSource (de.pdark.decentxml.XMLIOSource)4 File (java.io.File)4 IOException (java.io.IOException)3 Verifier (org.apache.maven.it.Verifier)3 AbstractTychoIntegrationTest (org.eclipse.tycho.test.AbstractTychoIntegrationTest)3 Test (org.junit.Test)3 XMLParser (de.pdark.decentxml.XMLParser)2 ZipFile (java.util.zip.ZipFile)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Attribute (de.pdark.decentxml.Attribute)1 Node (de.pdark.decentxml.Node)1 XMLParseException (de.pdark.decentxml.XMLParseException)1