org.apache.jackrabbit.commons
Class JcrUtils

java.lang.Object
  extended by org.apache.jackrabbit.commons.JcrUtils

public class JcrUtils
extends Object

Collection of static utility methods for use with the JCR API.

Since:
Apache Jackrabbit 2.0

Field Summary
static String REPOSITORY_URI
          The repository URI parameter name used by the getRepository(String) method.
 
Method Summary
static Iterable<Node> getChildNodes(Node node)
          Calls Node.getNodes() on the given node and returns the resulting NodeIterator as an Iterable instance for use in a Java 5 for-each loop.
static Iterable<Node> getChildNodes(Node node, String pattern)
          Calls Node.getNodes(String) on the given node with the given name pattern and returns the resulting NodeIterator as an Iterable instance for use in a Java 5 for-each loop.
static Iterable<Node> getChildNodes(Node node, String[] globs)
          Calls Node.getNodes(String[]) on the given node with the given name globs and returns the resulting NodeIterator as an Iterable instance for use in a Java 5 for-each loop.
static Iterable<Node> getNodes(QueryResult result)
          Calls QueryResult.getNodes() on the given query result and returns the resulting NodeIterator as an Iterable instance for use in a Java 5 for-each loop.
static Node getOrAddFolder(Node parent, String name)
          Returns the named child of the given node, creating it as an nt:folder node if it does not already exist.
static Node getOrAddNode(Node parent, String name)
          Returns the named child of the given node, creating the child if it does not already exist.
static Node getOrAddNode(Node parent, String name, String type)
          Returns the named child of the given node, creating the child if it does not already exist.
static Iterable<Property> getProperties(Node node)
          Calls Node.getProperties() on the given node and returns the resulting NodeIterator as an Iterable instance for use in a Java 5 for-each loop.
static Iterable<Property> getProperties(Node node, String pattern)
          Calls Node.getProperties(String) on the given node with the given name pattern and returns the resulting PropertyIterator as an Iterable instance for use in a Java 5 for-each loop.
static Iterable<Property> getProperties(Node node, String[] globs)
          Calls Node#getProperty(String[]) on the given node with the given name globs and returns the resulting PropertyIterator as an Iterable instance for use in a Java 5 for-each loop.
static Iterable<Property> getReferences(Node node)
          Calls Node.getReferences() on the given node and returns the resulting PropertyIterator as an Iterable instance for use in a Java 5 for-each loop.
static Iterable<Property> getReferences(Node node, String name)
          Calls Node.getReferences(String) on the given node and returns the resulting PropertyIterator as an Iterable instance for use in a Java 5 for-each loop.
static Repository getRepository()
          Returns the default repository of the current environment.
static Repository getRepository(Map<String,String> parameters)
          Looks up the available repository factories and returns the repository that one of the factories returns for the given settings.
static Repository getRepository(String uri)
          Returns the repository identified by the given URI.
static Iterable<Row> getRows(QueryResult result)
          Calls QueryResult.getRows() on the given query result and returns the resulting RowIterator as an Iterable instance for use in a Java 5 for-each loop.
static Iterable<Node> getSharedSet(Node node)
          Calls Node.getSharedSet() on the given node and returns the resulting NodeIterator as an Iterable instance for use in a Java 5 for-each loop.
static Iterable<Property> getWeakReferences(Node node)
          Calls Node.getWeakReferences() on the given node and returns the resulting PropertyIterator as an Iterable instance for use in a Java 5 for-each loop.
static Iterable<Property> getWeakReferences(Node node, String name)
          Calls Node.getReferences(String) on the given node and returns the resulting PropertyIterator as an Iterable instance for use in a Java 5 for-each loop.
static Node putFile(Node parent, String name, String mime, InputStream data)
          Creates or updates the named child of the given node.
static Node putFile(Node parent, String name, String mime, InputStream data, Calendar date)
          Creates or updates the named child of the given node.
static String toString(Item item)
          Returns a string representation of the given item.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REPOSITORY_URI

public static final String REPOSITORY_URI
The repository URI parameter name used by the getRepository(String) method. All RepositoryFactory implementations that want to support this repository access convention should implement processing of this parameter.

Client applications are recommended to use the getRepository(String) method instead of directly referencing this constant unless they explicitly want to pass also other RepositoryFactory parameters through the getRepository(Map) method.

See Also:
Constant Field Values
Method Detail

getRepository

public static Repository getRepository()
                                throws RepositoryException
Returns the default repository of the current environment. Implemented by calling getRepository(Map) with a null parameter map.

Returns:
default repository
Throws:
RepositoryException - if a default repository is not available or can not be accessed
See Also:
RepositoryFactory.getRepository(Map)

getRepository

public static Repository getRepository(Map<String,String> parameters)
                                throws RepositoryException
Looks up the available repository factories and returns the repository that one of the factories returns for the given settings.

Note that unlike RepositoryFactory.getRepository(Map) this method will throw an exception instead of returning null if the given parameters can not be interpreted.

Parameters:
parameters - repository settings
Returns:
repository reference
Throws:
RepositoryException - if the repository can not be accessed, or if an appropriate repository factory is not available

getRepository

public static Repository getRepository(String uri)
                                throws RepositoryException
Returns the repository identified by the given URI. This feature is implemented by calling the getRepository(Map) method with the REPOSITORY_URI parameter set to the given URI. Any query parameters are moved from the URI to the parameter map.

See the documentation of the repository implementation you want to use for whether it supports this repository URI convention and for what the repository URI should look like. For example, Jackrabbit 2.0 supports the following types of repository URIs:

http(s)://...
A remote repository connection using SPI2DAVex with the given URL. See the jackrabbit-jcr2dav component for more details.
file://...
An embedded Jackrabbit repository located in the given directory. See the jackrabbit-core component for more details.
jndi:...
JNDI lookup for the named repository. See the JndiRepositoryFactory class for more details.

Parameters:
uri - repository URI
Returns:
repository instance
Throws:
RepositoryException - if the repository can not be accessed, or if the given URI is unknown or invalid

getSharedSet

public static Iterable<Node> getSharedSet(Node node)
                                   throws RepositoryException
Calls Node.getSharedSet() on the given node and returns the resulting NodeIterator as an Iterable instance for use in a Java 5 for-each loop.

Parameters:
node - shared node
Returns:
nodes in the shared set
Throws:
RepositoryException - if the Node.getSharedSet() call fails
See Also:
NodeIterable

getChildNodes

public static Iterable<Node> getChildNodes(Node node)
                                    throws RepositoryException
Calls Node.getNodes() on the given node and returns the resulting NodeIterator as an Iterable instance for use in a Java 5 for-each loop.

Parameters:
node - parent node
Returns:
child nodes
Throws:
RepositoryException - if the Node.getNodes() call fails
See Also:
NodeIterable

getChildNodes

public static Iterable<Node> getChildNodes(Node node,
                                           String pattern)
                                    throws RepositoryException
Calls Node.getNodes(String) on the given node with the given name pattern and returns the resulting NodeIterator as an Iterable instance for use in a Java 5 for-each loop.

Parameters:
node - parent node
pattern - node name pattern
Returns:
matching child nodes
Throws:
RepositoryException - if the Node.getNodes(String) call fails
See Also:
NodeIterable

getChildNodes

public static Iterable<Node> getChildNodes(Node node,
                                           String[] globs)
                                    throws RepositoryException
Calls Node.getNodes(String[]) on the given node with the given name globs and returns the resulting NodeIterator as an Iterable instance for use in a Java 5 for-each loop.

Parameters:
node - parent node
pattern - node name pattern
Returns:
matching child nodes
Throws:
RepositoryException - if the Node.getNodes(String[]) call fails
See Also:
NodeIterable

getProperties

public static Iterable<Property> getProperties(Node node)
                                        throws RepositoryException
Calls Node.getProperties() on the given node and returns the resulting NodeIterator as an Iterable instance for use in a Java 5 for-each loop.

Parameters:
node - node
Returns:
properties of the node
Throws:
RepositoryException - if the Node.getProperties() call fails
See Also:
PropertyIterable

getProperties

public static Iterable<Property> getProperties(Node node,
                                               String pattern)
                                        throws RepositoryException
Calls Node.getProperties(String) on the given node with the given name pattern and returns the resulting PropertyIterator as an Iterable instance for use in a Java 5 for-each loop.

Parameters:
node - node
pattern - property name pattern
Returns:
matching properties of the node
Throws:
RepositoryException - if the Node.getProperties(String) call fails
See Also:
PropertyIterable

getProperties

public static Iterable<Property> getProperties(Node node,
                                               String[] globs)
                                        throws RepositoryException
Calls Node#getProperty(String[]) on the given node with the given name globs and returns the resulting PropertyIterator as an Iterable instance for use in a Java 5 for-each loop.

Parameters:
node - node
pattern - property name globs
Returns:
matching properties of the node
Throws:
RepositoryException - if the Node.getProperties(String[]) call fails
See Also:
PropertyIterable

getReferences

public static Iterable<Property> getReferences(Node node)
                                        throws RepositoryException
Calls Node.getReferences() on the given node and returns the resulting PropertyIterator as an Iterable instance for use in a Java 5 for-each loop.

Parameters:
node - reference target
Returns:
references that point to the given node
Throws:
RepositoryException - if the Node.getReferences() call fails
See Also:
PropertyIterable

getReferences

public static Iterable<Property> getReferences(Node node,
                                               String name)
                                        throws RepositoryException
Calls Node.getReferences(String) on the given node and returns the resulting PropertyIterator as an Iterable instance for use in a Java 5 for-each loop.

Parameters:
node - reference target
name - reference property name
Returns:
references with the given name that point to the given node
Throws:
RepositoryException - if the Node.getReferences(String) call fails
See Also:
PropertyIterable

getWeakReferences

public static Iterable<Property> getWeakReferences(Node node)
                                            throws RepositoryException
Calls Node.getWeakReferences() on the given node and returns the resulting PropertyIterator as an Iterable instance for use in a Java 5 for-each loop.

Parameters:
node - reference target
Returns:
weak references that point to the given node
Throws:
RepositoryException - if the Node.getWeakReferences() call fails
See Also:
PropertyIterable

getWeakReferences

public static Iterable<Property> getWeakReferences(Node node,
                                                   String name)
                                            throws RepositoryException
Calls Node.getReferences(String) on the given node and returns the resulting PropertyIterator as an Iterable instance for use in a Java 5 for-each loop.

Parameters:
node - reference target
name - reference property name
Returns:
weak references with the given name that point to the given node
Throws:
RepositoryException - if the Node.getWeakReferences(String) call fails
See Also:
PropertyIterable

getNodes

public static Iterable<Node> getNodes(QueryResult result)
                               throws RepositoryException
Calls QueryResult.getNodes() on the given query result and returns the resulting NodeIterator as an Iterable instance for use in a Java 5 for-each loop.

Parameters:
result - query result
Returns:
nodes in the query result
Throws:
RepositoryException - if the QueryResult.getNodes() call fails
See Also:
NodeIterable

getRows

public static Iterable<Row> getRows(QueryResult result)
                             throws RepositoryException
Calls QueryResult.getRows() on the given query result and returns the resulting RowIterator as an Iterable instance for use in a Java 5 for-each loop.

Parameters:
result - query result
Returns:
rows in the query result
Throws:
RepositoryException - if the QueryResult.getRows() call fails
See Also:
RowIterable

getOrAddNode

public static Node getOrAddNode(Node parent,
                                String name)
                         throws RepositoryException
Returns the named child of the given node, creating the child if it does not already exist. If the child node gets added, then its type will be determined by the child node definitions associated with the parent node. The caller is expected to take care of saving or discarding any transient changes.

Parameters:
parent - parent node
name - name of the child node
Returns:
the child node
Throws:
RepositoryException - if the child node can not be accessed or created
See Also:
Node.getNode(String), Node.addNode(String)

getOrAddNode

public static Node getOrAddNode(Node parent,
                                String name,
                                String type)
                         throws RepositoryException
Returns the named child of the given node, creating the child if it does not already exist. If the child node gets added, then it is created with the given node type. The caller is expected to take care of saving or discarding any transient changes.

Parameters:
parent - parent node
name - name of the child node
type - type of the child node, ignored if the child already exists
Returns:
the child node
Throws:
RepositoryException - if the child node can not be accessed or created
See Also:
Node.getNode(String), Node.addNode(String, String), Node.isNodeType(String)

getOrAddFolder

public static Node getOrAddFolder(Node parent,
                                  String name)
                           throws RepositoryException
Returns the named child of the given node, creating it as an nt:folder node if it does not already exist. The caller is expected to take care of saving or discarding any transient changes.

Note that the type of the returned node is not guaranteed to match nt:folder in case the node already existed. The caller can use an explicit Node.isNodeType(String) check if needed, or simply use a data-first approach and not worry about the node type until a constraint violation is encountered.

Parameters:
parent - parent node
name - name of the child node
Returns:
the child node
Throws:
RepositoryException - if the child node can not be accessed or created

putFile

public static Node putFile(Node parent,
                           String name,
                           String mime,
                           InputStream data)
                    throws RepositoryException
Creates or updates the named child of the given node. If the child does not already exist, then it is created using the nt:file node type. This file child node is returned from this method.

If the file node does not already contain a jcr:content child, then one is created using the nt:resource node type. The following properties are set on the jcr:content node:

jcr:mimeType
media type
jcr:encoding (optional)
charset parameter of the media type, if any
jcr:lastModified
current time
jcr:data
binary content

Note that the types of the returned node or the jcr:content child are not guaranteed to match nt:file and nt:resource in case the nodes already existed. The caller can use an explicit Node.isNodeType(String) check if needed, or simply use a data-first approach and not worry about the node type until a constraint violation is encountered.

The given binary content stream is closed by this method.

Parameters:
parent - parent node
name - name of the file
mime - media type of the file
data - binary content of the file
Returns:
the child node
Throws:
RepositoryException - if the child node can not be created or updated

putFile

public static Node putFile(Node parent,
                           String name,
                           String mime,
                           InputStream data,
                           Calendar date)
                    throws RepositoryException
Creates or updates the named child of the given node. If the child does not already exist, then it is created using the nt:file node type. This file child node is returned from this method.

If the file node does not already contain a jcr:content child, then one is created using the nt:resource node type. The following properties are set on the jcr:content node:

jcr:mimeType
media type
jcr:encoding (optional)
charset parameter of the media type, if any
jcr:lastModified
date of last modification
jcr:data
binary content

Note that the types of the returned node or the jcr:content child are not guaranteed to match nt:file and nt:resource in case the nodes already existed. The caller can use an explicit Node.isNodeType(String) check if needed, or simply use a data-first approach and not worry about the node type until a constraint violation is encountered.

The given binary content stream is closed by this method.

Parameters:
parent - parent node
name - name of the file
mime - media type of the file
data - binary content of the file
date - date of last modification
Returns:
the child node
Throws:
RepositoryException - if the child node can not be created or updated

toString

public static String toString(Item item)
Returns a string representation of the given item. The returned string is designed to be easily readable while providing maximum amount of information for logging and debugging purposes.

The returned string is not meant to be parsed and the exact contents can change in future releases. The current string representation of a node is "/path/to/node [type]" and the representation of a property is "@name = value(s)". Binary values are expressed like "<123 bytes>" and other values as their standard binary representation. Multi-valued properties have their values listed in like "[ v1, v2, v3, ... ]". No more than the three first values are included. Long string values are truncated.

Parameters:
item - given node or property
Returns:
string representation of the given item


Copyright © 2004-2010 The Apache Software Foundation. All Rights Reserved.