KickJava   Java API By Example, From Geeks To Geeks.

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


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

42 public class IntegerSG extends AtomicTypeSGImpl {
43   public static final JavaQName INTEGER_TYPE = JavaQNameImpl.getInstance(BigInteger JavaDoc.class);
44
45   /** <p>Creates a new instance of IntegerSG.</p>
46    */

47   public IntegerSG(SGFactory pFactory, SchemaSG pSchema, XSType pType) throws SAXException {
48     super(pFactory, pSchema, pType);
49   }
50
51   public JavaQName getRuntimeType(SimpleTypeSG pController) { return INTEGER_TYPE; }
52   protected String JavaDoc getDatatypeName() { return "Integer"; }
53   protected JavaQName getDatatypeType() { return INTEGER_TYPE; }
54
55   public TypedValue getCastFromString(SimpleTypeSG pController, String JavaDoc pValue) throws SAXException {
56     try {
57         return new TypedValueImpl("new java.math.BigInteger(\"" + pValue + "\")", INTEGER_TYPE);
58     } catch (NumberFormatException JavaDoc e) {
59       throw new LocSAXException("Failed to convert string value to BigInteger: " + pValue, getLocator());
60     }
61   }
62
63   public void forAllNonNullValues(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, SGlet pSGlet) throws SAXException {
64     LocalJavaField f = pMethod.newJavaField(INTEGER_TYPE);
65     f.addLine(pValue);
66     pMethod.addIf(f, " != null");
67     pSGlet.generate(pMethod, pValue);
68     pMethod.addEndIf();
69   }
70
71   public void forAllValues(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, SGlet pSGlet) throws SAXException {
72     pSGlet.generate(pMethod, pValue);
73   }
74
75   public void addValidation(SimpleTypeSG pController, JavaMethod pMethod,
76                             final DirectAccessible pValue) {
77     AtomicTypeSG atomicType = pController.getAtomicType();
78     Long JavaDoc totalDigits = atomicType.getTotalDigits();
79     if (totalDigits != null) {
80       pMethod.addIf(pValue, ".signum()", " == -1");
81       pMethod.addIf(pValue, ".toString().length() - 1", " > ", totalDigits);
82       pMethod.addThrowNew(IllegalArgumentException JavaDoc.class,
83                           JavaSource.getQuoted("Length of " + totalDigits + " digits exceeded: "), " + ", pValue);
84       pMethod.addEndIf();
85       pMethod.addElse();
86       pMethod.addIf(pValue, ".toString().length()", " > ", totalDigits);
87       pMethod.addThrowNew(IllegalArgumentException JavaDoc.class,
88                           JavaSource.getQuoted("Length of " + totalDigits + " digits exceeded: "), " + ", pValue);
89       pMethod.addEndIf();
90       pMethod.addEndIf();
91     }
92   }
93 }
94
Popular Tags