Search in sources :

Example 1 with Document

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

the class JcrNode method pasteFromClipboard.

/**
     * Paste from the clipboard to this (container) node.
     * <p>
     * Copyright Note: The code of this method was ported from eclipse'
     * PasteAction, which due to visibility restrictions was not reusable.
     * <p>
     * @param clipboard
     */
public void pasteFromClipboard(Clipboard clipboard) {
    if (!canBePastedTo(clipboard)) {
        // should not occur due to 'canBePastedTo' check done by 
        // corresponding action - checking here nevertheless
        MessageDialog.openInformation(null, "Cannot paste", "No applicable node (type) for pasting found.");
        return;
    }
    Repository repository = ServerUtil.getDefaultRepository(getProject());
    NodeTypeRegistry ntManager = (repository == null) ? null : repository.getNodeTypeRegistry();
    if (ntManager == null) {
        MessageDialog.openWarning(null, "Cannot paste", "Cannot paste if corresponding server is not started");
        return;
    }
    // try the resource transfer
    IResource[] resourceData = (IResource[]) clipboard.getContents(ResourceTransfer.getInstance());
    if (resourceData != null && resourceData.length > 0) {
        if (resourceData[0].getType() == IResource.PROJECT) {
            // do not support project pasting onto a jcr node
            MessageDialog.openInformation(null, "Cannot paste project(s)", "Pasting of a project onto a (JCR) node is not possible");
            return;
        } else {
            CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(null);
            operation.copyResources(resourceData, getDropContainer());
        }
        return;
    }
    // try the file transfer
    String[] fileData = (String[]) clipboard.getContents(FileTransfer.getInstance());
    if (fileData != null) {
        CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(null);
        operation.copyFiles(fileData, getDropContainer());
        return;
    }
    // then try the text transfer
    String text = (String) clipboard.getContents(TextTransfer.getInstance());
    if ((text != null) && (this.domElement != null)) {
        try {
            Document document = TolerantXMLParser.parse(text, "pasted from clipboard");
            this.domElement.addNode(document.getRootElement());
            this.underlying.save();
        } catch (IOException e) {
            MessageDialog.openError(null, "Could not paste from clipboard", "Exception encountered while pasting from clipboard: " + e);
            Activator.getDefault().getPluginLogger().error("Error pasting from clipboard: " + e, e);
        }
    }
}
Also used : Repository(org.apache.sling.ide.transport.Repository) NodeTypeRegistry(org.apache.sling.ide.transport.NodeTypeRegistry) IOException(java.io.IOException) Document(de.pdark.decentxml.Document) CopyFilesAndFoldersOperation(org.eclipse.ui.actions.CopyFilesAndFoldersOperation) IResource(org.eclipse.core.resources.IResource)

Example 2 with Document

use of de.pdark.decentxml.Document in project fabric8 by jboss-fuse.

the class RouteXml method marshalToDoc.

/**
 * Marshals the model to XML and updates the model's doc property to contain the
 * new marshalled model
 *
 * @param model
 */
public void marshalToDoc(XmlModel model) throws JAXBException {
    Marshaller marshaller = jaxbContext().createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, java.lang.Boolean.TRUE);
    try {
        marshaller.setProperty("com.sun.xml.bind.indentString", "  ");
    } catch (Exception e) {
        LOG.debug("Property not supported: {}", e);
    }
    Object value = model.marshalRootElement();
    Document doc = model.getDoc();
    Element docElem = doc.getRootElement();
    // JAXB only seems to do nice whitespace/namespace stuff when writing to stream
    // rather than DOM directly
    // marshaller.marshal(value, docElem);
    StringWriter buffer = new StringWriter();
    marshaller.marshal(value, buffer);
    // now lets parse the XML and insert the root element into the doc
    String xml = buffer.toString();
    if (!model.getNs().equals(springNS)) {
        // !!!
        xml = xml.replaceAll(springNS, model.getNs());
    }
    Document camelDoc = parse(new XMLStringSource(xml));
    Node camelElem = camelDoc.getRootElement();
    if (model.isRoutesContext() && camelDoc.getRootElement().getName().equals("camelContext")) {
        camelDoc.getRootElement().setName("routeContext");
    }
    if (model.isJustRoutes()) {
        replaceChild(doc, camelElem, docElem);
    } else {
        if (model.getNode() != null) {
            replaceCamelElement(docElem, camelElem, model.getNode());
        } else {
            docElem.addNode(camelElem);
        }
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) XMLStringSource(de.pdark.decentxml.XMLStringSource) StringWriter(java.io.StringWriter) Element(de.pdark.decentxml.Element) Node(de.pdark.decentxml.Node) Document(de.pdark.decentxml.Document) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 3 with Document

use of de.pdark.decentxml.Document in project fabric8 by jboss-fuse.

the class RouteXml method createExemplarDoc.

protected Document createExemplarDoc() throws IOException {
    String exemplar = "io/fabric8/camel/tooling/exemplar.xml";
    URL url = findResource(exemplar, null);
    if (url != null) {
        return parse(new XMLIOSource(url));
    } else {
        LOG.warn("Could not find file {} on the class path", exemplar);
        Document d = new Document();
        d.addNode(new Element("beans", springNamespace));
        return d;
    }
}
Also used : XMLIOSource(de.pdark.decentxml.XMLIOSource) Element(de.pdark.decentxml.Element) Document(de.pdark.decentxml.Document) URL(java.net.URL)

Example 4 with Document

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

the class ProductConfiguration method write.

public static void write(ProductConfiguration product, File file) throws IOException {
    OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
    Document document = product.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 5 with Document

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

the class UpdateSite method write.

public static void write(UpdateSite site, File file) throws IOException {
    OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
    Document document = site.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)

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