1 16 package org.apache.ws.jaxme.generator.sg.impl.ccsg; 17 18 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG; 19 import org.apache.ws.jaxme.generator.sg.GroupSG; 20 import org.apache.ws.jaxme.generator.sg.ParticleSG; 21 import org.apache.ws.jaxme.impl.JMUnmarshallerHandlerImpl; 22 import org.apache.ws.jaxme.js.DirectAccessible; 23 import org.apache.ws.jaxme.js.JavaComment; 24 import org.apache.ws.jaxme.js.JavaField; 25 import org.apache.ws.jaxme.js.JavaMethod; 26 import org.apache.ws.jaxme.js.JavaSource; 27 import org.apache.ws.jaxme.js.LocalJavaField; 28 import org.xml.sax.SAXException ; 29 30 31 36 public class SequenceHandlerSG extends GroupHandlerSG { 37 41 public SequenceHandlerSG(ComplexTypeSG pTypeSG, JavaSource pJs) 42 throws SAXException { 43 super(pTypeSG, pJs); 44 } 45 46 SequenceHandlerSG(GroupHandlerSG pOuterHandler, ComplexTypeSG pTypeSG, 47 GroupSG pGroup, JavaSource pJs) 48 throws SAXException { 49 super(pOuterHandler, pTypeSG, pGroup, pJs); 50 } 51 52 protected DirectAccessible getEndElementState() throws SAXException { 53 return getStateField(); 54 } 55 56 protected int getState(int pNum) { 57 return pNum+1; 58 } 59 60 private int fromState(int pState) { 61 return pState-1; 62 } 63 64 protected JavaField newStateField() throws SAXException { 65 JavaField jf = getJavaSource().newJavaField("__state", int.class, JavaSource.PRIVATE); 66 JavaComment jc = jf.newComment(); 67 jc.addLine("The current state. The following values are valid states:"); 68 jc.addLine(" 0 = Before parsing the element"); 69 for (int i = 0; i < particles.length; i++) { 70 ParticleSG particle = particles[i]; 71 if (particle.isGroup()) { 72 GroupSG group = particle.getGroupSG(); 73 if (group.isGlobal()) { 74 jc.addLine(" " + getState(i) + " = While parsing the nested group " + group.getName()); 75 } else { 76 jc.addLine(" " + getState(i) + " = While parsing the nested group " + GroupUtil.getGroupName(group)); 77 } 78 } else if (particle.isElement()) { 79 jc.addLine(" " + getState(i) + " = While or after parsing the child element " + particle.getObjectSG().getName()); 80 } else if (particle.isWildcard()) { 81 throw new IllegalStateException ("TODO: Add support for wildcards."); 82 } else { 83 throw new IllegalStateException ("Invalid particle type."); 84 } 85 } 86 87 return jf; 88 } 89 90 91 protected void acceptParticle(JavaMethod pJm, int pNum) throws SAXException { 92 pJm.addLine(getStateField(), " = " + getState(pNum) + ";"); 93 } 94 95 public JavaMethod newStartElementMethod() throws SAXException { 96 JavaMethod result = super.newStartElementMethod(); 97 LocalJavaField unmarshallerHandler = result.newJavaField(JMUnmarshallerHandlerImpl.class); 98 unmarshallerHandler.addLine("getHandler()"); 99 result.addSwitch(getStateField()); 100 result.addCase(new Integer (0)); 101 handleStartElementStates(unmarshallerHandler, 102 result, getFirstValidParticle(0), 103 getLastValidParticle(0)); 104 result.addBreak(); 105 for (int i = 0; i < particles.length; i++) { 106 int state = getState(i); 107 result.addCase(new Integer (state)); 108 handleStartElementStates(unmarshallerHandler, 109 result, getFirstValidParticle(state), 110 getLastValidParticle(state)); 111 result.addBreak(); 112 } 113 result.addDefault(); 114 result.addThrowNew(IllegalStateException .class, 115 JavaSource.getQuoted("Invalid state: "), 116 " + ", getStateField()); 117 result.addEndSwitch(); 118 result.addLine("return false;"); 119 return result; 120 } 121 122 126 private int getFirstValidParticle(int pState) { 127 if (pState == 0) { 128 return 0; 129 } else { 130 int i = fromState(pState); 131 ParticleSG particle = particles[i]; 132 if (particle.isMultiple()) { 133 return i; 134 } else if (i+1 < particles.length) { 135 return i+1; 136 } else { 137 return -1; 138 } 139 } 140 } 141 142 146 private int getLastValidParticle(int pState) { 147 int lastParticle; 148 if (pState == 0) { 149 lastParticle = 0; 150 } else { 151 lastParticle = fromState(pState)+1; 152 if (lastParticle >= particles.length) { 153 return fromState(pState); 154 } 155 } 156 while (lastParticle < particles.length-1) { 157 ParticleSG particle = particles[lastParticle]; 158 if (isRequiredParticle(particle)) { 159 break; 160 } else { 161 ++lastParticle; 162 } 163 } 164 return lastParticle; 165 } 166 167 public JavaMethod newIsFinishedMethod() throws SAXException { 168 JavaMethod result = super.newIsFinishedMethod(); 169 result.addSwitch(getStateField()); 170 boolean allOptional = true; 171 for (int i = particles.length-1; i >= 0; i--) { 172 ParticleSG particle = particles[i]; 173 result.addCase(new Integer (getState(i))); 174 if (isRequiredParticle(particle)) { 175 allOptional = false; 176 break; 177 } 178 } 179 if (allOptional) { 180 result.addCase(new Integer (0)); 181 } 182 result.addLine("return true;"); 183 result.addDefault(); 184 result.addLine("return false;"); 185 result.addEndSwitch(); 186 return result; 187 } 188 } | Popular Tags |