use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class FileResource method getValueMap.
@Override
public ValueMap getValueMap() {
if (valueMap == null) {
// we should simulate the corresponding JCR properties in a value map as well
if (file.exists() && file.canRead()) {
Map<String, Object> props = new HashMap<String, Object>();
props.put("jcr:primaryType", file.isFile() ? RESOURCE_TYPE_FILE : RESOURCE_TYPE_FOLDER);
props.put("jcr:createdBy", "system");
Calendar lastModifed = Calendar.getInstance();
lastModifed.setTimeInMillis(file.lastModified());
props.put("jcr:created", lastModifed);
// overlay properties with those from node descriptor content file, if it exists
ContentFile contentFile = getNodeDescriptorContentFile();
if (contentFile != null) {
for (Map.Entry<String, Object> entry : contentFile.getValueMap().entrySet()) {
// skip primary type if it is the default type assigned by contentparser when none is defined
if (StringUtils.equals(entry.getKey(), "jcr:primaryType") && StringUtils.equals((String) entry.getValue(), ParserOptions.DEFAULT_PRIMARY_TYPE)) {
continue;
}
props.put(entry.getKey(), entry.getValue());
}
}
valueMap = new ValueMapDecorator(props);
}
}
return valueMap;
}
use of org.apache.sling.api.resource.ValueMap 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.api.resource.ValueMap in project sling by apache.
the class ValueMapUtilTest method testToValueMap.
@Test
public void testToValueMap() {
Map<String, Object> content = new HashMap<>();
content.put("stringProp", "abc");
content.put("intProp", 123);
content.put("stringArray", new String[] { "a", "b", "c" });
content.put("stringList", ImmutableList.of("ab", "cd"));
content.put("intList", ImmutableList.of(12, 34));
ValueMap props = ValueMapUtil.toValueMap(content);
assertEquals("abc", props.get("stringProp", String.class));
assertEquals((Integer) 123, props.get("intProp", 0));
assertArrayEquals(new String[] { "a", "b", "c" }, props.get("stringArray", String[].class));
assertArrayEquals(new String[] { "ab", "cd" }, props.get("stringList", String[].class));
assertArrayEquals(new Integer[] { 12, 34 }, props.get("intList", Integer[].class));
}
use of org.apache.sling.api.resource.ValueMap 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.api.resource.ValueMap in project sling by apache.
the class JcrXmlContentTest method testContent_Datatypes.
@Test
public void testContent_Datatypes() {
Resource underTest = fsroot.getChild("folder3/content/jcr:content");
ValueMap props = underTest.getValueMap();
assertEquals("en", props.get("jcr:title", String.class));
assertEquals(true, props.get("includeAside", false));
assertEquals((Long) 1234567890123L, props.get("longProp", Long.class));
assertEquals((Double) 1.2345d, props.get("decimalProp", Double.class), 0.00001d);
assertArrayEquals(new String[] { "aa", "bb", "cc" }, props.get("stringPropMulti", String[].class));
assertArrayEquals(new Long[] { 1234567890123L, 55L }, props.get("longPropMulti", Long[].class));
}
Aggregations