Пример #1
0
 /** Evaluate an XPath expression as a boolean value. */
 public static boolean evaluateAsBoolean(
     PropertyContext propertyContext,
     List<Item> contextItems,
     int contextPosition,
     String xpathString,
     Map<String, String> prefixToURIMap,
     Map<String, ValueRepresentation> variableToValueMap,
     FunctionLibrary functionLibrary,
     FunctionContext functionContext,
     String baseURI,
     LocationData locationData) {
   final PooledXPathExpression xpathExpression =
       XPathCache.getXPathExpression(
           propertyContext,
           contextItems,
           contextPosition,
           "string(subsequence(" + xpathString + ", 1, 1))",
           prefixToURIMap,
           variableToValueMap,
           functionLibrary,
           baseURI,
           false,
           false,
           locationData);
   try {
     return xpathExpression.evaluateAsBoolean(functionContext);
   } catch (XPathException e) {
     throw handleXPathException(e, xpathString, "evaluating XPath expression", locationData);
   } finally {
     if (xpathExpression != null) xpathExpression.returnToPool();
   }
 }
Пример #2
0
 /**
  * Evaluate an XPath expression on the document as an attribute value template, and return its
  * string value.
  */
 public static String evaluateAsAvt(
     PropertyContext propertyContext,
     List<Item> contextItems,
     int contextPosition,
     String xpathString,
     Map<String, String> prefixToURIMap,
     Map<String, ValueRepresentation> variableToValueMap,
     FunctionLibrary functionLibrary,
     FunctionContext functionContext,
     String baseURI,
     LocationData locationData) {
   final PooledXPathExpression xpathExpression =
       XPathCache.getXPathExpression(
           propertyContext,
           contextItems,
           contextPosition,
           xpathString,
           prefixToURIMap,
           variableToValueMap,
           functionLibrary,
           baseURI,
           true,
           false,
           locationData);
   try {
     final Object result = xpathExpression.evaluateSingleKeepNodeInfo(functionContext);
     return (result != null) ? result.toString() : null;
   } catch (XPathException e) {
     throw handleXPathException(e, xpathString, "evaluating XPath expression", locationData);
   } finally {
     if (xpathExpression != null) xpathExpression.returnToPool();
   }
 }
Пример #3
0
 public void destroyObject(Object o) throws Exception {
   if (o instanceof PooledXPathExpression) {
     PooledXPathExpression xp = (PooledXPathExpression) o;
     xp.destroy();
   } else throw new OXFException(o.toString() + " is not a PooledXPathExpression");
 }
Пример #4
0
  public static PooledXPathExpression getXPathExpression(
      PropertyContext propertyContext,
      List<Item> contextItems,
      int contextPosition,
      String xpathString,
      Map<String, String> prefixToURIMap,
      Map<String, ValueRepresentation> variableToValueMap,
      FunctionLibrary functionLibrary,
      String baseURI,
      boolean isAvt,
      boolean testNoCache,
      LocationData locationData) {

    try {
      // Find pool from cache
      final Long validity = (long) 0;
      final Cache cache = ObjectCache.instance(XPATH_CACHE_NAME, XPATH_CACHE_DEFAULT_SIZE);
      final FastStringBuffer cacheKeyString = new FastStringBuffer(xpathString);
      {
        if (functionLibrary != null) { // This is ok
          cacheKeyString.append('|');
          cacheKeyString.append(Integer.toString(functionLibrary.hashCode()));
        }
      }
      {
        // NOTE: Mike Kay confirms on 2007-07-04 that compilation depends on the namespace context,
        // so we need
        // to use it as part of the cache key.

        // TODO: PERF: It turns out that this takes a lot of time. Now that the namespace
        // information is computed statically, we can do better.
        if (DEBUG_TEST_KEY_OPTIMIZATION) {
          // PERF TEST ONLY
          cacheKeyString.append("|DUMMYNSVAR|");
        } else {

          if (prefixToURIMap != null) {
            final Map<String, String> sortedMap =
                (prefixToURIMap instanceof TreeMap)
                    ? prefixToURIMap
                    : new TreeMap<String, String>(
                        prefixToURIMap); // this should make sure we always get the keys in the same
                                         // order
            for (Map.Entry<String, String> currentEntry : sortedMap.entrySet()) {
              cacheKeyString.append('|');
              cacheKeyString.append(currentEntry.getKey());
              cacheKeyString.append('=');
              cacheKeyString.append(currentEntry.getValue());
            }
          }
        }
      }
      if (DEBUG_TEST_KEY_OPTIMIZATION) {
        // PERF TEST ONLY
        // NOP
      } else {

        if (variableToValueMap != null && variableToValueMap.size() > 0) {
          // There are some variables in scope. They must be part of the key
          // TODO: Put this in static state as this can be determined statically once and for all
          for (final String variableName : variableToValueMap.keySet()) {
            cacheKeyString.append('|');
            cacheKeyString.append(variableName);
          }
        }
      }
      {
        // Add this to the key as evaluating "name" as XPath or as AVT is very different!
        cacheKeyString.append('|');
        cacheKeyString.append(Boolean.toString(isAvt));
      }

      // TODO: Add baseURI to cache key (currently, baseURI is pretty much unused)

      final Set<String> variableNames =
          (variableToValueMap != null) ? variableToValueMap.keySet() : null;
      final PooledXPathExpression expr;
      if (testNoCache) {
        // For testing only: don't get expression from cache
        final Object o =
            new XFormsCachePoolableObjetFactory(
                    null,
                    xpathString,
                    prefixToURIMap,
                    variableNames,
                    functionLibrary,
                    baseURI,
                    isAvt,
                    false,
                    locationData)
                .makeObject();
        expr = (PooledXPathExpression) o;
      } else {
        // Get or create pool
        final InternalCacheKey cacheKey =
            new InternalCacheKey("XPath Expression2", cacheKeyString.toString());
        ObjectPool pool = (ObjectPool) cache.findValid(propertyContext, cacheKey, validity);
        if (pool == null) {
          pool =
              createXPathPool(
                  xpathString,
                  prefixToURIMap,
                  variableNames,
                  functionLibrary,
                  baseURI,
                  isAvt,
                  locationData);
          cache.add(propertyContext, cacheKey, validity, pool);
        }

        // Get object from pool
        final Object o = pool.borrowObject();
        expr = (PooledXPathExpression) o;
      }

      // Set context items and position
      expr.setContextItems(contextItems, contextPosition);

      // Set variables
      expr.setVariables(variableToValueMap);

      return expr;
    } catch (Exception e) {
      throw handleXPathException(e, xpathString, "preparing XPath expression", locationData);
    }
  }