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