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.i18n.I18n;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027import java.io.InputStream;
028import java.io.Writer;
029import java.util.List;
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.size() == 0 ) )
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            }
110        }
111    }
112
113
114    /**
115     * Generate the ldif from a schema. The schema contains the inputStream
116     * and Writer.
117     * 
118     * @param schema The schema to transfom
119     * @throws Exception If the conversion fails
120     */
121    private static void generate( Schema schema ) throws Exception
122    {
123        if ( schema == null )
124        {
125            LOG.error( I18n.err( I18n.ERR_06005_NULL_SCHEMA ) );
126            throw new IllegalArgumentException( I18n.err( I18n.ERR_06006_NO_PROPERTY ) );
127        }
128
129        InputStream in = schema.getInput();
130        Writer out = schema.getOutput();
131
132        // First parse the schema
133        SchemaParser parser = new SchemaParser();
134        List<SchemaElement> elements = parser.parse( in );
135
136        // Start with the header (apache licence)
137        out.write( HEADER );
138
139        // Iterate through each schema elemnts
140        for ( SchemaElement element : elements )
141        {
142            out.write( element.toLdif( schema.getName() ) );
143
144            out.write( '\n' );
145        }
146
147        // Done. Flush the result and close the reader and writer
148        out.flush();
149
150        out.close();
151        in.close();
152    }
153}