View Javadoc
1   /*
2    * Copyright 2007 Arnaud Blandin, Ralf Joachim
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 org.exolab.castor.xml.schema.Facet;
17  import org.exolab.javasource.JClass;
18  import org.exolab.javasource.JSourceCode;
19  import org.exolab.javasource.JType;
20  
21  /**
22   * The xsd:normalizedString XML Schema type. It is simply a XSString with some specific validation.
23   * 
24   * @author <a href="mailto:blandin AT intalio DOT com">Arnaud Blandin</a>
25   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
26   * @version $Revision$ $Date: 2006-03-16 10:34:22 -0700 (Thu, 16 Mar 2006) $
27   */
28  public final class XSNormalizedString extends AbstractLengthFacet {
29    // --------------------------------------------------------------------------
30  
31    /** Name of this XSType. */
32    public static final String NAME = "normalizedString";
33  
34    /** Type number of this XSType. */
35    public static final short TYPE = XSType.NORMALIZEDSTRING_TYPE;
36  
37    /** The JType represented by this XSType. */
38    private static final JType JTYPE = new JClass("java.lang.String");
39  
40    // --------------------------------------------------------------------------
41  
42    /**
43     * No-arg constructor.
44     */
45    public XSNormalizedString() {
46      super(false);
47  
48      setWhiteSpace(Facet.WHITESPACE_PRESERVE);
49    }
50  
51    // --------------------------------------------------------------------------
52  
53    /**
54     * {@inheritDoc}
55     */
56    public String getName() {
57      return NAME;
58    }
59  
60    /**
61     * {@inheritDoc}
62     */
63    public short getType() {
64      return TYPE;
65    }
66  
67    /**
68     * {@inheritDoc}
69     */
70    public boolean isPrimitive() {
71      return false;
72    }
73  
74    /**
75     * {@inheritDoc}
76     */
77    public boolean isDateTime() {
78      return false;
79    }
80  
81    /**
82     * {@inheritDoc}
83     */
84    public JType getJType() {
85      return JTYPE;
86    }
87  
88    /**
89     * {@inheritDoc}
90     */
91    public String newInstanceCode() {
92      return "new java.lang.String();";
93    }
94  
95    /**
96     * {@inheritDoc}
97     */
98    public String createToJavaObjectCode(final String variableName) {
99      return variableName;
100   }
101 
102   /**
103    * {@inheritDoc}
104    */
105   public String createFromJavaObjectCode(final String variableName) {
106     return "(java.lang.String) " + variableName;
107   }
108 
109   // --------------------------------------------------------------------------
110 
111   /**
112    * {@inheritDoc}
113    */
114   public void validationCode(final JSourceCode jsc, final String fixedValue,
115       final String validatorInstanceName) {
116     jsc.add("org.exolab.castor.xml.validators.NameValidator typeValidator;\n"
117         + "typeValidator = new org.exolab.castor.xml.validators.NameValidator(\n"
118         + " org.exolab.castor.xml.XMLConstants.NAME_TYPE_CDATA);\n"
119         + "{0}.setValidator(typeValidator);", validatorInstanceName);
120 
121     if (fixedValue != null) {
122       jsc.add("typeValidator.setFixed(" + fixedValue + ");");
123     }
124 
125     codePatternFacet(jsc, "typeValidator");
126     codeWhiteSpaceFacet(jsc, "typeValidator");
127     codeLengthFacet(jsc, "typeValidator");
128   }
129 
130   // --------------------------------------------------------------------------
131 }