Search in sources :

Example 1 with SerializationKind

use of org.apache.sling.ide.serialization.SerializationKind in project sling by apache.

the class VltSerializationDataBuilder method calculateFileOrFolderPathHint.

private String calculateFileOrFolderPathHint(List<Aggregate> chain) throws RepositoryException {
    ListIterator<Aggregate> aggs = chain.listIterator();
    StringBuilder out = new StringBuilder();
    while (aggs.hasNext()) {
        Aggregate cur = aggs.next();
        if (aggs.previousIndex() == 0) {
            out.append(PlatformNameFormat.getPlatformPath(cur.getPath()));
        } else {
            out.append("/");
            out.append(PlatformNameFormat.getPlatformPath(cur.getRelPath()));
        }
        if (needsDir(cur)) {
            SerializationKind serializationKind = getSerializationKind(cur);
            if (serializationKind == SerializationKind.FILE) {
                out.append(".dir");
            }
            if (!aggs.hasNext() && serializationKind == SerializationKind.METADATA_FULL) {
                out.delete(out.lastIndexOf("/"), out.length());
            }
        }
    }
    return out.toString();
}
Also used : SerializationKind(org.apache.sling.ide.serialization.SerializationKind) Aggregate(org.apache.jackrabbit.vault.fs.api.Aggregate)

Example 2 with SerializationKind

use of org.apache.sling.ide.serialization.SerializationKind 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 3 with SerializationKind

use of org.apache.sling.ide.serialization.SerializationKind in project sling by apache.

the class ResourceChangeCommandFactory method normaliseResourceChildren.

/**
     * Normalises the of the specified <tt>resourceProxy</tt> by comparing the serialization data and the filesystem
     * data
     * 
     * @param serializationFile the file which contains the serialization data
     * @param resourceProxy the resource proxy
     * @param syncDirectory the sync directory
     * @param repository TODO
     * @throws CoreException
     */
private void normaliseResourceChildren(IFile serializationFile, ResourceProxy resourceProxy, IFolder syncDirectory, Repository repository) throws CoreException {
    // TODO - this logic should be moved to the serializationManager
    try {
        SerializationKindManager skm = new SerializationKindManager();
        skm.init(repository);
        String primaryType = (String) resourceProxy.getProperties().get(Repository.JCR_PRIMARY_TYPE);
        List<String> mixinTypesList = getMixinTypes(resourceProxy);
        SerializationKind serializationKind = skm.getSerializationKind(primaryType, mixinTypesList);
        if (serializationKind == SerializationKind.METADATA_FULL) {
            return;
        }
    } catch (RepositoryException e) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed creating a " + SerializationDataBuilder.class.getName(), e));
    }
    IPath serializationDirectoryPath = serializationFile.getFullPath().removeLastSegments(1);
    Iterator<ResourceProxy> childIterator = resourceProxy.getChildren().iterator();
    Map<String, IResource> extraChildResources = new HashMap<>();
    for (IResource member : serializationFile.getParent().members()) {
        if (member.equals(serializationFile)) {
            continue;
        }
        extraChildResources.put(member.getName(), member);
    }
    while (childIterator.hasNext()) {
        ResourceProxy child = childIterator.next();
        String childName = PathUtil.getName(child.getPath());
        String osPath = serializationManager.getOsPath(childName);
        // covered children might have a FS representation, depending on their child nodes, so
        // accept a directory which maps to their name
        extraChildResources.remove(osPath);
        // covered children do not need a filesystem representation
        if (resourceProxy.covers(child.getPath())) {
            continue;
        }
        IPath childPath = serializationDirectoryPath.append(osPath);
        IResource childResource = ResourcesPlugin.getWorkspace().getRoot().findMember(childPath);
        if (childResource == null) {
            Activator.getDefault().getPluginLogger().trace("For resource at with serialization data {0} the serialized child resource at {1} does not exist in the filesystem and will be ignored", serializationFile, childPath);
            childIterator.remove();
        }
    }
    for (IResource extraChildResource : extraChildResources.values()) {
        IPath extraChildResourcePath = extraChildResource.getFullPath().makeRelativeTo(syncDirectory.getFullPath()).makeAbsolute();
        resourceProxy.addChild(new ResourceProxy(serializationManager.getRepositoryPath(extraChildResourcePath.toPortableString())));
        Activator.getDefault().getPluginLogger().trace("For resource at with serialization data {0} the found a child resource at {1} which is not listed in the serialized child resources and will be added", serializationFile, extraChildResource);
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) RepositoryException(org.apache.sling.ide.transport.RepositoryException) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) SerializationKindManager(org.apache.sling.ide.serialization.SerializationKindManager) CoreException(org.eclipse.core.runtime.CoreException) SerializationKind(org.apache.sling.ide.serialization.SerializationKind) IResource(org.eclipse.core.resources.IResource)

Example 4 with SerializationKind

use of org.apache.sling.ide.serialization.SerializationKind in project sling by apache.

the class JcrNode method createChild.

public void createChild(final String childNodeName, final String childNodeType) {
    String thisNodeType = getPrimaryType();
    final SerializationKind parentSk = getSerializationKind(thisNodeType);
    final SerializationKind childSk = getSerializationKind(childNodeType);
    final SerializationManager serializationManager = Activator.getDefault().getSerializationManager();
    if (parentSk == SerializationKind.METADATA_FULL) {
        createDomChild(childNodeName, childNodeType);
    } else if (parentSk == SerializationKind.FILE) {
        throw new IllegalStateException("cannot create child of nt:file");
    } else if (childSk == SerializationKind.FOLDER) {
        IWorkspaceRunnable r = new IWorkspaceRunnable() {

            @Override
            public void run(IProgressMonitor monitor) throws CoreException {
                IFolder newFolder = prepareCreateFolderChild(childNodeName);
                if (parentSk == SerializationKind.METADATA_PARTIAL) {
                    // when the parent is partial and we're creating a folder here,
                    // then we're running into a SLING-3639 type of problem
                    // the way around this is to make a 'pointer' in the 'root'
                    // .content.xml, and have a directory structure leaving to
                    // the new node, together with a .content.xml describing
                    // the type (unless it's a nt:folder that is)
                    // 1) 'pointer' in the 'root .content.xml'
                    createDomChild(childNodeName, null);
                // 2) directory structure is created above already
                // 3) new .content.xml is done below
                }
                if (!childNodeType.equals("nt:folder")) {
                    createVaultFile(newFolder, ".content.xml", childNodeType);
                }
            }
        };
        try {
            ResourcesPlugin.getWorkspace().run(r, null);
            if (childNodeType.equals("nt:folder") && parentSk == SerializationKind.FOLDER) {
                // trigger a publish, as folder creation is not propagated to 
                // the SlingLaunchpadBehavior otherwise
                //TODO: make configurable? Fix in Eclipse/WST?
                ServerUtil.triggerIncrementalBuild((IFolder) resource, null);
            }
        } catch (CoreException e) {
            Activator.getDefault().getPluginLogger().error("Error creating child " + childNodeName + ": " + e, e);
            e.printStackTrace();
            MessageDialog.openError(Display.getDefault().getActiveShell(), "Error creating node", "Error creating child of " + thisNodeType + " with type " + childNodeType + ": " + e);
            return;
        }
    } else if ((parentSk == SerializationKind.FOLDER || parentSk == SerializationKind.METADATA_PARTIAL) && childSk == SerializationKind.METADATA_FULL) {
        createVaultFile((IFolder) resource, serializationManager.getOsPath(childNodeName) + ".xml", childNodeType);
    } else if (parentSk == SerializationKind.FOLDER && childSk == SerializationKind.METADATA_PARTIAL) {
        //	        createVaultFile((IFolder)resource, childNodeName+".xml", childNodeType);
        IWorkspaceRunnable r = new IWorkspaceRunnable() {

            @Override
            public void run(IProgressMonitor monitor) throws CoreException {
                IFolder f = (IFolder) resource;
                IFolder newFolder = null;
                newFolder = f.getFolder(serializationManager.getOsPath(childNodeName));
                newFolder.create(true, true, new NullProgressMonitor());
                createVaultFile(newFolder, ".content.xml", childNodeType);
            }
        };
        try {
            ResourcesPlugin.getWorkspace().run(r, null);
        } catch (CoreException e) {
            Activator.getDefault().getPluginLogger().error("Error creating child " + childNodeName + ": " + e, e);
            e.printStackTrace();
            MessageDialog.openError(Display.getDefault().getActiveShell(), "Error creating node", "Error creating child of " + thisNodeType + " with type " + childNodeType + ": " + e);
            return;
        }
    } else if (parentSk != SerializationKind.FOLDER && childSk == SerializationKind.METADATA_PARTIAL) {
        createDomChild(childNodeName, childNodeType);
    } else {
        if (childNodeType.equals("nt:file")) {
            IFolder f = (IFolder) resource;
            createNtFile(f, childNodeName, childNodeType);
            return;
        }
        //TODO: FILE not yet supported
        Activator.getDefault().getPluginLogger().error("Cannot create child node of type " + childNodeType + ", serializationKind " + childSk + " under child node of type " + thisNodeType + ", serializationKind " + parentSk);
        MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Error creating node", "Cannot create child of " + thisNodeType + " with type " + childNodeType + " (yet?)");
        return;
    }
}
Also used : IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) SerializationKind(org.apache.sling.ide.serialization.SerializationKind) SerializationManager(org.apache.sling.ide.serialization.SerializationManager) IFolder(org.eclipse.core.resources.IFolder)

Example 5 with SerializationKind

use of org.apache.sling.ide.serialization.SerializationKind in project sling by apache.

the class VltSerializationDataBuilder method buildSerializationData.

@Override
public SerializationData buildSerializationData(File contentSyncRoot, ResourceProxy resource) throws SerializationException {
    try {
        List<Aggregate> chain = findAggregateChain(resource);
        if (chain == null) {
            return null;
        }
        Aggregate aggregate = chain.get(chain.size() - 1);
        String fileOrFolderPathHint = calculateFileOrFolderPathHint(chain);
        String nameHint = PlatformNameFormat.getPlatformName(aggregate.getName());
        SerializationKind serializationKind = getSerializationKind(aggregate);
        if (resource.getPath().equals("/") || serializationKind == SerializationKind.METADATA_PARTIAL || serializationKind == SerializationKind.FILE || serializationKind == SerializationKind.FOLDER) {
            nameHint = Constants.DOT_CONTENT_XML;
        } else if (serializationKind == SerializationKind.METADATA_FULL) {
            nameHint += ".xml";
        }
        Activator.getDefault().getPluginLogger().trace("Got location {0} for path {1}", fileOrFolderPathHint, resource.getPath());
        if (!needsDir(aggregate)) {
            return SerializationData.empty(fileOrFolderPathHint, serializationKind);
        }
        DocViewSerializer s = new DocViewSerializer(aggregate);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        s.writeContent(out);
        byte[] result = out.toByteArray();
        return new SerializationData(fileOrFolderPathHint, nameHint, result, serializationKind);
    } catch (RepositoryException e) {
        throw new SerializationException(e);
    } catch (IOException e) {
        throw new SerializationException(e);
    }
}
Also used : SerializationException(org.apache.sling.ide.serialization.SerializationException) SerializationData(org.apache.sling.ide.serialization.SerializationData) SerializationKind(org.apache.sling.ide.serialization.SerializationKind) DocViewSerializer(org.apache.jackrabbit.vault.fs.impl.io.DocViewSerializer) RepositoryException(javax.jcr.RepositoryException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Aggregate(org.apache.jackrabbit.vault.fs.api.Aggregate)

Aggregations

SerializationKind (org.apache.sling.ide.serialization.SerializationKind)6 CoreException (org.eclipse.core.runtime.CoreException)3 Aggregate (org.apache.jackrabbit.vault.fs.api.Aggregate)2 RepositoryException (org.apache.sling.ide.transport.RepositoryException)2 ResourceProxy (org.apache.sling.ide.transport.ResourceProxy)2 IFolder (org.eclipse.core.resources.IFolder)2 IResource (org.eclipse.core.resources.IResource)2 IPath (org.eclipse.core.runtime.IPath)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 Attribute (de.pdark.decentxml.Attribute)1 Element (de.pdark.decentxml.Element)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 RepositoryException (javax.jcr.RepositoryException)1 DocViewSerializer (org.apache.jackrabbit.vault.fs.impl.io.DocViewSerializer)1