1 16 package org.apache.ws.jaxme.generator.sg.impl.ccsg; 17 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import javax.xml.bind.ValidationEvent; 22 23 import org.apache.ws.jaxme.ValidationEvents; 24 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG; 25 import org.apache.ws.jaxme.generator.sg.GroupSG; 26 import org.apache.ws.jaxme.generator.sg.ParticleSG; 27 import org.apache.ws.jaxme.impl.JMUnmarshallerHandlerImpl; 28 import org.apache.ws.jaxme.js.DirectAccessible; 29 import org.apache.ws.jaxme.js.JavaComment; 30 import org.apache.ws.jaxme.js.JavaField; 31 import org.apache.ws.jaxme.js.JavaMethod; 32 import org.apache.ws.jaxme.js.JavaSource; 33 import org.apache.ws.jaxme.js.LocalJavaField; 34 import org.xml.sax.SAXException ; 35 36 37 42 public class AllHandlerSG extends GroupHandlerSG { 43 private JavaField childNumField; 44 45 49 public AllHandlerSG(ComplexTypeSG pType, JavaSource pJs) 50 throws SAXException { 51 super(pType, pJs); 52 } 53 54 AllHandlerSG(GroupHandlerSG pOuterHandler, ComplexTypeSG pType, 55 GroupSG pGroup, JavaSource pJs) 56 throws SAXException { 57 super(pOuterHandler, pType, pGroup, pJs); 58 } 59 60 protected int getState(int pParticleNum) { return pParticleNum; } 61 62 private JavaField getChildNumField() { 63 if (childNumField == null) { 64 childNumField = getJavaSource().newJavaField("__childNum", int.class, JavaSource.PRIVATE); 65 JavaComment jc = childNumField.newComment(); 66 jc.addLine("Index of the particle being currently parsed"); 67 } 68 return childNumField; 69 } 70 71 protected DirectAccessible getEndElementState() throws SAXException { 72 return getChildNumField(); 73 } 74 75 protected void acceptParticle(JavaMethod pJm, int pNum) throws SAXException { 76 pJm.addIf(getStateField(), "[" + pNum, "]"); 77 ParticleSG particle = particles[pNum]; 78 if (particle.isElement()) { 79 pJm.addLine("getHandler().validationEvent(", ValidationEvent.class, ".WARNING, ", 80 JavaSource.getQuoted("The element " + particle.getObjectSG().getName() + 81 " has already been defined."), 82 ", ", ValidationEvents.class, ".EVENT_ALL_GROUP_REUSE, null);"); 83 } else if (particle.isGroup()) { 84 pJm.addLine("getHandler().validationEvent(", ValidationEvent.class, ".WARNING, ", 85 JavaSource.getQuoted("The group " + GroupUtil.getGroupName(particle.getGroupSG()) + 86 " has already been defined."), 87 ", ", ValidationEvents.class, ".EVENT_ALL_GROUP_REUSE, null);"); 88 } else if (particle.isWildcard()) { 89 throw new IllegalStateException ("TODO: Add support for wildcards."); 90 } else { 91 throw new IllegalStateException ("Invalid particle type"); 92 } 93 pJm.addEndIf(); 94 pJm.addLine(getChildNumField(), " = " + pNum + ";"); 95 pJm.addLine(getStateField(), "[" + pNum, "] = true;"); 96 } 97 98 public JavaMethod newStartElementMethod() throws SAXException { 99 JavaMethod result = super.newStartElementMethod(); 100 LocalJavaField unmarshallerHandler = result.newJavaField(JMUnmarshallerHandlerImpl.class); 101 unmarshallerHandler.addLine("getHandler()"); 102 handleStartElementStates(unmarshallerHandler, result, 0, particles.length-1); 103 result.addLine("return false;"); 104 return result; 105 } 106 107 public JavaMethod newIsFinishedMethod() throws SAXException { 108 JavaMethod result = super.newIsFinishedMethod(); 109 List requiredParticles = new ArrayList (); 110 for (int i = 0; i < particles.length; i++) { 111 if (isRequiredParticle(particles[i])) { 112 if (requiredParticles.size() > 0) { 113 requiredParticles.add(" && "); 114 } 115 requiredParticles.add(new Object []{getStateField(), 116 "[" + i + "]"}); 117 } 118 } 119 if (requiredParticles.size() == 0) { 120 result.addLine("return true;"); 121 } else { 122 result.addLine("return ", requiredParticles, ";"); 123 } 124 return result; 125 } 126 127 protected JavaField newStateField() throws SAXException { 128 JavaField jf = getJavaSource().newJavaField("__state", boolean[].class, JavaSource.PRIVATE); 129 jf.addLine("new ", boolean.class, "[" + particles.length + "]"); 130 JavaComment jc = jf.newComment(); 131 jc.addLine("This array indicates the state of the group. For any"); 132 jc.addLine("possible child, the corresponding boolean value is set,"); 133 jc.addLine("if the child is parsed."); 134 jc.addLine("If the same child occurs again, and the childs boolean"); 135 jc.addLine("value is true, then an exception is thrown."); 136 jc.addLine("These are the indices, to which the child elements are"); 137 jc.addLine("mapped:"); 138 for (int i = 0; i < particles.length; i++) { 139 ParticleSG particle = particles[i]; 140 if (particle.isGroup()) { 141 GroupSG group = particle.getGroupSG(); 142 if (group.isGlobal()) { 143 jc.addLine(" " + i + " = The nested group " + group.getName()); 144 } else { 145 jc.addLine(" " + i + " = An anonymous nested group."); 146 } 147 } else if (particle.isElement()) { 148 jc.addLine(" " + i + " = The child element " + particle.getObjectSG().getName()); 149 } else if (particle.isWildcard()) { 150 throw new IllegalStateException ("TODO: Add support for wildcards."); 151 } else { 152 throw new IllegalStateException ("Invalid particle type."); 153 } 154 } 155 return jf; 156 } 157 } | Popular Tags |