Search in sources :

Example 1 with ContentElement

use of org.apache.sling.fsprovider.internal.parser.ContentElement in project sling by apache.

the class FileMonitor method collectResourceChanges.

private List<ResourceChange> collectResourceChanges(final Monitorable monitorable, final ChangeType changeType) {
    List<ResourceChange> changes = new ArrayList<>();
    if (monitorable.status instanceof ContentFileStatus) {
        ContentFile contentFile = ((ContentFileStatus) monitorable.status).contentFile;
        if (changeType == ChangeType.CHANGED) {
            ContentElement content = contentFile.getContent();
            // we cannot easily report the diff of resource changes between two content files
            // so we simulate a removal of the toplevel node and then add all nodes contained in the current content file again.
            changes.add(buildContentResourceChange(ChangeType.REMOVED, transformPath(monitorable.path)));
            addContentResourceChanges(changes, ChangeType.ADDED, content, transformPath(monitorable.path));
        } else {
            addContentResourceChanges(changes, changeType, contentFile.getContent(), transformPath(monitorable.path));
        }
    } else {
        changes.add(buildContentResourceChange(changeType, transformPath(monitorable.path)));
    }
    return changes;
}
Also used : ContentFile(org.apache.sling.fsprovider.internal.mapper.ContentFile) ContentElement(org.apache.sling.fsprovider.internal.parser.ContentElement) ArrayList(java.util.ArrayList) ResourceChange(org.apache.sling.api.resource.observation.ResourceChange)

Example 2 with ContentElement

use of org.apache.sling.fsprovider.internal.parser.ContentElement in project sling by apache.

the class ContentFileResourceMapper method getChildren.

@SuppressWarnings("unchecked")
@Override
public Iterator<Resource> getChildren(final ResourceResolver resolver, final Resource parent) {
    if (contentFileExtensions.isEmpty()) {
        return null;
    }
    final String parentPath = parent.getPath();
    ContentFile parentContentFile = parent.adaptTo(ContentFile.class);
    // not a FsResource, try to create one from the resource
    if (parentContentFile == null) {
        parentContentFile = getFile(parentPath, null);
        if (parentContentFile == null) {
            // check if parent is a file resource that contains a file content resource
            File parentFile = parent.adaptTo(File.class);
            if (parentFile != null && parentFile.isDirectory()) {
                List<Resource> childResources = new ArrayList<>();
                for (File file : parentFile.listFiles()) {
                    String filenameSuffix = contentFileExtensions.getSuffix(file);
                    if (filenameSuffix != null && !isNodeDescriptor(file)) {
                        String path = parentPath + "/" + StringUtils.substringBeforeLast(file.getName(), filenameSuffix);
                        ContentFile contentFile = new ContentFile(file, path, null, contentFileCache);
                        childResources.add(new ContentFileResource(resolver, contentFile));
                    }
                }
                if (!childResources.isEmpty()) {
                    return childResources.iterator();
                }
            }
            // no children here
            return null;
        }
    }
    // get child resources from content fragments in content file
    List<ContentFile> children = new ArrayList<>();
    if (parentContentFile.hasContent()) {
        Iterator<Map.Entry<String, ContentElement>> childMaps = parentContentFile.getChildren();
        while (childMaps.hasNext()) {
            Map.Entry<String, ContentElement> entry = childMaps.next();
            children.add(parentContentFile.navigateToRelative(entry.getKey()));
        }
    }
    if (children.isEmpty()) {
        return null;
    } else {
        return IteratorUtils.transformedIterator(children.iterator(), new Transformer() {

            @Override
            public Object transform(Object input) {
                ContentFile contentFile = (ContentFile) input;
                return new ContentFileResource(resolver, contentFile);
            }
        });
    }
}
Also used : Transformer(org.apache.commons.collections.Transformer) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) ContentElement(org.apache.sling.fsprovider.internal.parser.ContentElement) File(java.io.File) Map(java.util.Map)

Example 3 with ContentElement

use of org.apache.sling.fsprovider.internal.parser.ContentElement in project sling by apache.

the class ContentFileTest method testContentLevel1.

@Test
public void testContentLevel1() {
    File file = new File("src/test/resources/fs-test/folder2/content.json");
    ContentFile underTest = new ContentFile(file, "/fs-test/folder2/content", "jcr:content", contentFileCache);
    assertEquals(file, underTest.getFile());
    assertEquals("jcr:content", underTest.getSubPath());
    assertTrue(underTest.hasContent());
    ContentElement content = underTest.getContent();
    assertEquals("app:PageContent", content.getProperties().get("jcr:primaryType"));
    ValueMap props = underTest.getValueMap();
    assertEquals("app:PageContent", props.get("jcr:primaryType"));
}
Also used : ContentElement(org.apache.sling.fsprovider.internal.parser.ContentElement) ValueMap(org.apache.sling.api.resource.ValueMap) File(java.io.File) Test(org.junit.Test)

Example 4 with ContentElement

use of org.apache.sling.fsprovider.internal.parser.ContentElement in project sling by apache.

the class ContentFileTest method testContentLevel5.

@Test
public void testContentLevel5() {
    File file = new File("src/test/resources/fs-test/folder2/content.json");
    ContentFile underTest = new ContentFile(file, "/fs-test/folder2/content", "jcr:content/par/image/file/jcr:content", contentFileCache);
    assertEquals(file, underTest.getFile());
    assertEquals("jcr:content/par/image/file/jcr:content", underTest.getSubPath());
    assertTrue(underTest.hasContent());
    ContentElement content = underTest.getContent();
    assertEquals("nt:resource", content.getProperties().get("jcr:primaryType"));
    ValueMap props = underTest.getValueMap();
    assertEquals("nt:resource", props.get("jcr:primaryType"));
}
Also used : ContentElement(org.apache.sling.fsprovider.internal.parser.ContentElement) ValueMap(org.apache.sling.api.resource.ValueMap) File(java.io.File) Test(org.junit.Test)

Example 5 with ContentElement

use of org.apache.sling.fsprovider.internal.parser.ContentElement in project sling by apache.

the class ContentFile method getContent.

/**
     * Content object referenced by sub path.
     * @return Map if resource, property value if property.
     */
public ContentElement getContent() {
    if (!contentInitialized) {
        ContentElement rootContent = contentFileCache.get(path, file);
        if (subPath == null) {
            content = rootContent;
        } else if (rootContent != null) {
            content = rootContent.getChild(subPath);
        }
        contentInitialized = true;
    }
    return content;
}
Also used : ContentElement(org.apache.sling.fsprovider.internal.parser.ContentElement)

Aggregations

ContentElement (org.apache.sling.fsprovider.internal.parser.ContentElement)7 File (java.io.File)5 ValueMap (org.apache.sling.api.resource.ValueMap)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Transformer (org.apache.commons.collections.Transformer)2 LinkedHashSet (java.util.LinkedHashSet)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceChange (org.apache.sling.api.resource.observation.ResourceChange)1 ContentFile (org.apache.sling.fsprovider.internal.mapper.ContentFile)1