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 + "# 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
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 if ( LOG.isWarnEnabled() )
80 {
81 LOG.warn( I18n.msg( I18n.MSG_15000_NO_SCHEMA_DEFINED ) );
82 }
83
84 return;
85 }
86
87
88
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
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
125
126
127
128
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
137 SchemaParser parser = new SchemaParser();
138 List<SchemaElement> elements = parser.parse( in );
139
140
141 out.write( HEADER );
142
143
144 for ( SchemaElement element : elements )
145 {
146 out.write( element.toLdif( schema.getName() ) );
147
148 out.write( '\n' );
149 }
150
151
152 out.flush();
153 }
154 }
155 }
156 }