1 package org.apache.ws.jaxme.generator.sg.impl; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 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 ; 19 import org.xml.sax.SAXParseException ; 20 21 22 26 public class ParsePrintSG extends SimpleTypeSGChainImpl { 27 private final JAXBJavaType javaType; 28 29 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 pList, String pMethod, 46 JavaQName pType) { 47 int offset = pMethod.indexOf('.'); 48 JavaQName qName; 49 String 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 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 pList, DirectAccessible pValue, Object 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 pValue, Object pData) 85 throws SAXParseException { 86 JavaQName type = getType(pController); 87 DirectAccessible value = getValue(pMethod, pValue, JavaQNameImpl.getInstance(String .class)); 88 89 String parseMethod = javaType.getParseMethod().trim(); 90 List list = new ArrayList (); 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 pValue, Object pData) { 106 JavaQName type = getType(pController); 107 LocalJavaField f = pMethod.newJavaField(String .class); 108 DirectAccessible value = getValue(pMethod, pValue, type); 109 List list = new ArrayList (); 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 .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 pValue, Object pData) 126 throws SAXException { 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 pValue) 135 throws SAXException { 136 if (javaType.getParseMethod() == null) { 137 return super.getCastFromString(pController, pValue); 138 } else { 139 throw new SAXParseException ("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 pValue, DirectAccessible pData) 146 throws SAXException { 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 |