View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jxpath.ri.model.beans;
19  
20  import org.apache.commons.jxpath.ri.QName;
21  import org.apache.commons.jxpath.ri.model.NodePointer;
22  
23  /**
24   * An iterator of attributes of a JavaBean. Returns bean properties as well as the "xml:lang" attribute.
25   */
26  public class BeanAttributeIterator extends PropertyIterator {
27  
28      private final NodePointer parent;
29      private int position;
30      private final boolean includeXmlLang;
31  
32      /**
33       * Constructs a new BeanAttributeIterator.
34       *
35       * @param parent parent pointer
36       * @param qName   name of this bean
37       */
38      public BeanAttributeIterator(final PropertyOwnerPointer parent, final QName qName) {
39          super(parent, qName.getPrefix() == null && (qName.getName() == null || qName.getName().equals("*")) ? null : qName.toString(), false, null);
40          this.parent = parent;
41          includeXmlLang = qName.getPrefix() != null && qName.getPrefix().equals("xml") && (qName.getName().equals("lang") || qName.getName().equals("*"));
42      }
43  
44      @Override
45      public NodePointer getNodePointer() {
46          return includeXmlLang && position == 1 ? new LangAttributePointer(parent) : super.getNodePointer();
47      }
48  
49      @Override
50      public int getPosition() {
51          return position;
52      }
53  
54      @Override
55      public boolean setPosition(final int position) {
56          this.position = position;
57          if (includeXmlLang) {
58              return position == 1 || super.setPosition(position - 1);
59          }
60          return super.setPosition(position);
61      }
62  }