    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  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     */
    017    package org.apache.commons.jxpath.ri.model.jdom;
    018
    019    import java.util.ArrayList;
    020    import java.util.Collections;
    021    import java.util.List;
    022
    023    import org.apache.commons.jxpath.ri.QName;
    024    import org.apache.commons.jxpath.ri.model.NodeIterator;
    025    import org.apache.commons.jxpath.ri.model.NodePointer;
    026    import org.jdom.Attribute;
    027    import org.jdom.Element;
    028    import org.jdom.Namespace;
    029
    030    /**
    031     * An iterator of attributes of a DOM Node.
    032     *
    033     * @author Dmitri Plotnikov
    034     * @version $Revision: 652845 $ $Date: 2008-05-02 12:46:46 -0500 (Fri, 02 May 2008) $
    035     */
    036    public class JDOMAttributeIterator implements NodeIterator {
    037        private NodePointer parent;
    038        private List attributes;
    039        private int position = 0;
    040
    041        /**
    042         * Create a new JDOMAttributeIterator.
    043         * @param parent pointer
    044         * @param name test
    045         */
    046        public JDOMAttributeIterator(NodePointer parent, QName name) {
    047            this.parent = parent;
    048            if (parent.getNode() instanceof Element) {
    049                Element element = (Element) parent.getNode();
    050                String prefix = name.getPrefix();
    051                Namespace ns = null;
    052                if (prefix != null) {
    053                    if (prefix.equals("xml")) {
    054                        ns = Namespace.XML_NAMESPACE;
    055                    }
    056                    else {
    057                        String uri = parent.getNamespaceResolver().getNamespaceURI(prefix);
    058                        if (uri != null) {
    059                            ns = Namespace.getNamespace(prefix, uri);
    060                        }
    061                        if (ns == null) {
    062                            // TBD: no attributes
    063                            attributes = Collections.EMPTY_LIST;
    064                            return;
    065                        }
    066                    }
    067                }
    068                else {
    069                    ns = Namespace.NO_NAMESPACE;
    070                }
    071
    072                String lname = name.getName();
    073                if (!lname.equals("*")) {
    074                    attributes = new ArrayList();
    075                    Attribute attr = element.getAttribute(lname, ns);
    076                    if (attr != null) {
    077                        attributes.add(attr);
    078                    }
    079                }
    080                else {
    081                    attributes = new ArrayList();
    082                    List allAttributes = element.getAttributes();
    083                    for (int i = 0; i < allAttributes.size(); i++) {
    084                        Attribute attr = (Attribute) allAttributes.get(i);
    085                        if (ns == Namespace.NO_NAMESPACE
    086                                || attr.getNamespace().equals(ns)) {
    087                            attributes.add(attr);
    088                        }
    089                    }
    090                }
    091            }
    092        }
    093
    094        public NodePointer getNodePointer() {
    095            if (position == 0) {
    096                if (!setPosition(1)) {
    097                    return null;
    098                }
    099                position = 0;
    100            }
    101            int index = position - 1;
    102            if (index < 0) {
    103                index = 0;
    104            }
    105            return new JDOMAttributePointer(
    106                parent,
    107                (Attribute) attributes.get(index));
    108        }
    109
    110        public int getPosition() {
    111            return position;
    112        }
    113
    114        public boolean setPosition(int position) {
    115            if (attributes == null) {
    116                return false;
    117            }
    118            this.position = position;
    119            return position >= 1 && position <= attributes.size();
    120        }
    121    }