View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.schema.converter;
21  
22  
23  import java.io.InputStream;
24  import java.io.Writer;
25  import java.util.List;
26  
27  import org.apache.directory.api.i18n.I18n;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  
32  /**
33   * A class used to translate a OpenLdap schema file to a Ldif file compatible
34   * with the ApacheDS meta schema format
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public final class SchemaToLdif
39  {
40      /** The ASF Header */
41      private static final String HEADER = "#\n" + "#  Licensed to the Apache Software Foundation (ASF) under one\n"
42          + "#  or more contributor license agreements.  See the NOTICE file\n"
43          + "#  distributed with this work for additional information\n"
44          + "#  regarding copyright ownership.  The ASF licenses this file\n"
45          + "#  to you under the Apache License, Version 2.0 (the\n"
46          + "#  \"License\"); you may not use this file except in compliance\n"
47          + "#  with the License.  You may obtain a copy of the License at\n" + "#  \n"
48          + "#    http://www.apache.org/licenses/LICENSE-2.0\n" + "#  \n"
49          + "#  Unless required by applicable law or agreed to in writing,\n"
50          + "#  software distributed under the License is distributed on an\n"
51          + "#  \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n"
52          + "#  KIND, either express or implied.  See the License for the\n"
53          + "#  specific language governing permissions and limitations\n" + "#  under the License. \n" + "#\n"
54          + "version: 1\n" + "\n";
55  
56      /** The logger */
57      private static final Logger LOG = LoggerFactory.getLogger( SchemaToLdif.class );
58  
59  
60      /**
61       * Private constructor.
62       */
63      private SchemaToLdif()
64      {
65      }
66  
67  
68      /**
69       * This method takes a list of schema and transform them to Ldif files 
70       * 
71       * @param schemas The list of schema to be transformed
72       * @throws ParserException If we get an error while converting the schemas
73       */
74      public static void transform( List<Schema> schemas ) throws ParserException
75      {
76          // Bypass if no schemas have yet been defined 
77          if ( ( schemas == null ) || schemas.isEmpty() )
78          {
79              LOG.warn( "No schemas defined!" );
80              return;
81          }
82  
83          // Make sure schema configurations have a name field and set defaults
84          // for any other missing properties of the bean: pkg and owner.
85          int i = 1;
86  
87          for ( Schema schema : schemas )
88          {
89              if ( schema.getName() == null )
90              {
91                  String msg = I18n.err( I18n.ERR_06003_NO_NAME, i );
92                  LOG.error( msg );
93                  throw new ParserException( msg );
94              }
95  
96          }
97  
98          // Generate for each schema 
99          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 }