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.beans;
018
019import org.apache.commons.jxpath.ri.QName;
020import org.apache.commons.jxpath.ri.model.NodePointer;
021
022/**
023 * An iterator of attributes of a JavaBean. Returns bean properties as
024 * well as the "xml:lang" attribute.
025 *
026 * @author Dmitri Plotnikov
027 * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
028 */
029public class BeanAttributeIterator extends PropertyIterator {
030    private NodePointer parent;
031    private int position = 0;
032    private boolean includeXmlLang;
033
034    /**
035     * Create a new BeanAttributeIterator.
036     * @param parent parent pointer
037     * @param name name of this bean
038     */
039    public BeanAttributeIterator(PropertyOwnerPointer parent, QName name) {
040        super(
041            parent,
042            (name.getPrefix() == null
043                && (name.getName() == null || name.getName().equals("*")))
044                ? null
045                : name.toString(),
046            false,
047            null);
048        this.parent = parent;
049        includeXmlLang =
050            (name.getPrefix() != null && name.getPrefix().equals("xml"))
051                && (name.getName().equals("lang")
052                || name.getName().equals("*"));
053    }
054
055    public NodePointer getNodePointer() {
056        return includeXmlLang && position == 1 ? new LangAttributePointer(parent) : super.getNodePointer();
057    }
058
059    public int getPosition() {
060        return position;
061    }
062
063    public boolean setPosition(int position) {
064        this.position = position;
065        if (includeXmlLang) {
066            return position == 1 || super.setPosition(position - 1);
067        }
068        return super.setPosition(position);
069    }
070}