1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
36
37
38 public final class SchemaToLdif
39 {
40
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
57 private static final Logger LOG = LoggerFactory.getLogger( SchemaToLdif.class );
58
59
60
61
62
63 private SchemaToLdif()
64 {
65 }
66
67
68
69
70
71
72
73
74 public static void transform( List<Schema> schemas ) throws ParserException
75 {
76
77 if ( ( schemas == null ) || schemas.isEmpty() )
78 {
79 LOG.warn( "No schemas defined!" );
80 return;
81 }
82
83
84
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
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
117
118
119
120
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
134 SchemaParser parser = new SchemaParser();
135 List<SchemaElement> elements = parser.parse( in );
136
137
138 out.write( HEADER );
139
140
141 for ( SchemaElement element : elements )
142 {
143 out.write( element.toLdif( schema.getName() ) );
144
145 out.write( '\n' );
146 }
147
148
149 out.flush();
150
151 out.close();
152 in.close();
153 }
154 }