Class XPathRewriterBase

java.lang.Object
de.wwu.scdh.annotation.selection.rewriter.XPathRewriterBase
Direct Known Subclasses:
XPathNormalizer, XPathRefinedByRFC5147CharSchemeBackwardMapper, XPathRefinedByRFC5147CharSchemeForwardMapper, XPathRefinedByRFC5147CharSchemeToTextBackwardMapper, XPathRefinedByRFC5147CharSchemeToTextForwardMapper

public abstract class XPathRewriterBase extends Object
This class provides methods common to normalizers and mappers, that operate on XPath selectors refined by RFC5147 character schemes.
  • Field Details

    • TRACE_TEXT_LEAF

      public static final Pattern TRACE_TEXT_LEAF
    • TEXT_LEAF

      public static Pattern TEXT_LEAF
    • EMPTY_NAMESPACE

      public static final Pattern EMPTY_NAMESPACE
    • xPathCompiler

      protected final net.sf.saxon.s9api.XPathCompiler xPathCompiler
    • xpath

      protected final String xpath
  • Constructor Details

  • Method Details

    • replaceTraceTextLeaf

      public static String replaceTraceTextLeaf(String xpath)
    • replaceEmptyNamespace

      public static String replaceEmptyNamespace(String xpath)
    • postProcForwardPaths

      public static String postProcForwardPaths(String xpath, RewriterConfig config)
    • replaceTextLeaf

      public static String replaceTextLeaf(String xpath)
    • replaceTextLeaf

      public static String replaceTextLeaf(String xpath, RewriterConfig config)
    • getXPath

      public String getXPath()
      Returns the XPath the mapper instance was configured with.
    • getTextNodeAtPosition

      protected org.apache.commons.lang3.tuple.Pair<net.sf.saxon.s9api.XdmNode,Integer> getTextNodeAtPosition(S9ApiResource<? extends net.sf.saxon.s9api.XdmValue> resource, String xpath, int position, Mode mode) throws SelectorException
      This method run the first stage of the normalization process. It gets the node where the pair of XPath and position of a character-scheme-refined XPath selector points to. Either this node is a text node, or the selector is invalid in the context of the current document. In general, the resulting position is not the same as the input position.

      There are several algorithms for this first stage of the normalization process and their return values differ for corner cases. The algorithm is selected by the mode argument. See Mode for possible values.

      A SelectorException is thrown if the xpath does not select exactly one node or if the position is not inside the fragment (subtree) selected by the xpath.

      Parameters:
      resource - the DOMResource on the base of which the normalization is done
      xpath - the XPath part of the XPath selector
      position - the position following the character scheme of RFC5147
      mode - a normalization algorithm selected from Mode
      Returns:
      a pair of node and position
      Throws:
      SelectorException - if the XPath does not select exactly one node
    • getFirstNodeAtPosition

      protected final org.apache.commons.lang3.tuple.Pair<net.sf.saxon.s9api.XdmNode,Integer> getFirstNodeAtPosition(S9ApiResource<? extends net.sf.saxon.s9api.XdmValue> resource, String xpath, int position) throws SelectorException
      The implementation of step 1 of the normalization algorithm in mode Mode.FIRST.
      Throws:
      SelectorException
    • getSecondNodeAtPosition

      protected final org.apache.commons.lang3.tuple.Pair<net.sf.saxon.s9api.XdmNode,Integer> getSecondNodeAtPosition(S9ApiResource<? extends net.sf.saxon.s9api.XdmValue> resource, String xpath, int position) throws SelectorException
      The implementation of step 1 of the normalization algorithm in mode Mode.SECOND.
      Throws:
      SelectorException
    • getFirstOfDeepestNodesAtPosition

      protected final org.apache.commons.lang3.tuple.Pair<net.sf.saxon.s9api.XdmNode,Integer> getFirstOfDeepestNodesAtPosition(S9ApiResource<? extends net.sf.saxon.s9api.XdmValue> resource, String xpath, int position) throws SelectorException
      The implementation of step 1 of the normalization algorithm in mode Mode.FIRST_OF_DEEPEST_NODES.
      Throws:
      SelectorException
    • getLastOfDeepestNodesAtPosition

      protected final org.apache.commons.lang3.tuple.Pair<net.sf.saxon.s9api.XdmNode,Integer> getLastOfDeepestNodesAtPosition(S9ApiResource<? extends net.sf.saxon.s9api.XdmValue> resource, String xpath, int position) throws SelectorException
      The implementation of step 1 of the normalization algorithm in mode Mode.LAST_OF_DEEPEST_NODES.
      Throws:
      SelectorException
    • getDeepTextNodeAtPositionStopAtEnd

      protected final org.apache.commons.lang3.tuple.Pair<net.sf.saxon.s9api.XdmNode,Integer> getDeepTextNodeAtPositionStopAtEnd(S9ApiResource<? extends net.sf.saxon.s9api.XdmValue> resource, String xpath, int position) throws SelectorException
      This is an implementation of the normalization step 1 for the mode Mode.DEEP_NODE_STOP_AT_END.
      Parameters:
      resource - the DOMResource on the base of which the normalization is done
      xpath - the XPath part of the XPath selector
      position - the position following the character scheme of RFC5147
      Returns:
      a pair of node and position
      Throws:
      SelectorException - if xpath does not evaluate to one node
    • getDeepTextNodeAtPositionStepOverEnd

      protected final org.apache.commons.lang3.tuple.Pair<net.sf.saxon.s9api.XdmNode,Integer> getDeepTextNodeAtPositionStepOverEnd(S9ApiResource<? extends net.sf.saxon.s9api.XdmValue> resource, String xpath, int position) throws SelectorException
      This is an implementation of the normalization step 1 for the mode Mode.DEEP_NODE_STEP_OVER_END.
      Parameters:
      resource - the DOMResource on the base of which the normalization is done
      xpath - the XPath part of the XPath selector
      position - the position following the character scheme of RFC5147
      Returns:
      a pair of node and position
      Throws:
      SelectorException - if xpath does not get one node
    • getNode

      protected final net.sf.saxon.s9api.XdmNode getNode(DOMResource resource, String xpath) throws SelectorException
      Get the node from the DOM resource given by the XPath passed as argument. If the XPath does not evaluate to a single node, this method raises an SelectorException.
      Parameters:
      resource - the DOMResource on the base of which the normalization is done
      xpath - the XPath as String
      Returns:
      an XdmNode which the XPath points to
      Throws:
      SelectorException
    • getNode

      protected final net.sf.saxon.s9api.XdmNode getNode(XdmValueResource resource, String xpath) throws SelectorException
      Get the node from the XDM value resource given by the XPath passed as argument. If the XPath does not evaluate to a single node, this method raises an SelectorException.
      Parameters:
      resource - the XdmValueResource on the base of which the normalization is done
      xpath - the XPath as String
      Returns:
      an XdmNode which the XPath points to
      Throws:
      SelectorException
    • getNode

      protected final net.sf.saxon.s9api.XdmNode getNode(net.sf.saxon.s9api.XdmValue value, String xpath) throws SelectorException
      Get the node from the DOM resource given by the XPath passed as argument. If the XPath does not evaluate to a single node, this method raises an SelectorException.
      Throws:
      SelectorException
    • getDescendantTextNodesWithPosition

      protected final List<org.apache.commons.lang3.tuple.Pair<net.sf.saxon.s9api.XdmNode,Integer>> getDescendantTextNodesWithPosition(net.sf.saxon.s9api.XdmNode fragment, int position)
      Helper method used by implementations of normalization stage 1, that search deep text nodes. This return a list of all pairs of a node and a position, that contain the character scheme position inside a given fragment.

      This method collects all candidates in case of referential ambiguity. See Mode.

      Parameters:
      fragment - as XdmNode inside a DOMResource
      position - the RFC 5147 character scheme position inside the fragment
    • getTextNodesWithPosition

      protected final List<org.apache.commons.lang3.tuple.Pair<net.sf.saxon.s9api.XdmNode,Integer>> getTextNodesWithPosition(net.sf.saxon.s9api.XdmNode fragment, int position)
      Helper method used by implementations of normalization stage 1, that search deep text nodes. This return a list of all pairs of a node and a position, that contain the character scheme position inside a given fragment, or in the first text node before or after it.

      This method collects all candidates in case of referential ambiguity. See Mode.

      Parameters:
      fragment - as XdmNode inside a DOMResource
      position - the RFC 5147 character scheme position inside the fragment
    • posInNormalizedNode

      protected int posInNormalizedNode(net.sf.saxon.s9api.XdmNode textNode, int pos, net.sf.saxon.s9api.XdmNode fragment) throws SelectorException
      Get the new position value based on the old one and the fragment selected by the normalized XPath.
      Parameters:
      textNode - the text node as XdmNode gotten from step 1
      pos - the position gotten form step 1
      fragment - the XdmNode selected by the XPath resulting from step 2
      Returns:
      the position from step 2
      Throws:
      SelectorException
    • reportNotFound

      protected org.apache.commons.lang3.tuple.Pair<net.sf.saxon.s9api.XdmNode,Integer> reportNotFound(String xpath, int position) throws SelectorException
      Report that the position is not found inside the selected fragment.
      Parameters:
      xpath - XPath expression selecting a fragment from a DOMResource
      position - the character scheme position inside the fragment
      Throws:
      SelectorException
    • getDepth

      protected static int getDepth(net.sf.saxon.s9api.XdmNode node)
      A utility method that gets the depth of a node by counting its ancestors.
    • getDepth

      protected static int getDepth(org.apache.commons.lang3.tuple.Pair<net.sf.saxon.s9api.XdmNode,Integer> pair)
      A utility method that gets the depth of a node of a pair of node and position like used in this module.
    • unespace

      protected String unespace(String in)
      A utility function for un-escaping XPaths.
    • pathExpressionWithXPath

      protected String pathExpressionWithXPath(String xpath, net.sf.saxon.s9api.XdmNode node, boolean escaped) throws SelectorException
      Get the path to the XdmNode given as parameter by using the provided XPath for generating a path expression.

      This is typically used in step 2 of normalization.

      Parameters:
      xpath - the XPath for generating a path expression, e.g., fn:xpath()
      node - the XdmNode for which to generate the path expression
      escaped - whether the generated path expression is to be escaped
      Throws:
      SelectorException