001    /*
002     * Copyright (C) 2003-2009 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     */
019    package org.crsh.jcr;
020    
021    import javax.jcr.Node;
022    import javax.jcr.Property;
023    import javax.jcr.PropertyType;
024    import javax.jcr.RepositoryException;
025    import javax.jcr.Value;
026    import java.io.InputStream;
027    import java.util.Calendar;
028    
029    /**
030     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
031     * @version $Revision$
032     */
033    public class JCRUtils {
034    
035      public static final int PATH = PropertyType.PATH;
036      public static final int STRING = PropertyType.STRING;
037      public static final int DATE = PropertyType.DATE;
038      public static final int DOUBLE = PropertyType.DOUBLE;
039      public static final int LONG = PropertyType.LONG;
040      public static final int BOOLEAN = PropertyType.BOOLEAN;
041      public static final int REFERENCE = PropertyType.REFERENCE;
042      public static final int BINARY = PropertyType.BINARY;
043    
044      private JCRUtils() {
045      }
046    
047      public static Property getProperty(Node node, String propertyName) throws RepositoryException {
048        return node.getProperty(propertyName);
049      }
050    
051      public static void setProperty(Node node, String propertyName, boolean value) throws RepositoryException {
052        node.setProperty(propertyName, value);
053      }
054    
055      public static void setProperty(Node node, String propertyName, Value value) throws RepositoryException {
056        node.setProperty(propertyName, value);
057      }
058    
059      public static boolean isJCRPropertyType(Object value) {
060        return value instanceof String ||
061          value instanceof Node ||
062          value instanceof Long ||
063          value instanceof Boolean ||
064          value instanceof Integer ||
065          value instanceof Short ||
066          value instanceof Byte ||
067          value instanceof Float ||
068          value instanceof Double ||
069          value instanceof Calendar ||
070          value instanceof InputStream ||
071          value instanceof Value[];
072      }
073    
074      public static String encodeName(String name) {
075        StringBuilder builder = new StringBuilder();
076        for (int i = 0;i < name.length();i++) {
077          char c = name.charAt(i);
078          if (Character.isLetterOrDigit(c)) {
079            builder.append(c);
080          } else {
081            String val = Integer.toString(c);
082            int padding = 4 - val.length();
083            builder.append("_x");
084            while (padding > 0) {
085              builder.append("0");
086            }
087            builder.append(val);
088          }
089        }
090        return builder.toString();
091      }
092    
093      public static String decodeName(String name) {
094        throw new UnsupportedOperationException();
095      }
096    }