Copyright© 2008-2022 Sitevision AB, all rights reserved.
T
- native script value@Requireable(value="Properties") public interface Properties<T>
Extraction of property values from Nodes are typically performed using PropertyUtil
but this utility is less obtrusive to work with in server-side script contexts (WebApps, RESTApps, Script module etc.).
Property value types
The value of a JCR Node property can be resolved as different types.
For instance a property with primary type PropertyType.DATE
can be resolved as Calendar
or as String
.
This utility returns values with a specific type:
PropertyType.DATE
.PropertyType.WEAKREFERENCE
.
Example
Below is an example that demonstrates the behaviour and usage of this utility.
var properties = require('Properties'); ... // get examples // Returns the identifier of the given Node, e.g: '4.540818ed12a6539aa3f80001645' pageId = properties.get(pageNode); // Returns the URI property value of the given Node, e.g. '/4.540818ed12a6539aa3f80001645.html' pageUri = properties.get(pageNode, 'URI'); // Returns the displayName property value of the given Node, e.g. 'The page' pageName = properties.get(pageNode, 'displayName'); pageName = properties.get(pageId, 'displayName'); pageName = properties.get(pageUri, 'displayName'); // Returns a native script object with given properties (that exist) of the specified Node e.g: // { // URI: '/4.540818ed12a6539aa3f80001645.html', // displayName: 'The page', // published: true, // publishDate: 1227618333142 // } pageData = properties.get(pageNode, 'URI', 'displayName', 'published', 'publishDate', 'nameOfNonExistingProperty'); // getArray examples // Returns the identifier of the given Node as array, e.g: ['4.540818ed12a6539aa3f80001645'] pageIdArray = properties.getArray(pageNode); // Returns the identifier for the children of given Node as array, e.g: // [ // '19.540818ed12a6539aa3f80006889', // '4.540818ed12a6539aa3f80001645_pageContent', // ... // ] childIdsArray = properties.getArray(pageNode.getNodes()); // Returns the displayName for the nodes of a menu from a given Node as array, e.g: // [ // 'The Page', // 'Another Page', // ... // ] menuNamesArray = properties.getArray(nodeIteratorUtil.getMenuItems(pageNode), 'displayName'); // Returns the displayName and URI for the hits of a SearchResult as array, e.g: // [ // { // displayName: 'Public API' // URI: '/docs/public-api', // }, // { // displayName: 'REST API' // URI: '/docs/rest-api', // }, // ... // ] hitsDataArray = properties.getArray(searchResult, 'displayName', 'URI');
Security reminder! All output should always be escaped to prevent XSS!
get(Object, String...)
or getArray(Object, String...)
and output the value with underscore escape (e.g. <%- theValueToEscape %>
).
getEscaped(Object, String...)
or
getArrayEscaped(Object, String...)
and output the (already) escaped value using Velocity (e.g. $theEscapedValue
).
Modifier and Type | Method and Description |
---|---|
T |
get(Object aJcrNodeResolvable,
String... aPropertyNames)
Gets named properties from a given Node-resolvable object.
|
T |
getArray(Object aIterable,
String... aPropertyNames)
Processes an iterable object and returns an array of named properties for each Node-resolvable object that is iterated.
|
T |
getArrayEscaped(Object aIterable,
String... aPropertyNames)
Processes an iterable object and returns an array of escaped named properties for each Node-resolvable object that is iterated.
|
T |
getEscaped(Object aJcrNodeResolvable,
String... aPropertyNames)
Gets escaped named properties from a given Node-resolvable object.
|
T get(Object aJcrNodeResolvable, String... aPropertyNames) throws Exception
See code examples in the interface description above.
aJcrNodeResolvable
- something that can be resolved as a Node.
Typically a Node, a Node identifier or a relative URL of a Node (the URI property)aPropertyNames
- the names of the properties of interestException
- if the scripting engine fails to convert property values to a native Javascript objectgetEscaped(Object, String...)
T getEscaped(Object aJcrNodeResolvable, String... aPropertyNames) throws Exception
This method is equivalent with get(Object, String...)
except that all string values will be escaped.
aJcrNodeResolvable
- something that can be resolved as a Node.
Typically a Node, a Node identifier or a relative URL of a Node (the URI property)aPropertyNames
- the names of the properties of interestException
- if the scripting engine fails to convert property values to a native Javascript objectT getArray(Object aIterable, String... aPropertyNames) throws Exception
This method is conceptually equivalent with the get
method but
the core functionality of this method is to process something that can be iterated in order to handle multiple nodes
and deliver properties for them as an array.
See code examples in the interface description above.
aIterable
- something that can be resolved as a Node or something that can be iterated and contains objects that can be resolved
as a Node. Typically an array, NodeIterator
, SearchResult
,
Collection
or FilterSplit
(the accepted nodes will be processed)aPropertyNames
- the names of the properties of interestException
- if the scripting engine fails to convert property values to a native Javascript objectNodeIteratorUtil
,
getArrayEscaped(Object, String...)
T getArrayEscaped(Object aIterable, String... aPropertyNames) throws Exception
This method is equivalent with getArray(Object, String...)
except that all string values will be escaped.
aIterable
- something that can be resolved as a Node or something that can be iterated and contains objects that can be resolved
as a Node. Typically an array, NodeIterator
, SearchResult
,
Collection
or FilterSplit
(the accepted nodes will be processed)aPropertyNames
- the names of the properties of interestException
- if the scripting engine fails to convert property values to a native Javascript objectSitevision - Portal and Content Management Made Easy
Sitevision is an advanced Java enterprise portal product and a portlet container (JSR 286) that implements Java Content Repository (JSR 283).
Copyright© 2008-2022 Sitevision AB, all rights reserved.