org.inria.ns.reflex.xml
Class CanonicalPath

java.lang.Object
  extended by org.inria.ns.reflex.xml.CanonicalPath
All Implemented Interfaces:
Presentable, QNameSet

public class CanonicalPath
extends Object
implements QNameSet, Presentable

Represent the canonical path of a node.

This path only stores the name and the position of each step + the node type of the last step.

The canReach(...) methods are convenient methods that check if a node (eventually represented by its canonical path) is contained or equal to another node.

The normalize(...) methods allow to disambiguish the prefixes used in this path ; one of these methods should be called before using the string form of this path, otherwise the string representation of this path could be displayed with a prefix mapped to several namespace URIs (can occur if this path is built from a node for which itself or one of its ancestor has renamed a prefix).

Author:
Philippe Poulard

Constructor Summary
CanonicalPath(Object node)
          Compute the canonical path of a node.
 
Method Summary
 boolean canReach(CanonicalPath canonicalPath)
          Check if a path can be reached by this path, that is to say that the subtree selected by the candidate path would be part of the subtree selected by this path.
 boolean canReach(Object node)
          Check if a node can be reached by this path, that is to say that the candidate node would be part of the subtree selected by this path.
 boolean equals(Object o)
          Compares the steps of 2 paths.
 NamespaceContext.Mutable getNamespaces()
          Return the set of namespace mappings defined by the normal form of this canonical path.
 void normalize()
          Normalize this path.
 void normalize(NamespaceContext.Mutable namespaces)
          Normalize this path regarding the given mutable namespace context.
 StringBuffer toPrettyString()
          Return a pretty string representation of this.
 StringBuffer toPrettyString(StringBuffer buf)
          Append a pretty string representation of this to the given buffer.
 String toString()
          Return the string representation of this path, that is to say the canonical XPath expression that allows to reach the node that was used to build this path.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CanonicalPath

public CanonicalPath(Object node)
Compute the canonical path of a node.

Parameters:
node - The node.
  • a DOM node
  • a SAX node
  • an X-operable object
See Also:
Node, SAXNode, XOperable
Method Detail

canReach

public boolean canReach(Object node)
Check if a node can be reached by this path, that is to say that the candidate node would be part of the subtree selected by this path.

Examples

this pathcan reach ?node's path
/a/b/c /a/b/c
/a/b/c /a/b/c/d/e
/a/b/c /d/e/f
/acme:foo[1]/bar[2] /acme:foo[1]/bar[2]/acme[3]
/a/a/a /a/a/a/a
/a/a/a/a /a/a/a

Parameters:
node - The candidate node.
  • a DOM node
  • a SAX node
  • an X-operable object
Returns:
true if this node contains the candidate node, false otherwise.
See Also:
canReach(CanonicalPath)

canReach

public boolean canReach(CanonicalPath canonicalPath)
Check if a path can be reached by this path, that is to say that the subtree selected by the candidate path would be part of the subtree selected by this path.

A path tested upon itself return true.

2 paths p1 and p2 that are equals return true with p1.canReach(p2) and p2.canReach(p1).

Parameters:
canonicalPath - The path to test.
Returns:
true if the given canonical path is a subpath of this path or is equal to this path, false otherwise.
See Also:
canReach(Object)

equals

public boolean equals(Object o)
Compares the steps of 2 paths.

Overrides:
equals in class Object
Parameters:
o - The object to compare.
Returns:
true if the object to compare is a canonical path that have the same steps of this path.

normalize

public void normalize()
Normalize this path.

An unprefixed name bound to a namespace URI is changed to a prefixed name. The prefix endorses the form "ns[A-Z][n]".

When needed, prefixes that are bound to several namespace URIs (in the case where they had been redefined) are changed (a number is appended to such prefixes).

In any case, before generating a new prefix, a lookup for an existing prefix already bound to the expected namespace URI is performed.

The first call to this method cause the normalization, next calls are ignored.

Specified by:
normalize in interface QNameSet
See Also:
normalize(NamespaceContext.Mutable), QNameSet.normalize()

getNamespaces

public NamespaceContext.Mutable getNamespaces()
Return the set of namespace mappings defined by the normal form of this canonical path. This method will normalize this canonical path only if necessary.

Returns:
The namespace context that contains the mappings for the normal form of this canonical path.
See Also:
normalize(org.inria.ns.reflex.xml.NamespaceContext.Mutable), QNameSet.normalize()

normalize

public void normalize(NamespaceContext.Mutable namespaces)
Normalize this path regarding the given mutable namespace context.

Unlike the other normalize() method, this one perform the normalization each time it is invoked, as a normalized path may be incoherent regarding a given namespace context.

The operations performed are the same of the other method but the existing mappings found in the namespace context are preserved ; new definitions are added if necessary.

To populate an empty namespace context, use preferably getNamespaces().

Specified by:
normalize in interface QNameSet
Parameters:
namespaces - The mutable namespace context that might be augmented with new definition if this path contains prefixed names.
See Also:
normalize(), QNameSet.normalize(NamespaceContext.Mutable), getNamespaces()

toString

public String toString()
Return the string representation of this path, that is to say the canonical XPath expression that allows to reach the node that was used to build this path.

*** The returned string is not normalized automatically. The relevant method must be invoked before. ***

Overrides:
toString in class Object
See Also:
normalize(), Object.toString()

toPrettyString

public StringBuffer toPrettyString()
Return a pretty string representation of this.

Specified by:
toPrettyString in interface Presentable
Returns:
A new string buffer.
See Also:
toPrettyString(StringBuffer)

toPrettyString

public StringBuffer toPrettyString(StringBuffer buf)
Append a pretty string representation of this to the given buffer.

After the normalization, the string value of this path is followed with all mappings used in this path (a single mapping per line).

Specified by:
toPrettyString in interface Presentable
Parameters:
buf - The buffer to append to.
Returns:
The buffer given.
See Also:
toString()