1 20 package org.apache.directory.ldapstudio.dsmlv2.request; 21 22 import java.util.List ; 23 24 import javax.naming.NamingEnumeration ; 25 import javax.naming.NamingException ; 26 import javax.naming.directory.DirContext ; 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 42 public class ModifyRequestDsml extends AbstractRequestDsml 43 { 44 50 public ModifyRequestDsml( LdapMessage ldapMessage ) 51 { 52 super( ldapMessage ); 53 } 54 55 56 59 public int getMessageType() 60 { 61 return instance.getMessageType(); 62 } 63 64 65 68 public Element toDsml( Element root ) 69 { 70 Element element = super.toDsml( root ); 71 72 ModifyRequest request = ( ModifyRequest ) instance; 73 74 if ( request.getObject() != null ) 76 { 77 element.addAttribute( "dn", request.getObject().toString() ); 78 } 79 80 List <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 ne = modificationItem.getAttribute().getAll(); 95 while ( ne.hasMoreElements() ) 96 { 97 Object value = ( Object ) 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 ) value ); 115 } 116 } 117 } 118 } 119 catch ( NamingException 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 |