org.apache.xml.utils.PrefixResolverDefault Java Examples

The following examples show how to use org.apache.xml.utils.PrefixResolverDefault. 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 File: SqlXmlHelperXalan.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Take the received string, which is an XML query expression, compile it, and
 * store the compiled query locally. Note that for now, we only support XPath
 * because that's what Xalan supports.
 * 
 * @param queryExpr
 *          The XPath expression to compile
 */
public void compileXQExpr(final String queryExpr, final String opName,
    final DocumentBuilder dBuilder) throws StandardException {
  try {

    /* The following XPath constructor compiles the expression
     * as part of the construction process.  We have to pass
     * in a PrefixResolver object in order to avoid NPEs when
     * invalid/unknown functions are used, so we just create
     * a dummy one, which means prefixes will not be resolved
     * in the query (Xalan will just throw an error if a prefix
     * is used).  In the future we may want to revisit this
     * to make it easier for users to query based on namespaces.
     */
    query = new XPath(queryExpr, null, new PrefixResolverDefault(
        dBuilder.newDocument()), XPath.SELECT);

  } catch (Throwable te) {

    /* Something went wrong during compilation of the
     * expression; wrap the error and re-throw it.
     * Note: we catch "Throwable" here to catch as many
     * Xalan-produced errors as possible in order to
     * minimize the chance of an uncaught Xalan error
     * (such as a NullPointerException) causing Derby
     * to fail in a more serious way.  In particular, an
     * uncaught Java exception like NPE can result in
     * Derby throwing "ERROR 40XT0: An internal error was
     * identified by RawStore module" for all statements on
     * the connection after the failure--which we clearly
     * don't want.  If we catch the error and wrap it,
     * though, the statement will fail but Derby will
     * continue to run as normal. 
     */
    throw StandardException.newException(SQLState.LANG_XML_QUERY_ERROR, te,
        opName, te.getMessage());

  }
}
 
Example #2
Source File: SqlXmlHelperXalan.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Take the received string, which is an XML query expression, compile it, and
 * store the compiled query locally. Note that for now, we only support XPath
 * because that's what Xalan supports.
 * 
 * @param queryExpr
 *          The XPath expression to compile
 */
public void compileXQExpr(final String queryExpr, final String opName,
    final DocumentBuilder dBuilder) throws StandardException {
  try {

    /* The following XPath constructor compiles the expression
     * as part of the construction process.  We have to pass
     * in a PrefixResolver object in order to avoid NPEs when
     * invalid/unknown functions are used, so we just create
     * a dummy one, which means prefixes will not be resolved
     * in the query (Xalan will just throw an error if a prefix
     * is used).  In the future we may want to revisit this
     * to make it easier for users to query based on namespaces.
     */
    query = new XPath(queryExpr, null, new PrefixResolverDefault(
        dBuilder.newDocument()), XPath.SELECT);

  } catch (Throwable te) {

    /* Something went wrong during compilation of the
     * expression; wrap the error and re-throw it.
     * Note: we catch "Throwable" here to catch as many
     * Xalan-produced errors as possible in order to
     * minimize the chance of an uncaught Xalan error
     * (such as a NullPointerException) causing Derby
     * to fail in a more serious way.  In particular, an
     * uncaught Java exception like NPE can result in
     * Derby throwing "ERROR 40XT0: An internal error was
     * identified by RawStore module" for all statements on
     * the connection after the failure--which we clearly
     * don't want.  If we catch the error and wrap it,
     * though, the statement will fail but Derby will
     * continue to run as normal. 
     */
    throw StandardException.newException(SQLState.LANG_XML_QUERY_ERROR, te,
        opName, te.getMessage());

  }
}
 
Example #3
Source File: XPathAPI.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 *  Evaluate XPath string to an XObject.
 *  XPath namespace prefixes are resolved from the namespaceNode.
 *  The implementation of this is a little slow, since it creates
 *  a number of objects each time it is called.  This could be optimized
 *  to keep the same objects around, but then thread-safety issues would arise.
 *
 *  @param contextNode The node to start searching from.
 *  @param str A valid XPath string.
 *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
 *  @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
 *  @see org.apache.xpath.objects.XObject
 *  @see org.apache.xpath.objects.XNull
 *  @see org.apache.xpath.objects.XBoolean
 *  @see org.apache.xpath.objects.XNumber
 *  @see org.apache.xpath.objects.XString
 *  @see org.apache.xpath.objects.XRTreeFrag
 *
 * @throws TransformerException
 */
public static XObject eval(Node contextNode, String str, Node namespaceNode)
        throws TransformerException
{

  // Since we don't have a XML Parser involved here, install some default support
  // for things like namespaces, etc.
  // (Changed from: XPathContext xpathSupport = new XPathContext();
  //    because XPathContext is weak in a number of areas... perhaps
  //    XPathContext should be done away with.)
  // Create an XPathContext that doesn't support pushing and popping of
  // variable resolution scopes.  Sufficient for simple XPath 1.0 expressions.
  XPathContext xpathSupport = new XPathContext(false);

  // Create an object to resolve namespace prefixes.
  // XPath namespaces are resolved from the input context node's document element
  // if it is a root node, or else the current context node (for lack of a better
  // resolution space, given the simplicity of this sample code).
  PrefixResolverDefault prefixResolver = new PrefixResolverDefault(
    (namespaceNode.getNodeType() == Node.DOCUMENT_NODE)
    ? ((Document) namespaceNode).getDocumentElement() : namespaceNode);

  // Create the XPath object.
  XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);

  // Execute the XPath, and have it return the result
  // return xpath.execute(xpathSupport, contextNode, prefixResolver);
  int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);

  return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
}
 
Example #4
Source File: CachedXPathAPI.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 *  Evaluate XPath string to an XObject. 
 *  XPath namespace prefixes are resolved from the namespaceNode.
 *  The implementation of this is a little slow, since it creates
 *  a number of objects each time it is called.  This could be optimized
 *  to keep the same objects around, but then thread-safety issues would arise.
 *
 *  @param contextNode The node to start searching from.
 *  @param str A valid XPath string.
 *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
 *  @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
 *  @see org.apache.xpath.objects.XObject
 *  @see org.apache.xpath.objects.XNull
 *  @see org.apache.xpath.objects.XBoolean
 *  @see org.apache.xpath.objects.XNumber
 *  @see org.apache.xpath.objects.XString
 *  @see org.apache.xpath.objects.XRTreeFrag
 *
 * @throws TransformerException
 */
public  XObject eval(Node contextNode, String str, Node namespaceNode)
        throws TransformerException
{

  // Since we don't have a XML Parser involved here, install some default support
  // for things like namespaces, etc.
  // (Changed from: XPathContext xpathSupport = new XPathContext();
  //    because XPathContext is weak in a number of areas... perhaps
  //    XPathContext should be done away with.)

  // Create an object to resolve namespace prefixes.
  // XPath namespaces are resolved from the input context node's document element
  // if it is a root node, or else the current context node (for lack of a better
  // resolution space, given the simplicity of this sample code).
  PrefixResolverDefault prefixResolver = new PrefixResolverDefault(
    (namespaceNode.getNodeType() == Node.DOCUMENT_NODE)
    ? ((Document) namespaceNode).getDocumentElement() : namespaceNode);

  // Create the XPath object.
  XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);

  // Execute the XPath, and have it return the result
  // return xpath.execute(xpathSupport, contextNode, prefixResolver);
  int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);

  return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
}