public class JcrUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static Value[] |
NO_VALUES
A pre-allocated empty array of values.
|
static String |
REPOSITORY_URI
The repository URI parameter name used by the
getRepository(String) method. |
| Modifier and Type | Method and Description |
|---|---|
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. |
Calendar |
getLastModified(Node node)
Returns the last modified date of the given file node.
|
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 Node |
getOrCreateByPath(Node baseNode,
String path,
boolean createUniqueLeaf,
String intermediateNodeType,
String nodeType,
boolean autoSave)
Creates or gets the
Node at the given path
relative to the baseNode. |
static Node |
getOrCreateByPath(String absolutePath,
boolean createUniqueLeaf,
String intermediateNodeType,
String nodeType,
Session session,
boolean autoSave)
Creates or gets the
Node at the given Path. |
static Node |
getOrCreateByPath(String absolutePath,
String nodeType,
Session session)
Creates or gets the
Node at the given Path. |
static Node |
getOrCreateByPath(String absolutePath,
String intermediateNodeType,
String nodeType,
Session session,
boolean autoSave)
Creates or gets the
Node at the given Path. |
static Node |
getOrCreateUniqueByPath(Node parent,
String nodeNameHint,
String nodeType)
Creates or gets the
Node at the given Path. |
static Node |
getOrCreateUniqueByPath(String pathHint,
String nodeType,
Session session)
Creates a
Node at the given Path. |
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.getProperties(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 int |
getPropertyType(String name)
Returns the numeric constant value of the property type with
the specified name.
|
static String[] |
getPropertyTypeNames(boolean includeUndefined)
Return the property type names including or excluding 'undefined' depending
on the specified flag.
|
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.
|
InputStream |
readFile(Node node)
Returns a stream for reading the contents of the file stored at the
given node.
|
void |
readFile(Node node,
OutputStream output)
Writes the contents of file stored at the given node to the given
stream.
|
void |
setLastModified(Node node,
Calendar date)
Sets the last modified date of the given file node.
|
static String |
toString(Item item)
Returns a string representation of the given item.
|
public static final String REPOSITORY_URI
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.
public static final Value[] NO_VALUES
public static Repository getRepository() throws RepositoryException
getRepository(Map) with a
null parameter map.RepositoryException - if a default repository is not available
or can not be accessedRepositoryFactory.getRepository(Map)public static Repository getRepository(Map<String,String> parameters) throws RepositoryException
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 - repository settingsRepositoryException - if the repository can not be accessed,
or if an appropriate repository factory
is not availablepublic static Repository getRepository(String uri) throws RepositoryException
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:
JndiRepositoryFactory class for more details.
uri - repository URIRepositoryException - if the repository can not be accessed,
or if the given URI is unknown or invalidpublic static Iterable<Node> getSharedSet(Node node) throws RepositoryException
Node.getSharedSet() on the given node and returns
the resulting NodeIterator as an Iterable instance
for use in a Java 5 for-each loop.node - shared nodeRepositoryException - if the Node.getSharedSet() call failsNodeIterablepublic static Iterable<Node> getChildNodes(Node node) throws RepositoryException
Node.getNodes() on the given node and returns the
resulting NodeIterator as an Iterable instance
for use in a Java 5 for-each loop.node - parent nodeRepositoryException - if the Node.getNodes() call failsNodeIterablepublic static Iterable<Node> getChildNodes(Node node, String pattern) throws RepositoryException
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.node - parent nodepattern - node name patternRepositoryException - if the Node.getNodes(String) call failsNodeIterablepublic static Iterable<Node> getChildNodes(Node node, String[] globs) throws RepositoryException
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.node - parent nodeglobs - node name patternRepositoryException - if the Node.getNodes(String[]) call failsNodeIterablepublic static Iterable<Property> getProperties(Node node) throws RepositoryException
Node.getProperties() on the given node and returns the
resulting NodeIterator as an Iterable instance
for use in a Java 5 for-each loop.node - nodeRepositoryException - if the Node.getProperties() call failsPropertyIterablepublic static Iterable<Property> getProperties(Node node, String pattern) throws RepositoryException
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.node - nodepattern - property name patternRepositoryException - if the Node.getProperties(String) call failsPropertyIterablepublic static Iterable<Property> getProperties(Node node, String[] globs) throws RepositoryException
Node.getProperties(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.node - nodeglobs - property name globsRepositoryException - if the Node.getProperties(String[]) call failsPropertyIterablepublic static Iterable<Property> getReferences(Node node) throws RepositoryException
Node.getReferences() on the given node and returns the
resulting PropertyIterator as an Iterable
instance for use in a Java 5 for-each loop.node - reference targetRepositoryException - if the Node.getReferences() call failsPropertyIterablepublic static Iterable<Property> getReferences(Node node, String name) throws RepositoryException
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.node - reference targetname - reference property nameRepositoryException - if the Node.getReferences(String) call failsPropertyIterablepublic static Iterable<Property> getWeakReferences(Node node) throws RepositoryException
Node.getWeakReferences() on the given node and returns the
resulting PropertyIterator as an Iterable
instance for use in a Java 5 for-each loop.node - reference targetRepositoryException - if the Node.getWeakReferences() call failsPropertyIterablepublic static Iterable<Property> getWeakReferences(Node node, String name) throws RepositoryException
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.node - reference targetname - reference property nameRepositoryException - if the Node.getWeakReferences(String) call failsPropertyIterablepublic static Iterable<Node> getNodes(QueryResult result) throws RepositoryException
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.result - query resultRepositoryException - if the QueryResult.getNodes() call failsNodeIterablepublic static Iterable<Row> getRows(QueryResult result) throws RepositoryException
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.result - query resultRepositoryException - if the QueryResult.getRows() call failsRowIterablepublic static Node getOrAddNode(Node parent, String name) throws RepositoryException
parent - parent nodename - name of the child nodeRepositoryException - if the child node can not be
accessed or createdNode.getNode(String),
Node.addNode(String)public static Node getOrAddNode(Node parent, String name, String type) throws RepositoryException
parent - parent nodename - name of the child nodetype - type of the child node, ignored if the child already existsRepositoryException - if the child node can not be accessed
or createdNode.getNode(String),
Node.addNode(String, String),
Node.isNodeType(String)public static Node getOrAddFolder(Node parent, String name) throws RepositoryException
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.
parent - parent nodename - name of the child nodeRepositoryException - if the child node can not be accessed
or createdpublic static Node putFile(Node parent, String name, String mime, InputStream data) throws RepositoryException
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:
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.
parent - parent nodename - name of the filemime - media type of the filedata - binary content of the fileRepositoryException - if the child node can not be created
or updatedpublic static Node putFile(Node parent, String name, String mime, InputStream data, Calendar date) throws RepositoryException
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:
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.
parent - parent nodename - name of the filemime - media type of the filedata - binary content of the filedate - date of last modificationRepositoryException - if the child node can not be created
or updatedpublic InputStream readFile(Node node) throws RepositoryException
The returned stream contains a reference to the underlying
Binary value instance that will be disposed when the stream
is closed. It is the responsibility of the caller to close the stream
once it is no longer needed.
node - node to be readRepositoryException - if the file can not be accessedpublic void readFile(Node node, OutputStream output) throws RepositoryException, IOException
readFile(Node) method.node - node to be readoutput - to which the file contents are writtenRepositoryException - if the file can not be accessedIOException - if the file can not be read or writtenpublic Calendar getLastModified(Node node) throws RepositoryException
node - file nodenull if not availableRepositoryException - if the last modified date can not be accessedpublic void setLastModified(Node node, Calendar date) throws RepositoryException
node - file nodedate - modified dateRepositoryException - if the last modified date can not be setpublic static String toString(Item item)
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.
item - given node or propertypublic static int getPropertyType(String name) throws IllegalArgumentException
PropertyType.valueFromName(String), but the name lookup
is case insensitive.name - name of the property type (case insensitive)IllegalArgumentException - if the given name is not a valid
property type namepublic static String[] getPropertyTypeNames(boolean includeUndefined)
includeUndefined - If true the returned array will contain the name
of the 'undefined' property type.public static Node getOrCreateByPath(String absolutePath, String nodeType, Session session) throws RepositoryException
Node at the given Path.
In case it has to create the Node all non-existent intermediate path-elements
will be created with the given NodeType.
Changes made are not saved by this method, so session.save()
has to be called to persist them.
absolutePath - absolute path to createnodeType - to use for creation of nodes If null the node type
is determined by the child node definitions of the parent node.session - to useRepositoryException - in case of exception accessing the Repositorypublic static Node getOrCreateByPath(String absolutePath, String intermediateNodeType, String nodeType, Session session, boolean autoSave) throws RepositoryException
Node at the given Path.
In case it has to create the Node all non-existent intermediate path-elements
will be created with the given intermediate node type and the returned node
will be created with the given nodeType.absolutePath - absolute path to createintermediateNodeType - to use for creation of intermediate nodes. If null the node type
is determined by the child node definitions of the parent node.nodeType - to use for creation of the final node. If null the node type
is determined by the child node definitions of the parent node.session - to useautoSave - Should save be called when a new node is created?RepositoryException - in case of exception accessing the Repositorypublic static Node getOrCreateUniqueByPath(String pathHint, String nodeType, Session session) throws RepositoryException
Node at the given Path. In case it has
to create the Node all non-existent intermediate path-elements will be
created with the given intermediate node type and the returned node will
be created with the given nodeType.
If the path points to an existing node, the leaf node name will be
regarded as a name hint and a unique node name will be created by
appending a number to the given name (eg. /some/path/foobar2).
Please note that the uniqueness check is not an atomic JCR operation,
so it is possible that you get a RepositoryException (path
already exists) if another concurrent session created the same node in
the meantime.
Changes made are not saved by this method, so session.save()
has to be called to persist them.
pathHint - path to createnodeType - to use for creation of nodes. . If null the node type
is determined by the child node definitions of the parent node.session - to useRepositoryException - in case of exception accessing the Repositorypublic static Node getOrCreateByPath(String absolutePath, boolean createUniqueLeaf, String intermediateNodeType, String nodeType, Session session, boolean autoSave) throws RepositoryException
Node at the given Path. In
case it has to create the Node all non-existent intermediate
path-elements will be created with the given intermediate node type and
the returned node will be created with the given nodeType.
If the parameter createUniqueLeaf is set, it will not get
an existing node but rather try to create a unique node by appending a
number to the last path element (leaf node). Please note that the
uniqueness check is not an atomic JCR operation, so it is possible
that you get a RepositoryException (path already exists) if
another concurrent session created the same node in the meantime.
absolutePath - absolute path to createcreateUniqueLeaf - whether the leaf of the path should be regarded as a name hint
and a unique node name should be created by appending a number
to the given name (eg. /some/path/foobar2)intermediateNodeType - to use for creation of intermediate nodes. If null the node type
is determined by the child node definitions of the parent node.nodeType - to use for creation of the final node. If null the node type
is determined by the child node definitions of the parent node.session - to useautoSave - Should save be called when a new node is created?RepositoryException - in case of exception accessing the Repositorypublic static Node getOrCreateUniqueByPath(Node parent, String nodeNameHint, String nodeType) throws RepositoryException
Node at the given Path. In
case it has to create the Node all non-existent intermediate
path-elements will be created with the given intermediate node type and
the returned node will be created with the given nodeType.
If the node name points to an existing node, the node name will be
regarded as a name hint and a unique node name will be created by
appending a number to the given name (eg. /some/path/foobar2).
Please note that the uniqueness check is not an atomic JCR operation,
so it is possible that you get a RepositoryException (path
already exists) if another concurrent session created the same node in
the meantime.
Changes made are not saved by this method, so session.save()
has to be called to persist them.
parent - existing parent node for the new nodenodeNameHint - name hint for the new nodenodeType - to use for creation of the node. If null the node type
is determined by the child node definitions of the parent node.RepositoryException - in case of exception accessing the Repositorypublic static Node getOrCreateByPath(Node baseNode, String path, boolean createUniqueLeaf, String intermediateNodeType, String nodeType, boolean autoSave) throws RepositoryException
Node at the given path
relative to the baseNode. In case it has to create the Node all
non-existent intermediate path-elements will be created with the given
intermediate node type and the returned node will be created with the
given nodeType.
If the parameter createUniqueLeaf is set, it will not get
an existing node but rather try to create a unique node by appending a
number to the last path element (leaf node). Please note that the
uniqueness check is not an atomic JCR operation, so it is possible
that you get a RepositoryException (path already exists) if
another concurrent session created the same node in the meantime.
baseNode - existing node that should be the base for the relative pathpath - relative path to createcreateUniqueLeaf - whether the leaf of the path should be regarded as a name hint
and a unique node name should be created by appending a number
to the given name (eg. /some/path/foobar2)intermediateNodeType - to use for creation of intermediate nodes. If null the node type
is determined by the child node definitions of the parent node.nodeType - to use for creation of the final node. If null the node type
is determined by the child node definitions of the parent node.autoSave - Should save be called when a new node is created?RepositoryException - in case of exception accessing the RepositoryCopyright © 2004-2011 The Apache Software Foundation. All Rights Reserved.