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 javax.jcr.nodetype.NodeType; 027 import javax.jcr.nodetype.PropertyDefinition; 028 import java.io.InputStream; 029 import java.util.Calendar; 030 031 /** 032 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> 033 * @version $Revision$ 034 */ 035 public class JCRUtils { 036 037 public static final int PATH = PropertyType.PATH; 038 public static final int STRING = PropertyType.STRING; 039 public static final int DATE = PropertyType.DATE; 040 public static final int DOUBLE = PropertyType.DOUBLE; 041 public static final int LONG = PropertyType.LONG; 042 public static final int BOOLEAN = PropertyType.BOOLEAN; 043 public static final int REFERENCE = PropertyType.REFERENCE; 044 public static final int BINARY = PropertyType.BINARY; 045 046 private JCRUtils() { 047 } 048 049 public static Property getProperty(Node node, String propertyName) throws RepositoryException { 050 return node.getProperty(propertyName); 051 } 052 053 public static void setProperty(Node node, String propertyName, boolean value) throws RepositoryException { 054 node.setProperty(propertyName, value); 055 } 056 057 public static void setProperty(Node node, String propertyName, Value value) throws RepositoryException { 058 node.setProperty(propertyName, value); 059 } 060 061 public static boolean isJCRPropertyType(Object value) { 062 return value instanceof String || 063 value instanceof Node || 064 value instanceof Long || 065 value instanceof Boolean || 066 value instanceof Integer || 067 value instanceof Short || 068 value instanceof Byte || 069 value instanceof Float || 070 value instanceof Double || 071 value instanceof Calendar || 072 value instanceof InputStream || 073 value instanceof Value[]; 074 } 075 076 public static String encodeName(String name) { 077 StringBuilder builder = new StringBuilder(); 078 for (int i = 0;i < name.length();i++) { 079 char c = name.charAt(i); 080 if (Character.isLetterOrDigit(c)) { 081 builder.append(c); 082 } else { 083 String val = Integer.toString(c); 084 int padding = 4 - val.length(); 085 builder.append("_x"); 086 while (padding > 0) { 087 builder.append("0"); 088 } 089 builder.append(val); 090 } 091 } 092 return builder.toString(); 093 } 094 095 public static String decodeName(String name) { 096 throw new UnsupportedOperationException(); 097 } 098 099 public static PropertyDefinition getPropertyDefinition(NodeType nodeType, String propertyName) throws RepositoryException { 100 for (PropertyDefinition def : nodeType.getPropertyDefinitions()) { 101 if (def.getName().equals(propertyName)) { 102 return def; 103 } 104 } 105 return null; 106 } 107 108 public static PropertyDefinition findPropertyDefinition(Node node, String propertyName) throws RepositoryException { 109 if (node.hasProperty(propertyName)) { 110 return node.getProperty(propertyName).getDefinition(); 111 } else { 112 NodeType primaryNodeType = node.getPrimaryNodeType(); 113 PropertyDefinition def = getPropertyDefinition(primaryNodeType, propertyName); 114 if (def == null) { 115 for (NodeType mixinNodeType : node.getMixinNodeTypes()) { 116 def = getPropertyDefinition(mixinNodeType, propertyName); 117 if (def != null) { 118 break; 119 } 120 } 121 } 122 if (def == null && !propertyName.equals("*")) { 123 def = getPropertyDefinition(primaryNodeType, "*"); 124 } 125 return def; 126 } 127 } 128 }