KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > dsmlv2 > request > ModifyRequestDsml


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 package org.apache.directory.ldapstudio.dsmlv2.request;
21
22 import java.util.List JavaDoc;
23
24 import javax.naming.NamingEnumeration JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.naming.directory.DirContext JavaDoc;
27
28 import org.apache.directory.ldapstudio.dsmlv2.ParserUtils;
29 import org.apache.directory.shared.ldap.codec.LdapMessage;
30 import org.apache.directory.shared.ldap.codec.modify.ModifyRequest;
31 import org.apache.directory.shared.ldap.message.ModificationItemImpl;
32 import org.dom4j.Element;
33 import org.dom4j.Namespace;
34 import org.dom4j.QName;
35
36 /**
37  * DSML Decorator for ModifyRequest
38  *
39  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
40  * @version $Rev$, $Date$
41  */

42 public class ModifyRequestDsml extends AbstractRequestDsml
43 {
44     /**
45      * Creates a new instance of ModifyRequestDsml.
46      *
47      * @param ldapMessage
48      * the message to decorate
49      */

50     public ModifyRequestDsml( LdapMessage ldapMessage )
51     {
52         super( ldapMessage );
53     }
54     
55     
56     /**
57      * {@inheritDoc}
58      */

59     public int getMessageType()
60     {
61         return instance.getMessageType();
62     }
63
64     
65     /**
66      * {@inheritDoc}
67      */

68     public Element toDsml( Element root )
69     {
70         Element element = super.toDsml( root );
71         
72         ModifyRequest request = ( ModifyRequest ) instance;
73         
74         // DN
75
if ( request.getObject() != null )
76         {
77             element.addAttribute( "dn", request.getObject().toString() );
78         }
79         
80         // Modifications
81
List JavaDoc<ModificationItemImpl> modifications = request.getModifications();
82         
83         for ( int i = 0; i < modifications.size(); i++ )
84         {
85             ModificationItemImpl modificationItem = modifications.get( i );
86             
87             Element modElement = element.addElement( "modification" );
88             if ( modificationItem.getAttribute() != null )
89             {
90                 modElement.addAttribute( "name", modificationItem.getAttribute().getID() );
91                 
92                 try
93                 {
94                     NamingEnumeration JavaDoc ne = modificationItem.getAttribute().getAll();
95                     while ( ne.hasMoreElements() )
96                     {
97                         Object JavaDoc value = ( Object JavaDoc ) ne.nextElement();
98                         
99                         if ( value != null )
100                         {
101                             if ( ParserUtils.needsBase64Encoding( value ) )
102                             {
103                                 Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI );
104                                 Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI );
105                                 element.getDocument().getRootElement().add( xsdNamespace );
106                                 element.getDocument().getRootElement().add( xsiNamespace );
107
108                                 Element valueElement = modElement.addElement( "value" ).addText( ParserUtils.base64Encode( value ) );
109                                 valueElement
110                                     .addAttribute( new QName( "type", xsiNamespace ), "xsd:" + ParserUtils.BASE64BINARY );
111                             }
112                             else
113                             {
114                                 modElement.addElement( "value" ).setText( (String JavaDoc) value );
115                             }
116                         }
117                     }
118                 }
119                 catch ( NamingException JavaDoc e )
120                 {
121                 }
122             }
123             
124             int operation = modificationItem.getModificationOp();
125             if ( operation == DirContext.ADD_ATTRIBUTE )
126             {
127                 modElement.addAttribute( "operation", "add" );
128             }
129             else if ( operation == DirContext.REPLACE_ATTRIBUTE )
130             {
131                 modElement.addAttribute( "operation", "replace" );
132             }
133             else if ( operation == DirContext.REMOVE_ATTRIBUTE )
134             {
135                 modElement.addAttribute( "operation", "delete" );
136             }
137         }
138         
139         return element;
140     }
141 }
142
Popular Tags