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 */
017package org.apache.commons.jxpath.ri.model.jdom;
018
019import java.util.ArrayList;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Set;
023
024import org.apache.commons.jxpath.ri.model.NodeIterator;
025import org.apache.commons.jxpath.ri.model.NodePointer;
026import org.jdom.Document;
027import org.jdom.Element;
028import org.jdom.Namespace;
029
030/**
031 * An iterator of namespaces of a DOM Node.
032 *
033 * @author Dmitri Plotnikov
034 * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
035 */
036public class JDOMNamespaceIterator implements NodeIterator {
037    private NodePointer parent;
038    private List namespaces;
039    private Set prefixes;
040    private int position = 0;
041
042    /**
043     * Create a new JDOMNamespaceIterator.
044     * @param parent the parent NodePointer.
045     */
046    public JDOMNamespaceIterator(NodePointer parent) {
047        this.parent = parent;
048        Object node = parent.getNode();
049        if (node instanceof Document) {
050            node = ((Document) node).getRootElement();
051        }
052        if (node instanceof Element) {
053            namespaces = new ArrayList();
054            prefixes = new HashSet();
055            collectNamespaces((Element) node);
056        }
057    }
058
059    /**
060     * Collect the namespaces from a JDOM Element.
061     * @param element the source Element
062     */
063    private void collectNamespaces(Element element) {
064        Namespace ns = element.getNamespace();
065        if (ns != null && !prefixes.contains(ns.getPrefix())) {
066            namespaces.add(ns);
067            prefixes.add(ns.getPrefix());
068        }
069        List others = element.getAdditionalNamespaces();
070        for (int i = 0; i < others.size(); i++) {
071            ns = (Namespace) others.get(i);
072            if (ns != null && !prefixes.contains(ns.getPrefix())) {
073                namespaces.add(ns);
074                prefixes.add(ns.getPrefix());
075            }
076        }
077        Object elementParent = element.getParent();
078        if (elementParent instanceof Element) {
079            collectNamespaces((Element) elementParent);
080        }
081    }
082
083    public NodePointer getNodePointer() {
084        if (position == 0) {
085            if (!setPosition(1)) {
086                return null;
087            }
088            position = 0;
089        }
090        int index = position - 1;
091        if (index < 0) {
092            index = 0;
093        }
094        Namespace ns = (Namespace) namespaces.get(index);
095        return new JDOMNamespacePointer(parent, ns.getPrefix(), ns.getURI());
096    }
097
098    public int getPosition() {
099        return position;
100    }
101
102    public boolean setPosition(int position) {
103        if (namespaces == null) {
104            return false;
105        }
106        this.position = position;
107        return position >= 1 && position <= namespaces.size();
108    }
109}