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