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            LOG.warn( "No schemas defined!" );
080            return;
081        }
082
083        // Make sure schema configurations have a name field and set defaults
084        // for any other missing properties of the bean: pkg and owner.
085        int i = 1;
086
087        for ( Schema schema : schemas )
088        {
089            if ( schema.getName() == null )
090            {
091                String msg = I18n.err( I18n.ERR_06003_NO_NAME, i );
092                LOG.error( msg );
093                throw new ParserException( msg );
094            }
095
096        }
097
098        // Generate for each schema 
099        for ( Schema schema : schemas )
100        {
101            try
102            {
103                LOG.info( "Generating {} schema.", schema.getName() );
104                generate( schema );
105            }
106            catch ( Exception e )
107            {
108                throw new ParserException( I18n.err( I18n.ERR_06004_CANNOT_GENERATE_SOURCES, schema.getName(),
109                    e.getMessage() ) );
110            }
111        }
112    }
113
114
115    /**
116     * Generate the ldif from a schema. The schema contains the inputStream
117     * and Writer.
118     * 
119     * @param schema The schema to transfom
120     * @throws Exception If the conversion fails
121     */
122    private static void generate( Schema schema ) throws Exception
123    {
124        if ( schema == null )
125        {
126            LOG.error( I18n.err( I18n.ERR_06005_NULL_SCHEMA ) );
127            throw new IllegalArgumentException( I18n.err( I18n.ERR_06006_NO_PROPERTY ) );
128        }
129
130        InputStream in = schema.getInput();
131        Writer out = schema.getOutput();
132
133        // First parse the schema
134        SchemaParser parser = new SchemaParser();
135        List<SchemaElement> elements = parser.parse( in );
136
137        // Start with the header (apache licence)
138        out.write( HEADER );
139
140        // Iterate through each schema elemnts
141        for ( SchemaElement element : elements )
142        {
143            out.write( element.toLdif( schema.getName() ) );
144
145            out.write( '\n' );
146        }
147
148        // Done. Flush the result and close the reader and writer
149        out.flush();
150
151        out.close();
152        in.close();
153    }
154}