You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:46:36 UTC
[sling-org-apache-sling-jcr-contentloader] 10/36: SLING-548: Add
methods for setting properties from objects.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.jcr.contentloader-2.0.4-incubator
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-contentloader.git
commit d05e8d207572e691e886985c993fef26362a9543
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Mon Jun 23 12:37:55 2008 +0000
SLING-548: Add methods for setting properties from objects.
git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/trunk/jcr/contentloader@670545 13f79535-47bb-0310-9956-ffa450edef68
---
.../jcr/contentloader/internal/ContentCreator.java | 56 +++++++++++++++++
.../jcr/contentloader/internal/ContentLoader.java | 73 ++++++++++++++++++++++
2 files changed, 129 insertions(+)
diff --git a/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java b/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java
index ade7562..bd72de7 100644
--- a/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java
+++ b/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java
@@ -20,23 +20,79 @@ package org.apache.sling.jcr.contentloader.internal;
import javax.jcr.RepositoryException;
+/**
+ * The <code>ContentCreator</code>
+ * is used by the {@link ContentReader} to create the actual content.
+ */
interface ContentCreator {
+ /**
+ * Create a new node.
+ * To add properties to this node, one of the createProperty() methods
+ * should be called.
+ * To add child nodes, this method should be called to create a new child node.
+ * If all properties and child nodes have been added {@link #finishNode()} must be called.
+ *
+ * @param name The name of the node.
+ * @param primaryNodeType The primary node type or null.
+ * @param mixinNodeTypes The mixin node types or null.
+ * @throws RepositoryException If anything goes wrong.
+ */
void createNode(String name,
String primaryNodeType,
String[] mixinNodeTypes)
throws RepositoryException;
+ /**
+ * Indicates that a node is finished.
+ * The parent node of the current node becomes the current node.
+ * @throws RepositoryException
+ */
void finishNode()
throws RepositoryException;
+ /**
+ * Create a new property to the current node.
+ * @param name The property name.
+ * @param propertyType The type of the property.
+ * @param value The string value.
+ * @throws RepositoryException
+ */
void createProperty(String name,
int propertyType,
String value)
throws RepositoryException;
+ /**
+ * Create a new multi value property to the current node.
+ * @param name The property name.
+ * @param propertyType The type of the property.
+ * @param values The string values.
+ * @throws RepositoryException
+ */
void createProperty(String name,
int propertyType,
String[] values)
throws RepositoryException;
+
+ /**
+ * Add a new property to the current node.
+ * @param name The property name.
+ * @param value The value.
+ * @throws RepositoryException
+ */
+ void createProperty(String name,
+ Object value)
+ throws RepositoryException;
+
+ /**
+ * Add a new multi value property to the current node.
+ * @param name The property name.
+ * @param propertyType The type of the property.
+ * @param values The values.
+ * @throws RepositoryException
+ */
+ void createProperty(String name,
+ Object[] values)
+ throws RepositoryException;
}
diff --git a/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java b/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
index 3e0a629..415b5a7 100644
--- a/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
+++ b/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
@@ -18,7 +18,10 @@
*/
package org.apache.sling.jcr.contentloader.internal;
+import java.io.InputStream;
import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -29,6 +32,8 @@ import javax.jcr.Node;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
+import javax.jcr.Value;
+import javax.jcr.ValueFactory;
/**
* The <code>ContentLoader</code> creates the nodes and properties.
@@ -194,6 +199,74 @@ public class ContentLoader implements ContentCreator {
node.setProperty(name, values, propertyType);
}
+ protected Value createValue(final ValueFactory factory, Object value) {
+ if ( value == null ) {
+ return null;
+ }
+ if ( value instanceof Long ) {
+ return factory.createValue((Long)value);
+ } else if ( value instanceof Date ) {
+ final Calendar c = Calendar.getInstance();
+ c.setTime((Date)value);
+ return factory.createValue(c);
+ } else if ( value instanceof Calendar ) {
+ return factory.createValue((Calendar)value);
+ } else if ( value instanceof Double ) {
+ return factory.createValue((Double)value);
+ } else if ( value instanceof Boolean ) {
+ return factory.createValue((Boolean)value);
+ } else if ( value instanceof InputStream ) {
+ return factory.createValue((InputStream)value);
+ } else {
+ return factory.createValue(value.toString());
+ }
+
+ }
+ /**
+ * @see org.apache.sling.jcr.contentloader.internal.ContentCreator#createProperty(java.lang.String, java.lang.Object)
+ */
+ public void createProperty(String name, Object value)
+ throws RepositoryException {
+ final Node node = this.parentNodeStack.peek();
+ // check if the property already exists, don't overwrite it in this case
+ if (node.hasProperty(name)
+ && !node.getProperty(name).isNew()) {
+ return;
+ }
+ if ( value == null ) {
+ if ( node.hasProperty(name) ) {
+ node.getProperty(name).remove();
+ }
+ } else {
+ final Value jcrValue = this.createValue(node.getSession().getValueFactory(), value);
+ node.setProperty(name, jcrValue);
+ }
+ }
+
+ /**
+ * @see org.apache.sling.jcr.contentloader.internal.ContentCreator#createProperty(java.lang.String, java.lang.Object[])
+ */
+ public void createProperty(String name, Object[] values)
+ throws RepositoryException {
+ final Node node = this.parentNodeStack.peek();
+ // check if the property already exists, don't overwrite it in this case
+ if (node.hasProperty(name)
+ && !node.getProperty(name).isNew()) {
+ return;
+ }
+ if ( values == null || values.length == 0 ) {
+ if ( node.hasProperty(name) ) {
+ node.getProperty(name).remove();
+ }
+ } else {
+ final Value[] jcrValues = new Value[values.length];
+ for(int i = 0; i < values.length; i++) {
+ jcrValues[i] = this.createValue(node.getSession().getValueFactory(), values[i]);
+ }
+ node.setProperty(name, jcrValues);
+ }
+ }
+
/**
* @see org.apache.sling.jcr.contentloader.internal.ContentCreator#finishNode()
*/
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.