KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > LDAPTransformer


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

16 package org.apache.cocoon.transformation;
17
18 import java.io.IOException JavaDoc;
19 import java.io.UnsupportedEncodingException JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import javax.naming.Context JavaDoc;
29 import javax.naming.NamingEnumeration JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.naming.directory.Attribute JavaDoc;
32 import javax.naming.directory.BasicAttribute JavaDoc;
33 import javax.naming.directory.DirContext JavaDoc;
34 import javax.naming.directory.InitialDirContext JavaDoc;
35 import javax.naming.directory.ModificationItem JavaDoc;
36 import javax.naming.directory.SearchControls JavaDoc;
37 import javax.naming.directory.SearchResult JavaDoc;
38
39 import org.apache.avalon.framework.logger.Logger;
40 import org.apache.avalon.framework.parameters.Parameters;
41 import org.apache.cocoon.ProcessingException;
42 import org.apache.cocoon.environment.SourceResolver;
43 import org.apache.cocoon.xml.XMLConsumer;
44 import org.apache.commons.collections.MapUtils;
45 import org.apache.commons.lang.BooleanUtils;
46 import org.apache.commons.lang.ObjectUtils;
47 import org.apache.commons.lang.StringUtils;
48 import org.xml.sax.Attributes JavaDoc;
49 import org.xml.sax.Locator JavaDoc;
50 import org.xml.sax.SAXException JavaDoc;
51 import org.xml.sax.ext.LexicalHandler JavaDoc;
52 import org.xml.sax.helpers.AttributesImpl JavaDoc;
53
54 /**
55  * The <code>LDAPTransformer</code> can be plugged into a pipeline to transform
56  * the SAX events into queries and responses to/from a LDAP interface.
57  * <br>
58  * The file will be specified in a parameter tag in the sitemap pipeline to the
59  * transformer as follows:
60  * <p>
61  * <code>
62  * &lt;map:transform type="ldap"/&gt;<br>
63  * </code>
64  * </p>
65  * <br>
66  *
67  * The following DTD is valid:<br>
68  * &lt;!ELEMENT execute-query (attribute+ | show-attribute? | scope? | initializer? | authentication? | error-element? | sax-error? doc-element? | row-element? | version? | serverurl? | rootdn? | password? | deref-link? | count-limit? | searchbase, filter)&gt;<br>
69  * &lt;!ELEMENT execute-increment (attribute | show-attribute? | scope? | initializer? | authentication? | error-element? | sax-error? | doc-element? | row-element? | version? | serverurl? | rootdn? | password? | deref-link? | count-limit? | searchbase, filter)&gt;<br>
70  * increments (+1) an integer attribute on a directory-server (ldap)<br>
71  * &lt;!ELEMENT execute-replace (attribute | show-attribute? | scope? | initializer? | authentication? | error-element? | sax-error? | doc-element? | row-element? | version? | serverurl? | rootdn? | password? | deref-link? | count-limit? | searchbase, filter)&gt;<br>
72  * replace attribute on a directory-server (ldap)<br>
73  * &lt;!ELEMENT execute-add (attribute | show-attribute? | scope? | initializer? | authentication? | error-element? | sax-error? | doc-element? | row-element? | version? | serverurl? | rootdn? | password? | deref-link? | count-limit? | searchbase, filter)&gt;<br>
74  * add attribute on a directory-server (ldap)<br>
75  * <br>
76  * &lt;!ELEMENT initializer (#PCDATA)&gt;+ (default: "com.sun.jndi.ldap.LdapCtxFactory")<br>
77  * &lt;!ELEMENT authentication (#PCDATA)&gt;+ (default: "simple")<br>
78  * &lt;!ELEMENT version (#PCDATA)&gt;+ (default: "2")<br>
79  * &lt;!ELEMENT serverurl (#PCDATA)&gt;+<br>
80  * &lt;!ELEMENT port (#PCDATA)&gt;+ (default: 389)<br>
81  * &lt;!ELEMENT rootdn (#PCDATA)&gt;+<br>
82  * &lt;!ELEMENT password (#PCDATA)&gt;+<br>
83  * &lt;!ELEMENT scope (ONELEVEL_SCOPE | SUBTREE_SCOPE | OBJECT_SCOPE)&gt;+ (default: ONELEVEL_SCOPE)<br>
84  * &lt;!ELEMENT searchbase (#PCDATA)&gt;+<br>
85  * &lt;!ELEMENT doc-element (#PCDATA)&gt;+ (default: "doc-element")<br>
86  * &lt;!ELEMENT row-element (#PCDATA)&gt;+ (default: "row-element")<br>
87  * &lt;!ELEMENT error-element (#PCDATA)&gt;+ (default: "ldap-error") (in case of error returned error tag)<br>
88  * &lt;!ELEMENT sax_error (TRUE | FALSE)&gt+; (default: FALSE) (throws SAX-Exception instead of error tag)<br>
89  * &lt;!ELEMENT attribute (#PCDATA)&gt;<br>
90  * &lt;!ATTLIST attribute name CDATA #IMPLIED
91                           mode (append|replace) 'replace' #IMPLIED &gt; (in case execute-replace or execute-add elements using) <br>
92
93  * &lt;!ELEMENT show-attribute (TRUE | FALSE)&gt; (default: TRUE)<br>
94  * &lt;!ELEMENT filter (#PCDATA | execute-query)+&gt;<br>
95  * &lt;!ELEMENT deref-link (TRUE | FALSE)&gt; (default: FALSE)<br>
96  * &lt;!ELEMENT count-limit (#PCDATA)&gt; (integer default: 0 -&gt; no limit)<br>
97  * &lt;!ELEMENT time-limit (#PCDATA)&gt; (integer default: 0 -&gt; infinite)<br>
98  * &lt;!ELEMENT debug (TRUE | FALSE)&gt+; (default: FALSE)<br>
99  * <br>
100  * + can also be defined as parameter in the sitemap.
101  * <br>
102  *
103  * @author Felix Knecht
104  * @author <a HREF="mailto:unico@hippo.nl">Unico Hommes</a>
105  * @author <a HREF="mailto:yuryx@mobicomk.donpac.ru">Yury Mikhienko</a>
106  * @version CVS $Id: LDAPTransformer.java 124685 2005-01-08 22:20:56Z antonio $
107  */

108 public class LDAPTransformer extends AbstractTransformer {
109
110     /** The LDAP namespace ("http://apache.org/cocoon/LDAP/1.0")*/
111     public static final String JavaDoc my_uri = "http://apache.org/cocoon/LDAP/1.0";
112     public static final String JavaDoc my_name = "LDAPTransformer";
113
114     /** The LDAP namespace element names */
115     public static final String JavaDoc MAGIC_ATTRIBUTE_ELEMENT = "attribute";
116     public static final String JavaDoc MAGIC_ATTRIBUTE_ELEMENT_ATTRIBUTE = "name";
117     public static final String JavaDoc MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE = "mode";
118     public static final String JavaDoc MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_DEFAULT = "replace";
119     public static final String JavaDoc MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A = "append";
120     public static final String JavaDoc MAGIC_AUTHENTICATION_ELEMENT = "authentication";
121     public static final String JavaDoc MAGIC_COUNT_LIMIT_ELEMENT = "count-limit";
122     public static final String JavaDoc MAGIC_DEBUG_ELEMENT = "debug";
123     public static final String JavaDoc MAGIC_DEREF_LINK_ELEMENT = "deref-link";
124     public static final String JavaDoc MAGIC_DOC_ELEMENT = "doc-element";
125     public static final String JavaDoc MAGIC_ENCODING_ELEMENT = "encoding";
126     public static final String JavaDoc MAGIC_ERROR_ELEMENT = "error-element";
127     public static final String JavaDoc MAGIC_EXECUTE_ADD = "execute-add";
128     public static final String JavaDoc MAGIC_EXECUTE_INCREMENT = "execute-increment";
129     public static final String JavaDoc MAGIC_EXECUTE_QUERY = "execute-query";
130     public static final String JavaDoc MAGIC_EXECUTE_REPLACE = "execute-replace";
131     public static final String JavaDoc MAGIC_FILTER_ELEMENT = "filter";
132     public static final String JavaDoc MAGIC_INITIALIZER_ELEMENT = "initializer";
133     public static final String JavaDoc MAGIC_PASSWORD_ELEMENT = "password";
134     public static final String JavaDoc MAGIC_PORT_ELEMENT = "port";
135     public static final String JavaDoc MAGIC_ROOT_DN_ELEMENT = "rootdn";
136     public static final String JavaDoc MAGIC_ROW_ELEMENT = "row-element";
137     public static final String JavaDoc MAGIC_SAX_ERROR = "sax-error";
138     public static final String JavaDoc MAGIC_SCOPE_ELEMENT = "scope";
139     public static final String JavaDoc MAGIC_SEARCHBASE_ELEMENT = "searchbase";
140     public static final String JavaDoc MAGIC_SERVERURL_ELEMENT = "serverurl";
141     public static final String JavaDoc MAGIC_SHOW_ATTRIBUTE_ELEMENT = "show-attribute";
142     public static final String JavaDoc MAGIC_TIME_LIMIT_ELEMENT = "time-limit";
143     public static final String JavaDoc MAGIC_VERSION_ELEMENT = "version";
144
145     /** The states we are allowed to be in */
146     public static final int STATE_OUTSIDE = 0;
147     public static final int STATE_INSIDE_EXECUTE_QUERY = 1;
148     public static final int STATE_INSIDE_EXECUTE_INCREMENT = 2;
149     public static final int STATE_INSIDE_EXECUTE_ELEMENT = 3;
150     public static final int STATE_INSIDE_INITIALIZER_ELEMENT = 4;
151     public static final int STATE_INSIDE_SERVERURL_ELEMENT = 5;
152     public static final int STATE_INSIDE_PORT_ELEMENT = 6;
153     public static final int STATE_INSIDE_SCOPE_ELEMENT = 7;
154     public static final int STATE_INSIDE_VERSION_ELEMENT = 8;
155     public static final int STATE_INSIDE_AUTHENTICATION_ELEMENT = 9;
156     public static final int STATE_INSIDE_ROOT_DN_ELEMENT = 10;
157     public static final int STATE_INSIDE_PASSWORD_ELEMENT = 11;
158     public static final int STATE_INSIDE_SEARCHBASE_ELEMENT = 12;
159     public static final int STATE_INSIDE_DOC_ELEMENT = 13;
160     public static final int STATE_INSIDE_ROW_ELEMENT = 14;
161     public static final int STATE_INSIDE_ATTRIBUTE_ELEMENT = 15;
162     public static final int STATE_INSIDE_SHOW_ATTRIBUTE_ELEMENT = 16;
163     public static final int STATE_INSIDE_ERROR_ELEMENT = 17;
164     public static final int STATE_INSIDE_FILTER_ELEMENT = 18;
165     public static final int STATE_INSIDE_DEREF_LINK_ELEMENT = 19;
166     public static final int STATE_INSIDE_COUNT_LIMIT_ELEMENT = 20;
167     public static final int STATE_INSIDE_TIME_LIMIT_ELEMENT = 21;
168     public static final int STATE_INSIDE_DEBUG_ELEMENT = 22;
169     public static final int STATE_INSIDE_SAX_ERROR_ELEMENT = 23;
170     public static final int STATE_INSIDE_EXECUTE_REPLACE = 24;
171     public static final int STATE_INSIDE_EXECUTE_ADD = 25;
172
173     /** Default parameters that might apply to all queries */
174     protected HashMap JavaDoc default_properties = new HashMap JavaDoc();
175
176     /** The name of the value element we're currently receiving */
177     protected String JavaDoc current_name;
178
179     /** The current state of the event receiving FSM */
180     protected int current_state = STATE_OUTSIDE;
181
182     /** The value of the value element we're currently receiving */
183     protected StringBuffer JavaDoc current_value = new StringBuffer JavaDoc();
184
185     /** The list of queries that we're currently working on */
186     protected Vector JavaDoc queries = new Vector JavaDoc();
187
188     /** The offset of the current query in the queries list */
189     protected int current_query_index = -1;
190
191     /** SAX producing state information */
192     protected XMLConsumer xml_consumer;
193     protected LexicalHandler JavaDoc lexical_handler;
194
195     /** SitemapComponent methods */
196
197     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc source, Parameters parameters)
198         throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
199         current_state = STATE_OUTSIDE;
200
201         setDefaultProperty(parameters, MAGIC_INITIALIZER_ELEMENT); // Check the initializer
202
setDefaultProperty(parameters, MAGIC_VERSION_ELEMENT); // Check the version
203
setDefaultProperty(parameters, MAGIC_AUTHENTICATION_ELEMENT); // Check the authentication
204
setDefaultProperty(parameters, MAGIC_SCOPE_ELEMENT); // Check the scope
205
setDefaultProperty(parameters, MAGIC_SERVERURL_ELEMENT); // Check the serverurl
206
setDefaultProperty(parameters, MAGIC_ROOT_DN_ELEMENT); // Check the ldap-root_dn
207
setDefaultProperty(parameters, MAGIC_PASSWORD_ELEMENT); // Check the ldap-pwd
208
setDefaultProperty(parameters, MAGIC_PORT_ELEMENT); // Check the port
209
setDefaultProperty(parameters, MAGIC_SEARCHBASE_ELEMENT); // Check the searchbase
210
setDefaultProperty(parameters, MAGIC_DOC_ELEMENT); // Check the doc-element
211
setDefaultProperty(parameters, MAGIC_ROW_ELEMENT); // Check the row-element
212
setDefaultProperty(parameters, MAGIC_ERROR_ELEMENT); // Check the error-element
213
setDefaultProperty(parameters, MAGIC_SAX_ERROR); // Check the sax-error
214
setDefaultProperty(parameters, MAGIC_DEREF_LINK_ELEMENT); // Check the deref-link-element
215
setDefaultProperty(parameters, MAGIC_COUNT_LIMIT_ELEMENT); // Check the count-limit-element
216
setDefaultProperty(parameters, MAGIC_TIME_LIMIT_ELEMENT); // Check the time-limit-element
217
setDefaultProperty(parameters, MAGIC_DEBUG_ELEMENT); // Check the debug-element
218
setDefaultProperty(parameters, MAGIC_ENCODING_ELEMENT); // Check the encoding
219
setDefaultProperty(parameters, MAGIC_FILTER_ELEMENT); // Check the filter-element
220
}
221
222     /** My very own methods */
223
224     /* Helper to set default properties */
225     protected void setDefaultProperty(Parameters parameters, String JavaDoc propertyName) {
226         String JavaDoc parameter = parameters.getParameter(propertyName, null);
227         if (parameter != null) {
228             default_properties.put(propertyName, parameter);
229         }
230     }
231     
232     /* Helper class to store a new Query in the stack */
233     protected void storeQuery(int nextState) {
234         current_state = nextState;
235         current_query_index = queries.size();
236         LDAPQuery query = new LDAPQuery(this);
237         queries.addElement(query);
238         getCurrentQuery().toDo = nextState;
239         getCurrentQuery().query_index = current_query_index;
240     }
241
242     /**
243      * This will be the meat of LDAPTransformer, where the query is run.
244      */

245     protected void executeQuery(int index) throws SAXException JavaDoc {
246         this.contentHandler.startPrefixMapping("", LDAPTransformer.my_uri);
247         LDAPQuery query = (LDAPQuery)queries.elementAt(index);
248         try {
249             query.execute();
250         } catch (NamingException JavaDoc e) {
251             getLogger().error(e.toString());
252             throw new SAXException JavaDoc(e);
253         } catch (Exception JavaDoc e) {
254             getLogger().error(e.toString());
255             throw new SAXException JavaDoc(e);
256         }
257         this.contentHandler.endPrefixMapping("");
258     }
259
260     protected static void throwIllegalStateException(String JavaDoc message) {
261         throw new IllegalStateException JavaDoc(my_name + ": " + message);
262     }
263
264     protected void startExecuteElement() {
265         switch (current_state) {
266             case LDAPTransformer.STATE_OUTSIDE :
267             case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT :
268                 storeQuery(LDAPTransformer.STATE_INSIDE_EXECUTE_QUERY);
269                 break;
270             default :
271                 throwIllegalStateException("Not expecting a start execute-query element");
272         }
273     }
274
275     protected void startExecuteElement(int state, String JavaDoc name) {
276         switch (current_state) {
277             case LDAPTransformer.STATE_OUTSIDE :
278             case LDAPTransformer.STATE_INSIDE_EXECUTE_QUERY :
279                 storeQuery(state);
280                 break;
281             default :
282                 throwIllegalStateException("Not expecting a start " + name + " element");
283         }
284     }
285
286     protected void endExecuteElement(int state, String JavaDoc name) throws SAXException JavaDoc {
287         if (current_state == state) {
288             executeQuery(current_query_index);
289             queries.remove(current_query_index);
290             --current_query_index;
291             if (current_query_index > -1) {
292                 current_state = getCurrentQuery().toDo;
293             } else {
294                 queries.removeAllElements();
295                 current_state = LDAPTransformer.STATE_OUTSIDE;
296             }
297         } else {
298             throwIllegalStateException("Not expecting a end " + name + " element");
299         }
300     }
301
302     protected void startQueryParameterElement(int state, String JavaDoc name) {
303         switch (current_state) {
304             case STATE_INSIDE_EXECUTE_QUERY :
305             case STATE_INSIDE_EXECUTE_INCREMENT :
306             case STATE_INSIDE_EXECUTE_REPLACE :
307             case STATE_INSIDE_EXECUTE_ADD :
308                 current_value.setLength(0);
309                 current_state = state;
310                 getCurrentQuery().current_state = state;
311                 break;
312             default :
313                 throwIllegalStateException("Not expecting a start " + name + " element");
314         }
315     }
316
317     protected void startParameterElement(int state, String JavaDoc name) {
318         switch (current_state) {
319             case STATE_INSIDE_EXECUTE_QUERY :
320             case STATE_INSIDE_EXECUTE_INCREMENT :
321             case STATE_INSIDE_EXECUTE_REPLACE :
322             case STATE_INSIDE_EXECUTE_ADD :
323                 current_value.setLength(0);
324                 current_state = state;
325                 break;
326             default :
327                 throwIllegalStateException("Not expecting a start " + name + " serverurl element");
328         }
329     }
330
331     protected void endInitializerElement() {
332         switch (current_state) {
333             case LDAPTransformer.STATE_INSIDE_INITIALIZER_ELEMENT :
334                 getCurrentQuery().initializer = current_value.toString();
335                 current_state = getCurrentQuery().toDo;
336                 break;
337             default :
338                 throwIllegalStateException("Not expecting a end initializer element");
339         }
340     }
341
342     protected void endScopeElement() {
343         switch (current_state) {
344             case LDAPTransformer.STATE_INSIDE_SCOPE_ELEMENT :
345                 getCurrentQuery().scope = current_value.toString();
346                 current_state = getCurrentQuery().toDo;
347                 break;
348             default :
349                 throwIllegalStateException("Not expecting a end scope element");
350         }
351     }
352
353     protected void endAuthenticationElement() {
354         switch (current_state) {
355             case LDAPTransformer.STATE_INSIDE_AUTHENTICATION_ELEMENT :
356                 getCurrentQuery().authentication = current_value.toString();
357                 current_state = getCurrentQuery().toDo;
358                 break;
359             default :
360                 throwIllegalStateException("Not expecting a end authentication element");
361         }
362     }
363
364     protected void endServerurlElement() {
365         switch (current_state) {
366             case LDAPTransformer.STATE_INSIDE_SERVERURL_ELEMENT :
367                 getCurrentQuery().serverurl = current_value.toString();
368                 current_state = getCurrentQuery().toDo;
369                 break;
370             default :
371                 throwIllegalStateException("Not expecting a end serverurl element");
372         }
373     }
374
375     protected void endPortElement() {
376         switch (current_state) {
377             case LDAPTransformer.STATE_INSIDE_PORT_ELEMENT :
378                 getCurrentQuery().port = Integer.parseInt(current_value.toString());
379                 current_state = getCurrentQuery().toDo;
380                 break;
381             default :
382                 throwIllegalStateException("Not expecting a end server element");
383         }
384     }
385
386     protected void startShowAttributeElement(Attributes JavaDoc attributes) {
387         switch (current_state) {
388             case STATE_INSIDE_EXECUTE_QUERY :
389             case STATE_INSIDE_EXECUTE_INCREMENT :
390           //case STATE_INSIDE_EXECUTE_REPLACE:
391
current_value.setLength(0);
392                 current_state = LDAPTransformer.STATE_INSIDE_SHOW_ATTRIBUTE_ELEMENT;
393                 break;
394             default :
395                 throwIllegalStateException("Not expecting a start show-attribute element");
396         }
397     }
398
399     protected void endShowAttributeElement() {
400         switch (current_state) {
401             case LDAPTransformer.STATE_INSIDE_SHOW_ATTRIBUTE_ELEMENT :
402                 if (!BooleanUtils.toBoolean(current_value.toString())) {
403                     getCurrentQuery().showAttribute = false;
404                 }
405                 current_state = getCurrentQuery().toDo;
406                 break;
407             default :
408                 throwIllegalStateException("Not expecting a end show-attribute element");
409         }
410     }
411
412     protected void endSearchbaseElement() {
413         switch (current_state) {
414             case LDAPTransformer.STATE_INSIDE_SEARCHBASE_ELEMENT :
415                 getCurrentQuery().searchbase = current_value.toString();
416                 current_state = getCurrentQuery().toDo;
417                 break;
418             default :
419                 throwIllegalStateException("Not expecting a end searchbase element");
420         }
421     }
422
423     protected void endDocElement() {
424         switch (current_state) {
425             case LDAPTransformer.STATE_INSIDE_DOC_ELEMENT :
426                 getCurrentQuery().doc_element = current_value.toString();
427                 current_state = getCurrentQuery().toDo;
428                 break;
429             default :
430                 throwIllegalStateException("Not expecting a end doc-element element");
431         }
432     }
433
434     protected void endRowElement() {
435         switch (current_state) {
436             case LDAPTransformer.STATE_INSIDE_ROW_ELEMENT :
437                 getCurrentQuery().row_element = current_value.toString();
438                 current_state = getCurrentQuery().toDo;
439                 break;
440             default :
441                 throwIllegalStateException("Not expecting a end row-element element");
442         }
443     }
444
445     protected void endErrorElement() {
446         switch (current_state) {
447             case LDAPTransformer.STATE_INSIDE_ERROR_ELEMENT :
448                 getCurrentQuery().error_element = current_value.toString();
449                 current_state = getCurrentQuery().toDo;
450                 break;
451             default :
452                 throwIllegalStateException("Not expecting a end error-element element");
453         }
454     }
455
456     protected void endSaxError() {
457         switch (current_state) {
458             case LDAPTransformer.STATE_INSIDE_SAX_ERROR_ELEMENT :
459                 if (current_value.toString().toUpperCase().equals("TRUE")) {
460                     getCurrentQuery().sax_error = true;
461                 }
462                 current_state = getCurrentQuery().toDo;
463                 break;
464             default :
465                 throwIllegalStateException("Not expecting a end sax-error element");
466         }
467     }
468
469     protected void endRootDnElement() {
470         switch (current_state) {
471             case LDAPTransformer.STATE_INSIDE_ROOT_DN_ELEMENT :
472                 getCurrentQuery().root_dn = current_value.toString();
473                 current_state = getCurrentQuery().toDo;
474                 break;
475             default :
476                 throwIllegalStateException("Not expecting a end root-dn element");
477         }
478     }
479
480     protected void endPasswordElement() {
481         switch (current_state) {
482             case LDAPTransformer.STATE_INSIDE_PASSWORD_ELEMENT :
483                 getCurrentQuery().password = current_value.toString();
484                 current_state = getCurrentQuery().toDo;
485                 break;
486             default :
487                 throwIllegalStateException("Not expecting a end password element");
488         }
489     }
490
491     protected void startAttributeElement(Attributes JavaDoc attributes) {
492         switch (current_state) {
493             case STATE_INSIDE_EXECUTE_QUERY :
494             case STATE_INSIDE_EXECUTE_INCREMENT :
495                 current_state = LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT;
496                 current_value.setLength(0);
497                 break;
498             case STATE_INSIDE_EXECUTE_REPLACE :
499                 boolean is_name_present = false;
500                 String JavaDoc mode = MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_DEFAULT;
501                 if (attributes != null && attributes.getLength() > 0) {
502                     AttributesImpl JavaDoc new_attributes = new AttributesImpl JavaDoc(attributes);
503                     for (int i = 0; i < new_attributes.getLength(); i++) {
504                         String JavaDoc attr_name = new_attributes.getLocalName(i);
505                         if (attr_name.equals(MAGIC_ATTRIBUTE_ELEMENT_ATTRIBUTE)) {
506                             String JavaDoc value = new_attributes.getValue(i);
507                             getCurrentQuery().addAttrList(value);
508                             is_name_present = true;
509                         } else if (attr_name.equals(MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE)) {
510                             if (new_attributes.getValue(i).equals(MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A))
511                                 mode = MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A;
512                         } else {
513                             this.getLogger().debug("Invalid attribute match: " + attr_name);
514                             throwIllegalStateException("Invalid attribute match in start attribute element");
515                         }
516                     }
517                 }
518                 if (!is_name_present) {
519                     this.getLogger().debug("Do not match 'value' attribute");
520                     throwIllegalStateException("Do not match 'value' attribute in start attribute element");
521                 }
522                 getCurrentQuery().addAttrModeVal(mode);
523                 current_state = LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT;
524                 current_value.setLength(0);
525                 break;
526             case STATE_INSIDE_EXECUTE_ADD :
527                 if (attributes != null && attributes.getLength() > 0) {
528                     AttributesImpl JavaDoc new_attributes = new AttributesImpl JavaDoc(attributes);
529                     for (int i = 0; i < new_attributes.getLength(); i++) {
530                         String JavaDoc attr_name = new_attributes.getLocalName(i);
531                         if (attr_name.equals(MAGIC_ATTRIBUTE_ELEMENT_ATTRIBUTE)) {
532                             String JavaDoc value = new_attributes.getValue(i);
533                             getCurrentQuery().addAttrList(value);
534                         } else {
535                             this.getLogger().debug("Invalid attribute match: " + attr_name);
536                             throwIllegalStateException("Invalid attribute match in start attribute element");
537                         }
538                     }
539                 } else {
540                     this.getLogger().debug("Do not match 'value' attribute");
541                     throwIllegalStateException("Do not match 'value' attribute in start attribute element");
542                 }
543                 current_state = LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT;
544                 current_value.setLength(0);
545                 break;
546             default :
547                 throwIllegalStateException("Not expecting a start attribute element");
548         }
549     }
550
551     protected void endAttributeElement() {
552         switch (current_state) {
553             case LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT :
554                 if ((getCurrentQuery().toDo == STATE_INSIDE_EXECUTE_REPLACE) || (getCurrentQuery().toDo == STATE_INSIDE_EXECUTE_ADD)) {
555                     getCurrentQuery().addAttrVal(current_value.toString());
556                     current_state = getCurrentQuery().toDo;
557                     break;
558                 }
559                 getCurrentQuery().addAttrList(current_value.toString());
560                 current_state = getCurrentQuery().toDo;
561                 break;
562             default :
563                 throwIllegalStateException("Not expecting a end attribute element");
564         }
565     }
566
567     protected void endVersionElement() {
568         switch (current_state) {
569             case LDAPTransformer.STATE_INSIDE_VERSION_ELEMENT :
570                 getCurrentQuery().version = current_value.toString();
571                 current_state = getCurrentQuery().toDo;
572                 break;
573             default :
574                 throwIllegalStateException("Not expecting a end version element");
575         }
576     }
577
578     protected void startFilterElement(Attributes JavaDoc attributes) {
579         switch (current_state) {
580             case STATE_INSIDE_EXECUTE_QUERY :
581             case STATE_INSIDE_EXECUTE_INCREMENT :
582             case STATE_INSIDE_EXECUTE_REPLACE :
583             case STATE_INSIDE_EXECUTE_ADD :
584                 current_state = LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT;
585                 getCurrentQuery().current_state = LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT;
586                 current_value.setLength(0);
587                 break;
588             default :
589                 throwIllegalStateException("Not expecting a start filter element");
590         }
591     }
592
593     protected void endFilterElement() {
594         switch (current_state) {
595             case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT :
596                 getCurrentQuery().filter = current_value.toString();
597                 current_state = getCurrentQuery().toDo;
598                 break;
599             default :
600                 throwIllegalStateException("Not expecting a end filter element");
601         }
602     }
603
604     protected void endDerefLinkElement() {
605         switch (current_state) {
606             case LDAPTransformer.STATE_INSIDE_DEREF_LINK_ELEMENT :
607                 getCurrentQuery().deref_link = BooleanUtils.toBoolean(current_value.toString());
608                 current_state = getCurrentQuery().toDo;
609                 break;
610             default :
611                 throwIllegalStateException("Not expecting a end deref-link element");
612         }
613     }
614
615     protected void endCountLimitElement() {
616         switch (current_state) {
617             case LDAPTransformer.STATE_INSIDE_COUNT_LIMIT_ELEMENT :
618                 getCurrentQuery().count_limit = Integer.parseInt(current_value.toString());
619                 current_state = getCurrentQuery().toDo;
620                 break;
621             default :
622                 throwIllegalStateException("Not expecting a end count-limit element");
623         }
624     }
625
626     protected void endTimeLimitElement() {
627         switch (current_state) {
628             case LDAPTransformer.STATE_INSIDE_TIME_LIMIT_ELEMENT :
629                 getCurrentQuery().time_limit = Integer.parseInt(current_value.toString());
630                 current_state = getCurrentQuery().toDo;
631                 break;
632             default :
633                 throwIllegalStateException("Not expecting a end time-limit element");
634         }
635     }
636
637     protected void endDebugElement() {
638         switch (current_state) {
639             case LDAPTransformer.STATE_INSIDE_DEBUG_ELEMENT :
640                 getCurrentQuery().debug = BooleanUtils.toBoolean(current_value.toString());
641                 current_state = getCurrentQuery().toDo;
642                 break;
643             default :
644                 throwIllegalStateException("Not expecting a end debug element");
645         }
646     }
647
648     protected LDAPQuery getCurrentQuery() {
649         return (LDAPQuery)queries.elementAt(current_query_index);
650     }
651
652     protected LDAPQuery getQuery(int i) {
653         return (LDAPQuery)queries.elementAt(i);
654     }
655
656     /** END my very own methods */
657
658     /** BEGIN SAX ContentHandler handlers */
659
660     public void setDocumentLocator(Locator JavaDoc locator) {
661         if (getLogger().isDebugEnabled()) {
662             getLogger().debug("PUBLIC ID: " + locator.getPublicId());
663             getLogger().debug("SYSTEM ID: " + locator.getSystemId());
664         }
665         if (super.contentHandler != null)
666             super.contentHandler.setDocumentLocator(locator);
667     }
668
669     public void startElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attributes) throws SAXException JavaDoc {
670         if (!uri.equals(my_uri)) {
671             super.startElement(uri, name, raw, attributes);
672             return;
673         }
674         getLogger().debug("RECEIVED START ELEMENT " + name + "(" + uri + ")");
675
676         if (name.equals(LDAPTransformer.MAGIC_EXECUTE_QUERY)) {
677             startExecuteElement();
678         } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_INCREMENT)) {
679             startExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_INCREMENT, name);
680         } else if (name.equals(LDAPTransformer.MAGIC_INITIALIZER_ELEMENT)) {
681             startQueryParameterElement(LDAPTransformer.STATE_INSIDE_INITIALIZER_ELEMENT, name);
682         } else if (name.equals(LDAPTransformer.MAGIC_AUTHENTICATION_ELEMENT)) {
683             startQueryParameterElement(LDAPTransformer.STATE_INSIDE_AUTHENTICATION_ELEMENT, name);
684         } else if (name.equals(LDAPTransformer.MAGIC_SCOPE_ELEMENT)) {
685             startQueryParameterElement(LDAPTransformer.STATE_INSIDE_SCOPE_ELEMENT, name);
686         } else if (name.equals(LDAPTransformer.MAGIC_VERSION_ELEMENT)) {
687             startParameterElement(LDAPTransformer.STATE_INSIDE_VERSION_ELEMENT, name);
688         } else if (name.equals(LDAPTransformer.MAGIC_SERVERURL_ELEMENT)) {
689             startParameterElement(LDAPTransformer.STATE_INSIDE_SERVERURL_ELEMENT, name);
690         } else if (name.equals(LDAPTransformer.MAGIC_PORT_ELEMENT)) {
691             startParameterElement(LDAPTransformer.STATE_INSIDE_PORT_ELEMENT, name);
692         } else if (name.equa