KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > model > ldif > container > LdifModSpec


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.model.ldif.container;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart;
29 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine;
30 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifModSpecSepLine;
31 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifModSpecTypeLine;
32
33
34 public class LdifModSpec extends LdifContainer implements LdifPart
35 {
36
37     private static final long serialVersionUID = 6708749639253050273L;
38
39
40     protected LdifModSpec()
41     {
42     }
43
44
45     public LdifModSpec( LdifModSpecTypeLine modSpecTypeLine )
46     {
47         super( modSpecTypeLine );
48     }
49
50
51     public void addAttrVal( LdifAttrValLine attrVal )
52     {
53         if ( attrVal == null )
54             throw new IllegalArgumentException JavaDoc( "null argument" );
55         this.parts.add( attrVal );
56     }
57
58
59     public void finish( LdifModSpecSepLine modSpecSepLine )
60     {
61         if ( modSpecSepLine == null )
62             throw new IllegalArgumentException JavaDoc( "null argument" );
63         this.parts.add( modSpecSepLine );
64     }
65
66
67     public LdifModSpecTypeLine getModSpecType()
68     {
69         return ( LdifModSpecTypeLine ) this.parts.get( 0 );
70     }
71
72
73     public LdifAttrValLine[] getAttrVals()
74     {
75         List JavaDoc l = new ArrayList JavaDoc();
76         for ( Iterator JavaDoc it = this.parts.iterator(); it.hasNext(); )
77         {
78             Object JavaDoc o = it.next();
79             if ( o instanceof LdifAttrValLine )
80             {
81                 l.add( o );
82             }
83         }
84         return ( LdifAttrValLine[] ) l.toArray( new LdifAttrValLine[l.size()] );
85     }
86
87
88     public LdifModSpecSepLine getModSpecSep()
89     {
90         if ( getLastPart() instanceof LdifModSpecSepLine )
91         {
92             return ( LdifModSpecSepLine ) getLastPart();
93         }
94         else
95         {
96             return null;
97         }
98     }
99
100
101     public boolean isAdd()
102     {
103         return this.getModSpecType().isAdd();
104     }
105
106
107     public boolean isReplace()
108     {
109         return this.getModSpecType().isReplace();
110     }
111
112
113     public boolean isDelete()
114     {
115         return this.getModSpecType().isDelete();
116     }
117
118
119     public static LdifModSpec createAdd( String JavaDoc attributeName )
120     {
121         return new LdifModSpec( LdifModSpecTypeLine.createAdd( attributeName ) );
122     }
123
124
125     public static LdifModSpec createReplace( String JavaDoc attributeName )
126     {
127         return new LdifModSpec( LdifModSpecTypeLine.createReplace( attributeName ) );
128     }
129
130
131     public static LdifModSpec createDelete( String JavaDoc attributeName )
132     {
133         return new LdifModSpec( LdifModSpecTypeLine.createDelete( attributeName ) );
134     }
135
136
137     public boolean isValid()
138     {
139         if ( !super.isAbstractValid() )
140         {
141             return false;
142         }
143
144         if ( this.getModSpecType() == null )
145         {
146             return false;
147         }
148
149         LdifAttrValLine[] attrVals = this.getAttrVals();
150         if ( attrVals.length > 0 )
151         {
152             String JavaDoc att = this.getModSpecType().getUnfoldedAttributeDescription();
153             for ( int i = 0; i < attrVals.length; i++ )
154             {
155                 if ( !att.equals( attrVals[i].getUnfoldedAttributeDescription() ) )
156                 {
157                     return false;
158                 }
159             }
160         }
161
162         if ( isAdd() )
163         {
164             return attrVals.length > 0;
165         }
166         else if ( isDelete() )
167         {
168             return true;
169         }
170         else if ( isReplace() )
171         {
172             return true;
173         }
174         else
175         {
176             return false;
177         }
178     }
179
180
181     public String JavaDoc getInvalidString()
182     {
183         if ( this.getModSpecType() == null )
184         {
185             return "Missing mod spec line ";
186         }
187         else if ( isAdd() && this.getAttrVals().length == 0 )
188         {
189             return "Modification must contain attribute value lines ";
190         }
191
192         LdifAttrValLine[] attrVals = this.getAttrVals();
193         if ( attrVals.length > 0 )
194         {
195             String JavaDoc att = this.getModSpecType().getUnfoldedAttributeDescription();
196             for ( int i = 0; i < attrVals.length; i++ )
197             {
198                 if ( !att.equals( attrVals[i].getUnfoldedAttributeDescription() ) )
199                 {
200                     return "Attribute descriptions don't match";
201                 }
202             }
203         }
204
205         return null;
206     }
207
208 }
209
Popular Tags