KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.text.ParseException JavaDoc;
29
30 import org.apache.directory.ldapstudio.schemas.Messages;
31 import org.apache.directory.server.core.tools.schema.AttributeTypeLiteral;
32 import org.apache.directory.server.core.tools.schema.ObjectClassLiteral;
33 import org.apache.directory.server.core.tools.schema.OpenLdapSchemaParser;
34 import org.apache.log4j.Logger;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.widgets.MessageBox;
37 import org.eclipse.ui.PlatformUI;
38
39
40 /**
41  * This class is a schema-parser factory.
42  * You should use one of the static generation methods to get a parser.
43  *
44  */

45 public class SchemaParser
46 {
47
48     private static Logger logger = Logger.getLogger( SchemaParser.class );
49     /******************************************
50      * Fields *
51      ******************************************/

52
53     private URL JavaDoc fileURL = null;
54     private AttributeTypeLiteral[] attributeTypes = null;
55     private ObjectClassLiteral[] objectClasses = null;
56
57
58     /******************************************
59      * Accessors *
60      ******************************************/

61
62     /**
63      * Use this method to access generated attribute types
64      * @return the attribute types stored in an array
65      */

66     public AttributeTypeLiteral[] getAttributeTypes()
67     {
68         return attributeTypes;
69     }
70
71
72     /**
73      * Use this method to access generated object classes
74      * @return the object classes stored in an array
75      */

76     public ObjectClassLiteral[] getObjectClasses()
77     {
78         return objectClasses;
79     }
80
81
82     private void setAttributeTypes( AttributeTypeLiteral[] attributeTypes )
83     {
84         this.attributeTypes = attributeTypes;
85     }
86
87
88     private void setObjectClasses( ObjectClassLiteral[] objectClasses )
89     {
90         this.objectClasses = objectClasses;
91     }
92
93
94     /******************************************
95      * Initialization *
96      ******************************************/

97
98     private SchemaParser( URL JavaDoc url )
99     {
100         this.fileURL = url;
101     }
102
103
104     /**
105      * Use this method to obtain a schema parser from an URL.
106      * @param url the location of the schema that will be parsed.
107      * @return the schema parser
108      */

109     public static SchemaParser parserFromURL( URL JavaDoc url )
110     {
111         return new SchemaParser( url );
112
113     }
114
115
116     /******************************************
117      * Logic *
118      ******************************************/

119
120     /**
121      * Launch schema parsing
122      * @throws IOException if error opening the .schema file
123      * @throws ParseException if error during parsing of the .schema file
124      */

125     public void parse() throws IOException JavaDoc, ParseException JavaDoc
126     {
127         InputStream JavaDoc in = null;
128         in = fileURL.openStream();
129
130         if ( in == null )
131             throw new FileNotFoundException JavaDoc( Messages.getString( "SchemaParser.No_path_or_url_specified" ) ); //$NON-NLS-1$
132

133         OpenLdapSchemaParser parser = new OpenLdapSchemaParser();
134         try
135         {
136             parser.parse( in );
137         }
138         catch ( ParseException JavaDoc e )
139         {
140             logger.error( "An error occured when parsing the file - " + e.getMessage() ); //$NON-NLS-1$
141
MessageBox messageBox = new MessageBox( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
142                 SWT.OK | SWT.ICON_ERROR );
143             messageBox
144                 .setMessage( Messages.getString( "SchemaParser.An_error_has_occurred_when_parsing_the_file" ) + fileURL.toString() + Messages.getString( "SchemaParser.The_schema_cannot_be_opened" ) + " " + Messages.getString( "SchemaParser.See_log_file_for_debug_information_about_the_schema." ) ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
145
messageBox.open();
146             throw e;
147         }
148
149         generateAttributeTypes( parser );
150         generateObjectClasses( parser );
151     }
152
153
154     /**
155      * Generate all attributeTypes from schema
156      * @param parser the schema parser
157      */

158     @SuppressWarnings JavaDoc("unchecked")//$NON-NLS-1$
159
private void generateAttributeTypes( OpenLdapSchemaParser parser )
160     {
161         int size = parser.getAttributeTypes().size();
162         setAttributeTypes( new AttributeTypeLiteral[size] );
163         setAttributeTypes( ( AttributeTypeLiteral[] ) parser.getAttributeTypes().toArray( getAttributeTypes() ) );
164     }
165
166
167     /**
168      * Generate all objectClasses from schema
169      * @param parser the schema parser
170      */

171     @SuppressWarnings JavaDoc("unchecked")//$NON-NLS-1$
172
private void generateObjectClasses( OpenLdapSchemaParser parser )
173     {
174         int size = parser.getObjectClassTypes().size();
175         setObjectClasses( new ObjectClassLiteral[size] );
176         setObjectClasses( ( ObjectClassLiteral[] ) parser.getObjectClassTypes().toArray( getObjectClasses() ) );
177     }
178 }
179
Popular Tags