1 package org.apache.ws.jaxme.generator.sg.impl.ccsg; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import javax.xml.namespace.QName ; 10 11 import org.apache.ws.jaxme.WildcardAttribute; 12 import org.apache.ws.jaxme.generator.sg.AttributeSG; 13 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG; 14 import org.apache.ws.jaxme.generator.sg.GroupSG; 15 import org.apache.ws.jaxme.generator.sg.ParticleSG; 16 import org.apache.ws.jaxme.generator.sg.SGlet; 17 import org.apache.ws.jaxme.generator.sg.TypeSG; 18 import org.apache.ws.jaxme.impl.JMSAXDriverController; 19 import org.apache.ws.jaxme.js.DirectAccessible; 20 import org.apache.ws.jaxme.js.JavaMethod; 21 import org.apache.ws.jaxme.js.JavaQName; 22 import org.apache.ws.jaxme.js.JavaSource; 23 import org.apache.ws.jaxme.js.LocalJavaField; 24 import org.apache.ws.jaxme.xs.xml.XsQName; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.helpers.AttributesImpl ; 27 28 29 33 public class DriverGeneratingVisitor extends ParticleVisitorImpl { 34 private final JavaSource js; 35 36 39 public DriverGeneratingVisitor(JavaSource pJs) { 40 js = pJs; 41 } 42 43 49 protected List getNames(ComplexTypeSG pType) throws SAXException { 50 final List names = new ArrayList (); 51 ParticleVisitor visitor = new ParticleVisitorImpl(){ 52 private void addAttributes(ComplexTypeSG pType) { 53 AttributeSG[] myAttributes = pType.getAttributes(); 54 for (int i = 0; i < myAttributes.length; i++) { 55 XsQName qName = myAttributes[i].getName(); 56 if (qName != null) { 57 names.add(qName); 58 } 59 } 60 } 61 public void emptyType(ComplexTypeSG pType) { 62 addAttributes(pType); 63 } 64 public void simpleContent(ComplexTypeSG pType) { 65 addAttributes(pType); 66 } 67 public void startComplexContent(ComplexTypeSG pType) { 68 addAttributes(pType); 69 } 70 public void simpleElementParticle(GroupSG pGroup, ParticleSG pParticle) throws SAXException { 71 names.add(pParticle.getObjectSG().getName()); 72 } 73 public void complexElementParticle(GroupSG pGroup, ParticleSG pParticle) throws SAXException { 74 names.add(pParticle.getObjectSG().getName()); 75 } 76 }; 77 new ParticleWalker(visitor).walk(pType); 78 return names; 79 } 80 81 private JavaMethod newGetPreferredPrefixMethod(ComplexTypeSG pType) throws SAXException { 82 List names = getNames(pType); 83 Map uris = new HashMap (); 84 for (Iterator iter = names.iterator(); iter.hasNext(); ) { 85 XsQName qName = (XsQName) iter.next(); 86 String prefix = qName.getPrefix(); 87 if (prefix != null) { 88 String uri = qName.getNamespaceURI(); 89 if (uri == null) uri = ""; 90 if (!uris.containsKey(uri)) { 91 uris.put(uri, prefix); 92 } 93 } 94 } 95 96 JavaMethod jm = js.newJavaMethod("getPreferredPrefix", String .class, JavaSource.PUBLIC); 97 DirectAccessible pURI = jm.addParam(String .class, "pURI"); 98 if (!uris.isEmpty()) { 99 jm.addIf(pURI, " == null"); 100 jm.addLine(pURI, " = \"\";"); 101 jm.addEndIf(); 102 boolean first = true; 103 for (Iterator iter = uris.entrySet().iterator(); iter.hasNext(); ) { 104 Map.Entry entry = (Map.Entry ) iter.next(); 105 String uri = (String ) entry.getKey(); 106 String prefix = (String ) entry.getValue(); 107 jm.addIf(first, pURI, ".equals(", JavaSource.getQuoted(uri), ")"); 108 jm.addLine("return ", JavaSource.getQuoted(prefix), ";"); 109 first = false; 110 } 111 jm.addEndIf(); 112 } 113 jm.addLine("return null;"); 114 return jm; 115 } 116 117 private JavaMethod newGetAttributesMethod(ComplexTypeSG pType) throws SAXException { 118 JavaMethod jm = js.newJavaMethod("getAttributes", AttributesImpl .class, JavaSource.PUBLIC); 119 final DirectAccessible pController = jm.addParam(JMSAXDriverController.class, "pController"); 120 DirectAccessible pObject = jm.addParam(Object .class, "pObject"); 121 jm.addThrows(SAXException .class); 122 final LocalJavaField result = jm.newJavaField(AttributesImpl .class); 123 result.addLine("new ", AttributesImpl .class, "()"); 124 AttributeSG[] myAttributes = pType.getAttributes(); 125 if (myAttributes.length > 0) { 126 JavaQName elementInterface = pType.getClassContext().getXMLInterfaceName(); 127 LocalJavaField element = jm.newJavaField(elementInterface); 128 element.addLine("(", elementInterface, ") ", pObject); 129 130 for (int i = 0; i < myAttributes.length; i++) { 131 final AttributeSG attribute = myAttributes[i]; 132 if (attribute.isWildcard()) { 133 LocalJavaField anyAttributes = jm.newJavaField(WildcardAttribute[].class); 134 anyAttributes.addLine(element, ".", attribute.getPropertySG().getXMLGetMethodName() + "Array", "()"); 135 DirectAccessible index = jm.addForArray(anyAttributes); 136 LocalJavaField wildcardAttribute = jm.newJavaField(WildcardAttribute.class); 137 wildcardAttribute.addLine(anyAttributes, "[", index, "]"); 138 LocalJavaField qName = jm.newJavaField(QName .class); 139 qName.addLine(wildcardAttribute, ".getName()"); 140 LocalJavaField uri = jm.newJavaField(String .class); 141 uri.addLine(qName, ".getNamespaceURI()"); 142 LocalJavaField localPart = jm.newJavaField(String .class); 143 localPart.addLine(qName, ".getLocalPart()"); 144 jm.addLine(result, ".addAttribute(", uri, ", ", localPart, 145 ", ", pController, ".getAttrQName(this, ", uri, ", ", localPart, 146 "), \"CDATA\", ", wildcardAttribute, ".getValue());"); 147 jm.addEndFor(); 148 } else { 149 SGlet sgLet = new SGlet(){ 150 public void generate(JavaMethod pMethod, Object pValue) throws SAXException { 151 String uri = JavaSource.getQuoted(attribute.getName().getNamespaceURI()); 152 String localName = JavaSource.getQuoted(attribute.getName().getLocalName()); 153 pMethod.addLine(result, ".addAttribute(", uri, ", ", localName, 154 ", ", pController, ".getAttrQName(this, ", uri, ", ", localName, "), \"CDATA\", ", 155 attribute.getTypeSG().getSimpleTypeSG().getCastToString(pMethod, pValue, pController), ");"); 156 } 157 }; 158 attribute.forAllNonNullValues(jm, element, sgLet); 159 } 160 } 161 } 162 jm.addLine("return ", result, ";"); 163 return jm; 164 } 165 166 private JavaMethod newMarshalChildsMethod(ComplexTypeSG pType) throws SAXException { 167 MarshalChildsMethodGeneratingVisitor visitor = new MarshalChildsMethodGeneratingVisitor(js); 168 new ParticleWalker(visitor).walk(pType); 169 return visitor.getMethod(); 170 } 171 172 private void generate(ComplexTypeSG pType) throws SAXException { 173 newGetAttributesMethod(pType); 174 newGetPreferredPrefixMethod(pType); 175 newMarshalChildsMethod(pType); 176 } 177 178 public void emptyType(ComplexTypeSG pType) throws SAXException { 179 generate(pType); 180 } 181 182 public void simpleContent(ComplexTypeSG pType) throws SAXException { 183 generate(pType); 184 } 185 186 public void startComplexContent(ComplexTypeSG pType) throws SAXException { 187 generate(pType); 188 } 189 190 public void complexElementParticle(GroupSG pGroup, ParticleSG pParticle) throws SAXException { 191 TypeSG tSG = pParticle.getObjectSG().getTypeSG(); 192 if (tSG.isComplex() && !tSG.isGlobalClass()) { 193 tSG.getComplexTypeSG().getXMLSerializer(js); 194 } 195 } 196 } 197 | Popular Tags |