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