Class XmlPattern

java.lang.Object
org.libxmq.imp.xml.XmlPattern

public final class XmlPattern extends Object
Streamable patterns, a port of libxml2 pattern.c (xml_pattern.c): the XPath subset used for xs:selector and xs:field of XML Schema identity constraints (and plain patterns like "a/b", "//c", "/a/@b", alternatives with |). A compiled pattern can be matched against a node of the native dom (patternMatch), or, when streamable, a stream context can be created that is fed with streamPush/streamPushAttr/streamPop while walking a tree in document order. Namespaces are given like libxml2 does: an array of href, prefix pairs terminated by a null href (or null for no namespaces). The prefix xml is always bound to the xml namespace. Compilation errors are not printed: libxml2 2.16 compiles its error messages away in pattern.c, it only returns null. patternCompileSafe returns the error code.
  • Field Details

    • XML_PATTERN_FLAG_DEFAULT

      public static final int XML_PATTERN_FLAG_DEFAULT
      simple pattern match
      See Also:
    • XML_PATTERN_FLAG_XPATH

      public static final int XML_PATTERN_FLAG_XPATH
      standard XPath pattern
      See Also:
    • XML_PATTERN_FLAG_XSSEL

      public static final int XML_PATTERN_FLAG_XSSEL
      XPath subset for schema selector
      See Also:
    • XML_PATTERN_FLAG_XSFIELD

      public static final int XML_PATTERN_FLAG_XSFIELD
      XPath subset for schema field
      See Also:
  • Method Details

    • patternFree

      public static void patternFree(XmlPattern comp)
      Port of xmlFreePattern. Free up the memory allocated by `comp`
      Parameters:
      comp - an XSLT comp
    • patternFreeList

      public static void patternFreeList(XmlPattern comp)
      Port of xmlFreePatternList. Free up the memory allocated by all the elements of `comp`
      Parameters:
      comp - an XSLT comp list
    • streamCtxtFree

      public static void streamCtxtFree(XmlStreamCtxt stream)
      Port of xmlFreeStreamCtxt. Free the stream context
      Parameters:
      stream - the stream context
    • streamPush

      public static int streamPush(XmlStreamCtxt stream, String name, String ns)
      Port of xmlStreamPush. Push new data onto the stream. Both `name` and `ns` being null means the / i.e. the root of the document. This can also act as a reset. Otherwise the function will act as if it has been given an element-node.
      Parameters:
      stream - the stream context
      name - the current name
      ns - the namespace name
      Returns:
      -1 in case of error, 1 if the current state in the stream is a match and 0 otherwise.
    • streamPushNode

      public static int streamPushNode(XmlStreamCtxt stream, String name, String ns, int node_type)
      Port of xmlStreamPushNode. Push new data onto the stream. Both `name` and `ns` being null means the / i.e. the root of the document. This can also act as a reset. Different from streamPush this function can be fed with nodes of type: element-, attribute-, text-, cdata-section-, comment- and processing-instruction-node.
      Parameters:
      stream - the stream context
      name - the current name
      ns - the namespace name
      node_type - the type of the node being pushed
      Returns:
      -1 in case of error, 1 if the current state in the stream is a match and 0 otherwise.
    • streamPushAttr

      public static int streamPushAttr(XmlStreamCtxt stream, String name, String ns)
      Port of xmlStreamPushAttr. Push new attribute data onto the stream. Both `name` and `ns` being null means the root of the document. This can also act as a reset. Otherwise the function will act as if it has been given an attribute-node.
      Parameters:
      stream - the stream context
      name - the current name
      ns - the namespace name
      Returns:
      -1 in case of error, 1 if the current state in the stream is a match and 0 otherwise.
    • streamPop

      public static int streamPop(XmlStreamCtxt stream)
      Port of xmlStreamPop. push one level from the stream.
      Parameters:
      stream - the stream context
      Returns:
      -1 in case of error, 0 otherwise.
    • streamWantsAnyNode

      public static int streamWantsAnyNode(XmlStreamCtxt stream_ctxt)
      Port of xmlStreamWantsAnyNode. Query if the streaming pattern additionally needs to be fed with text-, cdata-section-, comment- and processing-instruction-nodes. If the result is 0 then only element-nodes and attribute-nodes need to be pushed.
      Parameters:
      stream_ctxt - the stream context
      Returns:
      1 in case of need of nodes of the above described types, 0 otherwise. -1 on API errors.
    • patternCompileSafe

      public static int patternCompileSafe(String pattern, int flags, String[] namespaces, XmlPattern[] pattern_out)
      Port of xmlPatternCompileSafe. Compile a pattern.
      Parameters:
      pattern - the pattern to compile
      flags - compilation flags, see XML_PATTERN_*
      namespaces - the prefix definitions, array of [URI, prefix] or null
      pattern_out - output pattern (length 1)
      Returns:
      0 on success, 1 on error, -1 if a memory allocation failed.
    • patternCompile

      public static XmlPattern patternCompile(String pattern, int flags, String[] namespaces)
      Port of xmlPatterncompile. Compile a pattern.
      Parameters:
      pattern - the pattern to compile
      flags - compilation flags, see XML_PATTERN_*
      namespaces - the prefix definitions, array of [URI, prefix] or null
      Returns:
      the compiled form of the pattern or null in case of error
    • patternMatch

      public static int patternMatch(XmlPattern comp, XmlNode node)
      Port of xmlPatternMatch. Test whether the node matches the pattern
      Parameters:
      comp - the precompiled pattern
      node - a node
      Returns:
      1 if it matches, 0 if it doesn't and -1 in case of failure
    • patternGetStreamCtxt

      public static XmlStreamCtxt patternGetStreamCtxt(XmlPattern comp)
      Port of xmlPatternGetStreamCtxt. Get a streaming context for that pattern Use streamCtxtFree to free the context.
      Parameters:
      comp - the precompiled pattern
      Returns:
      the context or null in case of failure
    • patternStreamable

      public static int patternStreamable(XmlPattern comp)
      Port of xmlPatternStreamable. Check if the pattern is streamable i.e. patternGetStreamCtxt should work.
      Parameters:
      comp - the precompiled pattern
      Returns:
      1 if streamable, 0 if not and -1 in case of error.
    • patternMaxDepth

      public static int patternMaxDepth(XmlPattern comp)
      Port of xmlPatternMaxDepth. Check the maximum depth reachable by a pattern
      Parameters:
      comp - the precompiled pattern
      Returns:
      -2 if no limit (using //), otherwise the depth, and -1 in case of error
    • patternMinDepth

      public static int patternMinDepth(XmlPattern comp)
      Port of xmlPatternMinDepth. Check the minimum depth reachable by a pattern, 0 mean the / or . are part of the set.
      Parameters:
      comp - the precompiled pattern
      Returns:
      -1 in case of error otherwise the depth,
    • patternFromRoot

      public static int patternFromRoot(XmlPattern comp)
      Port of xmlPatternFromRoot. Check if the pattern must be looked at from the root.
      Parameters:
      comp - the precompiled pattern
      Returns:
      1 if true, 0 if false and -1 in case of error