KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > sg > impl > ParsePrintSG


1 package org.apache.ws.jaxme.generator.sg.impl;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.apache.ws.jaxme.generator.sg.SimpleTypeSG;
7 import org.apache.ws.jaxme.generator.sg.SimpleTypeSGChain;
8 import org.apache.ws.jaxme.generator.sg.impl.SimpleTypeSGChainImpl;
9 import org.apache.ws.jaxme.js.DirectAccessible;
10 import org.apache.ws.jaxme.js.JavaMethod;
11 import org.apache.ws.jaxme.js.JavaQName;
12 import org.apache.ws.jaxme.js.JavaQNameImpl;
13 import org.apache.ws.jaxme.js.JavaSource;
14 import org.apache.ws.jaxme.js.LocalJavaField;
15 import org.apache.ws.jaxme.js.TypedValue;
16 import org.apache.ws.jaxme.js.impl.TypedValueImpl;
17 import org.apache.ws.jaxme.xs.jaxb.JAXBJavaType;
18 import org.xml.sax.SAXException JavaDoc;
19 import org.xml.sax.SAXParseException JavaDoc;
20
21
22 /** Simple type chain for implementing
23  * <code>jaxb:javaType/@parseMethod</code> and/or
24  * <code>jaxb:javaType/@printMethod</code>.
25  */

26 public class ParsePrintSG extends SimpleTypeSGChainImpl {
27     private final JAXBJavaType javaType;
28
29     /** Creates a new instance, which generates code
30      * for the given type.
31      */

32     public ParsePrintSG(SimpleTypeSGChain pType, JAXBJavaType pJavaType) {
33         super(pType);
34         javaType = pJavaType;
35     }
36
37     private JavaQName getType(SimpleTypeSG pController) {
38         if (javaType.getName() == null) {
39             return pController.getRuntimeType();
40         } else {
41             return JavaQNameImpl.getInstance(javaType.getName(), true);
42         }
43     }
44
45     private void addCall(SimpleTypeSG pController, List JavaDoc pList, String JavaDoc pMethod,
46                          JavaQName pType) {
47         int offset = pMethod.indexOf('.');
48         JavaQName qName;
49         String JavaDoc method;
50         if (offset == -1) {
51             qName = getType(pController);
52             method = pMethod;
53         } else {
54             qName = JavaQNameImpl.getInstance(pMethod.substring(0, offset), true);
55             method = pMethod.substring(offset+1);
56         }
57         pList.add(qName);
58         pList.add(".");
59         pList.add(method);
60     }
61
62     private DirectAccessible getValue(JavaMethod pMethod, Object JavaDoc pValue, JavaQName pType) {
63         if (pValue instanceof DirectAccessible) {
64             return (DirectAccessible) pValue;
65         } else {
66             LocalJavaField s = pMethod.newJavaField(pType);
67             s.addLine(pValue);
68             return s;
69         }
70     }
71
72     private void addValues(List JavaDoc pList, DirectAccessible pValue, Object JavaDoc pData) {
73         pList.add("(");
74         pList.add(pValue);
75         if (javaType.hasNsContext()) {
76             pList.add(", ");
77             pList.add(pData);
78             pList.add(".getNamespaceSupport()");
79         }
80         pList.add(")");
81     }
82
83     private TypedValue getParseCall(SimpleTypeSG pController, JavaMethod pMethod,
84                                 Object JavaDoc pValue, Object JavaDoc pData)
85             throws SAXParseException JavaDoc {
86         JavaQName type = getType(pController);
87         DirectAccessible value = getValue(pMethod, pValue, JavaQNameImpl.getInstance(String JavaDoc.class));
88
89         String JavaDoc parseMethod = javaType.getParseMethod().trim();
90         List JavaDoc list = new ArrayList JavaDoc();
91         if (parseMethod.startsWith("new")
92             && parseMethod.length() > 3
93             && Character.isWhitespace(parseMethod.charAt(3))) {
94             JavaQName qName = JavaQNameImpl.getInstance(parseMethod.substring(3).trim(), true);
95             list.add("new ");
96             list.add(qName);
97         } else {
98             addCall(pController, list, parseMethod, type);
99         }
100         addValues(list, value, pData);
101         return new TypedValueImpl(list, type);
102     }
103
104     private TypedValue getPrintCall(SimpleTypeSG pController, JavaMethod pMethod,
105                                 Object JavaDoc pValue, Object JavaDoc pData) {
106         JavaQName type = getType(pController);
107         LocalJavaField f = pMethod.newJavaField(String JavaDoc.class);
108         DirectAccessible value = getValue(pMethod, pValue, type);
109         List JavaDoc list = new ArrayList JavaDoc();
110         addCall(pController, list, javaType.getPrintMethod(), type);
111         addValues(list, value, pData);
112         pMethod.addTry();
113         pMethod.addLine(f, " = ", list, ";");
114         DirectAccessible e = pMethod.addCatch(Exception JavaDoc.class);
115         pMethod.addLine(pData, ".printConversionEvent(pObject, ",
116                         JavaSource.getQuoted("Failed to convert value "),
117                         " + ", value, " + ", JavaSource.getQuoted(": "),
118                         " + ", e, ".getClass().getName(), ", e, ");");
119         pMethod.addLine(f, " = null;");
120         pMethod.addEndTry();
121         return f;
122     }
123
124     public TypedValue getCastFromString(SimpleTypeSG pController, JavaMethod pMethod,
125                                         Object JavaDoc pValue, Object JavaDoc pData)
126             throws SAXException JavaDoc {
127         if (javaType.getParseMethod() == null) {
128             return super.getCastFromString(pController, pMethod, pValue, pData);
129         } else {
130             return getParseCall(pController, pMethod, pValue, pData);
131         }
132     }
133
134     public TypedValue getCastFromString(SimpleTypeSG pController, String JavaDoc pValue)
135             throws SAXException JavaDoc {
136         if (javaType.getParseMethod() == null) {
137             return super.getCastFromString(pController, pValue);
138         } else {
139             throw new SAXParseException JavaDoc("Use of the parse method at compile time is unsupported.",
140                                         javaType.getLocator());
141         }
142     }
143
144     public TypedValue getCastToString(SimpleTypeSG pController, JavaMethod pMethod,
145                                   Object JavaDoc pValue, DirectAccessible pData)
146             throws SAXException JavaDoc {
147         if (javaType.getPrintMethod() == null) {
148             return super.getCastToString(pController, pMethod, pValue, pData);
149         } else {
150             return getPrintCall(pController, pMethod, pValue, pData);
151         }
152     }
153
154     public boolean isCausingParseConversionEvent(SimpleTypeSG pController) {
155         return true;
156     }
157 }
158
Popular Tags