001    /*
002     * Copyright 2002-2004 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.rules.reporting;
017    
018    import org.springframework.util.Assert;
019    
020    public class DefaultBeanPropertyNameRenderer implements BeanPropertyNameRenderer {
021    
022            /**
023             * Renders a unqualified bean property string in the format:
024             * "parentProperty.myBeanProperty" like "ParentProperty.MyBeanProperty".
025             * @param qualifiedName
026             * @return the formatted string
027             */
028            public String renderQualifiedName(String qualifiedName) {
029                    Assert.notNull(qualifiedName, "No qualified name specified");
030                    StringBuffer sb = new StringBuffer(qualifiedName.length() + 5);
031                    char[] chars = qualifiedName.toCharArray();
032                    sb.append(Character.toUpperCase(chars[0]));
033                    boolean foundDot = false;
034                    for (int i = 1; i < chars.length; i++) {
035                            char c = chars[i];
036                            if (Character.isLetter(c)) {
037                                    if (Character.isLowerCase(c)) {
038                                            if (foundDot) {
039                                                    sb.append(Character.toUpperCase(c));
040                                                    foundDot = false;
041                                            }
042                                            else {
043                                                    sb.append(c);
044                                            }
045                                    }
046                                    else {
047                                            sb.append(c);
048                                    }
049                            }
050                            else if (c == '.') {
051                                    sb.append(c);
052                                    foundDot = true;
053                            }
054                    }
055                    return sb.toString();
056            }
057    
058            /**
059             * Renders a unqualified bean property string in the format:
060             * "myBeanProperty" like "My Bean Property".
061             * @return the formatted string
062             */
063            public String renderShortName(String shortName) {
064                    Assert.notNull(shortName, "No short name specified");
065                    StringBuffer sb = new StringBuffer(shortName.length() + 5);
066                    char[] chars = shortName.toCharArray();
067                    sb.append(Character.toUpperCase(chars[0]));
068                    for (int i = 1; i < chars.length; i++) {
069                            char c = chars[i];
070                            if (Character.isUpperCase(c)) {
071                                    sb.append(' ');
072                            }
073                            sb.append(c);
074                    }
075                    return sb.toString();
076            }
077    }