001    package com.khubla.pragmatach.plugin.jcr;
002    
003    import javax.jcr.Node;
004    import javax.jcr.NodeIterator;
005    import javax.jcr.Property;
006    import javax.jcr.PropertyIterator;
007    import javax.jcr.Value;
008    
009    import org.json.JSONObject;
010    
011    import com.khubla.pragmatach.framework.api.PragmatachException;
012    
013    /**
014     * @author tome
015     */
016    public class PragmatachJCRToJSON {
017       /**
018        * Pretty simple; render all the properties to JSON
019        */
020       public static String renderNodeProperties(Node node) throws PragmatachException {
021          try {
022             /*
023              * json
024              */
025             final JSONObject jSONObject = new JSONObject();
026             /*
027              * walk the properties
028              */
029             final PropertyIterator propertyIterator = node.getProperties();
030             while (propertyIterator.hasNext()) {
031                /*
032                 * property
033                 */
034                final Property property = propertyIterator.nextProperty();
035                /*
036                 * name
037                 */
038                final String propertyName = property.getName();
039                if (false == property.isMultiple()) {
040                   jSONObject.put(propertyName, property.getValue().getString());
041                } else {
042                   final JSONObject containedObject = new JSONObject();
043                   int i = 0;
044                   for (final Value v : property.getValues()) {
045                      containedObject.put(Integer.toString(i++), v.getString());
046                   }
047                   jSONObject.put(propertyName, containedObject);
048                }
049             }
050             return jSONObject.toString();
051          } catch (final Exception e) {
052             throw new PragmatachException("Exception in render", e);
053          }
054       }
055    
056       /**
057        * Pretty simple; render all the subnode names to JSON
058        */
059       public static String renderSubnodeNames(Node node) throws PragmatachException {
060          try {
061             /*
062              * json
063              */
064             final JSONObject jSONObject = new JSONObject();
065             /*
066              * walk the nodes
067              */
068             final NodeIterator nodeIterator = node.getNodes();
069             while (nodeIterator.hasNext()) {
070                /*
071                 * node
072                 */
073                final Node subNode = nodeIterator.nextNode();
074                /*
075                 * add
076                 */
077                jSONObject.put(subNode.getName(), subNode.getPath());
078             }
079             return jSONObject.toString();
080          } catch (final Exception e) {
081             throw new PragmatachException("Exception in render", e);
082          }
083       }
084    }