KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > types > StringSG


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.generator.types;
18
19 import org.apache.ws.jaxme.generator.sg.AtomicTypeSG;
20 import org.apache.ws.jaxme.generator.sg.SGFactory;
21 import org.apache.ws.jaxme.generator.sg.SGlet;
22 import org.apache.ws.jaxme.generator.sg.SchemaSG;
23 import org.apache.ws.jaxme.generator.sg.SimpleTypeSG;
24 import org.apache.ws.jaxme.js.DirectAccessible;
25 import org.apache.ws.jaxme.js.JavaMethod;
26 import org.apache.ws.jaxme.js.JavaQName;
27 import org.apache.ws.jaxme.js.JavaQNameImpl;
28 import org.apache.ws.jaxme.js.JavaSource;
29 import org.apache.ws.jaxme.js.LocalJavaField;
30 import org.apache.ws.jaxme.js.TypedValue;
31 import org.apache.ws.jaxme.js.impl.TypedValueImpl;
32 import org.apache.ws.jaxme.xs.XSType;
33 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
34 import org.xml.sax.SAXException JavaDoc;
35
36
37 /**
38  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
39  */

40 public class StringSG extends AtomicTypeSGImpl {
41   /** <p>Creates a new instance of StringTypeSG.java.</p>
42    */

43   public StringSG(SGFactory pFactory, SchemaSG pSchema, XSType pType) throws SAXException {
44     super(pFactory, pSchema, pType);
45   }
46
47   /** The string type.
48    */

49   public final static JavaQName STRING_TYPE = JavaQNameImpl.getInstance(String JavaDoc.class);
50   protected String JavaDoc getDatatypeName() { return "String"; }
51   protected JavaQName getDatatypeType() { return STRING_TYPE; }
52
53   public JavaQName getRuntimeType(SimpleTypeSG pController) { return STRING_TYPE; }
54
55   public TypedValue getCastFromString(SimpleTypeSG pController, String JavaDoc pValue) {
56     return new TypedValueImpl(JavaSource.getQuoted(pValue), STRING_TYPE);
57   }
58
59   public TypedValue getCastFromString(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, Object JavaDoc pData) {
60     return new TypedValueImpl(pValue, STRING_TYPE);
61   }
62
63   public TypedValue getCastToString(SimpleTypeSG pController, Object JavaDoc pValue, DirectAccessible pData) {
64     return new TypedValueImpl(pValue, STRING_TYPE);
65   }
66
67   public void forAllNonNullValues(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, SGlet pSGlet) throws SAXException {
68     LocalJavaField f = pMethod.newJavaField(STRING_TYPE);
69     f.addLine(pValue);
70     pMethod.addIf(f, " != null");
71     pSGlet.generate(pMethod, pValue);
72     pMethod.addEndIf();
73   }
74
75   public void forAllValues(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, SGlet pSGlet) throws SAXException {
76     pSGlet.generate(pMethod, pValue);
77   }
78
79     public boolean isCausingParseConversionEvent(SimpleTypeSG pController) {
80         final AtomicTypeSG atomicType = pController.getAtomicType();
81         /* See addValidation(SimpleTypeSG, JavaMethod, DirectAccessible) for
82          * when an exception can be thrown.
83          */

84         return atomicType.getLength() != null
85            || atomicType.getMinLength() != null
86            || atomicType.getMaxLength() != null;
87     }
88
89     public void addValidation(SimpleTypeSG pController, JavaMethod pMethod, DirectAccessible pValue)
90             throws SAXException {
91         /* If you add additional checks here, you should possibly modify
92          * the isCausingParseConversionEvent(SimpleTypeSG) method as well.
93          */

94         final AtomicTypeSG atomicType = pController.getAtomicType();
95         Long JavaDoc length = atomicType.getLength();
96         Long JavaDoc maxLength = atomicType.getMaxLength();
97         Long JavaDoc minLength = atomicType.getMinLength();
98
99         if (minLength != null && minLength.longValue() < 0) {
100             throw new LocSAXException("Negative value for minLength detected: " + minLength, getLocator());
101         }
102         if (maxLength != null) {
103             if (maxLength.longValue() < 0) {
104                 throw new LocSAXException("Negative value for maxLength detected: " + maxLength, getLocator());
105             }
106             if (minLength != null) {
107                 if (maxLength.longValue() < minLength.longValue()) {
108                     throw new LocSAXException("MaxLength value of " + maxLength + " is smaller than minLength value of " + minLength,
109                             getLocator());
110                 }
111                 if (maxLength.longValue() == minLength.longValue()) {
112                     length = maxLength;
113                 }
114             }
115         }
116         if (length != null) {
117             if (length.longValue() < 0) {
118                 throw new LocSAXException("Negative value for length detected: " + length, getLocator());
119             }
120             if (maxLength != null) {
121                 if (maxLength.longValue() < length.longValue()) {
122                     throw new LocSAXException("MaxLength value of " + maxLength + " is smaller than length value of " + length,
123                             getLocator());
124                 }
125                 maxLength = null; // Avoid unnecessary checks
126
}
127             if (minLength != null) {
128                 if (minLength.longValue() > length.longValue()) {
129                     throw new LocSAXException("MinLength value of " + minLength + " is larger than length value of " + length,
130                             getLocator());
131                 }
132                 minLength = null; // Avoid unnecessary checks
133
}
134         }
135         
136         if (length != null || maxLength != null || minLength != null) {
137             if (pValue.isNullable()) {
138                 pMethod.addIf(pValue, " != null");
139             }
140             if (maxLength != null) {
141                 pMethod.addIf(pValue, ".length()", " > ", maxLength);
142                 pMethod.addThrowNew(IllegalArgumentException JavaDoc.class,
143                         JavaSource.getQuoted("Length of " + maxLength + " characters exceeded: "),
144                         " + ", pValue);
145                 pMethod.addEndIf();
146             }
147             if (minLength != null) {
148                 pMethod.addIf(pValue, ".length()", " < ", minLength);
149                 pMethod.addThrowNew(IllegalArgumentException JavaDoc.class,
150                         JavaSource.getQuoted("Length of " + minLength + " characters exceeded: "),
151                         " + ", pValue);
152                 pMethod.addEndIf();
153             }
154             if (length != null) {
155                 pMethod.addIf(pValue, ".length()", " != ", length);
156                 pMethod.addThrowNew(IllegalArgumentException JavaDoc.class,
157                         JavaSource.getQuoted("Length of " + length + " characters not matched: "),
158                         " + ", pValue);
159                 pMethod.addEndIf();
160             }
161             if (pValue.isNullable()) {
162                 pMethod.addEndIf();
163             }
164         }
165     }
166 }
167
Popular Tags