View Javadoc
1   /*
2    * Copyright 2007 Keith Visco, 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:string XML Schema type.
23   * 
24   * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
25   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
26   * @version $Revision$ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
27   */
28  public final class XSString extends AbstractLengthFacet {
29    // --------------------------------------------------------------------------
30  
31    /** Name of this XSType. */
32    public static final String NAME = "string";
33  
34    /** Type number of this XSType. */
35    public static final short TYPE = XSType.STRING_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 XSString() {
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.StringValidator typeValidator;\n"
117         + "typeValidator = new org.exolab.castor.xml.validators.StringValidator();\n"
118         + "{0}.setValidator(typeValidator);", validatorInstanceName);
119 
120     if (fixedValue != null) {
121       jsc.add("typeValidator.setFixed(" + fixedValue + ");");
122     }
123 
124     codePatternFacet(jsc, "typeValidator");
125     codeWhiteSpaceFacet(jsc, "typeValidator");
126     codeLengthFacet(jsc, "typeValidator");
127   }
128 
129   // --------------------------------------------------------------------------
130 }