View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jxpath.ri.model.beans;
17  
18  import org.apache.commons.jxpath.ri.QName;
19  import org.apache.commons.jxpath.ri.model.NodePointer;
20  
21  /***
22   * An iterator of attributes of a JavaBean. Returns bean properties as
23   * well as the "xml:lang" attribute.
24   *
25   * @author Dmitri Plotnikov
26   * @version $Revision: 1.8 $ $Date: 2004/02/29 14:17:41 $
27   */
28  public class BeanAttributeIterator extends PropertyIterator {
29      private NodePointer parent;
30      private int position = 0;
31      private boolean includeXmlLang;
32  
33      public BeanAttributeIterator(PropertyOwnerPointer parent, QName name) {
34          super(
35              parent,
36              (name.getPrefix() == null
37                  && (name.getName() == null || name.getName().equals("*")))
38                  ? null
39                  : name.toString(),
40              false,
41              null);
42          this.parent = parent;
43          includeXmlLang =
44              (name.getPrefix() != null && name.getPrefix().equals("xml"))
45                  && (name.getName().equals("lang") 
46                  || name.getName().equals("*"));
47      }
48  
49      public NodePointer getNodePointer() {
50          if (includeXmlLang && position == 1) {
51              return new LangAttributePointer(parent);
52          }
53          else {
54              return super.getNodePointer();
55          }
56      }
57  
58      public int getPosition() {
59          return position;
60      }
61  
62      public boolean setPosition(int position) {
63          this.position = position;
64          if (includeXmlLang) {
65              if (position == 1) {
66                  return true;
67              }
68              else {
69                  return super.setPosition(position - 1);
70              }
71          }
72          else {
73              this.position = position;
74              return super.setPosition(position);
75          }
76      }
77  }