001    /*
002     * Copyright 2002 - 2007 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: DOMAttributeMap.java 602 2008-01-24 13:34:51Z maxberger $ */
018    
019    package net.sourceforge.jeuclid.elements.support.attributes;
020    
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import org.w3c.dom.NamedNodeMap;
025    import org.w3c.dom.Node;
026    
027    /**
028     * Attributes derived from DOM.
029     * 
030     * @author Max Berger
031     * @version $Revision: 602 $
032     */
033    public class DOMAttributeMap extends AbstractAttributeMap {
034    
035        private final NamedNodeMap namedNodeMap;
036    
037        /**
038         * Creates a new AttributesMap based on DOM attributes.
039         * 
040         * @param attributeMap
041         *            the DOM attributes.
042         */
043        public DOMAttributeMap(final NamedNodeMap attributeMap) {
044            this.namedNodeMap = attributeMap;
045        }
046    
047        /** {@inheritDoc} */
048        @Override
049        protected String getAttribute(final String attrName) {
050            final Node valueNode = this.namedNodeMap.getNamedItem(attrName);
051            if (valueNode == null) {
052                return null;
053            } else {
054                return valueNode.getNodeValue();
055            }
056        }
057    
058        /** {@inheritDoc} */
059        @Override
060        protected String getAttributeNS(final String namespace,
061                final String attrName) {
062            final Node valueNode = this.namedNodeMap.getNamedItemNS(namespace,
063                    attrName);
064            if (valueNode == null) {
065                return null;
066            } else {
067                return valueNode.getNodeValue();
068            }
069        }
070    
071        /** {@inheritDoc} */
072        public Map<String, String> getAsMap() {
073            final Map<String, String> m = new HashMap<String, String>();
074            for (int i = 0; i < this.namedNodeMap.getLength(); i++) {
075                final Node n = this.namedNodeMap.item(i);
076                final String localName = n.getLocalName();
077                if (localName != null) {
078                    m.put(localName, n.getNodeValue());
079                } else {
080                    m.put(n.getNodeName(), n.getNodeValue());
081                }
082            }
083            return m;
084        }
085    
086        /** {@inheritDoc} */
087        @Override
088        public String toString() {
089            return this.getAsMap().toString();
090        }
091    
092    }