001/*
002 *  Copyright 2012 Anyware Services
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
017package org.ametys.core.util.dom;
018
019import java.util.ArrayList;
020import java.util.LinkedHashMap;
021import java.util.Map;
022
023import org.w3c.dom.DOMException;
024import org.w3c.dom.NamedNodeMap;
025import org.w3c.dom.Node;
026
027/**
028 * Implementation of {@link NamedNodeMap} to store attributes names and values.
029 */
030public class AmetysNamedNodeMap implements NamedNodeMap
031{
032    private Map<String, AmetysAttribute> _map = new LinkedHashMap<>();
033    private ArrayList<AmetysAttribute> _list = new ArrayList<>();
034    
035    /**
036     * Contructor.
037     * @param map a &lt;name, attribute&gt; map.
038     */
039    public AmetysNamedNodeMap(Map<String, AmetysAttribute> map)
040    {
041        _map = map;
042        
043        for (String name : map.keySet())
044        {
045            _list.add(map.get(name));
046        }
047    }
048    
049    @Override
050    public Node getNamedItem(String name)
051    {
052        return _map.get(name);
053    }
054
055    @Override
056    public Node item(int index)
057    {
058        if (index < 0 || index > getLength())
059        {
060            return null;
061        }
062        
063        return _list.get(index);
064    }
065
066    @Override
067    public int getLength()
068    {
069        return _map.keySet().size();
070    }
071
072    @Override
073    public Node getNamedItemNS(String namespaceURI, String localName) throws DOMException
074    {
075        return getNamedItem(localName);
076    }
077
078    @Override
079    public Node setNamedItemNS(Node arg) throws DOMException
080    {
081        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setNamedItemNS");
082    }
083
084    @Override
085    public Node removeNamedItemNS(String namespaceURI, String localName) throws DOMException
086    {
087        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "removeNamedItemNS");
088    }
089    
090    @Override
091    public Node setNamedItem(Node arg) throws DOMException
092    {
093        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setNamedItem");
094    }
095
096    @Override
097    public Node removeNamedItem(String name) throws DOMException
098    {
099        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "removeNamedItem");
100    }
101}