KickJava   Java API By Example, From Geeks To Geeks.

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


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 matching rules
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 MatchingRules
41 {
42     /** The Equality Matching Rules */
43     private static final ArrayList JavaDoc<MatchingRule> equalityMatchingRules;
44
45     /** The Ordering Matching Rules */
46     private static final ArrayList JavaDoc<MatchingRule> orderingMatchingRules;
47
48     /** The Substring Matching Rules */
49     private static final ArrayList JavaDoc<MatchingRule> substringMatchingRules;
50
51     // Equality Matching Rules Initialization
52
static
53     {
54         try
55         {
56             equalityMatchingRules = new ArrayList JavaDoc<MatchingRule>();
57             URL JavaDoc url = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/utils/matching_rules.xml" ); //$NON-NLS-1$
58
XMLConfiguration config = new XMLConfiguration( url );
59
60             // We get the number of matching rules to parse for EQUALITY
61
Object JavaDoc matchingRules = config.getProperty( "equality.matchingRule.name" ); //$NON-NLS-1$
62
if ( matchingRules instanceof Collection JavaDoc )
63             {
64                 for ( int i = 0; i < ( ( Collection JavaDoc ) matchingRules ).size(); i++ )
65                 {
66                     // We parse each syntax and get its properties
67
String JavaDoc name = config.getString( "equality.matchingRule(" + i + ").name" ); //$NON-NLS-1$ //$NON-NLS-2$
68
String JavaDoc oid = config.getString( "equality.matchingRule(" + i + ").oid" ); //$NON-NLS-1$ //$NON-NLS-2$
69
String JavaDoc syntax = config.getString( "equality.matchingRule(" + i + ").syntax" ); //$NON-NLS-1$ //$NON-NLS-2$
70

71                     // We create the corresponding syntax object and add it to the ArrayList
72
MatchingRule matchingRule = new MatchingRule( name, oid, syntax );
73                     equalityMatchingRules.add( matchingRule );
74                 }
75             }
76         }
77         catch ( Throwable JavaDoc e )
78         {
79             throw new RuntimeException JavaDoc( e.getMessage() );
80         }
81     }
82
83     // Ordering Matching Rules Initialization
84
static
85     {
86         try
87         {
88             orderingMatchingRules = new ArrayList JavaDoc<MatchingRule>();
89
90             URL JavaDoc url = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/utils/matching_rules.xml" ); //$NON-NLS-1$
91
XMLConfiguration config = new XMLConfiguration( url );
92
93             // We get the number of matching rules to parse for ORDERING
94
Object JavaDoc matchingRules = config.getProperty( "ordering.matchingRule.name" ); //$NON-NLS-1$
95
if ( matchingRules instanceof Collection JavaDoc )
96             {
97                 for ( int i = 0; i < ( ( Collection JavaDoc ) matchingRules ).size(); i++ )
98                 {
99                     // We parse each syntax and get its properties
100
String JavaDoc name = config.getString( "ordering.matchingRule(" + i + ").name" ); //$NON-NLS-1$ //$NON-NLS-2$
101
String JavaDoc oid = config.getString( "ordering.matchingRule(" + i + ").oid" ); //$NON-NLS-1$ //$NON-NLS-2$
102
String JavaDoc syntax = config.getString( "ordering.matchingRule(" + i + ").syntax" ); //$NON-NLS-1$ //$NON-NLS-2$
103

104                     // We create the corresponding syntax object and add it to the ArrayList
105
MatchingRule matchingRule = new MatchingRule( name, oid, syntax );
106                     orderingMatchingRules.add( matchingRule );
107                 }
108             }
109         }
110         catch ( Throwable JavaDoc e )
111         {
112             throw new RuntimeException JavaDoc( e.getMessage() );
113         }
114     }
115
116     // Substring Matching Rules Initialization
117
static
118     {
119         try
120         {
121             substringMatchingRules = new ArrayList JavaDoc<MatchingRule>();
122             URL JavaDoc url = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/utils/matching_rules.xml" ); //$NON-NLS-1$
123
XMLConfiguration config = new XMLConfiguration( url );
124
125             // We get the number of matching rules to parse for SUBSTRING
126
Object JavaDoc matchingRules = config.getProperty( "substring.matchingRule.name" ); //$NON-NLS-1$
127
if ( matchingRules instanceof Collection JavaDoc )
128             {
129                 for ( int i = 0; i < ( ( Collection JavaDoc ) matchingRules ).size(); i++ )
130                 {
131                     // We parse each syntax and get its properties
132
String JavaDoc name = config.getString( "substring.matchingRule(" + i + ").name" ); //$NON-NLS-1$ //$NON-NLS-2$
133
String JavaDoc oid = config.getString( "substring.matchingRule(" + i + ").oid" ); //$NON-NLS-1$ //$NON-NLS-2$
134
String JavaDoc syntax = config.getString( "substring.matchingRule(" + i + ").syntax" ); //$NON-NLS-1$ //$NON-NLS-2$
135

136                     // We create the corresponding syntax object and add it to the ArrayList
137
MatchingRule matchingRule = new MatchingRule( name, oid, syntax );
138                     substringMatchingRules.add( matchingRule );
139                 }
140             }
141         }
142         catch ( Throwable JavaDoc e )
143         {
144             throw new RuntimeException JavaDoc( e.getMessage() );
145         }
146     }
147
148
149     /**
150      * Returns the unique initialized ArrayList containing all Equality Matching Rules.
151      * @return
152      * the Equality Matching Rules ArrayList
153      */

154     public static ArrayList JavaDoc<MatchingRule> getEqualityMatchingRules()
155     {
156         return equalityMatchingRules;
157     }
158
159
160     /**
161      * Returns the unique initialized ArrayList containing all Ordering Matching Rules.
162      *
163      * @return
164      * the Ordering Matching Rules ArrayList
165      */

166     public static ArrayList JavaDoc<MatchingRule> getOrderingMatchingRules()
167     {
168         return orderingMatchingRules;
169     }
170
171
172     /**
173      * Returns the unique initialized ArrayList containing all Substring Matching Rules.
174      *
175      * @return
176      * the Substring Matching Rules ArrayList
177      */

178     public static ArrayList JavaDoc<MatchingRule> getSubstringMatchingRules()
179     {
180         return substringMatchingRules;
181     }
182
183
184     /**
185      * Gets a Matching Rule from a given name.
186      *
187      * @param name
188      * the name of the Matching Rule
189      * @return
190      * the corresponding Matching Rule
191      */

192     public static MatchingRule getMatchingRule( String JavaDoc name )
193     {
194         if ( name == null )
195         {
196             return null;
197         }
198
199         for ( MatchingRule matchingRule : equalityMatchingRules )
200         {
201             if ( name.equalsIgnoreCase( matchingRule.getName() ) )
202             {
203                 return matchingRule;
204             }
205         }
206
207         for ( MatchingRule matchingRule : orderingMatchingRules )
208         {
209             if ( name.equalsIgnoreCase( matchingRule.getName() ) )
210             {
211                 return matchingRule;
212             }
213         }
214
215         for ( MatchingRule matchingRule : substringMatchingRules )
216         {
217             if ( name.equalsIgnoreCase( matchingRule.getName() ) )
218             {
219                 return matchingRule;
220             }
221         }
222
223         return null;
224     }
225 }
226
Popular Tags