KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > internal > model > AttributeDescription


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.browser.core.internal.model;
22
23
24 import java.io.Serializable JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
30 import org.apache.directory.ldapstudio.browser.core.model.schema.AttributeTypeDescription;
31 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema;
32
33
34 public class AttributeDescription implements Serializable JavaDoc
35 {
36
37     private static final long serialVersionUID = 1L;
38
39     private String JavaDoc description;
40
41     private String JavaDoc parsedAttributeType;
42
43     private List JavaDoc parsedLangList;
44
45     private List JavaDoc parsedOptionList;
46
47
48     public AttributeDescription( String JavaDoc description )
49     {
50
51         this.description = description;
52
53         String JavaDoc[] attributeDescriptionComponents = description.split( IAttribute.OPTION_DELIMITER );
54         this.parsedAttributeType = attributeDescriptionComponents[0];
55         this.parsedLangList = new ArrayList JavaDoc();
56         this.parsedOptionList = new ArrayList JavaDoc();
57         for ( int i = 1; i < attributeDescriptionComponents.length; i++ )
58         {
59             if ( attributeDescriptionComponents[i].startsWith( IAttribute.OPTION_LANG_PREFIX ) )
60             {
61                 this.parsedLangList.add( attributeDescriptionComponents[i] );
62             }
63             else
64             {
65                 this.parsedOptionList.add( attributeDescriptionComponents[i] );
66             }
67         }
68     }
69
70
71     public String JavaDoc getDescription()
72     {
73         return description;
74     }
75
76
77     public String JavaDoc getParsedAttributeType()
78     {
79         return parsedAttributeType;
80     }
81
82
83     public List JavaDoc getParsedLangList()
84     {
85         return parsedLangList;
86     }
87
88
89     public List JavaDoc getParsedOptionList()
90     {
91         return parsedOptionList;
92     }
93
94
95     public String JavaDoc toOidString( Schema schema )
96     {
97
98         if ( schema == null )
99         {
100             return description;
101         }
102
103         AttributeTypeDescription atd = schema.getAttributeTypeDescription( parsedAttributeType );
104         String JavaDoc oidString = atd.getNumericOID();
105
106         if ( !parsedLangList.isEmpty() )
107         {
108             for ( Iterator JavaDoc it = parsedLangList.iterator(); it.hasNext(); )
109             {
110                 String JavaDoc element = ( String JavaDoc ) it.next();
111                 oidString += element;
112
113                 if ( it.hasNext() || !parsedOptionList.isEmpty() )
114                 {
115                     oidString += IAttribute.OPTION_DELIMITER;
116                 }
117             }
118         }
119         if ( !parsedOptionList.isEmpty() )
120         {
121             for ( Iterator JavaDoc it = parsedOptionList.iterator(); it.hasNext(); )
122             {
123                 String JavaDoc element = ( String JavaDoc ) it.next();
124                 oidString += element;
125
126                 if ( it.hasNext() )
127                 {
128                     oidString += IAttribute.OPTION_DELIMITER;
129                 }
130             }
131         }
132
133         return oidString;
134     }
135
136
137     public boolean isSubtypeOf( AttributeDescription other, Schema schema )
138     {
139
140         // this=name, other=givenName;lang-de -> false
141
// this=name;lang-en, other=givenName;lang-de -> false
142
// this=givenName, other=name -> true
143
// this=givenName;lang-de, other=givenName -> true
144
// this=givenName;lang-de, other=name -> true
145
// this=givenName;lang-en, other=name;lang-de -> false
146
// this=givenName, other=givenName;lang-de -> false
147

148         // check equal descriptions
149
if ( this.toOidString( schema ).equals( other.toOidString( schema ) ) )
150         {
151             return false;
152         }
153
154         // check type
155
AttributeTypeDescription myAtd = schema.getAttributeTypeDescription( this.getParsedAttributeType() );
156         AttributeTypeDescription otherAtd = schema.getAttributeTypeDescription( other.getParsedAttributeType() );
157         if ( myAtd != otherAtd )
158         {
159             AttributeTypeDescription superiorAtd = null;
160             String JavaDoc superiorName = myAtd.getSuperiorAttributeTypeDescriptionName();
161             while ( superiorName != null )
162             {
163                 superiorAtd = schema.getAttributeTypeDescription( superiorName );
164                 if ( superiorAtd == otherAtd )
165                 {
166                     break;
167                 }
168                 superiorName = superiorAtd.getSuperiorAttributeTypeDescriptionName();
169             }
170             if ( superiorAtd != otherAtd )
171             {
172                 return false;
173             }
174         }
175
176         // check options
177
List JavaDoc myOptionsList = new ArrayList JavaDoc( this.getParsedOptionList() );
178         List JavaDoc otherOptionsList = new ArrayList JavaDoc( other.getParsedOptionList() );
179         otherOptionsList.removeAll( myOptionsList );
180         if ( !otherOptionsList.isEmpty() )
181         {
182             return false;
183         }
184
185         // check language tags
186
List JavaDoc myLangList = new ArrayList JavaDoc( this.getParsedLangList() );
187         List JavaDoc otherLangList = new ArrayList JavaDoc( other.getParsedLangList() );
188         for ( Iterator JavaDoc myIt = myLangList.iterator(); myIt.hasNext(); )
189         {
190             String JavaDoc myLang = ( String JavaDoc ) myIt.next();
191             for ( Iterator JavaDoc otherIt = otherLangList.iterator(); otherIt.hasNext(); )
192             {
193                 String JavaDoc otherLang = ( String JavaDoc ) otherIt.next();
194                 if ( otherLang.endsWith( "-" ) )
195                 {
196                     if ( myLang.toLowerCase().startsWith( otherLang.toLowerCase() ) )
197                     {
198                         otherIt.remove();
199                     }
200                 }
201                 else
202                 {
203                     if ( myLang.equalsIgnoreCase( otherLang ) )
204                     {
205                         otherIt.remove();
206                     }
207                 }
208             }
209         }
210         if ( !otherLangList.isEmpty() )
211         {
212             return false;
213         }
214
215         return true;
216     }
217
218 }
219
Popular Tags