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   *    https://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          + "#    https://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              if ( LOG.isWarnEnabled() )
80              {
81                  LOG.warn( I18n.msg( I18n.MSG_15000_NO_SCHEMA_DEFINED ) );
82              }
83              
84              return;
85          }
86  
87          // Make sure schema configurations have a name field and set defaults
88          // for any other missing properties of the bean: pkg and owner.
89          int i = 1;
90  
91          for ( Schema schema : schemas )
92          {
93              if ( schema.getName() == null )
94              {
95                  String msg = I18n.err( I18n.ERR_15000_SCHEMA_ELEMENT_NAME_REQUIRED, i );
96                  LOG.error( msg );
97                  throw new ParserException( msg );
98              }
99  
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 }