KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > io > SchemaWriter


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  * http://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
21 package org.apache.directory.ldapstudio.schemas.io;
22
23
24 import java.io.BufferedWriter JavaDoc;
25 import java.io.FileWriter JavaDoc;
26 import java.text.DateFormat JavaDoc;
27 import java.util.Date JavaDoc;
28
29 import org.apache.directory.ldapstudio.schemas.model.AttributeType;
30 import org.apache.directory.ldapstudio.schemas.model.ObjectClass;
31 import org.apache.directory.ldapstudio.schemas.model.Schema;
32
33
34 /**
35  * This class is a LDAP schema generator
36  *
37  */

38 public class SchemaWriter
39 {
40
41     /**
42      * Launch schema generation
43      * @param schema the schema
44      * @param src the file-path where it will be generated
45      * @throws Exception if an exception during the generation of the schema
46      */

47     public void write( Schema schema, String JavaDoc src ) throws Exception JavaDoc
48     {
49         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
50         sb.append( "# " + schema.getName() + "\n" );
51         DateFormat JavaDoc format = DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.MEDIUM );
52         Date JavaDoc date = new Date JavaDoc();
53         sb.append( "# Generated by LDAP Studio on " + format.format( date ) + "\n" );
54         sb.append( "\n" );
55
56         for ( AttributeType at : schema.getAttributeTypesAsArray() )
57         {
58             sb.append( at.write() );
59             sb.append( "\n" );
60         }
61
62         for ( ObjectClass oc : schema.getObjectClassesAsArray() )
63         {
64             sb.append( oc.write() );
65             sb.append( "\n" );
66         }
67
68         // Writing schema file to the specified location.
69
BufferedWriter JavaDoc bufferedWriter;
70         bufferedWriter = new BufferedWriter JavaDoc( new FileWriter JavaDoc( src ) );
71         bufferedWriter.write( sb.toString() );
72         bufferedWriter.close();
73     }
74 }
75
Popular Tags