Search in sources :

Example 6 with ContentElement

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

the class FileVaultResourceMapper method getChildren.

@SuppressWarnings("unchecked")
@Override
public Iterator<Resource> getChildren(final ResourceResolver resolver, final Resource parent) {
    String parentPath = parent.getPath();
    Set<String> childPaths = new LinkedHashSet<>();
    // get children from content resource of parent
    ContentFile parentContentFile = getContentFile(parentPath, null);
    if (parentContentFile != null) {
        Iterator<Map.Entry<String, ContentElement>> childMaps = parentContentFile.getChildren();
        while (childMaps.hasNext()) {
            Map.Entry<String, ContentElement> entry = childMaps.next();
            String childPath = parentPath + "/" + entry.getKey();
            if (pathMatches(childPath)) {
                childPaths.add(childPath);
            }
        }
    }
    // additional check for children in file system
    File parentFile = getFile(parentPath);
    if (parentFile != null && parentFile.isDirectory()) {
        for (File childFile : parentFile.listFiles()) {
            String childPath = parentPath + "/" + PlatformNameFormat.getRepositoryName(childFile.getName());
            if (pathMatches(childPath) && !childPaths.contains(childPath)) {
                childPaths.add(childPath);
            }
        }
    }
    if (childPaths.isEmpty()) {
        return null;
    } else {
        return IteratorUtils.transformedIterator(childPaths.iterator(), new Transformer() {

            @Override
            public Object transform(Object input) {
                String path = (String) input;
                return getResource(resolver, path);
            }
        });
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Transformer(org.apache.commons.collections.Transformer) ContentElement(org.apache.sling.fsprovider.internal.parser.ContentElement) Map(java.util.Map) File(java.io.File)

Example 7 with ContentElement

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

the class ContentFileTest method testRootContent.

@Test
public void testRootContent() {
    File file = new File("src/test/resources/fs-test/folder2/content.json");
    ContentFile underTest = new ContentFile(file, "/fs-test/folder2/content", null, contentFileCache);
    assertEquals(file, underTest.getFile());
    assertNull(underTest.getSubPath());
    assertTrue(underTest.hasContent());
    ContentElement content = underTest.getContent();
    assertEquals("app:Page", content.getProperties().get("jcr:primaryType"));
    assertEquals("app:PageContent", content.getChild("jcr:content").getProperties().get("jcr:primaryType"));
    ValueMap props = underTest.getValueMap();
    assertEquals("app:Page", props.get("jcr:primaryType"));
    assertNull(props.get("jcr:content"));
}
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)

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