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.equals(LDAPTransformer.MAGIC_DOC_ELEMENT)) {
693             startParameterElement(LDAPTransformer.STATE_INSIDE_DOC_ELEMENT, name);
694         } else if (name.equals(LDAPTransformer.MAGIC_ROW_ELEMENT)) {
695             startParameterElement(LDAPTransformer.STATE_INSIDE_ROW_ELEMENT, name);
696         } else if (name.equals(LDAPTransformer.MAGIC_ERROR_ELEMENT)) {
697             startParameterElement(LDAPTransformer.STATE_INSIDE_ERROR_ELEMENT, name);
698         } else if (name.equals(LDAPTransformer.MAGIC_SAX_ERROR)) {
699             startParameterElement(LDAPTransformer.STATE_INSIDE_SAX_ERROR_ELEMENT, name);
700         } else if (name.equals(LDAPTransformer.MAGIC_ROOT_DN_ELEMENT)) {
701             startParameterElement(LDAPTransformer.STATE_INSIDE_ROOT_DN_ELEMENT, name);
702         } else if (name.equals(LDAPTransformer.MAGIC_PASSWORD_ELEMENT)) {
703             startParameterElement(LDAPTransformer.STATE_INSIDE_PASSWORD_ELEMENT, name);
704         } else if (name.equals(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT)) {
705             startAttributeElement(attributes);
706         } else if (name.equals(LDAPTransformer.MAGIC_SHOW_ATTRIBUTE_ELEMENT)) {
707             startShowAttributeElement(attributes);
708         } else if (name.equals(LDAPTransformer.MAGIC_SEARCHBASE_ELEMENT)) {
709             startParameterElement(LDAPTransformer.STATE_INSIDE_SEARCHBASE_ELEMENT, name);
710         } else if (name.equals(LDAPTransformer.MAGIC_FILTER_ELEMENT)) {
711             startFilterElement(attributes);
712         } else if (name.equals(LDAPTransformer.MAGIC_DEREF_LINK_ELEMENT)) {
713             startParameterElement(LDAPTransformer.STATE_INSIDE_DEREF_LINK_ELEMENT, name);
714         } else if (name.equals(LDAPTransformer.MAGIC_COUNT_LIMIT_ELEMENT)) {
715             startParameterElement(LDAPTransformer.STATE_INSIDE_COUNT_LIMIT_ELEMENT, name);
716         } else if (name.equals(LDAPTransformer.MAGIC_TIME_LIMIT_ELEMENT)) {
717             startParameterElement(LDAPTransformer.STATE_INSIDE_TIME_LIMIT_ELEMENT, name);
718         } else if (name.equals(LDAPTransformer.MAGIC_DEBUG_ELEMENT)) {
719             startParameterElement(LDAPTransformer.STATE_INSIDE_DEBUG_ELEMENT, name);
720         } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_REPLACE)) {
721             startExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_REPLACE, LDAPTransformer.MAGIC_EXECUTE_REPLACE);
722         } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_ADD)) {
723             startExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_ADD, LDAPTransformer.MAGIC_EXECUTE_ADD);
724         }
725     }
726
727     public void endElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw) throws SAXException JavaDoc {
728         if (!uri.equals(my_uri)) {
729             super.endElement(uri, name, raw);
730             return;
731         }
732         if (getLogger().isDebugEnabled()) {
733             getLogger().debug("RECEIVED END ELEMENT " + name + "(" + uri + ")");
734         }
735
736         if (name.equals(LDAPTransformer.MAGIC_EXECUTE_QUERY)) {
737             endExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_QUERY, LDAPTransformer.MAGIC_EXECUTE_QUERY);
738         } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_INCREMENT)) {
739             endExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_INCREMENT, LDAPTransformer.MAGIC_EXECUTE_INCREMENT);
740         } else if (name.equals(LDAPTransformer.MAGIC_INITIALIZER_ELEMENT)) {
741             endInitializerElement();
742         } else if (name.equals(LDAPTransformer.MAGIC_AUTHENTICATION_ELEMENT)) {
743             endAuthenticationElement();
744         } else if (name.equals(LDAPTransformer.MAGIC_SCOPE_ELEMENT)) {
745             endScopeElement();
746         } else if (name.equals(LDAPTransformer.MAGIC_VERSION_ELEMENT)) {
747             endVersionElement();
748         } else if (name.equals(LDAPTransformer.MAGIC_SERVERURL_ELEMENT)) {
749             endServerurlElement();
750         } else if (name.equals(LDAPTransformer.MAGIC_PORT_ELEMENT)) {
751             endPortElement();
752         } else if (name.equals(LDAPTransformer.MAGIC_DOC_ELEMENT)) {
753             endDocElement();
754         } else if (name.equals(LDAPTransformer.MAGIC_ROW_ELEMENT)) {
755             endRowElement();
756         } else if (name.equals(LDAPTransformer.MAGIC_ERROR_ELEMENT)) {
757             endErrorElement();
758         } else if (name.equals(LDAPTransformer.MAGIC_SAX_ERROR)) {
759             endSaxError();
760         } else if (name.equals(LDAPTransformer.MAGIC_ROOT_DN_ELEMENT)) {
761             endRootDnElement();
762         } else if (name.equals(LDAPTransformer.MAGIC_PASSWORD_ELEMENT)) {
763             endPasswordElement();
764         } else if (name.equals(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT)) {
765             endAttributeElement();
766         } else if (name.equals(LDAPTransformer.MAGIC_SHOW_ATTRIBUTE_ELEMENT)) {
767             endShowAttributeElement();
768         } else if (name.equals(LDAPTransformer.MAGIC_SEARCHBASE_ELEMENT)) {
769             endSearchbaseElement();
770         } else if (name.equals(LDAPTransformer.MAGIC_FILTER_ELEMENT)) {
771             endFilterElement();
772         } else if (name.equals(LDAPTransformer.MAGIC_DEREF_LINK_ELEMENT)) {
773             endDerefLinkElement();
774         } else if (name.equals(LDAPTransformer.MAGIC_COUNT_LIMIT_ELEMENT)) {
775             endCountLimitElement();
776         } else if (name.equals(LDAPTransformer.MAGIC_TIME_LIMIT_ELEMENT)) {
777             endTimeLimitElement();
778         } else if (name.equals(LDAPTransformer.MAGIC_DEBUG_ELEMENT)) {
779             endDebugElement();
780         } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_REPLACE)) {
781             endExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_REPLACE, LDAPTransformer.MAGIC_EXECUTE_REPLACE);
782         } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_ADD)) {
783             endExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_ADD, LDAPTransformer.MAGIC_EXECUTE_ADD);
784         }
785     }
786
787     public void characters(char ary[], int start, int length) throws SAXException JavaDoc {
788         if (current_state != LDAPTransformer.STATE_INSIDE_INITIALIZER_ELEMENT
789             && current_state != LDAPTransformer.STATE_INSIDE_AUTHENTICATION_ELEMENT
790             && current_state != LDAPTransformer.STATE_INSIDE_SCOPE_ELEMENT
791             && current_state != LDAPTransformer.STATE_INSIDE_VERSION_ELEMENT
792             && current_state != LDAPTransformer.STATE_INSIDE_SERVERURL_ELEMENT
793             && current_state != LDAPTransformer.STATE_INSIDE_PORT_ELEMENT
794             && current_state != LDAPTransformer.STATE_INSIDE_DOC_ELEMENT
795             && current_state != LDAPTransformer.STATE_INSIDE_ROW_ELEMENT
796             && current_state != LDAPTransformer.STATE_INSIDE_ERROR_ELEMENT
797             && current_state != LDAPTransformer.STATE_INSIDE_SAX_ERROR_ELEMENT
798             && current_state != LDAPTransformer.STATE_INSIDE_ROOT_DN_ELEMENT
799             && current_state != LDAPTransformer.STATE_INSIDE_PASSWORD_ELEMENT
800             && current_state != LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT
801             && current_state != LDAPTransformer.STATE_INSIDE_SHOW_ATTRIBUTE_ELEMENT
802             && current_state != LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT
803             && current_state != LDAPTransformer.STATE_INSIDE_DEREF_LINK_ELEMENT
804             && current_state != LDAPTransformer.STATE_INSIDE_COUNT_LIMIT_ELEMENT
805             && current_state != LDAPTransformer.STATE_INSIDE_TIME_LIMIT_ELEMENT
806             && current_state != LDAPTransformer.STATE_INSIDE_DEBUG_ELEMENT
807             && current_state != LDAPTransformer.STATE_INSIDE_SEARCHBASE_ELEMENT) {
808             super.characters(ary, start, length);
809         }
810         if (getLogger().isDebugEnabled()) {
811             getLogger().debug("RECEIVED CHARACTERS: " + new String JavaDoc(ary, start, length));
812         }
813         current_value.append(ary, start, length);
814     }
815
816     protected void start(String JavaDoc name, AttributesImpl JavaDoc attr) throws SAXException JavaDoc {
817         super.contentHandler.startElement("", name, name, attr);
818         attr.clear();
819     }
820
821     protected void end(String JavaDoc name) throws SAXException JavaDoc {
822         super.contentHandler.endElement("", name, name);
823     }
824
825     protected void data(String JavaDoc data) throws SAXException JavaDoc {
826         if (data != null) {
827             super.contentHandler.characters(data.toCharArray(), 0, data.length());
828         }
829     }
830
831     protected static String JavaDoc getStringValue(Object JavaDoc object) {
832         return ObjectUtils.toString(object);
833     }
834
835     public final Logger getTheLogger() {
836         return getLogger();
837     }
838
839     static class LDAPQuery {
840
841         /** What index are you in daddy's queries list */
842         protected int query_index;
843
844         /** The current state of the event receiving FSM */
845         protected int current_state;
846
847         /** Who's your daddy? */
848         protected LDAPTransformer transformer;
849
850         /** LDAP configuration information */
851         protected String JavaDoc initializer;
852         protected String JavaDoc serverurl;
853         protected int port;
854         protected String JavaDoc root_dn;
855         protected String JavaDoc password;
856         protected String JavaDoc version;
857         protected String JavaDoc scope;
858         protected String JavaDoc authentication;
859         private final static String JavaDoc LDAP_ENCODING = "ISO-8859-1";
860         protected String JavaDoc encoding;
861
862         /** LDAP environment information */
863         protected Properties JavaDoc env = new Properties JavaDoc();
864         protected DirContext JavaDoc ctx;
865
866         /** LDAP Query */
867         protected int toDo;
868         protected String JavaDoc searchbase;
869         protected List JavaDoc attrModeVal = new LinkedList JavaDoc();
870         protected List JavaDoc attrListe = new LinkedList JavaDoc();
871         protected List JavaDoc attrVale = new LinkedList JavaDoc();
872         protected String JavaDoc REPLACE_MODE_DEFAULT = StringUtils.defaultString(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_DEFAULT);
873         protected String JavaDoc REPLACE_MODE_APPEND = StringUtils.defaultString(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A);
874         protected boolean showAttribute;
875         protected String JavaDoc filter;
876         protected String JavaDoc doc_element;
877         protected String JavaDoc exec_element;
878         protected String JavaDoc row_element;
879         protected String JavaDoc error_element;
880         protected boolean sax_error;
881         protected boolean deref_link; // Dereference: true -> dereference the link during search
882
protected long count_limit; // Maximum number of entries to return: 0 -> no limit
883
protected int time_limit; // Number of milliseconds to wait before return: 0 -> infinite
884
protected boolean debug;
885
886         protected LDAPQuery(LDAPTransformer transformer) {
887             this.transformer = transformer;
888             initializer = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_INITIALIZER_ELEMENT, "com.sun.jndi.ldap.LdapCtxFactory");
889             serverurl = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_SERVERURL_ELEMENT, "localhost");
890             port = MapUtils.getIntValue(transformer.default_properties, LDAPTransformer.MAGIC_PORT_ELEMENT, 389);
891             root_dn = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_ROOT_DN_ELEMENT, "");
892             password = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_PASSWORD_ELEMENT, "");
893             version = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_VERSION_ELEMENT, "2");
894             scope = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_SCOPE_ELEMENT, "ONELEVEL_SCOPE");
895             authentication = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_AUTHENTICATION_ELEMENT, "simple");
896             searchbase = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_SEARCHBASE_ELEMENT, "");
897             showAttribute = MapUtils.getBooleanValue(transformer.default_properties, LDAPTransformer.MAGIC_SHOW_ATTRIBUTE_ELEMENT, true);
898             doc_element = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_DOC_ELEMENT, "doc-element");
899             row_element = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_ROW_ELEMENT, "row-element");
900             error_element = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_ERROR_ELEMENT, "ldap-error");
901             sax_error = MapUtils.getBooleanValue(transformer.default_properties, LDAPTransformer.MAGIC_SAX_ERROR);
902             deref_link = MapUtils.getBooleanValue(transformer.default_properties, LDAPTransformer.MAGIC_DEREF_LINK_ELEMENT);
903             count_limit = MapUtils.getLongValue(transformer.default_properties, LDAPTransformer.MAGIC_DEREF_LINK_ELEMENT);
904             time_limit = MapUtils.getIntValue(transformer.default_properties, LDAPTransformer.MAGIC_TIME_LIMIT_ELEMENT);
905             debug = MapUtils.getBooleanValue(transformer.default_properties, LDAPTransformer.MAGIC_DEBUG_ELEMENT);
906             encoding = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_ENCODING_ELEMENT, LDAP_ENCODING);
907             filter = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_FILTER_ELEMENT, "");
908         }
909
910         /** shared constraints initialization */
911         protected void initConstraints(SearchControls JavaDoc constraints) {
912             if (scope.equals("OBJECT_SCOPE")) {
913                 constraints.setSearchScope(SearchControls.OBJECT_SCOPE);
914             } else if (scope.equals("SUBTREE_SCOPE")) {
915                 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
916             } else {
917                 constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
918             }
919             constraints.setTimeLimit(time_limit);
920             constraints.setDerefLinkFlag(deref_link);
921             constraints.setCountLimit(count_limit);
922         }
923
924         protected void execute() throws Exception JavaDoc, NamingException JavaDoc {
925             String JavaDoc[] attrList = new String JavaDoc[attrListe.size()];
926
927             AttributesImpl JavaDoc attr = new AttributesImpl JavaDoc();
928             if (debug) {
929                 debugPrint();
930             }
931             SearchControls JavaDoc constraints = new SearchControls JavaDoc();
932             attrListe.toArray(attrList);
933             attrListe.clear();
934             try {
935                 connect();
936                 switch (toDo) {
937                     case LDAPTransformer.STATE_INSIDE_EXECUTE_QUERY :
938                         try {
939                             initConstraints(constraints);
940                             if (attrList.length > 0) {
941                                 constraints.setReturningAttributes(attrList);
942                             }
943
944                             if (!filter.equals("")) {
945                                 //filter is present
946
if (!doc_element.equals("")) {
947                                     transformer.start(doc_element, attr);
948                                 }
949                                 NamingEnumeration JavaDoc ldapresults = ctx.search(searchbase, filter, constraints);
950
951                                 while (ldapresults != null && ldapresults.hasMore()) {
952                                     if (!row_element.equals("")) {
953                                         transformer.start(row_element, attr);
954                                     }
955                                     SearchResult JavaDoc si = (SearchResult JavaDoc) ldapresults.next();
956                                     javax.naming.directory.Attributes JavaDoc attrs = si.getAttributes();
957                                     if (attrs != null) {
958                                         NamingEnumeration JavaDoc ae = attrs.getAll();
959                                         while (ae.hasMoreElements()) {
960                                             Attribute JavaDoc at = (Attribute JavaDoc) ae.next();
961                                             Enumeration JavaDoc vals = at.getAll();
962                                             String JavaDoc attrID = at.getID();
963                                             while (vals.hasMoreElements()) {
964                                                 if (showAttribute) {
965                                                     transformer.start(attrID, attr);
966                                                 }
967                                                 String JavaDoc attrVal = recodeFromLDAPEncoding((String JavaDoc) vals.nextElement());
968                                                 if (query_index > 0) {
969                                                     switch (transformer.getQuery(query_index - 1).current_state) {
970                                                         case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT :
971                                                             StringBuffer JavaDoc temp = new StringBuffer JavaDoc(transformer.getQuery(query_index - 1).filter);
972                                                             if (temp.length() > 0) {
973                                                                 temp.append(", ");
974                                                             }
975                                                             temp.append(attrID).append("=").append(attrVal);
976                                                             transformer.getQuery(query_index - 1).filter = temp.toString();
977                                                             break;
978                                                         default :
979                                                             transformer.start(attrID, attr);
980                                                     }
981                                                 } else {
982                                                     transformer.data(String.valueOf(attrVal));
983                                                 }
984                                                 if (showAttribute) {
985                                                     transformer.end(attrID);
986                                                 }
987                                             }
988                                         }
989                                     }
990                                     if (!row_element.equals("")) {
991                                         transformer.end(row_element);
992                                     }
993                                 }
994                                 if (!doc_element.equals("")) {
995                                     transformer.end(doc_element);
996                                 }
997                             } else {
998                                 //filter not present, get the values from absolete path
999
javax.naming.directory.Attributes JavaDoc attrs = ctx.getAttributes(searchbase, attrList);
1000                                if (!doc_element.equals("")) {
1001                                    transformer.start(doc_element, attr);
1002                                }
1003                                if (!row_element.equals("")) {
1004                                    transformer.start(row_element, attr);
1005                                }
1006                                if (attrs != null) {
1007                                    NamingEnumeration JavaDoc ae = attrs.getAll();
1008                                    while (ae.hasMoreElements()) {
1009                                        Attribute JavaDoc at = (Attribute JavaDoc) ae.next();
1010                                        Enumeration JavaDoc vals = at.getAll();
1011                                        String JavaDoc attrID = at.getID();
1012                                        while (vals.hasMoreElements()) {
1013                                            if (showAttribute) {
1014                                                transformer.start(attrID, attr);
1015                                            }
1016                                            String JavaDoc attrVal = recodeFromLDAPEncoding((String JavaDoc)vals.nextElement());
1017
1018                                            if (query_index > 0) {
1019                                                switch (transformer.getQuery(query_index - 1).current_state) {
1020                                                    case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT :
1021                                                        StringBuffer JavaDoc temp = new StringBuffer JavaDoc(transformer.getQuery(query_index - 1).filter);
1022                                                        if (temp.length() > 0) {
1023                                                            temp.append(", ");
1024                                                        }
1025                                                        temp.append(attrID).append("=").append(attrVal);
1026                                                        transformer.getQuery(query_index - 1).filter = temp.toString();
1027                                                        break;
1028                                                    default :
1029                                                        transformer.start(attrID, attr);
1030                                                }
1031                                            } else {
1032                                                transformer.data(String.valueOf(attrVal));
1033                                            }
1034                                            if (showAttribute) {
1035                                                transformer.end(attrID);
1036                                            }
1037                                        }
1038                                    }
1039                                }
1040                                if (!row_element.equals("")) {
1041                                    transformer.end(row_element);
1042                                }
1043                                if (!doc_element.equals("")) {
1044                                    transformer.end(doc_element);
1045                                }
1046                            }
1047                        } catch (Exception JavaDoc e) {
1048                            if (sax_error) {
1049                                throw new Exception JavaDoc("[LDAPTransformer] Error in LDAP-Query: " + e.toString());
1050                            } else {
1051                                transformer.start(error_element, attr);
1052                                transformer.data("[LDAPTransformer] Error in LDAP-Query: " + e);
1053                                transformer.end(error_element);
1054                                transformer.getTheLogger().error("[LDAPTransformer] Exception: " + e.toString());
1055                            }
1056                        }
1057                        break;
1058                    case LDAPTransformer.STATE_INSIDE_EXECUTE_INCREMENT :
1059                        try {
1060                            initConstraints(constraints);
1061                            if (attrList.length != 1) {
1062                                transformer.start(error_element, attr);
1063                                transformer.data("Increment must reference exactly 1 attribute.");
1064                                transformer.end(error_element);
1065                            } else {
1066                                constraints.setReturningAttributes(attrList);
1067                                NamingEnumeration JavaDoc ldapresults = ctx.search(searchbase, filter, constraints);
1068                                int attrVal = 0;
1069                                String JavaDoc attrID = "";
1070                                SearchResult JavaDoc si = null;
1071                                while (ldapresults != null && ldapresults.hasMore()) {
1072                                    si = (SearchResult JavaDoc) ldapresults.next();
1073                                    javax.naming.directory.Attributes JavaDoc attrs = si.getAttributes();
1074                                    if (attrs != null) {
1075                                        NamingEnumeration JavaDoc ae = attrs.getAll();
1076                                        while (ae.hasMoreElements()) {
1077                                            Attribute JavaDoc at = (Attribute JavaDoc) ae.next();
1078                                            Enumeration JavaDoc vals = at.getAll();
1079                                            attrID = at.getID();
1080                                            attrVal = Integer.parseInt((String JavaDoc) vals.nextElement());
1081                                        }
1082                                    }
1083                                }
1084                                ++attrVal;
1085                                // Specify the changes to make
1086
ModificationItem JavaDoc[] mods = new ModificationItem JavaDoc[1];
1087                                // Replace the "mail" attribute with a new value
1088
mods[0] =
1089                                    new ModificationItem JavaDoc(
1090                                        DirContext.REPLACE_ATTRIBUTE,
1091                                        new BasicAttribute JavaDoc(attrID, Integer.toString(attrVal)));
1092                                // Perform the requested modifications on the named object
1093
ctx.modifyAttributes(
1094                                    new StringBuffer JavaDoc(si.toString().substring(0, si.toString().indexOf(":")))
1095                                        .append(",")
1096                                        .append(searchbase)
1097                                        .toString(),
1098                                    mods);
1099                            }
1100                        } catch (Exception JavaDoc e) {
1101                            if (sax_error) {
1102                                throw new Exception JavaDoc("[LDAPTransformer] Error incrementing an attribute: " + e.toString());
1103                            } else {
1104                                transformer.start(error_element, attr);
1105                                transformer.data("[LDAPTransformer] Error incrementing an attribute: " + e.toString());
1106                                transformer.end(error_element);
1107                                transformer.getTheLogger().error("[LDAPTransformer] Error incrementing an attribute: " + e.toString());
1108                            }
1109                        }
1110                        break;
1111                    /* execute modes */
1112                    case LDAPTransformer.STATE_INSIDE_EXECUTE_REPLACE :
1113                        try {
1114                            String JavaDoc[] attrVal = new String JavaDoc[attrVale.size()];
1115                            String JavaDoc[] attrMode = new String JavaDoc[attrModeVal.size()];
1116                            String JavaDoc replaceMode = REPLACE_MODE_DEFAULT;
1117                            attrVale.toArray(attrVal);
1118                            attrVale.clear();
1119                            attrModeVal.toArray(attrMode);
1120                            attrModeVal.clear();
1121
1122                            if (attrVal.length != attrList.length) {
1123                                transformer.start(error_element, attr);
1124                                transformer.data("Attribute values must have the some number as a names");
1125                                transformer.end(error_element);
1126                                break;
1127                            }
1128                            HashMap JavaDoc attrMap = new HashMap JavaDoc(attrVal.length);
1129                            HashMap JavaDoc attrModeMap = new HashMap JavaDoc(attrMode.length);
1130
1131                            for (int i = 0; i < attrVal.length; i++) {
1132                                attrMap.put(attrList[i], attrVal[i]);
1133                                attrModeMap.put(attrList[i], attrMode[i]);
1134                            }
1135
1136                            initConstraints(constraints);
1137                            if (attrList.length < 1) {
1138                                transformer.start(error_element, attr);
1139                                transformer.data("Modify must reference 1 or more attribute.");
1140                                transformer.end(error_element);
1141                            } else {
1142                                if (!filter.equals("")) {
1143                                    constraints.setReturningAttributes(attrList);
1144                                    NamingEnumeration JavaDoc ldapresults = ctx.search(searchbase, filter, constraints);
1145                                    SearchResult JavaDoc si = null;
1146                                    /* start indicate element of executing query */
1147                                    if (!exec_element.equals("")) {
1148                                        transformer.start(exec_element, attr);
1149                                    }
1150                                    while (ldapresults != null && ldapresults.hasMore()) {
1151                                        if (!row_element.equals("")) {
1152                                            transformer.start(row_element, attr);
1153                                        }
1154
1155                                        si = (SearchResult JavaDoc) ldapresults.next();
1156                                        javax.naming.directory.Attributes JavaDoc attrs = si.getAttributes();
1157                                        if (attrs != null) {
1158                                            NamingEnumeration JavaDoc ae = attrs.getAll();
1159                                            while (ae.hasMoreElements()) {
1160                                                Attribute JavaDoc at = (Attribute JavaDoc) ae.next();
1161                                                Enumeration JavaDoc vals = at.getAll();
1162                                                String JavaDoc attrID = at.getID();
1163                                                ModificationItem JavaDoc[] mods = new ModificationItem JavaDoc[1];
1164                                                replaceMode = (String JavaDoc) attrModeMap.get(attrID);
1165
1166                                                String JavaDoc attrValue = recodeFromLDAPEncoding((String JavaDoc) vals.nextElement());
1167                                                String JavaDoc newAttrValue = "";
1168                                                /* Check the replacing method */
1169                                                if (replaceMode.equals(REPLACE_MODE_DEFAULT)) {
1170                                                    newAttrValue = (String JavaDoc)attrMap.get(attrID);
1171                                                } else if (replaceMode.equals(REPLACE_MODE_APPEND)) {
1172                                                    newAttrValue = attrValue + (String JavaDoc) attrMap.get(attrID);
1173                                                }
1174                                                newAttrValue = recodeToLDAPEncoding(newAttrValue);
1175
1176                                                mods[0] = new ModificationItem JavaDoc(DirContext.REPLACE_ATTRIBUTE,
1177                                                                               new BasicAttribute JavaDoc(attrID,newAttrValue));
1178
1179                                                // Perform the requested modifications on the named object
1180
ctx.modifyAttributes(
1181                                                        si.toString().substring(0, si.toString().indexOf(":")) + "," + searchbase,
1182                                                        mods);
1183
1184                                                /* confirm of success */
1185                                                transformer.start(attrID, attr);
1186                                                transformer.data("replaced");
1187                                                transformer.end(attrID);
1188                                            }
1189                                        }
1190
1191                                        if (!row_element.equals("")) {
1192                                            transformer.end(row_element);
1193                                        }
1194
1195                                    }
1196                                    if (!exec_element.equals("")) {
1197                                        transformer.end(exec_element);
1198                                    }
1199                                } else {
1200                                    //filter is not present
1201
javax.naming.directory.Attributes JavaDoc attrs = ctx.getAttributes(searchbase, attrList);
1202                                    /* start indicate element of executing query */
1203                                    if (!exec_element.equals("")) {
1204                                        transformer.start(exec_element, attr);
1205                                    }
1206                                    if (!row_element.equals("")) {
1207                                        transformer.start(row_element, attr);
1208                                    }
1209                                    if (attrs != null) {
1210                                        NamingEnumeration JavaDoc ae = attrs.getAll();
1211                                        while (ae.hasMoreElements()) {
1212                                            Attribute JavaDoc at = (Attribute JavaDoc) ae.next();
1213                                            Enumeration JavaDoc vals = at.getAll();
1214                                            String JavaDoc attrID = at.getID();
1215                                            ModificationItem JavaDoc[] mods = new ModificationItem JavaDoc[1];
1216                                            replaceMode = (String JavaDoc) attrModeMap.get(attrID);
1217
1218                                            String JavaDoc attrValue = recodeFromLDAPEncoding((String JavaDoc) vals.nextElement());
1219
1220                                            String JavaDoc newAttrValue = "";
1221                                            /* Check the replacing method */
1222                                            if (replaceMode.equals(REPLACE_MODE_DEFAULT)) {
1223                                                newAttrValue = (String JavaDoc) attrMap.get(attrID);
1224                                            } else if (replaceMode.equals(REPLACE_MODE_APPEND)) {
1225                                                newAttrValue = attrValue + (String JavaDoc) attrMap.get(attrID);
1226                                            }
1227                                            newAttrValue = recodeToLDAPEncoding(newAttrValue);
1228
1229                                            mods[0] = new ModificationItem JavaDoc(DirContext.REPLACE_ATTRIBUTE,
1230                                                                           new BasicAttribute JavaDoc(attrID, newAttrValue));
1231
1232                                            // Perform the requested modifications on the named object
1233
ctx.modifyAttributes(searchbase, mods);
1234
1235                                            /* confirm of success */
1236                                            transformer.start(attrID, attr);
1237                                            transformer.data("replaced");
1238                                            transformer.end(attrID);
1239                                        }
1240                                    }
1241
1242                                    if (!row_element.equals("")) {
1243                                        transformer.end(row_element);
1244                                    }
1245
1246                                    /* end indicate element of executing query */
1247                                    if (!exec_element.equals("")) {
1248                                        transformer.end(exec_element);
1249                                    }
1250                                }
1251                            }
1252
1253                        } catch (Exception JavaDoc e) {
1254                            if (sax_error) {
1255                                throw new Exception JavaDoc("[LDAPTransformer] Error replacing an attribute: " + e.toString());
1256                            } else {
1257                                transformer.start(error_element, attr);
1258                                transformer.data("[LDAPTransformer] Error replacing an attribute: " + e.toString());
1259                                transformer.end(error_element);
1260                                transformer.getTheLogger().error("[LDAPTransformer] Error replacing an attribute: " + e.toString());
1261                                if (!row_element.equals("")) {
1262                                    transformer.end(row_element);
1263                                }
1264                                if (!exec_element.equals("")) {
1265                                    transformer.end(exec_element);
1266                                }
1267                            }
1268                        }
1269                        break;
1270                    case LDAPTransformer.STATE_INSIDE_EXECUTE_ADD :
1271                        try {
1272                            String JavaDoc[] attrVal = new String JavaDoc[attrVale.size()];
1273                            attrVale.toArray(attrVal);
1274                            attrVale.clear();
1275                            if (attrVal.length != attrList.length) {
1276                                transformer.start(error_element, attr);
1277                                transformer.data("Attribute values must have the some number as a names");
1278                                transformer.end(error_element);
1279                                break;
1280                            }
1281                            HashMap JavaDoc attrMap = new HashMap JavaDoc(attrVal.length);
1282
1283                            for (int i = 0; i < attrVal.length; i++)
1284                                attrMap.put(attrList[i], attrVal[i]);
1285
1286                            initConstraints(constraints);
1287                            if (attrList.length < 1) {
1288                                transformer.start(error_element, attr);
1289                                transformer.data("Modify must reference 1 or more attribute.");
1290                                transformer.end(error_element);
1291                            } else {
1292                                if (!filter.equals("")) {
1293                                    constraints.setReturningAttributes(attrList);
1294                                    NamingEnumeration JavaDoc ldapresults = ctx.search(searchbase, filter, constraints);
1295                                    SearchResult JavaDoc si = null;
1296                                    /* start indicate element of executing query */
1297                                    if (!exec_element.equals("")) {
1298                                        transformer.start(exec_element, attr);
1299                                    }
1300                                    while (ldapresults != null && ldapresults.hasMore()) {
1301                                        if (!row_element.equals("")) {
1302                                            transformer.start(row_element, attr);
1303                                        }
1304
1305                                        si = (SearchResult JavaDoc) ldapresults.next();
1306                                        javax.naming.directory.Attributes JavaDoc attrs = si.getAttributes();
1307                                        if (attrs != null) {
1308                                            /* Replace the attribute if attribute already exist */
1309                                            NamingEnumeration JavaDoc ae = attrs.getAll();
1310                                            while (ae.hasMoreElements()) {
1311                                                Attribute JavaDoc at = (Attribute JavaDoc) ae.next();
1312                                                String JavaDoc attrID = at.getID();
1313                                                // Specify the changes to make
1314
ModificationItem JavaDoc[] mods = new ModificationItem JavaDoc[1];
1315
1316                                                String JavaDoc attrValue = recodeToLDAPEncoding((String JavaDoc)attrMap.get(attrID));
1317                                                mods[0] = new ModificationItem JavaDoc(DirContext.REPLACE_ATTRIBUTE,
1318                                                                               new BasicAttribute JavaDoc(attrID, attrValue));
1319                                                // Perform the requested modifications on the named object
1320
ctx.modifyAttributes(
1321                                                    new StringBuffer JavaDoc(si.toString().substring(0, si.toString().indexOf(":")))
1322                                                        .append(",")
1323                                                        .append(searchbase)
1324                                                        .toString(),
1325                                                    mods);
1326
1327                                                /* confirm of success */
1328                                                transformer.start(attrID, attr);
1329                                                transformer.data("replaced");
1330                                                transformer.end(attrID);
1331                                                /* Remove the attribute from map after replacing */
1332                                                attrMap.remove(attrID);
1333                                            }
1334                                        }
1335                                        /* Add the attributes */
1336                                        if (!attrMap.isEmpty()) {
1337                                            ModificationItem JavaDoc[] mods = new ModificationItem JavaDoc[1];
1338                                            for (int i = 0; i < attrList.length; i++) {
1339                                                if (attrMap.containsKey(attrList[i])) {
1340                                                    String JavaDoc attrValue = recodeToLDAPEncoding((String JavaDoc)attrMap.get(attrList[i]));
1341                                                    mods[0] = new ModificationItem JavaDoc(DirContext.ADD_ATTRIBUTE,
1342                                                                                   new BasicAttribute JavaDoc(attrList[i], attrValue));
1343                                                    // Perform the requested modifications on the named object
1344
ctx.modifyAttributes(
1345                                                        new StringBuffer JavaDoc(si.toString().substring(0, si.toString().indexOf(":")))
1346                                                            .append(",")
1347                                                            .append(searchbase)
1348                                                            .toString(),
1349                                                        mods);
1350
1351                                                    /* confirm of success */
1352                                                    transformer.start(attrList[i], attr);
1353                                                    transformer.data("add");
1354                                                    transformer.end(attrList[i]);
1355                                                }
1356                                            }
1357                                        }
1358                                        if (!row_element.equals("")) {
1359                                            transformer.end(row_element);
1360                                        }
1361                                    }
1362                                    if (!exec_element.equals("")) {
1363                                        transformer.end(exec_element);
1364                                    }
1365                                } else {
1366                                    //filter is not present
1367
javax.naming.directory.Attributes JavaDoc attrs = ctx.getAttributes(searchbase, attrList);
1368                                    /* start indicate element of executing query */
1369                                    if (!exec_element.equals("")) {
1370                                        transformer.start(exec_element, attr);
1371                                    }
1372                                    if (!row_element.equals("")) {
1373                                        transformer.start(row_element, attr);
1374                                    }
1375
1376                                    if (attrs != null) {
1377                                        NamingEnumeration JavaDoc ae = attrs.getAll();
1378                                        while (ae.hasMoreElements()) {
1379                                            Attribute JavaDoc at = (Attribute JavaDoc) ae.next();
1380                                            String JavaDoc attrID = at.getID();
1381                                            // Specify the changes to make
1382
ModificationItem JavaDoc[] mods = new ModificationItem JavaDoc[1];
1383
1384                                            String JavaDoc attrValue = recodeToLDAPEncoding((String JavaDoc)attrMap.get(attrID));
1385                                            mods[0] = new ModificationItem JavaDoc(DirContext.REPLACE_ATTRIBUTE,
1386                                                                           new BasicAttribute JavaDoc(attrID, attrValue));
1387                                            // Perform the requested modifications on the named object
1388
ctx.modifyAttributes(searchbase, mods);
1389
1390                                            /* confirm of success */
1391                                            transformer.start(attrID, attr);
1392                                            transformer.data("replaced");
1393                                            transformer.end(attrID);
1394                                            /* Remove the attribute from map after replacing */
1395                                            attrMap.remove(attrID);
1396                                        }
1397                                    }
1398                                    /* Add the attributes */
1399                                    if (!attrMap.isEmpty()) {
1400                                        ModificationItem JavaDoc[] mods = new ModificationItem JavaDoc[1];
1401                                        for (int i = 0; i < attrList.length; i++) {
1402                                            if (attrMap.containsKey(attrList[i])) {
1403                                                String JavaDoc attrValue = recodeToLDAPEncoding((String JavaDoc)attrMap.get(attrList[i]));
1404                                                mods[0] = new ModificationItem JavaDoc(DirContext.ADD_ATTRIBUTE,
1405                                                                               new BasicAttribute JavaDoc(attrList[i], attrValue));
1406                                                // Perform the requested modifications on the named object
1407
ctx.modifyAttributes(searchbase, mods);
1408                                                /* confirm of success */
1409                                                transformer.start(attrList[i], attr);
1410                                                transformer.data("add");
1411                                                transformer.end(attrList[i]);
1412                                            }
1413                                        }
1414                                    }
1415                                    if (!row_element.equals("")) {
1416                                        transformer.end(row_element);
1417                                    }
1418                                    /* end indicate element of executing query */
1419                                    if (!exec_element.equals("")) {
1420                                        transformer.end(exec_element);
1421                                    }
1422                                }
1423                            }
1424                        } catch (Exception JavaDoc e) {
1425                            if (sax_error) {
1426                                throw new Exception JavaDoc("[LDAPTransformer] Error replacing an attribute: " + e.toString());
1427                            } else {
1428                                transformer.start(error_element, attr);
1429                                transformer.data("[LDAPTransformer] Error replacing an attribute: " + e.toString());
1430                                transformer.end(error_element);
1431                                transformer.getTheLogger().error("[LDAPTransformer] Error replacing an attribute: " + e.toString());
1432                                if (!row_element.equals("")) {
1433                                    transformer.end(row_element);
1434                                }
1435                                if (!exec_element.equals("")) {
1436                                    transformer.end(exec_element);
1437                                }
1438                            }
1439                        }
1440                        break;
1441                    default :
1442                } //end switch
1443
} catch (NamingException JavaDoc e) {
1444                if (sax_error) {
1445                    throw new NamingException JavaDoc("[LDAPTransformer] Failed ldap-connection to directory service: " + e.toString());
1446                } else {
1447                    transformer.start(error_element, attr);
1448                    transformer.data("[LDAPTransformer] Failed ldap-connection to directory service.");
1449                    transformer.end(error_element);
1450                    transformer.getTheLogger().error("[LDAPTransformer] Failed to connect to " + serverurl + e.toString());
1451                }
1452            }
1453            try {
1454                disconnect();
1455            } catch (NamingException JavaDoc e) {
1456                if (sax_error) {
1457                    throw new NamingException JavaDoc("[LDAPTransformer] Failed ldap-disconnection from directory service: " + e.toString());
1458                } else {
1459                    transformer.start(error_element, attr);
1460                    transformer.data("[LDAPTransformer] Failed ldap-disconnection to directory service.");
1461                    transformer.end(error_element);
1462                    transformer.getTheLogger().error("[LDAPTransformer] Failed to disconnect from " + serverurl + e.toString());
1463                }
1464            }
1465        }
1466
1467        protected void addAttrList(String JavaDoc attr) {
1468            attrListe.add(attr);
1469        }
1470
1471        protected void addAttrModeVal(String JavaDoc mode) {
1472            attrModeVal.add(mode);
1473        }
1474
1475        protected void addAttrVal(String JavaDoc val) {
1476            attrVale.add(val);
1477        }
1478
1479        protected void connect() throws NamingException JavaDoc {
1480            if (root_dn != null && password != null) {
1481                env.put(Context.SECURITY_AUTHENTICATION, authentication);
1482                env.put(Context.SECURITY_PRINCIPAL, root_dn);
1483                env.put(Context.SECURITY_CREDENTIALS, password);
1484            }
1485
1486            env.put("java.naming.ldap.version", version);
1487            env.put(Context.INITIAL_CONTEXT_FACTORY, initializer);
1488            env.put(Context.PROVIDER_URL, serverurl + ":" + port);
1489
1490            try {
1491                ctx = new InitialDirContext JavaDoc(env);
1492            } catch (NamingException JavaDoc e) {
1493                env.clear();
1494                throw new NamingException JavaDoc(e.toString());
1495            }
1496        }
1497
1498        protected void disconnect() throws NamingException JavaDoc {
1499            try {
1500                if (ctx != null)
1501                    ctx.close();
1502            } catch (NamingException JavaDoc e) {
1503                ctx = null;
1504                env.clear();
1505                throw new NamingException JavaDoc(e.toString());
1506            }
1507            ctx = null;
1508            env.clear();
1509        }
1510
1511        protected void debugPrint() {
1512            Logger logger = transformer.getTheLogger();
1513            if (logger.isDebugEnabled()) {
1514                logger.debug("[LDAPTransformer] query_index: " + query_index);
1515                logger.debug("[LDAPTransformer] current_state: " + current_state);
1516                logger.debug("[LDAPTransformer] serverurl: " + serverurl);
1517                logger.debug("[LDAPTransformer] port: " + port);
1518                logger.debug("[LDAPTransformer] root_dn: " + root_dn);
1519                logger.debug("[LDAPTransformer] password: " + password);
1520                logger.debug("[LDAPTransformer] version: " + version);
1521                logger.debug("[LDAPTransformer] scope: " + scope);
1522                logger.debug("[LDAPTransformer] authentication: " + authentication);
1523                logger.debug("[LDAPTransformer] toDo: " + toDo);
1524                logger.debug("[LDAPTransformer] searchbase: " + searchbase);
1525                logger.debug("[LDAPTransformer] showAttribute: " + showAttribute);
1526                logger.debug("[LDAPTransformer] attribute: " + attrListe.toString());
1527                logger.debug("[LDAPTransformer] filter: " + filter);
1528                logger.debug("[LDAPTransformer] doc_element: " + doc_element);
1529                logger.debug("[LDAPTransformer] row_element: " + row_element);
1530                logger.debug("[LDAPTransformer] error_element: " + error_element);
1531                logger.debug("[LDAPTransformer] sax-error: " + sax_error);
1532                logger.debug("[LDAPTransformer] deref_link: " + deref_link);
1533                logger.debug("[LDAPTransformer] count_limit: " + count_limit);
1534                logger.debug("[LDAPTransformer] time_limit: " + time_limit);
1535            }
1536        }
1537
1538        /**
1539         * Recodes a String value from {@link #LDAP_ENCODING} to specified {@link #encoding}.
1540         * @param value the String to recode
1541         * @return the recoded String
1542         * @throws UnsupportedEncodingException if either the used encoding
1543         */

1544        private String JavaDoc recodeFromLDAPEncoding(String JavaDoc value) throws UnsupportedEncodingException JavaDoc {
1545            if (!LDAP_ENCODING.equals(encoding)) {
1546                value = new String JavaDoc(value.getBytes(LDAP_ENCODING), encoding);
1547            }
1548            return value;
1549        }
1550
1551        /**
1552         * Recodes a String value from specified {@link #encoding} to {@link #LDAP_ENCODING}.
1553         * @param value the String to recode
1554         * @return the recoded String
1555         * @throws UnsupportedEncodingException if either the used encoding
1556         */

1557        private String JavaDoc recodeToLDAPEncoding(String JavaDoc value) throws UnsupportedEncodingException JavaDoc {
1558            if (!LDAP_ENCODING.equals(encoding)) {
1559                value = new String JavaDoc(value.getBytes(encoding), LDAP_ENCODING);
1560            }
1561            return value;
1562        }
1563    }
1564}
1565
Popular Tags