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  package org.apache.commons.jxpath.ri.model.beans;
18  
19  import org.apache.commons.jxpath.ri.QName;
20  import org.apache.commons.jxpath.ri.model.NodePointer;
21  
22  /**
23   * An iterator of attributes of a JavaBean. Returns bean properties as
24   * well as the "xml:lang" attribute.
25   *
26   * @author Dmitri Plotnikov
27   * @version $Revision: 652845 $ $Date: 2008-05-02 13:46:46 -0400 (Fri, 02 May 2008) $
28   */
29  public class BeanAttributeIterator extends PropertyIterator {
30      private NodePointer parent;
31      private int position = 0;
32      private boolean includeXmlLang;
33  
34      /**
35       * Create a new BeanAttributeIterator.
36       * @param parent parent pointer
37       * @param name name of this bean
38       */
39      public BeanAttributeIterator(PropertyOwnerPointer parent, QName name) {
40          super(
41              parent,
42              (name.getPrefix() == null
43                  && (name.getName() == null || name.getName().equals("*")))
44                  ? null
45                  : name.toString(),
46              false,
47              null);
48          this.parent = parent;
49          includeXmlLang =
50              (name.getPrefix() != null && name.getPrefix().equals("xml"))
51                  && (name.getName().equals("lang")
52                  || name.getName().equals("*"));
53      }
54  
55      public NodePointer getNodePointer() {
56          return includeXmlLang && position == 1 ? new LangAttributePointer(parent) : super.getNodePointer();
57      }
58  
59      public int getPosition() {
60          return position;
61      }
62  
63      public boolean setPosition(int position) {
64          this.position = position;
65          if (includeXmlLang) {
66              return position == 1 || super.setPosition(position - 1);
67          }
68          return super.setPosition(position);
69      }
70  }