KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > model > Syntaxes


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.model;
22
23
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27
28 import org.apache.commons.configuration.XMLConfiguration;
29 import org.apache.directory.ldapstudio.schemas.Activator;
30 import org.eclipse.core.runtime.Platform;
31
32
33 /**
34  * This class allows to get the list of all syntaxes
35  * (which is initialized once parsing a XML file)
36  *
37  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
38  * @version $Rev$, $Date$
39  */

40 public class Syntaxes
41 {
42     /** The Syntaxes */
43     private static final ArrayList JavaDoc<Syntax> syntaxes;
44
45     // Syntaxes Initialization
46
static
47     {
48         try
49         {
50             syntaxes = new ArrayList JavaDoc<Syntax>();
51             URL JavaDoc url = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/utils/syntaxes.xml" ); //$NON-NLS-1$
52
XMLConfiguration config = new XMLConfiguration( url );
53
54             // We get the number of syntaxes to parse
55
Object JavaDoc syntaxesList = config.getProperty( "syntax.name" ); //$NON-NLS-1$
56
if ( syntaxesList instanceof Collection JavaDoc )
57             {
58                 for ( int i = 0; i < ( ( Collection JavaDoc ) syntaxesList ).size(); i++ )
59                 {
60                     // We parse each syntax and get its properties
61
String JavaDoc name = config.getString( "syntax(" + i + ").name" ); //$NON-NLS-1$ //$NON-NLS-2$
62
String JavaDoc oid = config.getString( "syntax(" + i + ").oid" ); //$NON-NLS-1$ //$NON-NLS-2$
63
String JavaDoc hr = config.getString( "syntax(" + i + ").hr" ); //$NON-NLS-1$ //$NON-NLS-2$
64

65                     // We create the corresponding syntax object
66
Syntax syntax = null;
67                     if ( hr.equals( "Y" ) ) { //$NON-NLS-1$
68
syntax = new Syntax( name, oid, true );
69                     }
70                     else if ( hr.equals( "N" ) ) { //$NON-NLS-1$
71
syntax = new Syntax( name, oid, true );
72                     }
73
74                     if ( syntax != null )
75                         syntaxes.add( syntax );
76                 }
77             }
78
79         }
80         catch ( Throwable JavaDoc e )
81         {
82             throw new RuntimeException JavaDoc( e.getMessage() );
83         }
84     }
85
86
87     /**
88      * Returns the unique initialized ArrayList containing all syntaxes.
89      *
90      * @return
91      * the syntaxes ArrayList
92      */

93     public static ArrayList JavaDoc<Syntax> getSyntaxes()
94     {
95         return syntaxes;
96     }
97
98
99     /**
100      * Return the syntax object corresponding to the name given in parameter.
101      * If no syntax is corresponding, it returns null.
102      *
103      * @param name
104      * the name of the syntax
105      * @return
106      * the coreesponding Syntax object
107      */

108     public static Syntax getSyntaxFromName( String JavaDoc name )
109     {
110         for ( Syntax syntax : syntaxes )
111         {
112             if ( syntax.getName().equals( name ) )
113             {
114                 return syntax;
115             }
116         }
117         return null;
118     }
119
120
121     /**
122      * Returns the syntax object corresponding to the oid given in parameter.
123      * If no syntax is corresponding, it returns null.
124      *
125      * @param oid
126      * the oid of the syntax
127      * @return
128      * the coreesponding Syntax object
129      */

130     public static Syntax getSyntaxFromOid( String JavaDoc oid )
131     {
132         for ( Syntax syntax : syntaxes )
133         {
134             if ( syntax.getOid().equals( oid ) )
135             {
136                 return syntax;
137             }
138         }
139         return null;
140     }
141 }
142
Popular Tags