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