1 16 package org.apache.ws.jaxme.generator.sg.impl.ccsg; 17 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.Set ; 21 22 import javax.xml.bind.ValidationEvent; 23 import javax.xml.namespace.QName ; 24 25 import org.apache.ws.jaxme.ValidationEvents; 26 import org.apache.ws.jaxme.generator.sg.AttributeSG; 27 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG; 28 import org.apache.ws.jaxme.generator.sg.TypeSG; 29 import org.apache.ws.jaxme.generator.types.BooleanSG; 30 import org.apache.ws.jaxme.generator.types.StringSG; 31 import org.apache.ws.jaxme.js.DirectAccessible; 32 import org.apache.ws.jaxme.js.JavaMethod; 33 import org.apache.ws.jaxme.js.JavaQName; 34 import org.apache.ws.jaxme.js.JavaQNameImpl; 35 import org.apache.ws.jaxme.js.JavaSource; 36 import org.apache.ws.jaxme.js.LocalJavaField; 37 import org.apache.ws.jaxme.js.TypedValue; 38 import org.apache.ws.jaxme.js.impl.TypedValueImpl; 39 import org.xml.sax.Attributes ; 40 import org.xml.sax.SAXException ; 41 42 43 47 public abstract class HandlerSGImpl implements HandlerSG { 48 protected final ComplexTypeSG ctSG; 49 private final JavaSource javaSource; 50 private DirectAccessible paramLocalName; 51 private DirectAccessible paramNamespaceURI; 52 private DirectAccessible paramResult; 53 private DirectAccessible paramQName; 54 private DirectAccessible paramAttrs; 55 56 protected HandlerSGImpl(ComplexTypeSG pType, JavaSource pJs) { 57 ctSG = pType; 58 javaSource = pJs; 59 } 60 61 63 public JavaSource getJavaSource() { 64 return javaSource; 65 } 66 67 protected void setParamAttrs(DirectAccessible pParamAttrs) { paramAttrs = pParamAttrs; } 68 69 protected DirectAccessible getParamAttrs() { return paramAttrs; } 70 71 protected void setParamLocalName(DirectAccessible pParamLocalName) { paramLocalName = pParamLocalName; } 72 73 protected DirectAccessible getParamLocalName() { return paramLocalName; } 74 75 protected void setParamNamespaceURI(DirectAccessible pParamNamespaceURI) { paramNamespaceURI = pParamNamespaceURI; } 76 77 protected DirectAccessible getParamNamespaceURI() { return paramNamespaceURI; } 78 79 protected void setParamQName(DirectAccessible pParamQName) { paramQName = pParamQName; } 80 81 protected DirectAccessible getParamQName() { return paramQName; } 82 83 protected void setParamResult(DirectAccessible pParamResult) { paramResult = pParamResult; } 84 85 protected DirectAccessible getParamResult() { return paramResult; } 86 87 89 private Set createSetOfExplicitURIs(AttributeSG[] pAttributes) { 90 Set result = new HashSet (); 91 for (int i = 0; i < pAttributes.length; i++) { 92 AttributeSG attr = pAttributes[i]; 93 if (!attr.isWildcard()) { 94 String uri = attr.getName().getNamespaceURI(); 95 if (uri == null) { uri = ""; } 96 result.add(uri); 97 } 98 } 99 return result; 100 } 101 102 public JavaMethod newAddAttributeMethod() throws SAXException { 103 AttributeSG[] myAttributes = ctSG.getAttributes(); 104 if (myAttributes.length == 0) { 105 return null; 106 } 107 108 JavaMethod jm = getJavaSource().newJavaMethod("addAttribute", JavaQNameImpl.VOID, JavaSource.PUBLIC); 109 DirectAccessible pURI = jm.addParam(String .class, "pURI"); 110 DirectAccessible pLocalName = jm.addParam(String .class, "pLocalName"); 111 DirectAccessible pValue = jm.addParam(String .class, "pValue"); 112 jm.addThrows(SAXException .class); 113 jm.addIf(pURI, " == null"); 114 jm.addLine(pURI, " = \"\";"); 115 jm.addEndIf(); 116 117 JavaQName resultType = ctSG.getClassContext().getXMLInterfaceName(); 118 LocalJavaField result = jm.newJavaField(resultType); 119 result.addLine("(", resultType, ") result"); 120 121 Set uris = createSetOfExplicitURIs(myAttributes); 122 boolean first = true; 123 for (Iterator iter = uris.iterator(); iter.hasNext(); ) { 124 String uri = (String ) iter.next(); 125 jm.addIf(first, JavaSource.getQuoted(uri), ".equals(", pURI, ")"); 126 first = false; 127 boolean firstInNamespace = true; 128 for (int i = 0; i < myAttributes.length; i++) { 129 AttributeSG attr = myAttributes[i]; 130 if (attr.isWildcard()) { 131 continue; 132 } 133 String jUri = attr.getName().getNamespaceURI(); 134 if (jUri == null) { jUri = ""; } 135 if (!uri.equals(jUri)) { 136 continue; 137 } 138 139 jm.addIf(firstInNamespace, JavaSource.getQuoted(attr.getName().getLocalName()), ".equals(", pLocalName, ")"); 140 firstInNamespace = false; 141 TypedValue v = createSimpleTypeConversion(jm, myAttributes[i].getTypeSG(), pValue, "@" + myAttributes[i].getName()); 142 attr.getPropertySG().setValue(jm, result, v, null); 143 jm.addLine("return;"); 144 } 145 if (!firstInNamespace) { 146 jm.addEndIf(); 147 } 148 } 149 if (!first) { 150 jm.addEndIf(); 151 } 152 153 AttributeSG wildcard = null; 154 for (int i = 0; i < myAttributes.length; i++) { 155 if (myAttributes[i].isWildcard()) { 156 wildcard = myAttributes[i]; 157 break; 158 } 159 } 160 if (wildcard == null) { 161 jm.addLine("super.addAttribute(", pURI, ", ", pLocalName, ", ", pValue, ");"); 162 } else { 163 jm.addTry(); 164 jm.addLine(result, ".", wildcard.getPropertySG().getXMLSetMethodName(), "(new ", 165 QName .class, "(", pURI, ", ", pLocalName, "), ", pValue, ");"); 166 jm.addCatch(IllegalArgumentException .class); 167 jm.addLine("getHandler().validationEvent(", ValidationEvent.class, ".ERROR, ", 168 JavaSource.getQuoted("Invalid namespace for anyAttribute: '"), 169 " + ", pURI, " + ", JavaSource.getQuoted("', attribute name is '"), 170 " + ", pLocalName, " + ", JavaSource.getQuoted("'"), 171 ", ", ValidationEvents.class, ".EVENT_UNKNOWN_ANY_ATTRIBUTE, null);"); 172 jm.addEndTry(); 173 } 174 return jm; 175 } 176 177 public JavaMethod newStartElementMethod() throws SAXException { 178 JavaSource js = getJavaSource(); 179 JavaMethod jm = js.newJavaMethod("startElement", boolean.class, JavaSource.PUBLIC); 180 DirectAccessible pNamespaceURI = jm.addParam(String .class, "pNamespaceURI"); 181 DirectAccessible pLocalName = jm.addParam(String .class, "pLocalName"); 182 DirectAccessible pQName = jm.addParam(String .class, "pQName"); 183 DirectAccessible pAttr = jm.addParam(Attributes .class, "pAttr"); 184 setParamNamespaceURI(pNamespaceURI); 185 setParamLocalName(pLocalName); 186 setParamQName(pQName); 187 setParamAttrs(pAttr); 188 setParamResult(null); 189 jm.addThrows(SAXException .class); 190 return jm; 191 } 192 193 public JavaMethod newEndElementMethod() throws SAXException { 194 JavaMethod jm = getJavaSource().newJavaMethod("endElement", void.class, JavaSource.PUBLIC); 195 DirectAccessible pNamespaceURI = jm.addParam(String .class, "pNamespaceURI"); 196 DirectAccessible pLocalName = jm.addParam(String .class, "pLocalName"); 197 DirectAccessible pQName = jm.addParam(String .class, "pQName"); 198 DirectAccessible pResult = jm.addParam(Object .class, "pResult"); 199 setParamNamespaceURI(pNamespaceURI); 200 setParamLocalName(pLocalName); 201 setParamQName(pQName); 202 setParamAttrs(null); 203 setParamResult(pResult); 204 jm.addThrows(SAXException .class); 205 return jm; 206 } 207 208 public JavaMethod newIsFinishedMethod() throws SAXException { 209 JavaMethod jm = getJavaSource().newJavaMethod("isFinished", boolean.class, JavaSource.PUBLIC); 210 return jm; 211 } 212 213 public JavaMethod newIsEmptyMethod() throws SAXException { 214 JavaMethod jm = getJavaSource().newJavaMethod("isEmpty", boolean.class, JavaSource.PUBLIC); 215 return jm; 216 } 217 218 public JavaMethod newIsAtomicMethod() throws SAXException { 219 JavaMethod jm = getJavaSource().newJavaMethod("isAtomic", boolean.class, JavaSource.PUBLIC); 220 return jm; 221 } 222 223 protected TypedValue createSimpleTypeConversion(JavaMethod pJm, TypeSG pType, 224 TypedValue pValue, String pName) 225 throws SAXException { 226 JavaQName runtimeType = pType.getSimpleTypeSG().getRuntimeType(); 227 boolean causingParseConversionEvent = pType.getSimpleTypeSG().isCausingParseConversionEvent(); 228 LocalJavaField f = null; 229 if (causingParseConversionEvent) { 230 f = pJm.newJavaField(runtimeType); 231 pJm.addTry(); 232 } 233 Object s = new Object []{"(", StringSG.STRING_TYPE, ") ", pValue}; 234 TypedValue result = new TypedValueImpl(pType.getSimpleTypeSG().getCastFromString(pJm, s, "getHandler()"), 235 pType.getSimpleTypeSG().getRuntimeType()); 236 if (causingParseConversionEvent) { 237 pJm.addLine(f, " = ", result, ";"); 238 result = f; 239 DirectAccessible e = pJm.addCatch(Exception .class); 240 pJm.addLine("getHandler().parseConversionEvent(", 241 JavaSource.getQuoted("Failed to convert value of " + pName + ": "), 242 " + ", pValue, ", ", e, ");"); 243 Object o; 244 if (runtimeType.isPrimitive()) { 245 if (BooleanSG.BOOLEAN_TYPE.equals(runtimeType)) { 246 o = "false"; 247 } else { 248 o = "0"; 249 } 250 } else { 251 o = "null"; 252 } 253 pJm.addLine(f, " = ", o, ";"); 254 pJm.addEndTry(); 255 } 256 return result; 257 } 258 259 public void generate() throws SAXException { 260 newAddAttributeMethod(); 261 newStartElementMethod(); 262 newEndElementMethod(); 263 newIsFinishedMethod(); 264 newIsEmptyMethod(); 265 newIsAtomicMethod(); 266 } 267 } 268 | Popular Tags |