001    /*--------------------------------------------------------------------------+
002    $Id: LowercaseResolver.java 26268 2010-02-18 10:44:30Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.xml;
019    
020    /**
021     * XML resolver which transforms the enum names by making them lower case and
022     * replacing underscores by dashes.
023     * 
024     * @author hummelb
025     * @author $Author: juergens $
026     * @version $Rev: 26268 $
027     * @levd.rating GREEN Hash: DE8C230CA712D8A3523A39D0A0362F05
028     */
029    public class LowercaseResolver<E extends Enum<E>, A extends Enum<A>> implements
030                    IXMLResolver<E, A> {
031    
032            /** Class object for attribute enum. */
033            private final Class<A> attributeClass;
034    
035            /**
036             * Create new resolver.
037             * 
038             * @param attributeClass
039             *            class object for attribute enum.
040             */
041            public LowercaseResolver(Class<A> attributeClass) {
042                    this.attributeClass = attributeClass;
043            }
044    
045            /** {@inheritDoc} */
046            public Class<A> getAttributeClass() {
047                    return attributeClass;
048            }
049    
050            /** {@inheritDoc} */
051            public String resolveAttributeName(A attribute) {
052                    return transform(attribute.name());
053            }
054    
055            /** {@inheritDoc} */
056            public String resolveElementName(E element) {
057                    return transform(element.name());
058            }
059    
060            /** Performs the name transformation. */
061            private String transform(String name) {
062                    return name.toLowerCase().replace('_', '-');
063            }
064    
065    }