KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > dsmlv2 > reponse > LdapResultDsml


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

20
21 package org.apache.directory.ldapstudio.dsmlv2.reponse;
22
23
24 import java.util.List JavaDoc;
25
26 import org.apache.directory.ldapstudio.dsmlv2.DsmlDecorator;
27 import org.apache.directory.ldapstudio.dsmlv2.ParserUtils;
28 import org.apache.directory.shared.ldap.codec.LdapMessage;
29 import org.apache.directory.shared.ldap.codec.LdapResult;
30 import org.apache.directory.shared.ldap.codec.util.LdapURL;
31 import org.dom4j.Element;
32
33
34 /**
35  * DSML Decorator for the LdapResult class.
36  *
37  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
38  * @version $Rev$, $Date$
39  */

40 public class LdapResultDsml implements DsmlDecorator
41 {
42     /** The LDAP Result to decorate */
43     private LdapResult result;
44
45     /** The associated LDAP Message */
46     private LdapMessage message;
47
48
49     /**
50      * Creates a new instance of LdapResultDsml.
51      *
52      * @param result
53      * the LdapResult to decorate
54      * @param message
55      * the associated message
56      */

57     public LdapResultDsml( LdapResult result, LdapMessage message )
58     {
59         this.result = result;
60         this.message = message;
61     }
62
63
64     /* (non-Javadoc)
65      * @see org.apache.directory.ldapstudio.dsmlv2.reponse.DsmlDecorator#toDsml(org.dom4j.Element)
66      */

67     public Element toDsml( Element root )
68     {
69         
70         // RequestID
71
int requestID = message.getMessageId();
72         if ( requestID != 0 )
73         {
74             root.addAttribute( "requestID", "" + requestID );
75         }
76         
77         // Matched DN
78
String JavaDoc matchedDN = result.getMatchedDN();
79         if ( !matchedDN.equals( "" ) )
80         {
81             root.addAttribute( "matchedDN", matchedDN );
82         }
83
84         // Controls
85
ParserUtils.addControls( root, message.getControls() );
86
87         // ResultCode
88
Element resultCodeElement = root.addElement( "resultCode" );
89         resultCodeElement.addAttribute( "code", "" + result.getResultCode().getResultCode() );
90         resultCodeElement.addAttribute( "descr", LdapResultEnum.getResultCodeDescr( result.getResultCode() ) );
91
92         // ErrorMessage
93
String JavaDoc errorMessage = ( result.getErrorMessage() );
94         if ( ( errorMessage != null ) && ( !errorMessage.equals( "" ) ) )
95         {
96             Element errorMessageElement = root.addElement( "errorMessage" );
97             errorMessageElement.addText( errorMessage );
98         }
99
100         // Referals
101
List JavaDoc<LdapURL> referals = result.getReferrals();
102         if ( referals != null )
103         {
104             for ( int i = 0; i < referals.size(); i++ )
105             {
106                 Element referalElement = root.addElement( "referal" );
107                 referalElement.addText( referals.get( i ).toString() );
108             }
109         }
110
111         return root;
112     }
113 }
114
Popular Tags