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.compiler.NodeTest;
21  import org.apache.commons.jxpath.ri.model.NodePointer;
22  
23  /**
24   * A Pointer that points to the "lang" attribute of a JavaBean. The value
25   * of the attribute is based on the locale supplied to it in the constructor.
26   *
27   * @author Dmitri Plotnikov
28   * @version $Revision: 652884 $ $Date: 2008-05-02 22:02:00 +0200 (Fr, 02 Mai 2008) $
29   */
30  public class LangAttributePointer extends NodePointer {
31  
32      private static final long serialVersionUID = -8665319197100034134L;
33  
34      /**
35       * Create a new LangAttributePointer.
36       * @param parent parent pointer.
37       */
38      public LangAttributePointer(NodePointer parent) {
39          super(parent);
40      }
41  
42      public QName getName() {
43          return new QName("xml", "lang");
44      }
45  
46      public String getNamespaceURI() {
47          return null;
48      }
49  
50      public boolean isCollection() {
51          return false;
52      }
53  
54      public int getLength() {
55          return 1;
56      }
57  
58      public Object getBaseValue() {
59          return parent.getLocale().toString().replace('_', '-');
60      }
61  
62      public Object getImmediateNode() {
63          return getBaseValue();
64      }
65  
66      public boolean isLeaf() {
67          return true;
68      }
69  
70      /**
71       * {@inheritDoc}
72       *
73       * Throws UnsupportedOperationException.
74       * @param value Object
75       */
76      public void setValue(Object value) {
77          throw new UnsupportedOperationException(
78                  "Cannot change locale using the 'lang' attribute");
79      }
80  
81      public String asPath() {
82          StringBuffer buffer = new StringBuffer();
83          if (parent != null) {
84              buffer.append(parent.asPath());
85              if (buffer.length() == 0
86                  || buffer.charAt(buffer.length() - 1) != '/') {
87                  buffer.append('/');
88              }
89          }
90          buffer.append("@xml:lang");
91          return buffer.toString();
92      }
93  
94      public int hashCode() {
95          return 0;
96      }
97  
98      public boolean equals(Object object) {
99          return object instanceof LangAttributePointer;
100     }
101 
102     public boolean testNode(NodeTest test) {
103         return false;
104     }
105 
106     public int compareChildNodePointers(
107         NodePointer pointer1,
108         NodePointer pointer2) {
109         // Won't happen - lang attributes don't have children
110         return 0;
111     }
112 }