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 package org.apache.directory.api.ldap.model.schema;
21
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.directory.api.i18n.I18n;
27
28
29 /**
30 * Represents an LDAP MatchingRuleUseDescription defined in RFC 2252.
31 * <p>
32 * According to ldapbis [MODELS]:
33 * </p>
34 *
35 * <pre>
36 * Values of the matchingRuleUse list the attributes which are suitable
37 * for use with an extensible matching rule.
38 *
39 * Matching rule use descriptions are written according to the following
40 * ABNF:
41 *
42 * MatchingRuleUseDescription = LPAREN WSP
43 * numericoid ; object identifier
44 * [ SP "NAME" SP qdescrs ] ; short names (descriptors)
45 * [ SP "DESC" SP qdstring ] ; description
46 * [ SP "OBSOLETE" ] ; not active
47 * SP "APPLIES" SP oids ; attribute types
48 * extensions WSP RPAREN ; extensions
49 *
50 * where:
51 * [numericoid] is the object identifier of the matching rule
52 * associated with this matching rule use description;
53 * NAME [qdescrs] are short names (descriptors) identifying this
54 * matching rule use;
55 * DESC [qdstring] is a short descriptive string;
56 * OBSOLETE indicates this matching rule use is not active;
57 * APPLIES provides a list of attribute types the matching rule applies
58 * to; and
59 * [extensions] describe extensions.
60 *
61 * The matchingRule within the MatchingRuleUse definition can be used by an
62 * extensible match assertion if the assertion is based on the attributes
63 * listed within the MatchingRuleUse definition. If an extensible match
64 * assertion is based on attributes other than those listed within the
65 * MatchingRuleUse definition then the assertion is deemed undefined.
66 *
67 * Also according to 3.3.20 of [SYNTAXES] (ldapbis working group):
68 *
69 * A value of the Matching Rule Use Description syntax indicates the
70 * attribute types to which a matching rule may be applied in an
71 * extensibleMatch search filter [PROT]. The LDAP-specific encoding of
72 * a value of this syntax is defined by the <MatchingRuleUseDescription>
73 * rule in [MODELS] above.
74 * </pre>
75 *
76 * @see <a
77 * href="http://www.ietf.org/internet-drafts/draft-ietf-ldapbis-models-11.txt">ldapbis
78 * [MODELS]</a>
79 * @see <a
80 * href="http://www.ietf.org/internet-drafts/draft-ietf-ldapbis-syntaxes-09.txt">ldapbis
81 * [SYNTAXES]</a>
82 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
83 */
84 public class MatchingRuleUse extends AbstractSchemaObject
85 {
86 /** The mandatory serialVersionUID */
87 public static final long serialVersionUID = 1L;
88
89 /** The list of attributes types OID the matching rule applies to */
90 private List<String> applicableAttributeOids;
91
92 /** The list of attributes types the matching rule applies to */
93 private List<AttributeType> applicableAttributes;
94
95
96 /**
97 * Creates a new instance of MatchingRuleUseDescription
98 */
99 public MatchingRuleUse( String oid )
100 {
101 super( SchemaObjectType.MATCHING_RULE_USE, oid );
102
103 applicableAttributeOids = new ArrayList<String>();
104 applicableAttributes = new ArrayList<AttributeType>();
105 }
106
107
108 /**
109 * @return The matchingRule's list of AttributeType OIDs the MRU applies to
110 */
111 public List<String> getApplicableAttributeOids()
112 {
113 return applicableAttributeOids;
114 }
115
116
117 /**
118 * @return The matchingRule's list of AttributeType OIDs the MRU applies to
119 */
120 public List<AttributeType> getApplicableAttributes()
121 {
122 return applicableAttributes;
123 }
124
125
126 /**
127 * Set the matchingRule's AttributeType OIDs the MRU applies to.
128 *
129 * @param applicableAttributeOids The AttributeType OIDs list
130 */
131 public void setApplicableAttributeOids( List<String> applicableAttributeOids )
132 {
133 if ( locked )
134 {
135 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
136 }
137
138 if ( !isReadOnly )
139 {
140 this.applicableAttributeOids = applicableAttributeOids;
141 }
142 }
143
144
145 /**
146 * Set the matchingRule's AttributeType the MRU applies to.
147 *
148 * @param applicableAttributes The AttributeType list
149 */
150 public void setApplicableAttributes( List<AttributeType> applicableAttributes )
151 {
152 if ( locked )
153 {
154 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
155 }
156
157 if ( !isReadOnly )
158 {
159 this.applicableAttributes = applicableAttributes;
160
161 // update the OIDS now
162 applicableAttributeOids.clear();
163
164 for ( AttributeType at : applicableAttributes )
165 {
166 applicableAttributeOids.add( at.getOid() );
167 }
168 }
169 }
170
171
172 /**
173 * Add a matchingRule's AttributeType OIDs the MRU applies to.
174 *
175 * @param oid A matchingRule's AttributeType OIDs the MRU applies to
176 */
177 public void addApplicableAttributeOids( String oid )
178 {
179 if ( locked )
180 {
181 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
182 }
183
184 if ( !isReadOnly && !applicableAttributeOids.contains( oid ) )
185 {
186 applicableAttributeOids.add( oid );
187 }
188 }
189
190
191 /**
192 * Add a matchingRule's AttributeType the MRU applies to.
193 *
194 * @param attributeType A matchingRule's AttributeType the MRU applies to
195 */
196 public void addApplicableAttribute( AttributeType attributeType )
197 {
198 if ( locked )
199 {
200 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
201 }
202
203 if ( !isReadOnly && !applicableAttributeOids.contains( attributeType.getOid() ) )
204 {
205 applicableAttributes.add( attributeType );
206 applicableAttributeOids.add( attributeType.getOid() );
207 }
208 }
209
210
211 /**
212 * @see Object#toString()
213 */
214 public String toString()
215 {
216 return SchemaObjectRenderer.OPEN_LDAP_SCHEMA_RENDERER.render( this );
217 }
218
219
220 /**
221 * Copy an MatchingRuleUse
222 */
223 public MatchingRuleUse copy()
224 {
225 MatchingRuleUse copy = new MatchingRuleUse( oid );
226
227 // Copy the SchemaObject common data
228 copy.copy( this );
229
230 // Clone the APPLY AttributeTypes
231 copy.applicableAttributeOids = new ArrayList<String>();
232
233 // Copy the APPLIES oid list
234 for ( String oid : applicableAttributeOids )
235 {
236 copy.applicableAttributeOids.add( oid );
237 }
238
239 // Copy the APPLIES list (will be empty)
240 copy.applicableAttributes = new ArrayList<AttributeType>();
241
242 return copy;
243 }
244
245
246 /**
247 * @see Object#equals(Object)
248 */
249 @Override
250 public boolean equals( Object o )
251 {
252 if ( !super.equals( o ) )
253 {
254 return false;
255 }
256
257 if ( !( o instanceof MatchingRuleUse ) )
258 {
259 return false;
260 }
261
262 @SuppressWarnings("unused")
263 MatchingRuleUse that = ( MatchingRuleUse ) o;
264
265 // TODO : complete the checks
266 return true;
267 }
268
269
270 /**
271 * {@inheritDoc}
272 */
273 public void clear()
274 {
275 // Clear the common elements
276 super.clear();
277
278 // Clear the references
279 applicableAttributes.clear();
280 applicableAttributeOids.clear();
281 }
282 }