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.converter.schema;
21
22
23 import org.apache.directory.api.i18n.I18n;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.io.InputStream;
28 import java.io.Writer;
29 import java.util.List;
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.size() == 0 ) )
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 }
110 }
111 }
112
113
114
115
116
117
118
119
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
133 SchemaParser parser = new SchemaParser();
134 List<SchemaElement> elements = parser.parse( in );
135
136
137 out.write( HEADER );
138
139
140 for ( SchemaElement element : elements )
141 {
142 out.write( element.toLdif( schema.getName() ) );
143
144 out.write( '\n' );
145 }
146
147
148 out.flush();
149
150 out.close();
151 in.close();
152 }
153 }