Class JcrUtil
- java.lang.Object
-
- com.day.cq.commons.jcr.JcrUtil
-
public class JcrUtil extends java.lang.ObjectUtility for common JCR tasks
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String[]HYPHEN_LABEL_CHAR_MAPPINGthe list of replacement string for non-valid jcr characters.static java.lang.StringNON_VALID_CHARSNon-valid name characters.static java.lang.String[]STANDARD_LABEL_CHAR_MAPPINGthe list of replacement string for non-valid jcr characters.
-
Constructor Summary
Constructors Constructor Description JcrUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Itemcopy(Item src, Node dstParent, java.lang.String name)Copy thesrcitem into thedstParentnode.static Nodecopy(Node src, Node dstParent, java.lang.String name)Copy thesrcnode into thedstParentnode.static Nodecopy(Node src, Node dstParent, java.lang.String name, boolean bestEffort)Copy thesrcnode into thedstParentnode.static Propertycopy(Property src, Node dstParent, java.lang.String name)Copy thesrcproperty into thedstParentnode.static intcopyResource(Session session, java.lang.String path, java.io.Writer out)Copies the contents of the resource to the given writer.static NodecreatePath(java.lang.String absolutePath, boolean createUniqueLeaf, java.lang.String intermediateNodeType, java.lang.String nodeType, Session session, boolean autoSave)Creates or gets theNodeat the given Path.static NodecreatePath(java.lang.String absolutePath, java.lang.String intermediateNodeType, java.lang.String nodeType, Session session, boolean autoSave)Creates or gets theNodeat the given Path.static NodecreatePath(java.lang.String absolutePath, java.lang.String nodeType, Session session)Creates or gets theNodeat the given Path.static NodecreatePath(Node baseNode, java.lang.String path, boolean createUniqueLeaf, java.lang.String intermediateNodeType, java.lang.String nodeType, Session session, boolean autoSave)Creates or gets theNodeat the given path relative to the baseNode.static NodecreateUniqueNode(Node parent, java.lang.String nodeNameHint, java.lang.String nodeType, Session session)Creates or gets theNodeat the given Path.static NodecreateUniquePath(java.lang.String pathHint, java.lang.String nodeType, Session session)Creates aNodeat the given Path.static java.lang.StringcreateValidChildName(Node node, java.lang.String name)Create a valid name for a child of thenode.static java.lang.StringcreateValidName(java.lang.String title)Create a valid label out of an arbitrary string.static java.lang.StringcreateValidName(java.lang.String title, java.lang.String[] labelCharMapping)Create a valid label out of an arbitrary string with a custom character mapping.static java.lang.StringcreateValidName(java.lang.String title, java.lang.String[] labelCharMapping, java.lang.String defaultReplacementCharacter)Create a valid label out of an arbitrary string with a custom character mapping.static ValuecreateValue(java.lang.Object value, Session session)Creates aJCR Valuefor the given object with the given Session.static java.lang.StringescapeIllegalJcrChars(java.lang.String name)Escapes all illegal JCR name characters of a string.static booleanisValidName(java.lang.String name)Checks if the name is not empty and contains only valid chars.static booleansetChildNodeOrder(Node parent, java.lang.String nameList)Restores the odering of the nodes according to the given comma seperated name list.static booleansetChildNodeOrder(Node parent, java.lang.String[] names)Restores the odering of the nodes according to the given name list.static voidsetProperty(Node node, java.lang.String propertyName, java.lang.Object propertyValue)Sets the value of the property.static java.lang.StringunescapeIllegalJcrChars(java.lang.String name)Unescapes previously escaped jcr chars.
-
-
-
Field Detail
-
STANDARD_LABEL_CHAR_MAPPING
public static final java.lang.String[] STANDARD_LABEL_CHAR_MAPPING
the list of replacement string for non-valid jcr characters. illegal characters are replaced by an underscore ("_").
-
HYPHEN_LABEL_CHAR_MAPPING
public static final java.lang.String[] HYPHEN_LABEL_CHAR_MAPPING
the list of replacement string for non-valid jcr characters. illegal characters are replaced by a hyphen ("-").
-
NON_VALID_CHARS
public static final java.lang.String NON_VALID_CHARS
Non-valid name characters.- See Also:
- Constant Field Values
-
-
Method Detail
-
escapeIllegalJcrChars
public static java.lang.String escapeIllegalJcrChars(java.lang.String name)
Escapes all illegal JCR name characters of a string. The encoding is loosely modeled after URI encoding, but only encodes the characters it absolutely needs to in order to make the resulting string a valid JCR name. UseunescapeIllegalJcrChars(String)for decoding.QName EBNF:
simplename ::= onecharsimplename | twocharsimplename | threeormorecharname onecharsimplename ::= (* Any Unicode character except: '.', '/', ':', '[', ']', '*', ''', '"', '|' or any whitespace character *) twocharsimplename ::= '.' onecharsimplename | onecharsimplename '.' | onecharsimplename onecharsimplename threeormorecharname ::= nonspace string nonspace string ::= char | string char char ::= nonspace | ' ' nonspace ::= (* Any Unicode character except: '/', ':', '[', ']', '*', ''', '"', '|' or any whitespace character *)
- Parameters:
name- the name to escape- Returns:
- the escaped name
-
unescapeIllegalJcrChars
public static java.lang.String unescapeIllegalJcrChars(java.lang.String name)
Unescapes previously escaped jcr chars.Please note, that this does not exactly the same as the url related
Text.unescape(String), since it handles the byte-encoding differently.- Parameters:
name- the name to unescape- Returns:
- the unescaped name
-
createPath
public static Node createPath(java.lang.String absolutePath, java.lang.String nodeType, Session session) throws RepositoryException
Creates or gets theNodeat 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.- Parameters:
absolutePath- absolute path to createnodeType- to use for creation of nodessession- to use- Returns:
- the Node at path
- Throws:
RepositoryException- in case of exception accessing the Repository
-
createPath
public static Node createPath(java.lang.String absolutePath, java.lang.String intermediateNodeType, java.lang.String nodeType, Session session, boolean autoSave) throws RepositoryException
Creates or gets theNodeat 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.- Parameters:
absolutePath- absolute path to createintermediateNodeType- to use for creation of intermediate nodesnodeType- to use for creation of the final nodesession- to useautoSave- Should save be called when a new node is created?- Returns:
- the Node at absolutePath
- Throws:
RepositoryException- in case of exception accessing the Repository
-
createUniquePath
public static Node createUniquePath(java.lang.String pathHint, java.lang.String nodeType, Session session) throws RepositoryException
Creates aNodeat 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 aRepositoryException(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.- Parameters:
pathHint- path to createnodeType- to use for creation of nodessession- to use- Returns:
- the newly created Node
- Throws:
RepositoryException- in case of exception accessing the Repository
-
createPath
public static Node createPath(java.lang.String absolutePath, boolean createUniqueLeaf, java.lang.String intermediateNodeType, java.lang.String nodeType, Session session, boolean autoSave) throws RepositoryException
Creates or gets theNodeat 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
createUniqueLeafis 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 aRepositoryException(path already exists) if another concurrent session created the same node in the meantime.- Parameters:
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 nodesnodeType- to use for creation of the final nodesession- to useautoSave- Should save be called when a new node is created?- Returns:
- the Node at absolutePath
- Throws:
RepositoryException- in case of exception accessing the Repository
-
createUniqueNode
public static Node createUniqueNode(Node parent, java.lang.String nodeNameHint, java.lang.String nodeType, Session session) throws RepositoryException
Creates or gets theNodeat 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 aRepositoryException(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.- Parameters:
parent- existing parent node for the new nodenodeNameHint- name hint for the new nodenodeType- to use for creation of the nodesession- to use- Returns:
- the newly created Node
- Throws:
RepositoryException- in case of exception accessing the Repository
-
createPath
public static Node createPath(Node baseNode, java.lang.String path, boolean createUniqueLeaf, java.lang.String intermediateNodeType, java.lang.String nodeType, Session session, boolean autoSave) throws RepositoryException
Creates or gets theNodeat 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
createUniqueLeafis 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 aRepositoryException(path already exists) if another concurrent session created the same node in the meantime.- Parameters:
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 nodesnodeType- to use for creation of the final nodesession- to useautoSave- Should save be called when a new node is created?- Returns:
- the Node at path
- Throws:
RepositoryException- in case of exception accessing the Repository
-
createValidName
public static java.lang.String createValidName(java.lang.String title)
Create a valid label out of an arbitrary string. Same as callingcreateValidName(String, String[])usingSTANDARD_LABEL_CHAR_MAPPING.- Parameters:
title- title to convert into a name- Returns:
- a valid label string
-
createValidName
public static java.lang.String createValidName(java.lang.String title, java.lang.String[] labelCharMapping)Create a valid label out of an arbitrary string with a custom character mapping.- Parameters:
title- title to convert into a namelabelCharMapping- a mapping of chars (index of the array) to strings that should be used as replacement for the characters- Returns:
- a valid label string
-
createValidName
public static java.lang.String createValidName(java.lang.String title, java.lang.String[] labelCharMapping, java.lang.String defaultReplacementCharacter)Create a valid label out of an arbitrary string with a custom character mapping.- Parameters:
title- title to convert into a namelabelCharMapping- a mapping of chars (index of the array) to strings that should be used as replacement for the charactersdefaultReplacementCharacter- the default character to use for characters not mapped in the labelCharMapping parameter- Returns:
- a valid label string
-
createValidChildName
public static java.lang.String createValidChildName(Node node, java.lang.String name) throws RepositoryException
Create a valid name for a child of thenode. Generates a valid name and test if child already exists. If name is already existing, iterate until a unique one is found- Parameters:
node- parent nodename- the name to check- Returns:
- a valid label string
- Throws:
RepositoryException- in case of error, accessing the Repository
-
isValidName
public static boolean isValidName(java.lang.String name)
Checks if the name is not empty and contains only valid chars.- Parameters:
name- the name to check- Returns:
trueif the name is valid
-
createValue
public static Value createValue(java.lang.Object value, Session session) throws RepositoryException
Creates aJCR Valuefor the given object with the given Session. Selects the thePropertyTypeaccording the instance of the object's Class- Parameters:
value- objectsession- to create value for- Returns:
- the value or null if not convertible to a valid PropertyType
- Throws:
RepositoryException- in case of error, accessing the Repository
-
setProperty
public static void setProperty(Node node, java.lang.String propertyName, java.lang.Object propertyValue) throws RepositoryException
Sets the value of the property. Selects thePropertyTypeaccording to the instance of the object's class.- Parameters:
node- The node where the property will be set on.propertyName- The name of the property.propertyValue- The value for the property.- Throws:
RepositoryException- if a repository error occurs
-
copy
public static Item copy(Item src, Node dstParent, java.lang.String name) throws RepositoryException
Copy thesrcitem into thedstParentnode. The name of the newly created item is set toname.- Parameters:
src- The item to copy to the new locationdstParent- The node into which thesrcnode is to be copiedname- The name of the newly created item. If this isnullthe new item gets the same name as thesrcitem.- Returns:
- the new item
- Throws:
RepositoryException- May be thrown in case of any problem copying the content.- See Also:
copy(Node, Node, String),copy(Property , Node, String)
-
copy
public static Node copy(Node src, Node dstParent, java.lang.String name) throws RepositoryException
Copy thesrcnode into thedstParentnode. The name of the newly created node is set toname.This method does a recursive (deep) copy of the subtree rooted at the source node to the destination. Any protected child nodes and and properties are not copied.
- Parameters:
src- The node to copy to the new locationdstParent- The node into which thesrcnode is to be copiedname- The name of the newly created node. If this isnullthe new node gets the same name as thesrcnode.- Returns:
- the newly created node
- Throws:
RepositoryException- May be thrown in case of any problem copying the content.
-
copy
public static Node copy(Node src, Node dstParent, java.lang.String name, boolean bestEffort) throws RepositoryException
Copy thesrcnode into thedstParentnode. The name of the newly created node is set toname.This method does a recursive (deep) copy of the subtree rooted at the source node to the destination. Any protected child nodes and and properties are not copied. The
bestEffortargument specifies whether or not copying is aborted when adding a mixin, creating a property or creating a child node fails with aRepositoryException.- Parameters:
src- The node to copy to the new locationdstParent- The node into which thesrcnode is to be copiedname- The name of the newly created node. If this isnullthe new node gets the same name as thesrcnode.bestEffort-truefor best effort copying: skip mixins, properties and child nodes which cannot be added.- Returns:
- the newly created node
- Throws:
RepositoryException- May be thrown in case of any problem copying the content.
-
copy
public static Property copy(Property src, Node dstParent, java.lang.String name) throws RepositoryException
Copy thesrcproperty into thedstParentnode. The name of the newly created property is set toname.If the source property is protected, this method does nothing.
- Parameters:
src- The property to copy to the new locationdstParent- The node into which thesrcproperty is to be copiedname- The name of the newly created property. If this isnullthe new property gets the same name as thesrcproperty.- Returns:
- the new property or
nullif nothing was copied - Throws:
RepositoryException- May be thrown in case of any problem copying the content.
-
setChildNodeOrder
public static boolean setChildNodeOrder(Node parent, java.lang.String nameList) throws RepositoryException
Restores the odering of the nodes according to the given comma seperated name list. Please note, that no changes are saved.- Parameters:
parent- the parent nodenameList- the list of names- Returns:
trueif the nodes were reordered- Throws:
RepositoryException- if an error occurrs.
-
setChildNodeOrder
public static boolean setChildNodeOrder(Node parent, java.lang.String[] names) throws RepositoryException
Restores the odering of the nodes according to the given name list. Please note, that no changes are saved.- Parameters:
parent- the parent nodenames- the list of names- Returns:
trueif the nodes were reordered- Throws:
RepositoryException- if an error occurrs.
-
copyResource
public static int copyResource(Session session, java.lang.String path, java.io.Writer out) throws java.io.IOException, RepositoryException
Copies the contents of the resource to the given writer. The resource atpathneeds to have a binary property at the relative path "jcr:content/jcr:data". the string is encoded using the optional "jcr:content/jcr:encoding" property.- Parameters:
session- sessionpath- file node pathout- the writer- Returns:
- the number of writter characters
- Throws:
java.io.IOException- if an I/O error occursRepositoryException- if a repository error occurs
-
-