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.ldap.schema.converter; 021 022 023import java.io.InputStream; 024import java.io.Writer; 025import java.util.List; 026 027import org.apache.directory.api.i18n.I18n; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031 032/** 033 * A class used to translate a OpenLdap schema file to a Ldif file compatible 034 * with the ApacheDS meta schema format 035 * 036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 037 */ 038public final class SchemaToLdif 039{ 040 /** The ASF Header */ 041 private static final String HEADER = "#\n" + "# Licensed to the Apache Software Foundation (ASF) under one\n" 042 + "# or more contributor license agreements. See the NOTICE file\n" 043 + "# distributed with this work for additional information\n" 044 + "# regarding copyright ownership. The ASF licenses this file\n" 045 + "# to you under the Apache License, Version 2.0 (the\n" 046 + "# \"License\"); you may not use this file except in compliance\n" 047 + "# with the License. You may obtain a copy of the License at\n" + "# \n" 048 + "# http://www.apache.org/licenses/LICENSE-2.0\n" + "# \n" 049 + "# Unless required by applicable law or agreed to in writing,\n" 050 + "# software distributed under the License is distributed on an\n" 051 + "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n" 052 + "# KIND, either express or implied. See the License for the\n" 053 + "# specific language governing permissions and limitations\n" + "# under the License. \n" + "#\n" 054 + "version: 1\n" + "\n"; 055 056 /** The logger */ 057 private static final Logger LOG = LoggerFactory.getLogger( SchemaToLdif.class ); 058 059 060 /** 061 * Private constructor. 062 */ 063 private SchemaToLdif() 064 { 065 } 066 067 068 /** 069 * This method takes a list of schema and transform them to Ldif files 070 * 071 * @param schemas The list of schema to be transformed 072 * @throws ParserException If we get an error while converting the schemas 073 */ 074 public static void transform( List<Schema> schemas ) throws ParserException 075 { 076 // Bypass if no schemas have yet been defined 077 if ( ( schemas == null ) || schemas.isEmpty() ) 078 { 079 if ( LOG.isWarnEnabled() ) 080 { 081 LOG.warn( I18n.msg( I18n.MSG_15000_NO_SCHEMA_DEFINED ) ); 082 } 083 084 return; 085 } 086 087 // Make sure schema configurations have a name field and set defaults 088 // for any other missing properties of the bean: pkg and owner. 089 int i = 1; 090 091 for ( Schema schema : schemas ) 092 { 093 if ( schema.getName() == null ) 094 { 095 String msg = I18n.err( I18n.ERR_15000_SCHEMA_ELEMENT_NAME_REQUIRED, i ); 096 LOG.error( msg ); 097 throw new ParserException( msg ); 098 } 099 100 } 101 102 // Generate for each schema 103 for ( Schema schema : schemas ) 104 { 105 try 106 { 107 if ( LOG.isInfoEnabled() ) 108 { 109 LOG.info( I18n.msg( I18n.MSG_15001_GENERATING_SCHEMA, schema.getName() ) ); 110 } 111 112 generate( schema ); 113 } 114 catch ( Exception e ) 115 { 116 throw new ParserException( I18n.err( I18n.ERR_15004_CANNOT_GENERATE_SOURCES, schema.getName(), 117 e.getMessage() ) ); 118 } 119 } 120 } 121 122 123 /** 124 * Generate the ldif from a schema. The schema contains the inputStream 125 * and Writer. 126 * 127 * @param schema The schema to transfom 128 * @throws Exception If the conversion fails 129 */ 130 private static void generate( Schema schema ) throws Exception 131 { 132 try ( InputStream in = schema.getInput() ) 133 { 134 try ( Writer out = schema.getOutput() ) 135 { 136 // First parse the schema 137 SchemaParser parser = new SchemaParser(); 138 List<SchemaElement> elements = parser.parse( in ); 139 140 // Start with the header (apache licence) 141 out.write( HEADER ); 142 143 // Iterate through each schema elemnts 144 for ( SchemaElement element : elements ) 145 { 146 out.write( element.toLdif( schema.getName() ) ); 147 148 out.write( '\n' ); 149 } 150 151 // Done. Flush the result and close the reader and writer 152 out.flush(); 153 } 154 } 155 } 156}