Search in sources :

Example 1 with Attribute

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

the class JcrNode method changePrimaryType.

void changePrimaryType(String newPrimaryType) {
    Repository repository = ServerUtil.getDefaultRepository(getProject());
    NodeTypeRegistry ntManager = (repository == null) ? null : repository.getNodeTypeRegistry();
    if (ntManager == null) {
        MessageDialog.openWarning(null, "Unable to change primary type", "Unable to change primary type since project " + getProject().getName() + " is not associated with a server or the server is not started.");
        return;
    }
    try {
        if (!ntManager.isAllowedPrimaryChildNodeType(getParent().getPrimaryType(), newPrimaryType)) {
            if (!MessageDialog.openQuestion(null, "Unable to change primary type", "Parent (type '" + getParent().getPrimaryType() + "')" + " does not accept child with primary type '" + newPrimaryType + "'. Change anyway?")) {
                return;
            }
        }
    } catch (RepositoryException e1) {
        MessageDialog.openWarning(null, "Unable to change primary type", "Exception occured while trying to " + "verify node types: " + e1);
        return;
    }
    String thisNodeType = getPrimaryType();
    final SerializationKind currentSk = getSerializationKind(thisNodeType);
    final SerializationKind newSk = getSerializationKind(newPrimaryType);
    if (currentSk.equals(newSk)) {
        if (newSk != SerializationKind.FOLDER) {
            // easiest - we should just be able to change the type in the .content.xml
            properties.doSetPropertyValue("jcr:primaryType", newPrimaryType);
        } else {
            if (thisNodeType.equals("nt:folder")) {
                // switching away from an nt:folder might require creating a .content.xml
                createVaultFile((IFolder) resource, ".content.xml", newPrimaryType);
            } else if (newPrimaryType.equals("nt:folder")) {
                // 1)
                if (domElement != null) {
                    MessageDialog.openWarning(null, "Unable to change primaryType", "Unable to change jcr:primaryType to nt:folder" + " since the node is contained in a .content.xml");
                    return;
                }
                // verify 2)
                if (!verifyNodeTypeChange(ntManager, newPrimaryType)) {
                    return;
                }
                if (!(resource instanceof IFolder)) {
                    MessageDialog.openWarning(null, "Unable to change primaryType", "Unable to change jcr:primaryType to nt:folder" + " as there is no underlying folder");
                    return;
                }
                IFolder folder = (IFolder) resource;
                // 3) delete the .content.xml
                IFile contentXml = folder.getFile(".content.xml");
                if (contentXml.exists()) {
                    try {
                        contentXml.delete(true, new NullProgressMonitor());
                    } catch (CoreException e) {
                        Logger logger = Activator.getDefault().getPluginLogger();
                        logger.error("Could not delete " + contentXml.getFullPath() + ", e=" + e, e);
                        MessageDialog.openError(null, "Could not delete file", "Could not delete " + contentXml.getFullPath() + ", " + e);
                    }
                }
            } else {
                properties.doSetPropertyValue("jcr:primaryType", newPrimaryType);
            }
        }
        return;
    }
    if (newSk == SerializationKind.FOLDER) {
        // switching to a folder
        if (currentSk == SerializationKind.FILE) {
            MessageDialog.openWarning(null, "Unable to change primary type", "Changing from a file to a folder type is currently not supported");
            return;
        }
        if (newPrimaryType.equals("nt:folder")) {
            // verify
            if (!verifyNodeTypeChange(ntManager, newPrimaryType)) {
                return;
            }
        }
        try {
            // create the new directory structure pointing to 'this'
            IFolder newFolder = getParent().prepareCreateFolderChild(getJcrPathName());
            if (!newPrimaryType.equals("nt:folder")) {
                // move any children from the existing 'this' to a new vault file
                createVaultFileWithContent(newFolder, ".content.xml", newPrimaryType, domElement);
            }
            // remove myself
            if (domElement != null) {
                domElement.remove();
                if (underlying != null) {
                    underlying.save();
                }
            }
            // add a pointer in the corresponding .content.xml to point to this (folder) child
            getParent().createDomChild(getJcrPathName(), null);
            if (newPrimaryType.equals("nt:folder")) {
                // delete the .content.xml
                if (properties != null && properties.getUnderlying() != null) {
                    IFile contentXml = properties.getUnderlying().file;
                    if (contentXml != null && contentXml.exists()) {
                        contentXml.delete(true, new NullProgressMonitor());
                    }
                }
            }
            ServerUtil.triggerIncrementalBuild(newFolder, null);
            return;
        } catch (CoreException e) {
            MessageDialog.openWarning(null, "Unable to change primaryType", "Exception occurred: " + e);
            Logger logger = Activator.getDefault().getPluginLogger();
            logger.error("Exception occurred", e);
            return;
        }
    } else if (newSk == SerializationKind.FILE) {
        MessageDialog.openWarning(null, "Unable to change primary type", "Changing to/from a file is currently not supported");
        return;
    } else {
        // otherwise we're going from a folder to partial-or-full
        if (domElement == null && (resource instanceof IFolder)) {
            createVaultFile((IFolder) resource, ".content.xml", newPrimaryType);
        } else {
            // set the "pointer"'s jcr:primaryType
            if (domElement.getAttributeMap().containsKey("jcr:primaryType")) {
                domElement.setAttribute("jcr:primaryType", newPrimaryType);
            } else {
                domElement.addAttribute("jcr:primaryType", newPrimaryType);
            }
            // then copy all the other attributes - plus children if there are nay
            Element propDomElement = properties.getDomElement();
            if (propDomElement != null) {
                List<Attribute> attributes = propDomElement.getAttributes();
                for (Iterator<Attribute> it = attributes.iterator(); it.hasNext(); ) {
                    Attribute anAttribute = it.next();
                    if (anAttribute.getName().startsWith("xmlns:")) {
                        continue;
                    }
                    if (anAttribute.getName().equals("jcr:primaryType")) {
                        continue;
                    }
                    if (domElement.getAttributeMap().containsKey(anAttribute.getName())) {
                        domElement.setAttribute(anAttribute.getName(), anAttribute.getValue());
                    } else {
                        domElement.addAttribute(anAttribute);
                    }
                }
                List<Element> c2 = propDomElement.getChildren();
                if (c2 != null && c2.size() != 0) {
                    domElement.addNodes(c2);
                }
            }
            if (properties.getUnderlying() != null && properties.getUnderlying().file != null) {
                try {
                    properties.getUnderlying().file.delete(true, new NullProgressMonitor());
                    // prune empty directories:
                    prune(properties.getUnderlying().file.getParent());
                } catch (CoreException e) {
                    MessageDialog.openError(null, "Unable to change primary type", "Could not delete vault file " + properties.getUnderlying().file + ": " + e);
                    Activator.getDefault().getPluginLogger().error("Error changing jcr:primaryType. Could not delete vault file " + properties.getUnderlying().file + ": " + e.getMessage(), e);
                    return;
                }
            }
            underlying.save();
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) Attribute(de.pdark.decentxml.Attribute) Element(de.pdark.decentxml.Element) RepositoryException(org.apache.sling.ide.transport.RepositoryException) Logger(org.apache.sling.ide.log.Logger) Repository(org.apache.sling.ide.transport.Repository) CoreException(org.eclipse.core.runtime.CoreException) SerializationKind(org.apache.sling.ide.serialization.SerializationKind) Iterator(java.util.Iterator) NodeTypeRegistry(org.apache.sling.ide.transport.NodeTypeRegistry) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) IFolder(org.eclipse.core.resources.IFolder)

Example 2 with Attribute

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

the class ProductConfiguration method getW32Icons.

public List<String> getW32Icons() {
    Element domLauncher = dom.getChild("launcher");
    if (domLauncher == null) {
        return null;
    }
    Element win = domLauncher.getChild("win");
    if (win == null) {
        return null;
    }
    List<String> icons = new ArrayList<>();
    String useIco = win.getAttributeValue("useIco");
    if (Boolean.valueOf(useIco)) {
        // for (Element ico : win.getChildren("ico"))
        {
            Element ico = win.getChild("ico");
            // should be only 1
            icons.add(ico.getAttributeValue("path"));
        }
    } else {
        for (Element bmp : win.getChildren("bmp")) {
            List<Attribute> attibuteNames = bmp.getAttributes();
            if (attibuteNames != null && attibuteNames.size() > 0)
                icons.add(attibuteNames.get(0).getValue());
        }
    }
    return icons;
}
Also used : Attribute(de.pdark.decentxml.Attribute) Element(de.pdark.decentxml.Element) ArrayList(java.util.ArrayList)

Example 3 with Attribute

use of de.pdark.decentxml.Attribute 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 4 with Attribute

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

the class ModifiableProperties method renameProperty.

public void renameProperty(String oldKey, String newKey) {
    Attribute a = domElement.getAttribute(oldKey);
    a.setName(newKey);
    genericJcrRootFile.save();
}
Also used : Attribute(de.pdark.decentxml.Attribute)

Example 5 with Attribute

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

the class ModifiableProperties method doSetPropertyValue.

void doSetPropertyValue(Object id, Object value) {
    if (id instanceof Map.Entry<?, ?>) {
        Map.Entry<String, String> entry = (Map.Entry<String, String>) id;
        entry.setValue(String.valueOf(value));
        Attribute a = domElement.getAttribute(entry.getKey());
        a.setValue(String.valueOf(value));
        genericJcrRootFile.save();
    } else if (value instanceof String) {
        Attribute a = domElement.getAttribute((String) id);
        a.setValue((String) value);
        genericJcrRootFile.save();
    } else {
        System.out.println("UNSUPPORTED VALUE TYPE: " + value.getClass());
    }
}
Also used : Entry(java.util.Map.Entry) Attribute(de.pdark.decentxml.Attribute) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Attribute (de.pdark.decentxml.Attribute)7 CoreException (org.eclipse.core.runtime.CoreException)3 Element (de.pdark.decentxml.Element)2 ArrayList (java.util.ArrayList)2 RepositoryException (org.apache.sling.ide.transport.RepositoryException)2 IFile (org.eclipse.core.resources.IFile)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 Document (de.pdark.decentxml.Document)1 XMLParseException (de.pdark.decentxml.XMLParseException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Logger (org.apache.sling.ide.log.Logger)1 SerializationKind (org.apache.sling.ide.serialization.SerializationKind)1