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