1 16 package org.apache.ws.jaxme.generator.sg.impl.ccsg; 17 18 import javax.xml.bind.ValidationEvent; 19 20 import org.apache.ws.jaxme.ValidationEvents; 21 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG; 22 import org.apache.ws.jaxme.generator.sg.GroupSG; 23 import org.apache.ws.jaxme.generator.sg.ParticleSG; 24 import org.apache.ws.jaxme.impl.JMUnmarshallerHandlerImpl; 25 import org.apache.ws.jaxme.js.DirectAccessible; 26 import org.apache.ws.jaxme.js.JavaComment; 27 import org.apache.ws.jaxme.js.JavaField; 28 import org.apache.ws.jaxme.js.JavaMethod; 29 import org.apache.ws.jaxme.js.JavaSource; 30 import org.apache.ws.jaxme.js.LocalJavaField; 31 import org.xml.sax.SAXException ; 32 33 34 39 public class ChoiceHandlerSG extends GroupHandlerSG { 40 private JavaField childNumField; 41 42 46 public ChoiceHandlerSG(ComplexTypeSG pType, JavaSource pJs) 47 throws SAXException { 48 super(pType, pJs); 49 } 50 51 ChoiceHandlerSG(GroupHandlerSG pOuterHandler, ComplexTypeSG pType, 52 GroupSG pGroup, JavaSource pJs) 53 throws SAXException { 54 super(pOuterHandler, pType, pGroup, pJs); 55 } 56 57 protected int getState(int pParticleNum) { return pParticleNum; } 58 59 protected void acceptParticle(JavaMethod pJm, int pNum) throws SAXException { 60 pJm.addIf(getStateField()); 61 ParticleSG particle = particles[pNum]; 62 pJm.addIf(getChildNumField(), " != " + pNum); 63 pJm.addLine("getHandler().validationEvent(", ValidationEvent.class, ".WARNING, ", 64 JavaSource.getQuoted("Multiple different particles present in a choive group."), 65 ", ", ValidationEvents.class, ".EVENT_CHOICE_GROUP_REUSE, null);"); 66 if (!particles[pNum].isMultiple()) { 67 pJm.addElse(); 68 if (particle.isElement()) { 69 pJm.addLine("getHandler().validationEvent(", ValidationEvent.class, ".WARNING, ", 70 JavaSource.getQuoted("The element " + particle.getObjectSG().getName() + 71 " has already been defined."), 72 ", ", ValidationEvents.class, ".EVENT_CHOICE_GROUP_REUSE, null);"); 73 } else if (particle.isGroup()) { 74 pJm.addLine("getHandler().validationEvent(", ValidationEvent.class, ".WARNING, ", 75 JavaSource.getQuoted("The group " + GroupUtil.getGroupName(particle.getGroupSG()) + 76 " has already been defined."), 77 ", ", ValidationEvents.class, ".EVENT_CHOICE_GROUP_REUSE, null);"); 78 } else if (particle.isWildcard()) { 79 throw new IllegalStateException ("TODO: Add support for wildcards."); 80 } else { 81 throw new IllegalStateException ("Invalid particle type"); 82 } 83 } 84 pJm.addEndIf(); 85 pJm.addEndIf(); 86 pJm.addLine(getStateField(), " = true;"); 87 pJm.addLine(getChildNumField(), " = " + pNum + ";"); 88 } 89 90 public JavaMethod newStartElementMethod() throws SAXException { 91 JavaMethod result = super.newStartElementMethod(); 92 LocalJavaField unmarshallerHandler = result.newJavaField(JMUnmarshallerHandlerImpl.class); 93 unmarshallerHandler.addLine("getHandler()"); 94 handleStartElementStates(unmarshallerHandler, result, 0, particles.length-1); 95 result.addLine("return false;"); 96 return result; 97 } 98 99 private JavaField getChildNumField() { 100 if (childNumField == null) { 101 childNumField = getJavaSource().newJavaField("__childNum", int.class, JavaSource.PRIVATE); 102 JavaComment jc = childNumField.newComment(); 103 jc.addLine("Index of the particle being currently parsed"); 104 } 105 return childNumField; 106 } 107 108 protected DirectAccessible getEndElementState() throws SAXException { 109 return getChildNumField(); 110 } 111 112 public JavaMethod newIsFinishedMethod() throws SAXException { 113 JavaMethod result = super.newIsFinishedMethod(); 114 Object o = getStateField(); 115 for (int i = 0; i < particles.length; i++) { 116 if (!isRequiredParticle(particles[i])) { 117 o = "true"; 118 break; 119 } 120 } 121 result.addLine("return ", o, ";"); 122 return result; 123 } 124 125 protected JavaField newStateField() throws SAXException { 126 JavaField jf = getJavaSource().newJavaField("__state", boolean.class, JavaSource.PRIVATE); 127 JavaComment jc = jf.newComment(); 128 jc.addLine("Will be set to true, if the first child is parsed."); 129 jc.addLine("It is an error, if another child is parsed, and the"); 130 jc.addLine("fields value is true."); 131 return jf; 132 } 133 } 134 | Popular Tags |