001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.jxpath.ri.model.beans;
019
020import org.apache.commons.jxpath.ri.QName;
021import org.apache.commons.jxpath.ri.compiler.NodeTest;
022import org.apache.commons.jxpath.ri.model.NodePointer;
023
024/**
025 * 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.
026 */
027public class LangAttributePointer extends NodePointer {
028
029    private static final long serialVersionUID = -8665319197100034134L;
030
031    /**
032     * Constructs a new LangAttributePointer.
033     *
034     * @param parent parent pointer.
035     */
036    public LangAttributePointer(final NodePointer parent) {
037        super(parent);
038    }
039
040    @Override
041    public String asPath() {
042        final StringBuilder buffer = new StringBuilder();
043        if (parent != null) {
044            buffer.append(parent.asPath());
045            if (buffer.length() == 0 || buffer.charAt(buffer.length() - 1) != '/') {
046                buffer.append('/');
047            }
048        }
049        buffer.append("@xml:lang");
050        return buffer.toString();
051    }
052
053    @Override
054    public int compareChildNodePointers(final NodePointer pointer1, final NodePointer pointer2) {
055        // Won't happen - lang attributes don't have children
056        return 0;
057    }
058
059    @Override
060    public boolean equals(final Object object) {
061        return object instanceof LangAttributePointer;
062    }
063
064    @Override
065    public Object getBaseValue() {
066        return parent.getLocale().toString().replace('_', '-');
067    }
068
069    @Override
070    public Object getImmediateNode() {
071        return getBaseValue();
072    }
073
074    @Override
075    public int getLength() {
076        return 1;
077    }
078
079    @Override
080    public QName getName() {
081        return new QName("xml", "lang");
082    }
083
084    @Override
085    public String getNamespaceURI() {
086        return null;
087    }
088
089    @Override
090    public int hashCode() {
091        return 0;
092    }
093
094    @Override
095    public boolean isCollection() {
096        return false;
097    }
098
099    @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}