KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > dsmlv2 > Dsmlv2ResponseParser


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.dsmlv2;
22
23
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.io.StringReader JavaDoc;
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 /**
40  * This class represents the DSMLv2 Parser.
41  * It can be used to parse a DSMLv2 Response input.
42  *
43  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
44  * @version $Rev$, $Date$
45  */

46 public class Dsmlv2ResponseParser
47 {
48     /** The associated DSMLv2 container */
49     private Dsmlv2Container container;
50
51
52     /**
53      * Creates a new instance of Dsmlv2ResponseParser.
54      *
55      * @throws XmlPullParserException
56      * if an error occurs while the initialization of the parser
57      */

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     /**
73      * Sets the input string the parser is going to parse
74      *
75      * @param str
76      * the string the parser is going to parse
77      * @throws XmlPullParserException
78      * if an error occurs in the parser
79      */

80     public void setInput( String JavaDoc str ) throws FileNotFoundException JavaDoc, XmlPullParserException
81     {
82         container.getParser().setInput( new StringReader JavaDoc( str ) );
83     }
84
85
86     /**
87      * Sets the input file the parser is going to parse
88      *
89      * @param fileName
90      * the name of the file
91      * @throws FileNotFoundException
92      * if the file does not exist
93      * @throws XmlPullParserException
94      * if an error occurs in the parser
95      */

96     public void setInputFile( String JavaDoc fileName ) throws FileNotFoundException JavaDoc, XmlPullParserException
97     {
98         Reader JavaDoc reader = new FileReader JavaDoc( fileName );
99         container.getParser().setInput( reader );
100     }
101
102
103     /**
104      * Sets the input stream the parser is going to process
105      *
106      * @param inputStream
107      * contains a raw byte input stream of possibly unknown encoding (when inputEncoding is null)
108      * @param inputEncoding
109      * if not null it MUST be used as encoding for inputStream
110      * @throws XmlPullParserException
111      * if an error occurs in the parser
112      */

113     public void setInput( InputStream JavaDoc inputStream, String JavaDoc inputEncoding ) throws XmlPullParserException
114     {
115         container.getParser().setInput( inputStream, inputEncoding );
116     }
117
118
119     /**
120      * Launches the parsing on the input
121      *
122      * @throws XmlPullParserException
123      * when an unrecoverable error occurs
124      * @throws IOException
125      */

126     public void parse() throws XmlPullParserException, IOException JavaDoc
127     {
128         Dsmlv2ResponseGrammar grammar = Dsmlv2ResponseGrammar.getInstance();
129
130         grammar.executeAction( container );
131     }
132
133
134     /**
135      * Launches the parsing of the Batch Response only
136      *
137      * @throws XmlPullParserException
138      * if an error occurs in the parser
139      */

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 JavaDoc 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     /**
178      * Processes the task required in the grammar to the given tag type
179      *
180      * @param container
181      * the DSML container
182      * @param tagType
183      * the tag type
184      * @throws XmlPullParserException
185      * when an error occurs during the parsing
186      */

187     private void processTag( Dsmlv2Container container, int tagType ) throws XmlPullParserException
188     {
189         XmlPullParser xpp = container.getParser();
190
191         String JavaDoc 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     /**
213      * Gets the Batch Response or null if the it has not been parsed yet
214      *
215      * @return
216      * the Batch Response or null if the it has not been parsed yet
217      */

218     public BatchResponse getBatchResponse()
219     {
220         return container.getBatchResponse();
221     }
222
223
224     /**
225      * Returns the next Request or null if there's no more request
226      * @return
227      * the next Request or null if there's no more request
228      * @throws XmlPullParserException
229      * when an error occurs during the parsing
230      */

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 JavaDoc 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 JavaDoc 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     /**
291      * Parses all the responses
292      *
293      * @throws XmlPullParserException
294      * when an error occurs during the parsing
295      */

296     public void parseAllResponses() throws XmlPullParserException
297     {
298         while ( getNextResponse() != null )
299         {
300             continue;
301         }
302     }
303 }
304
Popular Tags