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 */ 020package org.apache.directory.api.converter.schema; 021 022 023import org.apache.directory.api.ldap.model.entry.Attribute; 024import org.apache.directory.api.ldap.model.entry.DefaultAttribute; 025import org.apache.directory.api.ldap.model.entry.DefaultEntry; 026import org.apache.directory.api.ldap.model.entry.Entry; 027import org.apache.directory.api.ldap.model.exception.LdapException; 028import org.apache.directory.api.ldap.model.ldif.LdifUtils; 029import org.apache.directory.api.util.Strings; 030 031import java.util.ArrayList; 032import java.util.HashMap; 033import java.util.List; 034import java.util.Map; 035 036 037/** 038 * An abstract SchemaElement implementation. It contains shared 039 * elements from AttributeType and ObjectClass, like obsolete, oid, 040 * description, names and extensions (not implemented) 041 * 042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 043 */ 044public abstract class SchemaElementImpl implements SchemaElement 045{ 046 /** The schema element oid */ 047 protected String oid; 048 049 /** The schema element description */ 050 protected String description; 051 052 /** The list of names for this schemaElements */ 053 protected List<String> names = new ArrayList<String>(); 054 055 /** The obsolete flag */ 056 protected boolean obsolete = false; 057 058 /** The optional list of extensions */ 059 protected Map<String, List<String>> extensions = new HashMap<String, List<String>>(); 060 061 062 /** 063 * {@inheritDoc} 064 */ 065 public boolean isObsolete() 066 { 067 return obsolete; 068 } 069 070 071 /** 072 * {@inheritDoc} 073 */ 074 public void setObsolete( boolean obsolete ) 075 { 076 this.obsolete = obsolete; 077 } 078 079 080 /** 081 * {@inheritDoc} 082 */ 083 public String getOid() 084 { 085 return oid; 086 } 087 088 089 /** 090 * {@inheritDoc} 091 */ 092 public String getDescription() 093 { 094 return description; 095 } 096 097 098 /** 099 * {@inheritDoc} 100 */ 101 public void setDescription( String description ) 102 { 103 this.description = description; 104 } 105 106 107 /** 108 * @see SchemaElement#getNames() 109 */ 110 public List<String> getNames() 111 { 112 return names; 113 } 114 115 116 /** 117 * {@inheritDoc} 118 */ 119 public void setNames( List<String> names ) 120 { 121 this.names = names; 122 } 123 124 125 /** 126 * {@inheritDoc} 127 */ 128 public List<String> getExtension( String key ) 129 { 130 return extensions.get( key ); 131 } 132 133 134 /** 135 * {@inheritDoc} 136 */ 137 public Map<String, List<String>> getExtensions() 138 { 139 return extensions; 140 } 141 142 143 /** 144 * {@inheritDoc} 145 */ 146 public void setExtensions( Map<String, List<String>> extensions ) 147 { 148 this.extensions = extensions; 149 } 150 151 152 /** 153 * @return The OID as a Ldif line 154 */ 155 private String oidToLdif() 156 { 157 return "m-oid: " + oid + '\n'; 158 } 159 160 161 /** 162 * @return the Names as Ldif lines 163 * @throws org.apache.directory.api.ldap.model.exception.LdapException If the conversion goes wrong 164 */ 165 private String nameToLdif() throws LdapException 166 { 167 if ( names.size() == 0 ) 168 { 169 return ""; 170 } 171 else 172 { 173 Entry entry = new DefaultEntry(); 174 Attribute attribute = new DefaultAttribute( "m-name" ); 175 176 for ( String name : names ) 177 { 178 attribute.add( name ); 179 } 180 181 entry.put( attribute ); 182 183 return LdifUtils.convertAttributesToLdif( entry ); 184 } 185 } 186 187 188 /** 189 * @return The description as a ldif line 190 * @throws org.apache.directory.api.ldap.model.exception.LdapException If the conversion goes wrong 191 */ 192 private String descToLdif() throws LdapException 193 { 194 if ( Strings.isEmpty( description ) ) 195 { 196 return ""; 197 } 198 else 199 { 200 Entry entry = new DefaultEntry(); 201 Attribute attribute = new DefaultAttribute( "m-description", description ); 202 203 entry.put( attribute ); 204 205 return LdifUtils.convertAttributesToLdif( entry ); 206 } 207 } 208 209 210 /** 211 * Transform a Schema Element to a LDIF String 212 * 213 * @param schemaName The schema element to transform 214 * @return The Schema Element as a ldif String 215 * @throws org.apache.directory.api.ldap.model.exception.LdapException If the conversion goes wrong 216 */ 217 public abstract String dnToLdif( String schemaName ) throws LdapException; 218 219 220 /** 221 * Return the extensions formated as Ldif lines 222 * 223 * @param id The attributeId : can be m-objectClassExtension or 224 * m-attributeTypeExtension 225 * @return The extensions formated as ldif lines 226 * @throws org.apache.directory.api.ldap.model.exception.LdapException If the conversion goes wrong 227 */ 228 protected String extensionsToLdif( String id ) throws LdapException 229 { 230 StringBuilder sb = new StringBuilder(); 231 232 Entry entry = new DefaultEntry(); 233 Attribute attribute = new DefaultAttribute( id ); 234 235 for ( String extension : extensions.keySet() ) 236 { 237 attribute.add( extension ); 238 } 239 240 sb.append( LdifUtils.convertAttributesToLdif( entry ) ); 241 242 return sb.toString(); 243 } 244 245 246 /** 247 * Transform a Schema to a LDIF formated String 248 * 249 * @param schemaName The schema to transform 250 * @param type The ObjectClass type 251 * @return A LDIF String representing the schema 252 * @throws org.apache.directory.api.ldap.model.exception.LdapException If the transformation can't be done 253 */ 254 protected String schemaToLdif( String schemaName, String type ) throws LdapException 255 { 256 StringBuilder sb = new StringBuilder(); 257 258 // The Dn 259 sb.append( dnToLdif( schemaName ) ); 260 261 // ObjectClasses 262 sb.append( "objectclass: " ).append( type ).append( '\n' ); 263 sb.append( "objectclass: metaTop\n" ); 264 sb.append( "objectclass: top\n" ); 265 266 // The oid 267 sb.append( oidToLdif() ); 268 269 // The name 270 sb.append( nameToLdif() ); 271 272 // The desc 273 sb.append( descToLdif() ); 274 275 // The obsolete flag, only if "true" 276 if ( obsolete ) 277 { 278 sb.append( "m-obsolete: TRUE\n" ); 279 } 280 281 return sb.toString(); 282 } 283}