Java Code Examples for org.apache.xpath.objects.XString

The following examples show how to use org.apache.xpath.objects.XString. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: j2objc   Source File: FuncGenerateId.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{

  int which = getArg0AsNode(xctxt);

  if (DTM.NULL != which)
  {
    // Note that this is a different value than in previous releases
    // of Xalan. It's sensitive to the exact encoding of the node
    // handle anyway, so fighting to maintain backward compatability
    // really didn't make sense; it may change again as we continue
    // to experiment with balancing document and node numbers within
    // that value.
    return new XString("N" + Integer.toHexString(which).toUpperCase());
  }
  else
    return XString.EMPTYSTRING;
}
 
Example 2
Source Project: j2objc   Source File: FuncQname.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{

  int context = getArg0AsNode(xctxt);
  XObject val;

  if (DTM.NULL != context)
  {
    DTM dtm = xctxt.getDTM(context);
    String qname = dtm.getNodeNameX(context);
    val = (null == qname) ? XString.EMPTYSTRING : new XString(qname);
  }
  else
  {
    val = XString.EMPTYSTRING;
  }

  return val;
}
 
Example 3
Source Project: j2objc   Source File: FuncConcat.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{

  StringBuffer sb = new StringBuffer();

  // Compiler says we must have at least two arguments.
  sb.append(m_arg0.execute(xctxt).str());
  sb.append(m_arg1.execute(xctxt).str());

  if (null != m_arg2)
    sb.append(m_arg2.execute(xctxt).str());

  if (null != m_args)
  {
    for (int i = 0; i < m_args.length; i++)
    {
      sb.append(m_args[i].execute(xctxt).str());
    }
  }

  return new XString(sb.toString());
}
 
Example 4
Source Project: j2objc   Source File: FunctionDef1Arg.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Execute the first argument expression that is expected to return a
 * string.  If the argument is null, then get the string value from the
 * current context node.
 *
 * @param xctxt Runtime XPath context.
 *
 * @return The string value of the first argument, or the string value of the
 *         current context node if the first argument is null.
 *
 * @throws javax.xml.transform.TransformerException if an error occurs while
 *                                   executing the argument expression.
 */
protected XMLString getArg0AsString(XPathContext xctxt)
        throws javax.xml.transform.TransformerException
{
  if(null == m_arg0)
  {
    int currentNode = xctxt.getCurrentNode();
    if(DTM.NULL == currentNode)
      return XString.EMPTYSTRING;
    else
    {
      DTM dtm = xctxt.getDTM(currentNode);
      return dtm.getStringValue(currentNode);
    }
    
  }
  else
    return m_arg0.execute(xctxt).xstr();   
}
 
Example 5
Source Project: htmlunit   Source File: XPathHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Evaluates an XPath expression from the specified node, returning the resultant nodes.
 *
 * @param <T> the type class
 * @param node the node to start searching from
 * @param xpathExpr the XPath expression
 * @param resolver the prefix resolver to use for resolving namespace prefixes, or null
 * @return the list of objects found
 */
@SuppressWarnings("unchecked")
public static <T> List<T> getByXPath(final DomNode node, final String xpathExpr,
        final PrefixResolver resolver) {
    if (xpathExpr == null) {
        throw new IllegalArgumentException("Null is not a valid XPath expression");
    }

    PROCESS_XPATH_.set(Boolean.TRUE);
    final List<T> list = new ArrayList<>();
    try {
        final XObject result = evaluateXPath(node, xpathExpr, resolver);

        if (result instanceof XNodeSet) {
            final NodeList nodelist = ((XNodeSet) result).nodelist();
            for (int i = 0; i < nodelist.getLength(); i++) {
                list.add((T) nodelist.item(i));
            }
        }
        else if (result instanceof XNumber) {
            list.add((T) Double.valueOf(result.num()));
        }
        else if (result instanceof XBoolean) {
            list.add((T) Boolean.valueOf(result.bool()));
        }
        else if (result instanceof XString) {
            list.add((T) result.str());
        }
        else {
            throw new RuntimeException("Unproccessed " + result.getClass().getName());
        }
    }
    catch (final Exception e) {
        throw new RuntimeException("Could not retrieve XPath >" + xpathExpr + "< on " + node, e);
    }
    finally {
        PROCESS_XPATH_.set(Boolean.FALSE);
    }
    return list;
}
 
Example 6
Source Project: HtmlUnit-Android   Source File: XPathUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Evaluates an XPath expression from the specified node, returning the resultant nodes.
 *
 * @param <T> the type class
 * @param node the node to start searching from
 * @param xpathExpr the XPath expression
 * @param resolver the prefix resolver to use for resolving namespace prefixes, or null
 * @return the list of objects found
 */
@SuppressWarnings("unchecked")
public static <T> List<T> getByXPath(final DomNode node, final String xpathExpr, final PrefixResolver resolver) {
    if (xpathExpr == null) {
        throw new NullPointerException("Null is not a valid XPath expression");
    }

    PROCESS_XPATH_.set(Boolean.TRUE);
    final List<T> list = new ArrayList<>();
    try {
        final XObject result = evaluateXPath(node, xpathExpr, resolver);

        if (result instanceof XNodeSet) {
            final NodeList nodelist = ((XNodeSet) result).nodelist();
            for (int i = 0; i < nodelist.getLength(); i++) {
                list.add((T) nodelist.item(i));
            }
        }
        else if (result instanceof XNumber) {
            list.add((T) Double.valueOf(result.num()));
        }
        else if (result instanceof XBoolean) {
            list.add((T) Boolean.valueOf(result.bool()));
        }
        else if (result instanceof XString) {
            list.add((T) result.str());
        }
        else {
            throw new RuntimeException("Unproccessed " + result.getClass().getName());
        }
    }
    catch (final Exception e) {
        throw new RuntimeException("Could not retrieve XPath >" + xpathExpr + "< on " + node, e);
    }
    finally {
        PROCESS_XPATH_.set(Boolean.FALSE);
    }
    return list;
}
 
Example 7
Source Project: j2objc   Source File: ElemWithParam.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get the XObject representation of the variable.
 *
 * @param transformer non-null reference to the the current transform-time state.
 * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
 *
 * @return the XObject representation of the variable.
 *
 * @throws TransformerException
 */
public XObject getValue(TransformerImpl transformer, int sourceNode)
        throws TransformerException
{

  XObject var;
  XPathContext xctxt = transformer.getXPathContext();

  xctxt.pushCurrentNode(sourceNode);

  try
  {
    if (null != m_selectPattern)
    {
      var = m_selectPattern.execute(xctxt, sourceNode, this);

      var.allowDetachToRelease(false);
    }
    else if (null == getFirstChildElem())
    {
      var = XString.EMPTYSTRING;
    }
    else
    {

      // Use result tree fragment
      int df = transformer.transformToRTF(this);

      var = new XRTreeFrag(df, xctxt, this);
    }
  }
  finally
  {
    xctxt.popCurrentNode();
  }

  return var;
}
 
Example 8
Source Project: j2objc   Source File: XPathContext.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get the value of a node as a number.
 * @param n Node to be converted to a number.  May be null.
 * @return value of n as a number.
 */
public double toNumber(org.w3c.dom.Node n)
{
  // %REVIEW% You can't get much uglier than this...
  int nodeHandle = getDTMHandleFromNode(n);
  DTM dtm = getDTM(nodeHandle);
  XString xobj = (XString)dtm.getStringValue(nodeHandle);
  return xobj.num();
}
 
Example 9
Source Project: j2objc   Source File: FuncUnparsedEntityURI.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{

  String name = m_arg0.execute(xctxt).str();
  int context = xctxt.getCurrentNode();
  DTM dtm = xctxt.getDTM(context);
  int doc = dtm.getDocument();
  
  String uri = dtm.getUnparsedEntityURI(name);

  return new XString(uri);
}
 
Example 10
Source Project: j2objc   Source File: FuncNamespace.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{

  int context = getArg0AsNode(xctxt);
  
  String s;
  if(context != DTM.NULL)
  {
    DTM dtm = xctxt.getDTM(context);
    int t = dtm.getNodeType(context);
    if(t == DTM.ELEMENT_NODE)
    {
      s = dtm.getNamespaceURI(context);
    }
    else if(t == DTM.ATTRIBUTE_NODE)
    {

      // This function always returns an empty string for namespace nodes.
      // We check for those here.  Fix inspired by Davanum Srinivas.

      s = dtm.getNodeName(context);
      if(s.startsWith("xmlns:") || s.equals("xmlns"))
        return XString.EMPTYSTRING;

      s = dtm.getNamespaceURI(context);
    }
    else
      return XString.EMPTYSTRING;
  }
  else 
    return XString.EMPTYSTRING;
  
  return ((null == s) ? XString.EMPTYSTRING : new XString(s));
}
 
Example 11
Source Project: j2objc   Source File: FuncSubstringAfter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{

  XMLString s1 = m_arg0.execute(xctxt).xstr();
  XMLString s2 = m_arg1.execute(xctxt).xstr();
  int index = s1.indexOf(s2);

  return (-1 == index)
         ? XString.EMPTYSTRING
         : (XString)s1.substring(index + s2.length());
}
 
Example 12
Source Project: j2objc   Source File: FuncLocalPart.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{

  int context = getArg0AsNode(xctxt);
  if(DTM.NULL == context)
    return XString.EMPTYSTRING;
  DTM dtm = xctxt.getDTM(context);
  String s = (context != DTM.NULL) ? dtm.getLocalName(context) : "";
  if(s.startsWith("#") || s.equals("xmlns"))
    return XString.EMPTYSTRING;

  return new XString(s);
}
 
Example 13
Source Project: j2objc   Source File: FuncSubstringBefore.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{

  String s1 = m_arg0.execute(xctxt).str();
  String s2 = m_arg1.execute(xctxt).str();
  int index = s1.indexOf(s2);

  return (-1 == index)
         ? XString.EMPTYSTRING : new XString(s1.substring(0, index));
}
 
Example 14
Source Project: j2objc   Source File: FuncDoclocation.java    License: Apache License 2.0 5 votes vote down vote up
/**
   * Execute the function.  The function must return
   * a valid object.
   * @param xctxt The current execution context.
   * @return A valid XObject.
   *
   * @throws javax.xml.transform.TransformerException
   */
  public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  {

    int whereNode = getArg0AsNode(xctxt);
    String fileLocation = null;

    if (DTM.NULL != whereNode)
    {
      DTM dtm = xctxt.getDTM(whereNode);
      
      // %REVIEW%
      if (DTM.DOCUMENT_FRAGMENT_NODE ==  dtm.getNodeType(whereNode))
      {
        whereNode = dtm.getFirstChild(whereNode);
      }

      if (DTM.NULL != whereNode)
      {        
        fileLocation = dtm.getDocumentBaseURI();
//        int owner = dtm.getDocument();
//        fileLocation = xctxt.getSourceTreeManager().findURIFromDoc(owner);
      }
    }

    return new XString((null != fileLocation) ? fileLocation : "");
  }
 
Example 15
Source Project: j2objc   Source File: XPathParser.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * The value of the Literal is the sequence of characters inside
 * the " or ' characters>.
 *
 * Literal  ::=  '"' [^"]* '"'
 * | "'" [^']* "'"
 *
 *
 * @throws javax.xml.transform.TransformerException
 */
protected void Literal() throws javax.xml.transform.TransformerException
{

  int last = m_token.length() - 1;
  char c0 = m_tokenChar;
  char cX = m_token.charAt(last);

  if (((c0 == '\"') && (cX == '\"')) || ((c0 == '\'') && (cX == '\'')))
  {

    // Mutate the token to remove the quotes and have the XString object
    // already made.
    int tokenQueuePos = m_queueMark - 1;

    m_ops.m_tokenQueue.setElementAt(null,tokenQueuePos);

    Object obj = new XString(m_token.substring(1, last));

    m_ops.m_tokenQueue.setElementAt(obj,tokenQueuePos);

    // lit = m_token.substring(1, last);
    m_ops.setOp(m_ops.getOp(OpMap.MAPINDEX_LENGTH), tokenQueuePos);
    m_ops.setOp(OpMap.MAPINDEX_LENGTH, m_ops.getOp(OpMap.MAPINDEX_LENGTH) + 1);

    nextToken();
  }
  else
  {
    error(XPATHErrorResources.ER_PATTERN_LITERAL_NEEDS_BE_QUOTED,
          new Object[]{ m_token });  //"Pattern literal ("+m_token+") needs to be quoted!");
  }
}
 
Example 16
Source Project: htmlunit   Source File: LowerCaseFunction.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public XObject execute(final XPathContext xctxt) throws TransformerException {
    return new XString(((XString) getArg0AsString(xctxt)).str().toLowerCase(Locale.ROOT));
}
 
Example 17
Source Project: HtmlUnit-Android   Source File: LowerCaseFunction.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public XObject execute(final XPathContext xctxt) throws TransformerException {
    return new XString(((XString) getArg0AsString(xctxt)).str().toLowerCase(Locale.ROOT));
}
 
Example 18
Source Project: j2objc   Source File: FuncFormatNumb.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{

  // A bit of an ugly hack to get our context.
  ElemTemplateElement templElem =
    (ElemTemplateElement) xctxt.getNamespaceContext();
  StylesheetRoot ss = templElem.getStylesheetRoot();
  java.text.DecimalFormat formatter = null;
  java.text.DecimalFormatSymbols dfs = null;
  double num = getArg0().execute(xctxt).num();
  String patternStr = getArg1().execute(xctxt).str();

  // TODO: what should be the behavior here??
  if (patternStr.indexOf(0x00A4) > 0)
    ss.error(XSLTErrorResources.ER_CURRENCY_SIGN_ILLEGAL);  // currency sign not allowed

  // this third argument is not a locale name. It is the name of a
  // decimal-format declared in the stylesheet!(xsl:decimal-format
  try
  {
    Expression arg2Expr = getArg2();

    if (null != arg2Expr)
    {
      String dfName = arg2Expr.execute(xctxt).str();
      QName qname = new QName(dfName, xctxt.getNamespaceContext());

      dfs = ss.getDecimalFormatComposed(qname);

      if (null == dfs)
      {
        warn(xctxt, XSLTErrorResources.WG_NO_DECIMALFORMAT_DECLARATION,
             new Object[]{ dfName });  //"not found!!!

        //formatter = new java.text.DecimalFormat(patternStr);
      }
      else
      {

        //formatter = new java.text.DecimalFormat(patternStr, dfs);
        formatter = new java.text.DecimalFormat();

        formatter.setDecimalFormatSymbols(dfs);
        formatter.applyLocalizedPattern(patternStr);
      }
    }

    //else
    if (null == formatter)
    {

      // look for a possible default decimal-format
      dfs = ss.getDecimalFormatComposed(new QName(""));

      if (dfs != null)
      {
        formatter = new java.text.DecimalFormat();

        formatter.setDecimalFormatSymbols(dfs);
        formatter.applyLocalizedPattern(patternStr);
      }
      else
      {
        dfs = new java.text.DecimalFormatSymbols(java.util.Locale.US);

        dfs.setInfinity(Constants.ATTRVAL_INFINITY);
        dfs.setNaN(Constants.ATTRVAL_NAN);

        formatter = new java.text.DecimalFormat();

        formatter.setDecimalFormatSymbols(dfs);

        if (null != patternStr)
          formatter.applyLocalizedPattern(patternStr);
      }
    }

    return new XString(formatter.format(num));
  }
  catch (Exception iae)
  {
    templElem.error(XSLTErrorResources.ER_MALFORMED_FORMAT_STRING,
                    new Object[]{ patternStr });

    return XString.EMPTYSTRING;

    //throw new XSLProcessorException(iae);
  }
}
 
Example 19
Source Project: j2objc   Source File: ElemVariable.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Get the XObject representation of the variable.
 *
 * @param transformer non-null reference to the the current transform-time state.
 * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
 *
 * @return the XObject representation of the variable.
 *
 * @throws TransformerException
 */
public XObject getValue(TransformerImpl transformer, int sourceNode)
        throws TransformerException
{

  XObject var;
  XPathContext xctxt = transformer.getXPathContext();

  xctxt.pushCurrentNode(sourceNode);
 
  try
  {
    if (null != m_selectPattern)
    {
      var = m_selectPattern.execute(xctxt, sourceNode, this);

      var.allowDetachToRelease(false);
    }
    else if (null == getFirstChildElem())
    {
      var = XString.EMPTYSTRING;
    }
    else
    {

      // Use result tree fragment.
      // Global variables may be deferred (see XUnresolvedVariable) and hence
      // need to be assigned to a different set of DTMs than local variables
      // so they aren't popped off the stack on return from a template.
      int df;

// Bugzilla 7118: A variable set via an RTF may create local
// variables during that computation. To keep them from overwriting
// variables at this level, push a new variable stack.
////// PROBLEM: This is provoking a variable-used-before-set
////// problem in parameters. Needs more study.
try
{
	//////////xctxt.getVarStack().link(0);
	if(m_parentNode instanceof Stylesheet) // Global variable
		df = transformer.transformToGlobalRTF(this);
	else
		df = transformer.transformToRTF(this);
  	}
finally{ 
	//////////////xctxt.getVarStack().unlink(); 
	}

      var = new XRTreeFrag(df, xctxt, this);
    }
  }
  finally
  {      
    xctxt.popCurrentNode();
  }

  return var;
}
 
Example 20
Source Project: j2objc   Source File: ElemVariable.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * If the children of a variable is a single xsl:value-of or text literal, 
 * it is cheaper to evaluate this as an expression, so try and adapt the 
 * child an an expression.
 *
 * @param varElem Should be a ElemParam, ElemVariable, or ElemWithParam.
 *
 * @return An XPath if rewrite is possible, else null.
 *
 * @throws TransformerException
 */
static XPath rewriteChildToExpression(ElemTemplateElement varElem)
        throws TransformerException
{

  ElemTemplateElement t = varElem.getFirstChildElem();

  // Down the line this can be done with multiple string objects using 
  // the concat function.
  if (null != t && null == t.getNextSiblingElem())
  {
    int etype = t.getXSLToken();

    if (Constants.ELEMNAME_VALUEOF == etype)
    {
      ElemValueOf valueof = (ElemValueOf) t;

      // %TBD% I'm worried about extended attributes here.
      if (valueof.getDisableOutputEscaping() == false
              && valueof.getDOMBackPointer() == null)
      {
        varElem.m_firstChild = null;

        return new XPath(new XRTreeFragSelectWrapper(valueof.getSelect().getExpression()));
      }
    }
    else if (Constants.ELEMNAME_TEXTLITERALRESULT == etype)
    {
      ElemTextLiteral lit = (ElemTextLiteral) t;

      if (lit.getDisableOutputEscaping() == false
              && lit.getDOMBackPointer() == null)
      {
        String str = lit.getNodeValue();
        XString xstr = new XString(str);

        varElem.m_firstChild = null;

        return new XPath(new XRTreeFragSelectWrapper(xstr));
      }
    }
  }

  return null;
}
 
Example 21
Source Project: j2objc   Source File: FuncSubstring.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{

  XMLString s1 = m_arg0.execute(xctxt).xstr();
  double start = m_arg1.execute(xctxt).num();
  int lenOfS1 = s1.length();
  XMLString substr;

  if (lenOfS1 <= 0)
    return XString.EMPTYSTRING;
  else
  {
    int startIndex;

    if (Double.isNaN(start))
    {

      // Double.MIN_VALUE doesn't work with math below 
      // so just use a big number and hope I never get caught.
      start = -1000000;
      startIndex = 0;
    }
    else
    {
      start = Math.round(start);
      startIndex = (start > 0) ? (int) start - 1 : 0;
    }

    if (null != m_arg2)
    {
      double len = m_arg2.num(xctxt);
      int end = (int) (Math.round(len) + start) - 1;

      // Normalize end index.
      if (end < 0)
        end = 0;
      else if (end > lenOfS1)
        end = lenOfS1;

      if (startIndex > lenOfS1)
        startIndex = lenOfS1;

      substr = s1.substring(startIndex, end);
    }
    else
    {
      if (startIndex > lenOfS1)
        startIndex = lenOfS1;
      substr = s1.substring(startIndex);
    }
  }

  return (XString)substr; // cast semi-safe
}
 
Example 22
Source Project: j2objc   Source File: FuncTranslate.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{

  String theFirstString = m_arg0.execute(xctxt).str();
  String theSecondString = m_arg1.execute(xctxt).str();
  String theThirdString = m_arg2.execute(xctxt).str();
  int theFirstStringLength = theFirstString.length();
  int theThirdStringLength = theThirdString.length();

  // A vector to contain the new characters.  We'll use it to construct
  // the result string.
  StringBuffer sbuffer = new StringBuffer();

  for (int i = 0; i < theFirstStringLength; i++)
  {
    char theCurrentChar = theFirstString.charAt(i);
    int theIndex = theSecondString.indexOf(theCurrentChar);

    if (theIndex < 0)
    {

      // Didn't find the character in the second string, so it
      // is not translated.
      sbuffer.append(theCurrentChar);
    }
    else if (theIndex < theThirdStringLength)
    {

      // OK, there's a corresponding character in the
      // third string, so do the translation...
      sbuffer.append(theThirdString.charAt(theIndex));
    }
    else
    {

      // There's no corresponding character in the
      // third string, since it's shorter than the
      // second string.  In this case, the character
      // is removed from the output string, so don't
      // do anything.
    }
  }

  return new XString(sbuffer.toString());
}
 
Example 23
Source Project: j2objc   Source File: FuncNormalizeSpace.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{
  XMLString s1 = getArg0AsString(xctxt);

  return (XString)s1.fixWhiteSpace(true, true, false);
}
 
Example 24
Source Project: j2objc   Source File: Compiler.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Compile a literal string value.
 * 
 * @param opPos The current position in the m_opMap array.
 *
 * @return reference to {@link org.apache.xpath.objects.XString} instance.
 *
 * @throws TransformerException if a error occurs creating the Expression.
 */
protected Expression literal(int opPos)
{

  opPos = getFirstChildPos(opPos);

  return (XString) getTokenQueue().elementAt(getOp(opPos));
}
 
Example 25
Source Project: j2objc   Source File: String.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Apply the operation to two operands, and return the result.
 *
 *
 * @param right non-null reference to the evaluated right operand.
 *
 * @return non-null reference to the XObject that represents the result of the operation.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject operate(XObject right) throws javax.xml.transform.TransformerException
{
  return (XString)right.xstr(); // semi-safe cast.
}
 
Example 26
Source Project: j2objc   Source File: XPathVisitor.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Visit a string literal.
 * @param owner The owner of the expression, to which the expression can 
 *              be reset if rewriting takes place.
 * @param str The string literal object.
 * @return true if the sub expressions should be traversed.
 */
public boolean visitStringLiteral(ExpressionOwner owner, XString str)
{
	return true;
}
 
Example 27
Source Project: j2objc   Source File: FuncString.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{
  return (XString)getArg0AsString(xctxt);
}