001    /*
002     * Copyright 2007 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.beans;
017    
018    import org.springframework.beans.PropertyAccessor;
019    
020    /**
021     * Utility methods for classes that perform bean property access
022     * according to the {@link PropertyAccessor} interface.
023     *
024     * @author Arne Limburg
025     */
026    public abstract class PropertyAccessorUtils extends org.springframework.beans.PropertyAccessorUtils {
027    
028        /**
029         * Returns the path to the parent component of the provided property path.
030         */
031       public static String getParentPropertyPath(String propertyPath) {
032           int propertySeparatorIndex = getLastNestedPropertySeparatorIndex(propertyPath);
033           return propertySeparatorIndex == -1? "": propertyPath.substring(0, propertySeparatorIndex);
034       }
035    
036       /**
037        * Returns the last component of the specified property path
038        */
039       public static String getPropertyName(String propertyPath) {
040           int propertySeparatorIndex = PropertyAccessorUtils.getLastNestedPropertySeparatorIndex(propertyPath);
041           return propertySeparatorIndex == -1? propertyPath: propertyPath.substring(propertySeparatorIndex + 1);
042       }
043    
044       /**
045        * Tests whether the specified property path denotes an indexed property.
046        */
047       public static boolean isIndexedProperty(String propertyName) {
048           return propertyName.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR) != -1
049               && propertyName.charAt(propertyName.length() - 1) == PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR;
050       }
051    
052       public static boolean isNestedProperty(String propertyPath) {
053           return getFirstNestedPropertySeparatorIndex(propertyPath) != -1;
054       }
055    
056       public static int getNestingLevel(String propertyName) {
057           propertyName = getPropertyName(propertyName);
058           int nestingLevel = 0;
059           boolean inKey = false;
060           for (int i = 0; i < propertyName.length(); i++) {
061               switch (propertyName.charAt(i)) {
062               case PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR:
063                   if (!inKey) {
064                       nestingLevel++;
065                   }
066               case PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR:
067                   inKey = !inKey;
068               }
069           }
070           return nestingLevel;
071       }
072    }