Search in sources :

Example 1 with Aggregate

use of org.apache.jackrabbit.vault.fs.api.Aggregate in project sling by apache.

the class VltSerializationDataBuilder method findAggregateChain.

/**
     * Returns the aggregates for a specific resource
     * 
     * <p>
     * In the simplest case, a single element is returned in the chain, signalling that the aggregate is a top-level
     * one.
     * </p>
     * 
     * <p>
     * For leaf aggregates, the list contains the top-most aggregates first and ends up with the leaf-most ones.
     * </p>
     * 
     * @param resource the resource to find the aggregate chain for
     * @return a list of aggregates
     * @throws IOException
     * @throws RepositoryException
     */
private List<Aggregate> findAggregateChain(ResourceProxy resource) throws IOException, RepositoryException {
    VaultFile vaultFile = fs.getFile(PlatformNameFormat.getPlatformPath(resource.getPath()));
    if (vaultFile == null || vaultFile.getAggregate() == null) {
        // this file might be a leaf aggregate of a vaultfile higher in the resource path ; so look for a
        // parent higher
        String parentPath = Text.getRelativeParent(resource.getPath(), 1);
        while (!parentPath.equals("/")) {
            VaultFile parentFile = fs.getFile(PlatformNameFormat.getPlatformPath(parentPath));
            if (parentFile != null) {
                Aggregate parentAggregate = parentFile.getAggregate();
                ArrayList<Aggregate> parents = new ArrayList<>();
                parents.add(parentAggregate);
                List<Aggregate> chain = lookForAggregateInLeaves(resource, parentAggregate, parents);
                if (chain != null) {
                    return chain;
                }
            }
            parentPath = Text.getRelativeParent(parentPath, 1);
        }
        return null;
    }
    return Collections.singletonList(vaultFile.getAggregate());
}
Also used : ArrayList(java.util.ArrayList) VaultFile(org.apache.jackrabbit.vault.fs.api.VaultFile) Aggregate(org.apache.jackrabbit.vault.fs.api.Aggregate)

Example 2 with Aggregate

use of org.apache.jackrabbit.vault.fs.api.Aggregate 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 3 with Aggregate

use of org.apache.jackrabbit.vault.fs.api.Aggregate 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

Aggregate (org.apache.jackrabbit.vault.fs.api.Aggregate)3 SerializationKind (org.apache.sling.ide.serialization.SerializationKind)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 RepositoryException (javax.jcr.RepositoryException)1 VaultFile (org.apache.jackrabbit.vault.fs.api.VaultFile)1 DocViewSerializer (org.apache.jackrabbit.vault.fs.impl.io.DocViewSerializer)1 SerializationData (org.apache.sling.ide.serialization.SerializationData)1 SerializationException (org.apache.sling.ide.serialization.SerializationException)1