001    /*
002     * Copyright 2002 - 2006 JEuclid, http://jeuclid.sf.net
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of 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,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: AbstractAttributeMap.java 310 2007-05-18 20:26:36Z maxberger $ */
018    
019    package net.sourceforge.jeuclid.elements.support.attributes;
020    
021    /**
022     * Generic class for reading and parsing attributes.
023     * 
024     * @author Max Berger
025     * @version $Revision: 310 $
026     */
027    public abstract class AbstractAttributeMap implements AttributeMap {
028    
029        private static final String MATHMLNAMESPACE = "http://www.w3.org/1998/Math/MathML";
030    
031        /**
032         * Override this method to get an attribute value without a namespace.
033         * 
034         * @param attrName
035         *            name of the attribute
036         * @return value of the attribute
037         */
038        protected abstract String getAttribute(String attrName);
039    
040        /**
041         * Override this method to get an attribtue with a namespace.
042         * 
043         * @param namespace
044         *            the namespace
045         * @param attrName
046         *            the attribtue name
047         * @return value of the attribute.
048         */
049        protected abstract String getAttributeNS(String namespace, String attrName);
050    
051        /** {@inheritDoc} */
052        public boolean hasAttribute(final String attrName) {
053            return this.getString(attrName) != null;
054        }
055    
056        /** {@inheritDoc} */
057        public String getString(final String attrName) {
058            String attrValue = this.getAttributeNS(
059                    AbstractAttributeMap.MATHMLNAMESPACE, attrName);
060            if (attrValue == null) {
061                attrValue = this.getAttribute(attrName);
062            }
063            return attrValue;
064        }
065    
066        /** {@inheritDoc} */
067        public String getString(final String attrName, final String defaultValue) {
068            String attrValue = this.getString(attrName);
069            if (attrValue == null) {
070                attrValue = defaultValue;
071            }
072            return attrValue;
073        }
074    
075        /** {@inheritDoc} */
076        public boolean getBoolean(final String attrName,
077                final boolean defaultValue) {
078            final String strValue = this.getString(attrName, Boolean
079                    .toString(defaultValue));
080            return Boolean.parseBoolean(strValue);
081        }
082    
083    }