KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import javax.naming.NamingEnumeration JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25 import javax.naming.directory.Attribute JavaDoc;
26 import javax.naming.directory.Attributes 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.add.AddRequest;
31 import org.dom4j.Element;
32 import org.dom4j.Namespace;
33 import org.dom4j.QName;
34
35
36 /**
37  * DSML Decorator for AddRequest
38  *
39  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
40  * @version $Rev$, $Date$
41  */

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

50     public AddRequestDsml( 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         AddRequest request = ( AddRequest ) instance;
73
74         // DN
75
if ( request.getEntry() != null )
76         {
77             element.addAttribute( "dn", request.getEntry().toString() );
78         }
79
80         // Attributes
81
Attributes JavaDoc attributes = request.getAttributes();
82         if ( attributes != null )
83         {
84             NamingEnumeration JavaDoc ne = attributes.getAll();
85             while ( ne.hasMoreElements() )
86             {
87                 Attribute JavaDoc attribute = ( Attribute JavaDoc ) ne.nextElement();
88                 Element attributeElement = element.addElement( "attr" );
89                 attributeElement.addAttribute( "name", attribute.getID() );
90     
91                 // Looping on Values Enumeration
92
try
93                 {
94                     NamingEnumeration JavaDoc ne2 = attribute.getAll();
95     
96                     while ( ne2.hasMoreElements() )
97                     {
98                         Object JavaDoc value = ne2.nextElement();
99     
100                         if ( ParserUtils.needsBase64Encoding( value ) )
101                         {
102                             Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI );
103                             Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI );
104                             attributeElement.getDocument().getRootElement().add( xsdNamespace );
105                             attributeElement.getDocument().getRootElement().add( xsiNamespace );
106     
107                             Element valueElement = attributeElement.addElement( "value" ).addText(
108                                 ParserUtils.base64Encode( value ) );
109                             valueElement
110                                 .addAttribute( new QName( "type", xsiNamespace ), "xsd:" + ParserUtils.BASE64BINARY );
111                         }
112                         else
113                         {
114                             attributeElement.addElement( "value" ).addText( value.toString() );
115                         }
116                     }
117                 }
118                 catch ( NamingException JavaDoc e )
119                 {
120                 }
121             }
122         }
123
124         return element;
125     }
126 }
127
Popular Tags