KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > model > ldif > lines > LdifControlLine


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.lines;
22
23
24 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
25 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils;
26
27
28 public class LdifControlLine extends LdifValueLineBase
29 {
30
31     private static final long serialVersionUID = -3961159214439218610L;
32
33     private String JavaDoc rawCriticality;
34
35     private String JavaDoc rawControlValueType;
36
37     private String JavaDoc rawControlValue;
38
39
40     protected LdifControlLine()
41     {
42     }
43
44
45     public LdifControlLine( int offset, String JavaDoc rawControlSpec, String JavaDoc rawControlType, String JavaDoc rawOid,
46         String JavaDoc rawCriticality, String JavaDoc rawControlValueType, String JavaDoc rawControlValue, String JavaDoc rawNewLine )
47     {
48         super( offset, rawControlSpec, rawControlType, rawOid, rawNewLine );
49         this.rawCriticality = rawCriticality;
50         this.rawControlValueType = rawControlValueType;
51         this.rawControlValue = rawControlValue;
52     }
53
54
55     public String JavaDoc getRawControlSpec()
56     {
57         return super.getRawLineStart();
58     }
59
60
61     public String JavaDoc getUnfoldedControlSpec()
62     {
63         return super.getUnfoldedLineStart();
64     }
65
66
67     public String JavaDoc getRawControlType()
68     {
69         return super.getRawValueType();
70     }
71
72
73     public String JavaDoc getUnfoldedControlType()
74     {
75         return super.getUnfoldedValueType();
76     }
77
78
79     public String JavaDoc getRawOid()
80     {
81         return super.getRawValue();
82     }
83
84
85     public String JavaDoc getUnfoldedOid()
86     {
87         return super.getUnfoldedValue();
88     }
89
90
91     public String JavaDoc getRawCriticality()
92     {
93         return getNonNull( this.rawCriticality );
94     }
95
96
97     public String JavaDoc getUnfoldedCriticality()
98     {
99         return unfold( this.getRawCriticality() );
100     }
101
102
103     public boolean isCritical()
104     {
105         return this.getUnfoldedCriticality().endsWith( "true" );
106     }
107
108
109     public String JavaDoc getRawControlValueType()
110     {
111         return getNonNull( this.rawControlValueType );
112     }
113
114
115     public String JavaDoc getUnfoldedControlValueType()
116     {
117         return unfold( this.getRawControlValueType() );
118     }
119
120
121     public String JavaDoc getRawControlValue()
122     {
123         return getNonNull( this.rawControlValue );
124     }
125
126
127     public String JavaDoc getUnfoldedControlValue()
128     {
129         return unfold( this.getRawControlValue() );
130     }
131
132
133     public String JavaDoc toRawString()
134     {
135         return this.getRawControlSpec() + this.getRawControlType() + this.getRawOid() + this.getRawCriticality()
136             + this.getRawControlValueType() + this.getRawControlValue() + this.getRawNewLine();
137     }
138
139
140     public boolean isValid()
141     {
142         return this.getUnfoldedControlSpec().length() > 0
143             && this.getUnfoldedControlType().length() > 0
144             && this.getUnfoldedOid().length() > 0
145             && ( this.rawCriticality == null || this.getUnfoldedCriticality().endsWith( "true" ) || this
146                 .getUnfoldedCriticality().endsWith( "false" ) )
147             && ( ( this.rawControlValueType == null && this.rawControlValue == null ) || ( this.rawControlValueType != null && this.rawControlValue != null ) )
148             && this.getUnfoldedNewLine().length() > 0;
149     }
150
151
152     public String JavaDoc getInvalidString()
153     {
154         if ( this.getUnfoldedControlSpec().length() == 0 )
155         {
156             return "Missing 'control'";
157         }
158         else if ( this.getUnfoldedOid().length() == 0 )
159         {
160             return "Missing OID";
161         }
162         else if ( ( this.rawCriticality != null && !this.getUnfoldedCriticality().endsWith( "true" ) && !this
163             .getUnfoldedCriticality().endsWith( "false" ) ) )
164         {
165             return "Invalid criticality, must be 'true' or 'false'";
166         }
167         else
168         {
169             return super.getInvalidString();
170         }
171     }
172
173
174     /**
175      *
176      * @return the binary representation of the control value, may be null
177      */

178     public final byte[] getControlValueAsBinary()
179     {
180         Object JavaDoc o = getControlValueAsObject();
181         if ( o instanceof String JavaDoc )
182         {
183             return LdifUtils.utf8encode( ( String JavaDoc ) o );
184         }
185         else if ( o instanceof byte[] )
186         {
187             return ( byte[] ) o;
188         }
189         else
190         {
191             return new byte[0];
192         }
193     }
194
195
196     public final Object JavaDoc getControlValueAsObject()
197     {
198         if ( this.isControlValueTypeSafe() )
199         {
200             return this.getUnfoldedControlValue();
201         }
202         else if ( this.isControlValueTypeBase64() )
203         {
204             return LdifUtils.base64decodeToByteArray( this.getUnfoldedControlValue() );
205         }
206         else
207         {
208             return null;
209         }
210     }
211
212
213     public boolean isControlValueTypeBase64()
214     {
215         return this.getUnfoldedControlValueType().startsWith( "::" );
216     }
217
218
219     public boolean isControlValueTypeSafe()
220     {
221         return this.getUnfoldedControlValueType().startsWith( ":" ) && !this.isControlValueTypeBase64();
222     }
223
224
225     public static LdifControlLine create( String JavaDoc oid, String JavaDoc criticality, String JavaDoc controlValue )
226     {
227         if ( LdifUtils.mustEncode( controlValue ) )
228         {
229             return create( oid, criticality, LdifUtils.utf8encode( controlValue ) );
230         }
231         else
232         {
233             LdifControlLine controlLine = new LdifControlLine( 0, "control", ":", oid, criticality,
234                 controlValue != null ? ":" : null, controlValue != null ? controlValue : null,
235                 BrowserCoreConstants.LINE_SEPARATOR );
236             return controlLine;
237         }
238     }
239
240
241     public static LdifControlLine create( String JavaDoc oid, String JavaDoc criticality, byte[] controlValue )
242     {
243         LdifControlLine controlLine = new LdifControlLine( 0, "control", ":", oid, criticality, controlValue != null
244             && controlValue.length > 0 ? "::" : null, controlValue != null && controlValue.length > 0 ? LdifUtils
245             .base64encode( controlValue ) : null, BrowserCoreConstants.LINE_SEPARATOR );
246         return controlLine;
247     }
248
249 }
250
Popular Tags