Package org.apache.sling.api.resource

Examples of org.apache.sling.api.resource.ValueMap


                }
            }

            if (resourcePath == null && name != null) {
                // try to get from value map
                ValueMap map = getValueMap(adaptable);
                if (map != null) {
                    resourcePath = map.get(name, String.class);
                }
            }
        }

        if (resourcePath != null) {
View Full Code Here


    /**
     * Returns the id of this view.
     * @return the id of this view
     */
    public String getViewId() {
      final ValueMap props = getResource().adaptTo(ValueMap.class);
      final String clusterId = props.get(VIEW_PROPERTY_CLUSTER_ID, String.class);
      if (clusterId != null && clusterId.length() > 0) {
        return clusterId;
      } else {
        return getResource().getName();
      }
View Full Code Here

        return "valuemap";
    }

    public Object getValue(Object adaptable, String name, Type type, AnnotatedElement element,
            DisposalCallbackRegistry callbackRegistry) {
        ValueMap map = getValueMap(adaptable);
        if (map == null) {
            return null;
        } else if (type instanceof Class<?>) {
            Class<?> clazz = (Class<?>) type;
            try {
                return map.get(name, clazz);
            } catch (ClassCastException e) {
                // handle case of primitive/wrapper arrays
                if (clazz.isArray()) {
                    Class<?> componentType = clazz.getComponentType();
                    if (componentType.isPrimitive()) {
                        Class<?> wrapper = ClassUtils.primitiveToWrapper(componentType);
                        if (wrapper != componentType) {
                            Object wrapperArray = map.get(name, Array.newInstance(wrapper, 0).getClass());
                            if (wrapperArray != null) {
                                return unwrapArray(wrapperArray, componentType);
                            }
                        }
                    } else {
                        Class<?> primitiveType = ClassUtils.wrapperToPrimitive(componentType);
                        if (primitiveType != componentType) {
                            Object primitiveArray = map.get(name, Array.newInstance(primitiveType, 0).getClass());
                            if (primitiveArray != null) {
                                return wrapArray(primitiveArray, componentType);
                            }
                        }
                    }
                }
                return null;
            }
        } else if (type instanceof ParameterizedType) {
            // list support
            ParameterizedType pType = (ParameterizedType) type;
            if (pType.getActualTypeArguments().length != 1) {
                return null;
            }
            Class<?> collectionType = (Class<?>) pType.getRawType();
            if (!(collectionType.equals(Collection.class) || collectionType.equals(List.class))) {
                return null;
            }

            Class<?> itemType = (Class<?>) pType.getActualTypeArguments()[0];
            Object array = map.get(name, Array.newInstance(itemType, 0).getClass());
            if (array == null) {
                return null;

            }
View Full Code Here

        return true;
    }

    /** Compile a string builder containing the properties of a resource - used for logging **/
    public static StringBuilder getPropertiesForLogging(final Resource resource) {
        ValueMap valueMap;
        try{
            valueMap = resource.adaptTo(ValueMap.class);
        } catch(RuntimeException re) {
            return new StringBuilder("non-existing resource: "+resource+" ("+re.getMessage()+")");
        }
        if (valueMap==null) {
            return new StringBuilder("non-existing resource: "+resource+" (no ValueMap)");
        }
        final Set<Entry<String, Object>> entrySet = valueMap.entrySet();
        final StringBuilder sb = new StringBuilder();
        for (Iterator<Entry<String, Object>> it = entrySet.iterator(); it
                .hasNext();) {
            Entry<String, Object> entry = it.next();
            sb.append(" ");
View Full Code Here

    private void readProperties(final Resource res) {
        final Map<String, String> props = new HashMap<String, String>();
        if (res != null) {
            final Resource propertiesChild = res.getChild("properties");
            if (propertiesChild != null) {
                final ValueMap properties = propertiesChild.adaptTo(ValueMap.class);
                if (properties != null) {
                    for (Iterator<String> it = properties.keySet().iterator(); it
                            .hasNext();) {
                        String key = it.next();
                        if (!key.equals("jcr:primaryType")) {
                            props.put(key, properties.get(key, String.class));
                        }
                    }
                }
            }
        }
View Full Code Here

        final Resource viewRes = view.getResource();
        if (viewRes == null) {
            throw new IllegalStateException("viewRes must not be null");
        }
        final ValueMap valueMap = viewRes.adaptTo(ValueMap.class);
        if (valueMap == null) {
            throw new IllegalStateException("valueMap must not be null");
        }
        String leaderId = valueMap.get("leaderId", String.class);
        final Resource members = viewRes.getChild("members");
        if (members == null) {
            throw new IllegalStateException("members must not be null");
        }
        final Iterator<Resource> it1 = members
View Full Code Here

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("first", "first-value");
        map.put("third", "third-value");
        map.put("intProperty", new Integer(3));
        map.put("arrayProperty", new String[] { "three", "four" });
        ValueMap vm = new ValueMapDecorator(map);

        Resource res = mock(Resource.class);
        when(res.adaptTo(ValueMap.class)).thenReturn(vm);

        SimplePropertyModel model = factory.getAdapter(res, SimplePropertyModel.class);
View Full Code Here

    @Test
    public void testCreatedNestedModel() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("required", "required");
        ValueMap vm = new ValueMapDecorator(map);
        when(resource.adaptTo(ValueMap.class)).thenReturn(vm);

        NestedModel model = factory.createModel(resource, NestedModel.class);
        Assert.assertNotNull(model);
        Assert.assertEquals("required", model.getNestedModel().getRequired());
View Full Code Here

    @Test(expected=MissingElementsException.class)
    public void testCreatedNestedModelWithMissingElements() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("invalid", "required");
        ValueMap vm = new ValueMapDecorator(map);
        when(resource.adaptTo(ValueMap.class)).thenReturn(vm);

        factory.createModel(resource, NestedModel.class);
    }
View Full Code Here

    private Resource getMockResourceWithProps() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("first", "first-value");
        map.put("third", "third-value");
        ValueMap vm = new ValueMapDecorator(map);

        Resource res = mock(Resource.class);
        when(res.adaptTo(ValueMap.class)).thenReturn(vm);
        return res;
    }
View Full Code Here

TOP

Related Classes of org.apache.sling.api.resource.ValueMap

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.