KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.xml.bind.DatatypeConverterInterface;
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.SchemaSG;
24 import org.apache.ws.jaxme.generator.sg.SimpleTypeSG;
25 import org.apache.ws.jaxme.impl.DatatypeConverterImpl;
26 import org.apache.ws.jaxme.js.DirectAccessible;
27 import org.apache.ws.jaxme.js.JavaField;
28 import org.apache.ws.jaxme.js.JavaInnerClass;
29 import org.apache.ws.jaxme.js.JavaMethod;
30 import org.apache.ws.jaxme.js.JavaQName;
31 import org.apache.ws.jaxme.js.JavaQNameImpl;
32 import org.apache.ws.jaxme.js.JavaSource;
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 /**
41  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
42  */

43 public abstract class AtomicTypeSGImpl extends SimpleTypeSGImpl {
44   private final AtomicTypeSG atomicTypeSG;
45
46   /** <p>Creates a new instance of AtomicTypeSGImpl.</p>
47    */

48   protected AtomicTypeSGImpl(SGFactory pFactory, SchemaSG pSchema, XSType pType) throws SAXException {
49     super(pFactory, pSchema, pType);
50     atomicTypeSG = new org.apache.ws.jaxme.generator.sg.impl.AtomicTypeSGImpl(pType.getSimpleType().getAtomicType());
51   }
52
53   public boolean isAtomic(SimpleTypeSG pController) { return true; }
54
55   protected abstract String JavaDoc getDatatypeName();
56   protected abstract JavaQName getDatatypeType();
57
58   public AtomicTypeSG getAtomicType(SimpleTypeSG pController) {
59     return atomicTypeSG;
60   }
61
62    public TypedValue getCastFromString(SimpleTypeSG pController, String JavaDoc pValue)
63             throws SAXException {
64         try {
65             return new TypedValueImpl(new Object JavaDoc[] {
66                     "javax.xml.bind.DatatypeConverter",
67                     ".parse" + getDatatypeName() + "(\"", pValue, "\")" },
68                     getDatatypeType());
69         } catch (RuntimeException JavaDoc e) {
70             try {
71                 throw new LocSAXException("Failed to convert string value to "
72                         + getDatatypeName() + " instance: " + pValue, getLocator());
73             } catch (Exception JavaDoc e1) {
74                 throw new SAXException("Failed to convert string value to "
75                         + getDatatypeName() + " instance: " + pValue);
76             }
77         }
78     }
79   
80   public TypedValue getCastFromString(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, Object JavaDoc pData) throws SAXException {
81     Object JavaDoc value;
82     if (pData == null) {
83       JavaSource js = pMethod.getJavaSource();
84       while (js.isInnerClass()) {
85         js = ((JavaInnerClass) js).getOuterClass();
86       }
87       JavaField[] fields = js.getFields();
88       JavaQName qName = JavaQNameImpl.getInstance(DatatypeConverterInterface.class);
89       JavaField converter = null;
90       for (int i = 0; i < fields.length; i++) {
91         if (qName.equals(fields[i].getType()) && JavaSource.DEFAULT_PROTECTION.equals(fields[i].getProtection()) &&
92             fields[i].isStatic() && fields[i].isFinal()) {
93           converter = fields[i];
94           break;
95         }
96       }
97       if (converter == null) {
98         converter = js.newJavaField("__dataTypeConverter", qName);
99         converter.setStatic(true);
100         converter.setFinal(true);
101         converter.addLine("new ", DatatypeConverterImpl.class, "()");
102       }
103       value = new Object JavaDoc[]{converter, ".parse" + getDatatypeName() + "(", pValue, ")"};
104     } else {
105       value = new Object JavaDoc[]{pData, ".getDatatypeConverter().parse" + getDatatypeName() + "(", pValue, ")"};
106     }
107     return new TypedValueImpl(value, getDatatypeType());
108   }
109
110   public TypedValue getCastToString(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, DirectAccessible pData) {
111     return getCastToString(pController, pValue, pData);
112   }
113
114   public TypedValue getCastToString(SimpleTypeSG pController, Object JavaDoc pValue, DirectAccessible pData) {
115     return new TypedValueImpl(new Object JavaDoc[]{pData, ".getDatatypeConverter().print" + getDatatypeName() + "(", pValue, ")"},
116                               String JavaDoc.class);
117   }
118
119   public Object JavaDoc getEqualsCheck(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue1, Object JavaDoc pValue2)
120       throws SAXException {
121     return new Object JavaDoc[]{pValue1, ".equals(", pValue2, ")"};
122   }
123
124     public boolean isCausingParseConversionEvent(SimpleTypeSG pController) {
125         return true;
126     }
127 }
128
Popular Tags