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;
}
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);
}
});
}
}
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"));
}
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"));
}
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;
}
Aggregations