View Javadoc

1   package org.springmodules.validation.bean.context.web;
2   
3   import java.beans.PropertyEditorSupport;
4   
5   import org.springframework.util.StringUtils;
6   
7   /**
8    * A property editor for the {@link ValidationContextUrlMapping} class.
9    * <p/>
10   * This editor expects text in the following format:
11   * <p/>
12   *              <center><i>url_pattern=context1,context2,...,contextN</i></center>
13   * <p/>
14   * Where <i>url_pattern</i> is the URL pattern to be mapped to the context tokens and
15   * <i>context1,context2,...,contextN</i> is the comma-separated list of validation context tokens.
16   *
17   * @author Uri Boness
18   */
19  public class ValidationContextUrlMappingArrayPropertyEditor extends PropertyEditorSupport {
20  
21      private final static String MAPPINGS_SEPARATORS = "\n\r";
22      private final static char MAPPING_OPERATOR = '=';
23  
24      public String getAsText() {
25          throw new UnsupportedOperationException("This property edito only supports one way conversion (text to value)");
26      }
27  
28      public void setAsText(String text) throws IllegalArgumentException {
29          text = text.trim();
30          String[] mappingLines = StringUtils.tokenizeToStringArray(text, MAPPINGS_SEPARATORS);
31          ValidationContextUrlMapping[] mappings = new ValidationContextUrlMapping[mappingLines.length];
32          for (int i=0; i<mappings.length; i++) {
33              String mappingLine = mappingLines[i].trim();
34              int index = mappingLine.lastIndexOf(MAPPING_OPERATOR);
35              String pattern = mappingLine.substring(0, index);
36              String tokensString = mappingLine.substring(index+1, mappingLine.length());
37              String[] tokens = StringUtils.commaDelimitedListToStringArray(tokensString);
38              mappings[i] = new ValidationContextUrlMapping(pattern, tokens);
39          }
40          setValue(mappings);
41      }
42  
43  }