Author: bdelacretaz
Date: Tue May  3 08:25:01 2016
New Revision: 1742070

URL: http://svn.apache.org/viewvc?rev=1742070&view=rev
Log:
SLING-5707 - Add fromResource to HapiUtil and deprecate fromNode - contributed 
by Andrei Dulvac, thanks \!

Modified:
    sling/trunk/contrib/extensions/hapi/core/pom.xml
    
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/HApiUtil.java
    
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
    
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
    
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/package-info.java

Modified: sling/trunk/contrib/extensions/hapi/core/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/core/pom.xml?rev=1742070&r1=1742069&r2=1742070&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/hapi/core/pom.xml (original)
+++ sling/trunk/contrib/extensions/hapi/core/pom.xml Tue May  3 08:25:01 2016
@@ -28,7 +28,7 @@
     </parent>
 
     <artifactId>org.apache.sling.hapi</artifactId>
-    <version>1.0.1-SNAPSHOT</version>
+    <version>2.0.0-SNAPSHOT</version>
     <packaging>bundle</packaging>
 
     <name>HApi - Sling Hypermedia API tools</name>
@@ -125,7 +125,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.scripting.sightly</artifactId>
-            <version>1.0.0</version>
+            <version>1.0.18</version>
             <scope>provided</scope>
         </dependency>
     </dependencies>

Modified: 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/HApiUtil.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/HApiUtil.java?rev=1742070&r1=1742069&r2=1742070&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/HApiUtil.java
 (original)
+++ 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/HApiUtil.java
 Tue May  3 08:25:01 2016
@@ -22,6 +22,7 @@ package org.apache.sling.hapi;
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 
+import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 
 public interface HApiUtil {
@@ -29,7 +30,7 @@ public interface HApiUtil {
     String DEFAULT_RESOURCE_TYPE = "sling/hapi/components/type";
 
     /**
-     * <p>Get a HApi type object from a type identifier.</p>
+     * <p>Get a HApi type jcr node from a type identifier.</p>
      * <p>The JCR node must be [nt:unstructured], a descendant of any of the 
HAPi search path defined by the
      * {@see HAPI_PATHS} config and the sling:resourceType should be set to 
the value defined by the {@see HAPI_RESOURCE_TYPE} config</p>
      * <p>The first result is returned</p>
@@ -40,10 +41,25 @@ public interface HApiUtil {
      * @return The first node that matches that type or null if none is found.
      * @throws RepositoryException
      */
+    @Deprecated
     Node getTypeNode(ResourceResolver resolver, String type) throws 
RepositoryException;
 
 
     /**
+     * <p>Get a HApi type Resource from a type identifier.</p>
+     * <p>The Resource must be [nt:unstructured], a descendant of any of the 
HAPi search path defined by the
+     * {@see HAPI_PATHS} config and the sling:resourceType should be set to 
the value defined by the {@see HAPI_RESOURCE_TYPE} config</p>
+     * <p>The first result is returned</p>
+     * @param resolver The sling resource resolver object
+     * @param type The type identifier, which is either in the form of a jcr 
path,
+     *             same as the path for {@link: 
ResourceResolver#getResource(String)}. If the path cannot be resolved, type is 
treated like
+     *             a fully qualified domain name, which has to match the 
"fqdn" property on the Resource which represents the type.
+     * @return The first Resource that matches that type or null if none is 
found.
+     * @throws RepositoryException
+     */
+    Resource getTypeResource(ResourceResolver resolver, String type) throws 
RepositoryException;
+
+    /**
      * <p>Get a HApi type object from a type identifier.</p>
      * <p>The type identifier is resolved to a {@link javax.jcr.Node} and then
      * {@link #fromNode(org.apache.sling.api.resource.ResourceResolver, 
javax.jcr.Node)} is called.</p>
@@ -86,9 +102,40 @@ public interface HApiUtil {
      * @return The HApiType
      * @throws RepositoryException
      */
+    @Deprecated
     HApiType fromNode(ResourceResolver resolver, Node typeNode) throws 
RepositoryException;
 
     /**
+     * <p>Get a HApi type object from the {@link 
org.apache.sling.api.resource.Resource}.</p>
+     * The Resource has the following properties:
+     * <ul>
+     *     <li>name: A 'Name' of the type (mandatory)</li>
+     *     <li>description: A 'String' with the description text for this type 
(mandatory)</li>
+     *     <li>fqdn: A 'String' with the fully qualified domain name; A 
namespace like a java package (mandatory)</li>
+     *     <li>extends: A type identifier (either a path or a fqdn); 
(optional). This defines the parent type of this type</li>
+     *     <li>parameter: A multivalue property to define a list of java-like 
generic types
+     *     that can be used as types for properties; (optional)</li>
+     * </ul>
+     *
+     * <p>The properties of this type are defined as children resources.</p>
+     * <p>The name of property resource defines the name of the property for 
this type. </p>
+     * The children property nodes have the following properties:
+     * <ul>
+     *     <li>type: The type identifier (mandatory). Can be of type 'Name' or 
'Path'
+     *      See {@link 
HApiUtil#getTypeNode(org.apache.sling.api.resource.ResourceResolver, String)}
+     *      for the format of this value</li>
+     *     <li>description: A 'String' with the description for this property 
(mandatory)</li>
+     *     <li>multiple: A 'Boolean' that defines whether this property can 
exist multiple times on an object of this type (optional)</li>
+     * </ul>
+     *
+     * @param resolver The resource resolver
+     * @param typeResource The sling Resource of the HApi type
+     * @return The HApiType
+     * @throws RepositoryException
+     */
+    HApiType fromResource(ResourceResolver resolver, Resource typeResource) 
throws RepositoryException;
+
+    /**
      * Get a new instance of AttributeHelper for the type identified by 'type'
      * @param resolver
      * @param type See {@link 
#getTypeNode(org.apache.sling.api.resource.ResourceResolver, String)}

Modified: 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java?rev=1742070&r1=1742069&r2=1742070&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
 (original)
+++ 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
 Tue May  3 08:25:01 2016
@@ -166,4 +166,9 @@ public class HApiTypeImpl implements HAp
     public void setProperties(Map<String, HApiProperty> properties) {
         this.properties = properties;
     }
+
+    @Override
+    public String toString() {
+        return this.getName() + ": Properties: " + this.getProperties();
+    }
 }

Modified: 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java?rev=1742070&r1=1742069&r2=1742070&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
 (original)
+++ 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
 Tue May  3 08:25:01 2016
@@ -22,7 +22,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -41,6 +40,7 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.apache.sling.hapi.HApiProperty;
 import org.apache.sling.hapi.HApiType;
@@ -83,14 +83,23 @@ public class HApiUtilImpl implements HAp
      * {@inheritDoc}
      */
     @Override
+    @Deprecated
     public Node getTypeNode(ResourceResolver resolver, String type) throws 
RepositoryException {
+        return getTypeResource(resolver, type).adaptTo(Node.class);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Resource getTypeResource(ResourceResolver resolver, String type) 
throws RepositoryException {
         Session session = resolver.adaptTo(Session.class);
 
         // Try to resolve the resource as a path
         Resource res = resolver.getResource(type);
         if (null != res) {
             LOG.debug("res = " + res.getName() + " " + res.getPath());
-            return res.adaptTo(Node.class);
+            return res;
         } else {
             for (String path : new HashSet<String>(Arrays.asList(hApiPaths))) {
                 // Remove trailing slash from path
@@ -111,7 +120,7 @@ public class HApiUtilImpl implements HAp
 
                 NodeIterator nodeIter = result.getNodes();
                 if (nodeIter.hasNext()) {
-                    return nodeIter.nextNode();
+                    return resolver.getResource(nodeIter.nextNode().getPath());
                 } else {
                     // continue
                 }
@@ -128,12 +137,12 @@ public class HApiUtilImpl implements HAp
      */
     @Override
     public HApiType fromPath(ResourceResolver resolver, String type) throws 
RepositoryException {
-        Node typeNode = this.getTypeNode(resolver, type);
-        LOG.debug("typeNode=" + typeNode);
-        if (null == typeNode) {
+        Resource typeResource = this.getTypeResource(resolver, type);
+        LOG.debug("typeResource=" + typeResource);
+        if (null == typeResource) {
             return new AbstractHapiTypeImpl(type);
         } else {
-            return fromNode(resolver, typeNode);
+            return fromResource(resolver, typeResource);
         }
     }
 
@@ -141,15 +150,26 @@ public class HApiUtilImpl implements HAp
      * {@inheritDoc}
      */
     @Override
+    @Deprecated
     public HApiType fromNode(ResourceResolver resolver, Node typeNode) throws 
RepositoryException {
         if (null == typeNode) return null;
-        String name = typeNode.getProperty("name").getValue().getString();
-        String description = 
typeNode.getProperty("description").getValue().getString();
-        String path = typeNode.getPath();
-        String fqdn = typeNode.getProperty("fqdn").getValue().getString();
+        Resource resource = resolver.getResource(typeNode.getPath());
+        return fromResource(resolver, resource);
+    }
 
+    /**
+     * {@inheritDoc}
+     */
+    public HApiType fromResource(ResourceResolver resolver, Resource 
typeResource) throws RepositoryException {
+        if (null == typeResource) return null;
+
+        ValueMap resProps = typeResource.adaptTo(ValueMap.class);
+        String name = resProps.get("name", (String) null);
+        String description = resProps.get("description", (String) null);
+        String path = typeResource.getPath();
+        String fqdn = resProps.get("fqdn", (String) null);
         // get parameters
-        Value[] parameterValues = typeNode.hasProperty("parameters") ? 
typeNode.getProperty("parameters").getValues() : new Value[]{};
+        Value[] parameterValues = resProps.get("parameters", new Value[]{});
         List<String> parameters = new 
ArrayList<String>(parameterValues.length);
 
         for (Value p : Arrays.asList(parameterValues)) {
@@ -158,31 +178,30 @@ public class HApiUtilImpl implements HAp
         HApiTypeImpl newType = new HApiTypeImpl(name, description, 
serverContextPath, path, fqdn, parameters, null, null, false);
         TypesCache.getInstance(this).addType(newType);
 
+
         try {
-            // get parent if it exists
+            // Get parent if it exists
             HApiType parent = null;
-            String parentPath = typeNode.hasProperty("extends") ? 
typeNode.getProperty("extends").getString() : null;
+            String parentPath = resProps.get("extends", (String) null);
             if (null != parentPath) {
                 parent = TypesCache.getInstance(this).getType(resolver, 
parentPath);
             }
 
             // Get properties
             Map<String, HApiProperty> properties = new HashMap<String, 
HApiProperty>();
+            for (Resource res : typeResource.getChildren()) {
+                ValueMap resValueMap = res.adaptTo(ValueMap.class);
 
-            // Add the properties from this node
-            Iterator<Node> it = typeNode.getNodes();
-            while (it.hasNext()) {
-                Node propNode = it.next();
-                String propName = propNode.getName();
-                String propDescription = propNode.hasProperty("description") ? 
propNode.getProperty("description").getString() : "";
-
-                String typePath = 
propNode.getProperty("type").getValue().getString();
+                String propName = res.getName();
+                String propDescription = resValueMap.get("description", "");
+                String typePath = resValueMap.get("type", (String) null);
                 HApiType propType = 
TypesCache.getInstance(this).getType(resolver, typePath);
-                Boolean propMultiple = propNode.hasProperty("multiple") ? 
propNode.getProperty("multiple").getBoolean() : false;
+                Boolean propMultiple = resValueMap.get("multiple", false);
 
                 HApiProperty prop = new HApiPropertyImpl(propName, 
propDescription, propType, propMultiple);
                 properties.put(prop.getName(), prop);
             }
+            // Set parent and properties
             newType.setParent(parent);
             newType.setProperties(properties);
 
@@ -196,6 +215,7 @@ public class HApiUtilImpl implements HAp
             throw e;
         }
 
+        LOG.debug("Created type {}", newType);
         return newType;
     }
 

Modified: 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/package-info.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/package-info.java?rev=1742070&r1=1742069&r2=1742070&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/package-info.java
 (original)
+++ 
sling/trunk/contrib/extensions/hapi/core/src/main/java/org/apache/sling/hapi/package-info.java
 Tue May  3 08:25:01 2016
@@ -17,7 +17,7 @@
  * under the License.
  
******************************************************************************/
 
-@Version("1.0.0")
+@Version("2.0.0")
 package org.apache.sling.hapi;
 
 import aQute.bnd.annotation.Version;


Reply via email to