You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bval.apache.org by mb...@apache.org on 2010/09/09 19:25:25 UTC
svn commit: r995503 - in
/incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util:
NestedPathNavigator.java PathNavigation.java
Author: mbenson
Date: Thu Sep 9 17:25:25 2010
New Revision: 995503
URL: http://svn.apache.org/viewvc?rev=995503&view=rev
Log:
extract path navigation algorithm (first pass)
Added:
incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/PathNavigation.java (with props)
Modified:
incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NestedPathNavigator.java
Modified: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NestedPathNavigator.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NestedPathNavigator.java?rev=995503&r1=995502&r2=995503&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NestedPathNavigator.java (original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NestedPathNavigator.java Thu Sep 9 17:25:25 2010
@@ -17,12 +17,10 @@
package org.apache.bval.jsr303.util;
import java.lang.reflect.Type;
-import java.util.StringTokenizer;
-
-import javax.validation.ValidationException;
import org.apache.bval.DynamicMetaBean;
import org.apache.bval.jsr303.UnknownPropertyException;
+import org.apache.bval.jsr303.util.PathNavigation.Callback;
import org.apache.bval.model.MetaBean;
import org.apache.bval.model.MetaProperty;
import org.apache.bval.model.ValidationContext;
@@ -66,39 +64,13 @@ public class NestedPathNavigator {
* @param propertyPath
*/
public void navigate(ValidationContext<?> validationContext, String propertyPath) {
- try {
- Traversal helper = new Traversal(validationContext);
- StringTokenizer tokens = new StringTokenizer(propertyPath, ".[]", true);
- while (tokens.hasMoreTokens()) {
- String token = tokens.nextToken();
- if (".".equals(token)) {
- continue;
- }
-
- if ("[".equals(token)) {
- String key = tokens.nextToken();
- token = tokens.nextToken();
- if (!"]".equals(token)) {
- throw new ValidationException("']' missing, invalid property format: " + propertyPath);
- }
- helper.handleIndexOrKey(key);
- continue;
- }
-
- // else, it is a property name
- helper.handleProperty(token);
- }
- } catch (ValidationException ex) {
- throw ex;
- } catch (Exception ex) {
- throw new ValidationException("invalid property format: " + propertyPath, ex);
- }
+ PathNavigation.navigate(propertyPath, new Traversal(validationContext));
}
/**
* Graph traversal worker.
*/
- protected static class Traversal {
+ protected static class Traversal implements Callback {
private ValidationContext<?> validationContext;
private Type type;
private Class<?> rawType;
@@ -113,11 +85,9 @@ public class NestedPathNavigator {
}
/**
- * Handle an index or key embedded in [].
- *
- * @param token
+ * {@inheritDoc}
*/
- protected void handleIndexOrKey(String token) {
+ public void handleIndexOrKey(String token) {
moveDownIfNecessary();
AccessStrategy access;
@@ -137,11 +107,9 @@ public class NestedPathNavigator {
}
/**
- * Handle a .-delimited property token.
- *
- * @param token
+ * {@inheritDoc}
*/
- protected void handleProperty(String token) {
+ public void handleProperty(String token) {
moveDownIfNecessary();
MetaBean metaBean = validationContext.getMetaBean();
Added: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/PathNavigation.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/PathNavigation.java?rev=995503&view=auto
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/PathNavigation.java (added)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/PathNavigation.java Thu Sep 9 17:25:25 2010
@@ -0,0 +1,92 @@
+/*
+ * 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.bval.jsr303.util;
+
+import java.util.StringTokenizer;
+
+import javax.validation.ValidationException;
+
+/**
+ * Defines a path navigation algorithm and a means of interacting with same.
+ *
+ * @version $Rev$ $Date$
+ */
+public class PathNavigation {
+ /**
+ * Interface defining calls that PathNavigation makes as it traverses a path
+ * expression.
+ *
+ * @version $Rev$ $Date$
+ */
+ public interface Callback {
+ /**
+ * Handle a .-delimited property.
+ *
+ * @param name
+ */
+ void handleProperty(String name);
+
+ /**
+ * Handle an index or key embedded in [].
+ *
+ * @param value
+ */
+ void handleIndexOrKey(String value);
+ }
+
+ /**
+ * Create a new PathNavigation instance.
+ */
+ private PathNavigation() {
+ }
+
+ /**
+ * Navigate a path using the specified callback.
+ *
+ * @param propertyPath
+ * @param callback
+ */
+ public static void navigate(String propertyPath, Callback callback) {
+ try {
+ StringTokenizer tokens = new StringTokenizer(propertyPath, ".[]", true);
+ while (tokens.hasMoreTokens()) {
+ String token = tokens.nextToken();
+ if (".".equals(token)) {
+ continue;
+ }
+
+ if ("[".equals(token)) {
+ String key = tokens.nextToken();
+ token = tokens.nextToken();
+ if (!"]".equals(token)) {
+ throw new ValidationException("']' missing, invalid property format: " + propertyPath);
+ }
+ callback.handleIndexOrKey(key);
+ continue;
+ }
+
+ // else, it is a property name
+ callback.handleProperty(token);
+ }
+ } catch (ValidationException ex) {
+ throw ex;
+ } catch (Exception ex) {
+ throw new ValidationException("invalid property format: " + propertyPath, ex);
+ }
+ }
+
+}
Propchange: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/PathNavigation.java
------------------------------------------------------------------------------
svn:eol-style = native