1 20 21 package org.apache.directory.ldapstudio.dsmlv2; 22 23 24 import java.io.FileNotFoundException ; 25 import java.io.FileReader ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.Reader ; 29 import java.io.StringReader ; 30 31 import org.apache.directory.ldapstudio.dsmlv2.reponse.BatchResponse; 32 import org.apache.directory.ldapstudio.dsmlv2.reponse.Dsmlv2ResponseGrammar; 33 import org.apache.directory.shared.ldap.codec.LdapResponse; 34 import org.xmlpull.v1.XmlPullParser; 35 import org.xmlpull.v1.XmlPullParserException; 36 import org.xmlpull.v1.XmlPullParserFactory; 37 38 39 46 public class Dsmlv2ResponseParser 47 { 48 49 private Dsmlv2Container container; 50 51 52 58 public Dsmlv2ResponseParser() throws XmlPullParserException 59 { 60 this.container = new Dsmlv2Container(); 61 62 this.container.setGrammar( Dsmlv2ResponseGrammar.getInstance() ); 63 64 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 65 factory.setNamespaceAware( true ); 66 XmlPullParser xpp = factory.newPullParser(); 67 68 container.setParser( xpp ); 69 } 70 71 72 80 public void setInput( String str ) throws FileNotFoundException , XmlPullParserException 81 { 82 container.getParser().setInput( new StringReader ( str ) ); 83 } 84 85 86 96 public void setInputFile( String fileName ) throws FileNotFoundException , XmlPullParserException 97 { 98 Reader reader = new FileReader ( fileName ); 99 container.getParser().setInput( reader ); 100 } 101 102 103 113 public void setInput( InputStream inputStream, String inputEncoding ) throws XmlPullParserException 114 { 115 container.getParser().setInput( inputStream, inputEncoding ); 116 } 117 118 119 126 public void parse() throws XmlPullParserException, IOException 127 { 128 Dsmlv2ResponseGrammar grammar = Dsmlv2ResponseGrammar.getInstance(); 129 130 grammar.executeAction( container ); 131 } 132 133 134 140 public void parseBatchResponse() throws XmlPullParserException 141 { 142 XmlPullParser xpp = container.getParser(); 143 144 int eventType = xpp.getEventType(); 145 do 146 { 147 if ( eventType == XmlPullParser.START_DOCUMENT ) 148 { 149 container.setState( Dsmlv2StatesEnum.INIT_GRAMMAR_STATE ); 150 } 151 else if ( eventType == XmlPullParser.END_DOCUMENT ) 152 { 153 container.setState( Dsmlv2StatesEnum.END_STATE ); 154 } 155 else if ( eventType == XmlPullParser.START_TAG ) 156 { 157 processTag( container, Tag.START ); 158 } 159 else if ( eventType == XmlPullParser.END_TAG ) 160 { 161 processTag( container, Tag.END ); 162 } 163 try 164 { 165 eventType = xpp.next(); 166 } 167 catch ( IOException e ) 168 { 169 throw new XmlPullParserException( "An IOException ocurred during parsing : " + e.getMessage(), xpp, 170 null ); 171 } 172 } 173 while ( container.getState() != Dsmlv2StatesEnum.BATCH_RESPONSE_LOOP ); 174 } 175 176 177 187 private void processTag( Dsmlv2Container container, int tagType ) throws XmlPullParserException 188 { 189 XmlPullParser xpp = container.getParser(); 190 191 String tagName = xpp.getName().toLowerCase(); 192 193 GrammarTransition transition = container.getTransition( container.getState(), new Tag( tagName, tagType ) ); 194 195 if ( transition != null ) 196 { 197 container.setState( transition.getNextState() ); 198 199 if ( transition.hasAction() ) 200 { 201 transition.getAction().action( container ); 202 } 203 } 204 else 205 { 206 throw new XmlPullParserException( "The tag " + new Tag( tagName, tagType ) 207 + " can't be found at this position", xpp, null ); 208 } 209 } 210 211 212 218 public BatchResponse getBatchResponse() 219 { 220 return container.getBatchResponse(); 221 } 222 223 224 231 public LdapResponse getNextResponse() throws XmlPullParserException 232 { 233 if ( container.getBatchResponse() == null ) 234 { 235 parseBatchResponse(); 236 } 237 238 XmlPullParser xpp = container.getParser(); 239 240 int eventType = xpp.getEventType(); 241 do 242 { 243 while ( eventType == XmlPullParser.TEXT ) 244 { 245 try 246 { 247 xpp.next(); 248 } 249 catch ( IOException e ) 250 { 251 throw new XmlPullParserException( "An IOException ocurred during parsing : " + e.getMessage(), xpp, 252 null ); 253 } 254 eventType = xpp.getEventType(); 255 } 256 257 if ( eventType == XmlPullParser.START_DOCUMENT ) 258 { 259 container.setState( Dsmlv2StatesEnum.INIT_GRAMMAR_STATE ); 260 } 261 else if ( eventType == XmlPullParser.END_DOCUMENT ) 262 { 263 container.setState( Dsmlv2StatesEnum.END_STATE ); 264 return null; 265 } 266 else if ( eventType == XmlPullParser.START_TAG ) 267 { 268 processTag( container, Tag.START ); 269 } 270 else if ( eventType == XmlPullParser.END_TAG ) 271 { 272 processTag( container, Tag.END ); 273 } 274 try 275 { 276 eventType = xpp.next(); 277 } 278 catch ( IOException e ) 279 { 280 throw new XmlPullParserException( "An IOException ocurred during parsing : " + e.getMessage(), xpp, 281 null ); 282 } 283 } 284 while ( container.getState() != Dsmlv2StatesEnum.BATCH_RESPONSE_LOOP ); 285 286 return container.getBatchResponse().getCurrentResponse(); 287 } 288 289 290 296 public void parseAllResponses() throws XmlPullParserException 297 { 298 while ( getNextResponse() != null ) 299 { 300 continue; 301 } 302 } 303 } 304 | Popular Tags |