KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.BrowserCoreConstants;
30 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifInvalidPart;
31 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart;
32 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifLineBase;
33
34
35 public abstract class LdifContainer implements Serializable JavaDoc
36 {
37
38     protected List JavaDoc parts;
39
40
41     protected LdifContainer()
42     {
43     }
44
45
46     protected LdifContainer( LdifPart part )
47     {
48         this.parts = new ArrayList JavaDoc( 1 );
49         if ( part == null )
50             throw new IllegalArgumentException JavaDoc( "null argument" );
51         this.parts.add( part );
52     }
53
54
55     public final int getOffset()
56     {
57         return ( ( LdifPart ) this.parts.get( 0 ) ).getOffset();
58     }
59
60
61     public final int getLength()
62     {
63         LdifPart lastPart = this.getLastPart();
64         return lastPart.getOffset() + lastPart.getLength() - getOffset();
65     }
66
67
68     public final void addInvalid( LdifInvalidPart invalid )
69     {
70         if ( invalid == null )
71             throw new IllegalArgumentException JavaDoc( "null argument" );
72         this.parts.add( invalid );
73     }
74
75
76     public final LdifPart getLastPart()
77     {
78         return ( LdifPart ) parts.get( parts.size() - 1 );
79     }
80
81
82     public final LdifPart[] getParts()
83     {
84         return ( LdifPart[] ) this.parts.toArray( new LdifPart[parts.size()] );
85     }
86
87
88     public final String JavaDoc toString()
89     {
90         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
91
92         sb.append( getClass().getName() );
93         sb.append( ":" );
94         sb.append( BrowserCoreConstants.LINE_SEPARATOR );
95
96         LdifPart[] parts = this.getParts();
97         for ( int i = 0; i < parts.length; i++ )
98         {
99             sb.append( " " );
100             sb.append( parts[i].toString() );
101             sb.append( BrowserCoreConstants.LINE_SEPARATOR );
102         }
103
104         return sb.toString();
105     }
106
107
108     public final String JavaDoc toRawString()
109     {
110         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
111
112         LdifPart[] parts = this.getParts();
113         for ( int i = 0; i < parts.length; i++ )
114         {
115             sb.append( parts[i].toRawString() );
116         }
117
118         return sb.toString();
119     }
120
121
122     public final String JavaDoc toFormattedString()
123     {
124         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
125
126         LdifPart[] parts = this.getParts();
127         for ( int i = 0; i < parts.length; i++ )
128         {
129             sb.append( parts[i].toFormattedString() );
130         }
131
132         return sb.toString();
133     }
134
135
136     public abstract boolean isValid();
137
138
139     /**
140      * true if
141      * <ul>
142      * <li>at least one line
143      * <li>no LdifUnknownPart
144      * <li>all parts are valid
145      * </ul>
146      */

147     protected boolean isAbstractValid()
148     {
149         if ( this.parts.isEmpty() )
150             return false;
151
152         boolean containsLine = false;
153         LdifPart[] parts = this.getParts();
154         for ( int i = 0; i < parts.length; i++ )
155         {
156             if ( parts[i] instanceof LdifInvalidPart )
157             {
158                 return false;
159             }
160             if ( !parts[i].isValid() )
161             {
162                 return false;
163             }
164             if ( parts[i] instanceof LdifLineBase )
165             {
166                 containsLine = true;
167             }
168         }
169         return containsLine;
170     }
171
172
173     public String JavaDoc getInvalidString()
174     {
175         if ( this.parts.isEmpty() )
176             return "Empty Container";
177
178         LdifPart[] parts = this.getParts();
179         for ( int i = 0; i < parts.length; i++ )
180         {
181             if ( !parts[i].isValid() )
182             {
183                 return parts[i].getInvalidString();
184             }
185         }
186
187         return null;
188     }
189
190
191     public final void adjustOffset( int adjust )
192     {
193         for ( Iterator JavaDoc it = this.parts.iterator(); it.hasNext(); )
194         {
195             LdifPart part = ( LdifPart ) it.next();
196             part.adjustOffset( adjust );
197         }
198     }
199
200 }
201
Popular Tags