001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 package org.apache.directory.shared.ldap.name; 021 022 import java.io.IOException; 023 import java.io.ObjectInput; 024 import java.io.ObjectOutput; 025 026 import org.apache.directory.shared.ldap.util.StringTools; 027 import org.slf4j.Logger; 028 import org.slf4j.LoggerFactory; 029 030 /** 031 * A helper class which serialize and deserialize a RDN 032 * 033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 034 * @version $Rev$, $Date$ 035 */ 036 public class RdnSerializer 037 { 038 /** The LoggerFactory used by this class */ 039 protected static final Logger LOG = LoggerFactory.getLogger( RdnSerializer.class ); 040 041 /** 042 * Serialize a RDN instance 043 * 044 * A RDN is composed of on to many ATAVs (AttributeType And Value). 045 * We should write all those ATAVs sequencially, following the 046 * structure : 047 * 048 * <li>nbAtavs</li> The number of ATAVs to write. Can't be 0. 049 * <li>upName</li> The User provided RDN 050 * <li>normName</li> The normalized RDN. It can be empty if the normalized 051 * name equals the upName. 052 * <li>atavs</li> 053 * <p> 054 * For each ATAV :<p> 055 * <li>start</li> The position of this ATAV in the upName string 056 * <li>length</li> The ATAV user provided length 057 * <li>Call the ATAV write method</li> The ATAV itself 058 * 059 * @param rdn The RDN to serialize 060 * @param out the stream in which the RDN will be serialized 061 * @throws IOException If we can't write in this stream 062 */ 063 public static void serialize( RDN rdn, ObjectOutput out ) throws IOException 064 { 065 out.writeInt( rdn.getNbAtavs() ); 066 out.writeUTF( rdn.getName() ); 067 out.writeUTF( rdn.getNormName() ); 068 out.writeInt( rdn.getStart() ); 069 out.writeInt( rdn.getLength() ); 070 071 switch ( rdn.getNbAtavs() ) 072 { 073 case 0 : 074 break; 075 076 case 1 : 077 AVASerializer.serialize( rdn.getAtav(), out ); 078 break; 079 080 default : 081 for ( AVA atav:rdn ) 082 { 083 AVASerializer.serialize( atav, out ); 084 } 085 086 break; 087 } 088 } 089 090 091 /** 092 * Deserialize a RDN instance 093 * 094 * We read back the data to create a new RDB. The structure 095 * read is exposed in the {@link RDN#writeExternal(ObjectOutput)} 096 * method<p> 097 * 098 * @param in The input stream from which the RDN is read 099 * @return a deserialized RDN 100 * @throws IOException If the stream can't be read 101 */ 102 public static RDN deserialize( ObjectInput in ) throws IOException 103 { 104 // Read the ATAV number 105 int nbAtavs = in.readInt(); 106 107 // Read the UPName 108 String upName = in.readUTF(); 109 110 // Read the normName 111 String normName = in.readUTF(); 112 113 if ( StringTools.isEmpty( normName ) ) 114 { 115 normName = upName; 116 } 117 118 // Read the RDN's position and length 119 int start = in.readInt(); 120 int length = in.readInt(); 121 122 // Now creates the RDN 123 RDN rdn = new RDN( length, start, upName, normName ); 124 125 // Read through the Atavs 126 switch ( nbAtavs ) 127 { 128 case 0 : 129 return rdn; 130 131 case 1 : 132 AVA atav = AVASerializer.deserialize( in ); 133 134 rdn.addAttributeTypeAndValue( atav ); 135 136 return rdn; 137 138 default : 139 for ( int i = 0; i < nbAtavs; i++ ) 140 { 141 atav = AVASerializer.deserialize( in ); 142 rdn.addAttributeTypeAndValue( atav ); 143 } 144 145 return rdn; 146 } 147 } 148 }