Search in sources :

Example 1 with Element

use of de.pdark.decentxml.Element 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 Element

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

the class JcrNode method hashCode.

@Override
public int hashCode() {
    if (underlying == null) {
        if (resource == null) {
            if (domElement == null) {
                return toString().hashCode();
            } else {
                Element domElementCopy = domElement.copy();
                domElementCopy.clearChildren();
                return domElementCopy.toString().hashCode() + parent.hashCode();
            }
        } else {
            return resource.getFullPath().hashCode();
        }
    } else {
        if (domElement == null) {
            return underlying.hashCode();
        }
        Element domElementCopy = domElement.copy();
        domElementCopy.clearChildren();
        return underlying.hashCode() + domElementCopy.toString().hashCode();
    }
}
Also used : Element(de.pdark.decentxml.Element)

Example 3 with Element

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

the class JcrNode method createChild.

protected void createChild(String nodeName, String nodeType, Element domElement, GenericJcrRootFile effectiveUnderlying) {
    if (domElement == null) {
        throw new IllegalArgumentException("domNode must not be null");
    }
    if (effectiveUnderlying == null) {
        throw new IllegalArgumentException("effectiveUnderlying must not be null");
    }
    Element element = new Element(nodeName);
    if (nodeType != null) {
        element.addAttribute("jcr:primaryType", nodeType);
    }
    StringBuilder indent = new StringBuilder();
    Element parElement = domElement.getParentElement();
    while (parElement != null) {
        indent.append("    ");
        parElement = parElement.getParentElement();
    }
    domElement.addNode(new Text("\n    " + indent.toString()));
    element = domElement.addNode(element);
    domElement.addNode(new Text("\n" + indent.toString()));
    JcrNode childNode = new JcrNode(this, element, null);
    effectiveUnderlying.save();
}
Also used : Element(de.pdark.decentxml.Element) Text(de.pdark.decentxml.Text)

Example 4 with Element

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

the class JcrNode method equals.

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof JcrNode)) {
        return false;
    }
    JcrNode other = (JcrNode) obj;
    if (resource != null && other.resource != null) {
        if (resource.equals(other.resource)) {
            return true;
        } else {
            return false;
        }
    } else if (resource != null && other.resource == null) {
        return false;
    } else if (resource == null && other.resource != null) {
        return false;
    }
    if (other.underlying == null && underlying != null) {
        return false;
    } else if (other.underlying != null && underlying == null) {
        return false;
    }
    if (underlying != null && !underlying.equals(other.underlying)) {
        return false;
    }
    if (parent != null && other.parent != null) {
        if (!parent.equals(other.parent)) {
            return false;
        }
        Element domElementCopy = domElement.copy();
        domElementCopy.clearChildren();
        Element otherDomElementCopy = other.domElement.copy();
        otherDomElementCopy.clearChildren();
        return domElementCopy.toString().equals(otherDomElementCopy.toString());
    }
    return toString().equals(obj.toString());
}
Also used : Element(de.pdark.decentxml.Element)

Example 5 with Element

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

the class JcrNode method delete.

public void delete() {
    if (parent == null) {
        // then I dont know how to delete
        return;
    }
    parent.children.remove(this);
    if (resource != null) {
        IWorkspaceRunnable r = new IWorkspaceRunnable() {

            @Override
            public void run(IProgressMonitor monitor) throws CoreException {
                resource.delete(true, monitor);
                if (dirSibling != null) {
                    dirSibling.getResource().delete(true, monitor);
                }
            }
        };
        try {
            ResourcesPlugin.getWorkspace().run(r, null);
        } catch (CoreException e) {
            Activator.getDefault().getPluginLogger().error("Error renaming resource (" + resource + "): " + e, e);
        }
    }
    if (domElement != null) {
        Element parentNode = domElement.getParentElement();
        domElement.remove();
        if (parentNode != null) {
            List<Node> allChildNodes = parentNode.getNodes();
            boolean nonTextChild = false;
            for (Iterator<Node> it = allChildNodes.iterator(); it.hasNext(); ) {
                Node node = it.next();
                if (node.getType() != Type.TEXT) {
                    nonTextChild = true;
                }
            }
            if (!nonTextChild) {
                for (Iterator<Node> it = allChildNodes.iterator(); it.hasNext(); ) {
                    Node node = it.next();
                    it.remove();
                }
                if (!parentNode.hasNodes()) {
                    parentNode.setCompactEmpty(true);
                }
            }
        }
    }
    if (underlying != null) {
        underlying.save();
    }
}
Also used : IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) Element(de.pdark.decentxml.Element) Node(de.pdark.decentxml.Node)

Aggregations

Element (de.pdark.decentxml.Element)8 IFile (org.eclipse.core.resources.IFile)2 IFolder (org.eclipse.core.resources.IFolder)2 CoreException (org.eclipse.core.runtime.CoreException)2 Attribute (de.pdark.decentxml.Attribute)1 Node (de.pdark.decentxml.Node)1 Text (de.pdark.decentxml.Text)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Logger (org.apache.sling.ide.log.Logger)1 SerializationKind (org.apache.sling.ide.serialization.SerializationKind)1 NodeTypeRegistry (org.apache.sling.ide.transport.NodeTypeRegistry)1 Repository (org.apache.sling.ide.transport.Repository)1 RepositoryException (org.apache.sling.ide.transport.RepositoryException)1 IContainer (org.eclipse.core.resources.IContainer)1 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)1 IPath (org.eclipse.core.runtime.IPath)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1