6.2.8 Item Read Methods

The Item interface also contains a number of other methods, inherited by both Node and Property. The following methods provide access to and information about nodes and properties. Item also has other methods applicable to level 2. In a level 1-only implementation they will either do nothing or throw an exception.

javax.jcr.
Item

String

getPath()

Returns the absolute path to this item.

If the path includes items that are same name sibling nodes or multi-value properties then those elements in the path will include the appropriate “square bracket” index notation (for example, /a/b[3]/c).

A RepositoryException is thrown if an error occurs.

String

getName()

Returns the name of this item. The name is the last item in the path, minus any square-bracket index that may exist. If this item is the root node of the workspace (i.e., if this.getDepth() == 0), an empty string will be returned.

A RepositoryException is thrown if an error occurs.

Item

getAncestor(int depth)

Returns the ancestor of the specified depth below the root. An ancestor of depth x is the Item that is x levels down along the path from the root node to this Item.

  • depth = 0 returns the root node.

  • depth = 1 returns the child of the root node along the path to this Item.

  • depth = 2 returns the grandchild of the root node along the path to this Item.

  • And so on to depth = n, where n is the depth of this Item, which returns this Item itself. If depth > n is specified then an ItemNotFoundException is thrown.

An ItemNotFoundException will be thrown if depth < 0 or depth > n where n is the is the depth of this item along the path returned by getPath().

An AccessDeniedException is thrown if the current session does not have sufficient access permissions to retrieve the specified node.

A RepositoryException is thrown if another error occurs.

Node

getParent()

Returns the parent of this Item.

An ItemNotFoundException is thrown if there is no parent node. This only happens if this item is the root node of a workspace.

An AccessDeniedException is thrown if the current session does not have sufficient access permissions to retrieve the parent of this item.

A RepositoryException is thrown if another error occurs.

int

getDepth()

Returns the depth below the root node of this Item (counting this Item itself):

  • The root node returns 0.

  • A property or child node of the root node returns 1.

  • A property or child node of a child node of the root returns 2.

  • And so on to this Item.

A RepositoryException is thrown if an error occurs.

Session

getSession()

Returns the Session through which this Item was acquired.

A RepositoryException is thrown if an error occurs.

boolean

isNode()

Returns true if this Item is a Node; returns false if this Item is a Property.

boolean

isSame(Item otherItem)

Returns true if this Item object (the Java object instance) represents the same actual repository item as the object otherItem.

This method does not compare the states of the two items. For example, if two Item objects representing the same actual repository item have been retrieved through two different sessions and one has been modified, then this method will still return true when comparing these two objects. Note that if two Item objects representing the same repository item are retrieved through the same session they will always reflect the same state (see 7.1.3 Reflecting Item State) so comparing state is not an issue.

A RepositoryException is thrown if an error occurs.

void

accept(ItemVisitor visitor)

Accepts an ItemVisitor and calls the appropriate visit method according to whether this Item is a Node or a Property.

This method provides support for the visitor design pattern. It takes an ItemVisitor object that must implement two methods: visit(Node node) and visit(Property property). Depending on whether this Item is a Node or a Property one of the visit methods is called with this Item as the parameter.

The API also provides the abstract class TraversingItemVisitor implementing ItemVisitor, which automatically traverses the hierarchy calling accept at each node and property. It provides the methods entering and leaving that can be overridden in a subclass to perform custom operations.

Throws a RepositoryException if an error occurs.