001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019package org.crsh.jcr;
020
021import javax.jcr.Node;
022import javax.jcr.Property;
023import javax.jcr.PropertyType;
024import javax.jcr.RepositoryException;
025import javax.jcr.Value;
026import javax.jcr.nodetype.NodeType;
027import javax.jcr.nodetype.PropertyDefinition;
028import java.io.InputStream;
029import java.util.Calendar;
030
031public class JCRUtils {
032
033  public static final int PATH = PropertyType.PATH;
034  public static final int STRING = PropertyType.STRING;
035  public static final int DATE = PropertyType.DATE;
036  public static final int DOUBLE = PropertyType.DOUBLE;
037  public static final int LONG = PropertyType.LONG;
038  public static final int BOOLEAN = PropertyType.BOOLEAN;
039  public static final int REFERENCE = PropertyType.REFERENCE;
040  public static final int BINARY = PropertyType.BINARY;
041
042  private JCRUtils() {
043  }
044
045  public static Property getProperty(Node node, String propertyName) throws RepositoryException {
046    return node.getProperty(propertyName);
047  }
048
049  public static void setProperty(Node node, String propertyName, boolean value) throws RepositoryException {
050    node.setProperty(propertyName, value);
051  }
052
053  public static void setProperty(Node node, String propertyName, Value value) throws RepositoryException {
054    node.setProperty(propertyName, value);
055  }
056
057  public static boolean isJCRPropertyType(Object value) {
058    return value instanceof String ||
059      value instanceof Node ||
060      value instanceof Long ||
061      value instanceof Boolean ||
062      value instanceof Integer ||
063      value instanceof Short ||
064      value instanceof Byte ||
065      value instanceof Float ||
066      value instanceof Double ||
067      value instanceof Calendar ||
068      value instanceof InputStream ||
069      value instanceof Value[];
070  }
071
072  public static String encodeName(String name) {
073    StringBuilder builder = new StringBuilder();
074    for (int i = 0;i < name.length();i++) {
075      char c = name.charAt(i);
076      if (Character.isLetterOrDigit(c)) {
077        builder.append(c);
078      } else {
079        String val = Integer.toString(c);
080        int padding = 4 - val.length();
081        builder.append("_x");
082        while (padding > 0) {
083          builder.append("0");
084        }
085        builder.append(val);
086      }
087    }
088    return builder.toString();
089  }
090
091  public static String decodeName(String name) {
092    throw new UnsupportedOperationException();
093  }
094
095  public static PropertyDefinition getPropertyDefinition(NodeType nodeType, String propertyName) throws RepositoryException {
096    for (PropertyDefinition def : nodeType.getPropertyDefinitions()) {
097      if (def.getName().equals(propertyName)) {
098        return def;
099      }
100    }
101    return null;
102  }
103
104  public static PropertyDefinition findPropertyDefinition(Node node, String propertyName) throws RepositoryException {
105    if (node.hasProperty(propertyName)) {
106      return node.getProperty(propertyName).getDefinition();
107    } else {
108      NodeType primaryNodeType = node.getPrimaryNodeType();
109      PropertyDefinition def = getPropertyDefinition(primaryNodeType, propertyName);
110      if (def == null) {
111        for (NodeType mixinNodeType : node.getMixinNodeTypes()) {
112          def = getPropertyDefinition(mixinNodeType, propertyName);
113          if (def != null) {
114            break;
115          }
116        }
117      }
118      if (def == null && !propertyName.equals("*")) {
119        def = getPropertyDefinition(primaryNodeType, "*");
120      }
121      return def;
122    }
123  }
124}