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
018package org.apache.commons.jxpath.ri.model.beans;
019
020import org.apache.commons.jxpath.ri.QName;
021import org.apache.commons.jxpath.ri.model.NodePointer;
022
023/**
024 * An iterator of attributes of a JavaBean. Returns bean properties as well as the "xml:lang" attribute.
025 */
026public class BeanAttributeIterator extends PropertyIterator {
027
028    private final NodePointer parent;
029    private int position;
030    private final boolean includeXmlLang;
031
032    /**
033     * Constructs a new BeanAttributeIterator.
034     *
035     * @param parent parent pointer
036     * @param qName   name of this bean
037     */
038    public BeanAttributeIterator(final PropertyOwnerPointer parent, final QName qName) {
039        super(parent, qName.getPrefix() == null && (qName.getName() == null || qName.getName().equals("*")) ? null : qName.toString(), false, null);
040        this.parent = parent;
041        includeXmlLang = qName.getPrefix() != null && qName.getPrefix().equals("xml") && (qName.getName().equals("lang") || qName.getName().equals("*"));
042    }
043
044    @Override
045    public NodePointer getNodePointer() {
046        return includeXmlLang && position == 1 ? new LangAttributePointer(parent) : super.getNodePointer();
047    }
048
049    @Override
050    public int getPosition() {
051        return position;
052    }
053
054    @Override
055    public boolean setPosition(final int position) {
056        this.position = position;
057        if (includeXmlLang) {
058            return position == 1 || super.setPosition(position - 1);
059        }
060        return super.setPosition(position);
061    }
062}