286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: FuncTranslate.java,v 1.2.4.1 2005/09/14 20:18:45 jeffsuttor Exp $
286N/A */
286N/Apackage com.sun.org.apache.xpath.internal.functions;
286N/A
286N/Aimport com.sun.org.apache.xpath.internal.XPathContext;
286N/Aimport com.sun.org.apache.xpath.internal.objects.XObject;
286N/Aimport com.sun.org.apache.xpath.internal.objects.XString;
286N/A
286N/A/**
286N/A * Execute the Translate() function.
286N/A * @xsl.usage advanced
286N/A */
286N/Apublic class FuncTranslate extends Function3Args
286N/A{
286N/A static final long serialVersionUID = -1672834340026116482L;
286N/A
286N/A /**
286N/A * Execute the function. The function must return
286N/A * a valid object.
286N/A * @param xctxt The current execution context.
286N/A * @return A valid XObject.
286N/A *
286N/A * @throws javax.xml.transform.TransformerException
286N/A */
286N/A public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
286N/A {
286N/A
286N/A String theFirstString = m_arg0.execute(xctxt).str();
286N/A String theSecondString = m_arg1.execute(xctxt).str();
286N/A String theThirdString = m_arg2.execute(xctxt).str();
286N/A int theFirstStringLength = theFirstString.length();
286N/A int theThirdStringLength = theThirdString.length();
286N/A
286N/A // A vector to contain the new characters. We'll use it to construct
286N/A // the result string.
286N/A StringBuffer sbuffer = new StringBuffer();
286N/A
286N/A for (int i = 0; i < theFirstStringLength; i++)
286N/A {
286N/A char theCurrentChar = theFirstString.charAt(i);
286N/A int theIndex = theSecondString.indexOf(theCurrentChar);
286N/A
286N/A if (theIndex < 0)
286N/A {
286N/A
286N/A // Didn't find the character in the second string, so it
286N/A // is not translated.
286N/A sbuffer.append(theCurrentChar);
286N/A }
286N/A else if (theIndex < theThirdStringLength)
286N/A {
286N/A
286N/A // OK, there's a corresponding character in the
286N/A // third string, so do the translation...
286N/A sbuffer.append(theThirdString.charAt(theIndex));
286N/A }
286N/A else
286N/A {
286N/A
286N/A // There's no corresponding character in the
286N/A // third string, since it's shorter than the
286N/A // second string. In this case, the character
286N/A // is removed from the output string, so don't
286N/A // do anything.
286N/A }
286N/A }
286N/A
286N/A return new XString(sbuffer.toString());
286N/A }
286N/A}