Author: mduerig
Date: Fri Sep 7 08:12:48 2012
New Revision: 1381927
URL: http://svn.apache.org/viewvc?rev=1381927&view=rev
Log:
OAK-66: JCR Node Type Management
value constraints for String and Boolean
Added:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/BooleanConstraint.java
(with props)
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/Constraints.java
(with props)
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/StringConstraint.java
(with props)
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeImpl.java
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java
jackrabbit/oak/trunk/oak-jcr/pom.xml
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeImpl.java?rev=1381927&r1=1381926&r2=1381927&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeImpl.java
Fri Sep 7 08:12:48 2012
@@ -39,6 +39,7 @@ import javax.jcr.nodetype.PropertyDefini
import javax.security.auth.Subject;
import org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter;
+import org.apache.jackrabbit.oak.plugins.type.constraint.Constraints;
import org.apache.jackrabbit.oak.spi.security.principal.AdminPrincipal;
import org.apache.jackrabbit.oak.util.NodeUtil;
import org.slf4j.Logger;
@@ -274,7 +275,12 @@ class NodeTypeImpl implements NodeType {
if ((propertyName.equals(name) && !isProtected(definition))
|| "*".equals(name)) {
if (!definition.isMultiple()) {
- // TODO: Check value type, constraints, etc.
+
+ // TODO: Check value type, etc.
+ if (!meetsValueConstraints(value, definition.getValueConstraints()))
{
+ return false;
+ }
+
return true;
}
}
@@ -301,6 +307,20 @@ class NodeTypeImpl implements NodeType {
return false;
}
+ private static boolean meetsValueConstraints(Value value, String[] constraints) {
+ if (constraints == null || constraints.length == 0) {
+ return true;
+ }
+
+ for (String constraint : constraints) {
+ if (Constraints.valueConstraint(value.getType(), constraint).apply(value)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
@Override
public boolean canAddChildNode(String childNodeName) {
for (NodeDefinition definition : getChildNodeDefinitions()) {
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java?rev=1381927&r1=1381926&r2=1381927&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java
Fri Sep 7 08:12:48 2012
@@ -360,7 +360,7 @@ class TypeValidator implements Validator
@Override
public int hashCode() {
- return super.hashCode();
+ return value.hashCode();
}
@Override
@@ -370,7 +370,7 @@ class TypeValidator implements Validator
@Override
public String toString() {
- return super.toString();
+ return value.toString();
}
}
Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/BooleanConstraint.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/BooleanConstraint.java?rev=1381927&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/BooleanConstraint.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/BooleanConstraint.java
Fri Sep 7 08:12:48 2012
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.plugins.type.constraint;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+
+import com.google.common.base.Predicate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class BooleanConstraint implements Predicate<Value> {
+ private static final Logger log = LoggerFactory.getLogger(BooleanConstraint.class);
+
+ private final Boolean requiredValue;
+
+ public BooleanConstraint(String definition) {
+ if ("true".equals(definition)) {
+ requiredValue = true;
+ }
+ else if ("false".equals(definition)) {
+ requiredValue = false;
+ }
+ else {
+ requiredValue = null;
+ log.warn('\'' + definition + "' is not a valid value constraint format for BOOLEAN
values");
+ }
+ }
+
+ @Override
+ public boolean apply(Value value) {
+ try {
+ return value != null && requiredValue != null && value.getBoolean()
== requiredValue;
+ }
+ catch (RepositoryException e) {
+ log.warn("Error checking boolean constraint", e);
+ return false;
+ }
+ }
+}
Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/BooleanConstraint.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/BooleanConstraint.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/Constraints.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/Constraints.java?rev=1381927&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/Constraints.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/Constraints.java
Fri Sep 7 08:12:48 2012
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.plugins.type.constraint;
+
+import javax.jcr.PropertyType;
+import javax.jcr.Value;
+
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class Constraints {
+ private static final Logger log = LoggerFactory.getLogger(Constraints.class);
+
+ private Constraints() {
+ }
+
+ public static Predicate<Value> valueConstraint(int type, String constraint) {
+ switch (type) {
+ case PropertyType.STRING:
+ return stringConstraint(constraint);
+ case PropertyType.BINARY:
+ return binaryConstraint(constraint);
+ case PropertyType.LONG:
+ return longConstraint(constraint);
+ case PropertyType.DOUBLE:
+ return doubleConstraint(constraint);
+ case PropertyType.DATE:
+ return dateConstraint(constraint);
+ case PropertyType.BOOLEAN:
+ return booleanConstraint(constraint);
+ case PropertyType.NAME:
+ return nameConstraint(constraint);
+ case PropertyType.PATH:
+ return pathConstraint(constraint);
+ case PropertyType.REFERENCE:
+ return referenceConstraint(constraint);
+ case PropertyType.WEAKREFERENCE:
+ return weakRefConstraint(constraint);
+ case PropertyType.URI:
+ return uriConstraint(constraint);
+ case PropertyType.DECIMAL:
+ return decimalConstraint(constraint);
+ default:
+ String msg = "Invalid property type: " + type;
+ log.warn(msg);
+ throw new IllegalArgumentException(msg);
+ }
+ }
+
+ private static Predicate<Value> stringConstraint(String constraint) {
+ return new StringConstraint(constraint);
+ }
+
+ private static Predicate<Value> binaryConstraint(String constraint) {
+ return Predicates.alwaysTrue(); // todo implement binaryConstraint
+ }
+
+ private static Predicate<Value> longConstraint(String constraint) {
+ return Predicates.alwaysTrue(); // todo implement longConstraint
+ }
+
+ private static Predicate<Value> doubleConstraint(String constraint) {
+ return Predicates.alwaysTrue(); // todo implement doubleConstraint
+ }
+
+ private static Predicate<Value> dateConstraint(String constraint) {
+ return Predicates.alwaysTrue(); // todo implement dateConstraint
+ }
+
+ private static BooleanConstraint booleanConstraint(String constraint) {
+ return new BooleanConstraint(constraint);
+ }
+
+ private static Predicate<Value> nameConstraint(String constraint) {
+ return Predicates.alwaysTrue(); // todo implement nameConstraint
+ }
+
+ private static Predicate<Value> pathConstraint(String constraint) {
+ return Predicates.alwaysTrue(); // todo implement pathConstraint
+ }
+
+ private static Predicate<Value> referenceConstraint(String constraint) {
+ return Predicates.alwaysTrue(); // todo implement referenceConstraint
+ }
+
+ private static Predicate<Value> weakRefConstraint(String constraint) {
+ return Predicates.alwaysTrue(); // todo implement weakRefConstraint
+ }
+
+ private static Predicate<Value> uriConstraint(String constraint) {
+ return Predicates.alwaysTrue(); // todo implement uriConstraint
+ }
+
+ private static Predicate<Value> decimalConstraint(String constraint) {
+ return Predicates.alwaysTrue(); // todo implement decimalConstraint
+ }
+}
Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/Constraints.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/Constraints.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/StringConstraint.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/StringConstraint.java?rev=1381927&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/StringConstraint.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/StringConstraint.java
Fri Sep 7 08:12:48 2012
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.plugins.type.constraint;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+
+import com.google.common.base.Predicate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class StringConstraint implements Predicate<Value> {
+ private static final Logger log = LoggerFactory.getLogger(StringConstraint.class);
+
+ private final Pattern pattern;
+
+ public StringConstraint(String definition) {
+ Pattern p;
+ try {
+ // FIXME matching case insensitive as a workaround
+ // forJSR-283: character case mismatch between property type names and node type
definitions
+ // see http://markmail.org/message/asyaqqkn5nucvcjk
+ int ignoreCase = Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
+ p = Pattern.compile(definition, ignoreCase);
+ }
+ catch (PatternSyntaxException pse) {
+ String msg = '\'' + definition + "' is not valid regular expression syntax";
+ log.warn(msg);
+ p = null;
+ }
+ pattern = p;
+ }
+
+ @Override
+ public boolean apply(Value value) {
+ if (value == null) {
+ return false;
+ }
+
+ try {
+ Matcher matcher = pattern.matcher(value.getString());
+ return matcher.matches();
+ }
+ catch (RepositoryException e) {
+ return false;
+ }
+ }
+}
Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/StringConstraint.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/StringConstraint.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Modified: jackrabbit/oak/trunk/oak-jcr/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/pom.xml?rev=1381927&r1=1381926&r2=1381927&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/pom.xml (original)
+++ jackrabbit/oak/trunk/oak-jcr/pom.xml Fri Sep 7 08:12:48 2012
@@ -75,7 +75,6 @@ org.apache.jackrabbit.test.api.SetValueC
org.apache.jackrabbit.test.api.nodetype.PropertyDefTest#testIsMandatory
org.apache.jackrabbit.test.api.nodetype.CanSetPropertyBinaryTest#testValueConstraintNotSatisfied<!--
OAK-66 -->
org.apache.jackrabbit.test.api.nodetype.CanSetPropertyBinaryTest#testValueConstraintNotSatisfiedMultiple<!--
OAK-66 -->
-org.apache.jackrabbit.test.api.nodetype.CanSetPropertyStringTest#testValueConstraintNotSatisfied<!--
OAK-66 -->
org.apache.jackrabbit.test.api.nodetype.CanSetPropertyDateTest#testConversions
org.apache.jackrabbit.test.api.nodetype.CanSetPropertyPathTest#testConversions
org.apache.jackrabbit.test.api.nodetype.CanAddChildNodeCallWithNodeTypeTest#testCanAddAbstractType
<!-- OAK-66 -->
|