View Javadoc
1   /*
2    * Copyright 2006-2007 Keith Visco, Edward Kuns
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.castor.builder.types;
15  
16  import java.util.LinkedList;
17  import java.util.List;
18  
19  import org.exolab.castor.xml.schema.Facet;
20  import org.exolab.javasource.JSourceCode;
21  
22  /**
23   * A base class for types which support the pattern facet.
24   * 
25   * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
26   * @author <a href="mailto:edward DOT kuns AT aspect DOT com">Edward Kuns</a>
27   * @version $Revision: 6678 $ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
28   * @since 1.1
29   */
30  public abstract class AbstractPatternFacet extends XSType {
31    // --------------------------------------------------------------------------
32  
33    /** The list of pattern facets. */
34    private List<String> _patterns = new LinkedList<String>();
35  
36    // --------------------------------------------------------------------------
37  
38    /**
39     * Adds a pattern branch for this XSType. To successfully pass the pattern facets, only one branch
40     * needs to pass.
41     * 
42     * @param pattern The regular expression for this XSType.
43     */
44    public final void addPattern(final String pattern) {
45      _patterns.add(pattern);
46    }
47  
48    /**
49     * Get list of pattern facets.
50     * 
51     * @return List of pattern facets.
52     */
53    public final List<String> getPatterns() {
54      return _patterns;
55    }
56  
57    // --------------------------------------------------------------------------
58  
59    /**
60     * {@inheritDoc}
61     */
62    protected void setFacet(final Facet facet) {
63      addPatternFacet(facet);
64    }
65  
66    /**
67     * Transfer given facet if it is a pattern.
68     *
69     * @param facet The facet to transfer.
70     */
71    protected final void addPatternFacet(final Facet facet) {
72      if (Facet.PATTERN.equals(facet.getName())) {
73        addPattern(facet.getValue());
74      }
75    }
76  
77    /**
78     * Generate the source code for pattern facet validation.
79     *
80     * @param jsc The JSourceCode to fill in.
81     * @param validatorName The name of the TypeValidator that the patterns should be added to.
82     */
83    protected final void codePatternFacet(final JSourceCode jsc, final String validatorName) {
84      for (String pattern : _patterns) {
85        jsc.add("{0}.addPattern(\"{1}\");", validatorName, escapePattern(pattern));
86      }
87    }
88  
89    /**
90     * Escapes special characters in the given String so that it can be printed correctly.
91     *
92     * @param str The String to escape.
93     * @return The escaped String, or null if the given String was null.
94     */
95    private static String escapePattern(final String str) {
96      if (str == null) {
97        return str;
98      }
99  
100     // -- make sure we have characters to escape
101     if (str.indexOf('\\') < 0 && str.indexOf('\"') < 0) {
102       return str;
103     }
104 
105     StringBuilder sb = new StringBuilder();
106 
107     char[] chars = str.toCharArray();
108     for (char ch : chars) {
109       if (ch == '\\') {
110         sb.append(ch);
111       }
112       if (ch == '\"') {
113         sb.append('\\');
114       }
115       sb.append(ch);
116     }
117 
118     return sb.toString();
119   }
120 
121   // --------------------------------------------------------------------------
122 }