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.javasource.JClass;
17  import org.exolab.javasource.JSourceCode;
18  import org.exolab.javasource.JType;
19  
20  /**
21   * The xsd:ID XML Schema type.
22   * 
23   * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
24   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
25   * @version $Revision$ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
26   */
27  public final class XSId extends AbstractLengthFacet {
28    // --------------------------------------------------------------------------
29  
30    /** Name of this XSType. */
31    public static final String NAME = "ID";
32  
33    /** Type number of this XSType. */
34    public static final short TYPE = XSType.ID_TYPE;
35  
36    /** The JType represented by this XSType. */
37    private static final JType JTYPE = new JClass("java.lang.String");
38  
39    // --------------------------------------------------------------------------
40  
41    /**
42     * {@inheritDoc}
43     */
44    public String getName() {
45      return NAME;
46    }
47  
48    /**
49     * {@inheritDoc}
50     */
51    public short getType() {
52      return TYPE;
53    }
54  
55    /**
56     * {@inheritDoc}
57     */
58    public boolean isPrimitive() {
59      return false;
60    }
61  
62    /**
63     * {@inheritDoc}
64     */
65    public boolean isDateTime() {
66      return false;
67    }
68  
69    /**
70     * {@inheritDoc}
71     */
72    public JType getJType() {
73      return JTYPE;
74    }
75  
76    /**
77     * {@inheritDoc}
78     */
79    public String newInstanceCode() {
80      return "new java.lang.String();";
81    }
82  
83    /**
84     * {@inheritDoc}
85     */
86    public String createToJavaObjectCode(final String variableName) {
87      return variableName;
88    }
89  
90    /**
91     * {@inheritDoc}
92     */
93    public String createFromJavaObjectCode(final String variableName) {
94      return "(java.lang.String) " + variableName;
95    }
96  
97    // --------------------------------------------------------------------------
98  
99    /**
100    * {@inheritDoc}
101    */
102   public void validationCode(final JSourceCode jsc, final String fixedValue,
103       final String validatorInstanceName) {
104     jsc.add("org.exolab.castor.xml.validators.IdValidator typeValidator;\n"
105         + "typeValidator = new org.exolab.castor.xml.validators.IdValidator();\n"
106         + "{0}.setValidator(typeValidator);", validatorInstanceName);
107 
108     if (fixedValue != null) {
109       jsc.add("typeValidator.setFixed(" + fixedValue + ");");
110     }
111 
112     codePatternFacet(jsc, "typeValidator");
113     codeWhiteSpaceFacet(jsc, "typeValidator");
114     codeLengthFacet(jsc, "typeValidator");
115   }
116 
117   // --------------------------------------------------------------------------
118 }