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.converter.schema; 021 022 023 import java.util.ArrayList; 024 import java.util.List; 025 026 import org.apache.directory.shared.ldap.constants.SchemaConstants; 027 import org.apache.directory.shared.ldap.entry.Entry; 028 import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry; 029 import org.apache.directory.shared.ldap.exception.LdapException; 030 import org.apache.directory.shared.ldap.ldif.LdifUtils; 031 import org.apache.directory.shared.ldap.name.DN; 032 import org.apache.directory.shared.ldap.name.RDN; 033 import org.apache.directory.shared.ldap.schema.ObjectClassTypeEnum; 034 035 036 /** 037 * A bean used to encapsulate the literal String values of an ObjectClass 038 * definition found within an OpenLDAP schema configuration file. 039 * 040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 041 * @version $Rev: 437016 $ 042 */ 043 public class ObjectClassHolder extends SchemaElementImpl 044 { 045 /** The list of superiors */ 046 private List<String> superiors = new ArrayList<String>(); 047 048 /** The list of mandatory attributes */ 049 private List<String> must = new ArrayList<String>(); 050 051 /** The list of optional attributes */ 052 private List<String> may = new ArrayList<String>(); 053 054 /** The ObjectClass type */ 055 private ObjectClassTypeEnum classType = ObjectClassTypeEnum.STRUCTURAL; 056 057 058 /** 059 * Create an instance of ObjectClass element 060 */ 061 public ObjectClassHolder( String oid ) 062 { 063 this.oid = oid; 064 } 065 066 067 /** 068 * Get the list of superior for this objectClass 069 * @return A list of all inherited objectClasses 070 */ 071 public List<String> getSuperiors() 072 { 073 return superiors; 074 } 075 076 077 /** 078 * Set the list of inherited objectClasses 079 * @param superiors The list of inherited objectClasses 080 */ 081 public void setSuperiors( List<String> superiors ) 082 { 083 this.superiors = superiors; 084 } 085 086 087 /** 088 * @return The list of mandatory attributes 089 */ 090 public List<String> getMust() 091 { 092 return must; 093 } 094 095 096 /** 097 * Set the list of mandatory attributes 098 * @param must The list of mandatory attributes 099 */ 100 public void setMust( List<String> must ) 101 { 102 this.must = must; 103 } 104 105 106 /** 107 * @return The list of optional attributes 108 */ 109 public List<String> getMay() 110 { 111 return may; 112 } 113 114 115 /** 116 * Set the list of optional attributes 117 * @param may The list of optional attributes 118 */ 119 public void setMay( List<String> may ) 120 { 121 this.may = may; 122 } 123 124 125 /** 126 * @return The objectClass type 127 */ 128 public ObjectClassTypeEnum getClassType() 129 { 130 return classType; 131 } 132 133 134 /** 135 * Set the objectClass type. 136 * @param classType The objectClass type. 137 */ 138 public void setClassType( ObjectClassTypeEnum classType ) 139 { 140 this.classType = classType; 141 } 142 143 144 /** 145 * Convert this objectClass to a Ldif string 146 * 147 * @param schemaName The name of the schema file containing this objectClass 148 * @return A ldif formatted string 149 */ 150 public String toLdif( String schemaName ) throws LdapException 151 { 152 StringBuilder sb = new StringBuilder(); 153 154 sb.append( schemaToLdif( schemaName, "metaObjectClass" ) ); 155 156 // The superiors 157 if ( superiors.size() != 0 ) 158 { 159 for ( String superior : superiors ) 160 { 161 sb.append( "m-supObjectClass: " ).append( superior ).append( '\n' ); 162 } 163 } 164 165 // The kind of class 166 if ( classType != ObjectClassTypeEnum.STRUCTURAL ) 167 { 168 sb.append( "m-typeObjectClass: " ).append( classType ).append( '\n' ); 169 } 170 171 // The 'must' 172 if ( must.size() != 0 ) 173 { 174 for ( String attr : must ) 175 { 176 sb.append( "m-must: " ).append( attr ).append( '\n' ); 177 } 178 } 179 180 // The 'may' 181 if ( may.size() != 0 ) 182 { 183 for ( String attr : may ) 184 { 185 sb.append( "m-may: " ).append( attr ).append( '\n' ); 186 } 187 } 188 189 // The extensions 190 if ( extensions.size() != 0 ) 191 { 192 extensionsToLdif( "m-extensionObjectClass" ); 193 } 194 195 return sb.toString(); 196 } 197 198 199 /** 200 * Return a String representing this ObjectClass. 201 */ 202 public String toString() 203 { 204 return getOid(); 205 } 206 207 208 /* (non-Javadoc) 209 * @see org.apache.directory.shared.converter.schema.SchemaElementImpl#dnToLdif(java.lang.String) 210 */ 211 public String dnToLdif( String schemaName ) throws LdapException 212 { 213 StringBuilder sb = new StringBuilder(); 214 215 String dn = "m-oid=" + oid + ", " + SchemaConstants.OBJECT_CLASSES_PATH + ", cn=" + RDN.escapeValue( schemaName ) + ", ou=schema"; 216 217 // First dump the DN only 218 Entry entry = new DefaultClientEntry( new DN( dn ) ); 219 sb.append( LdifUtils.convertEntryToLdif( entry ) ); 220 221 return sb.toString(); 222 } 223 }