KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils;
29
30
31 public class LdifValueLineBase extends LdifNonEmptyLineBase
32 {
33
34     private static final long serialVersionUID = -7030930374861554147L;
35
36     private String JavaDoc rawValueType;
37
38     private String JavaDoc rawValue;
39
40
41     protected LdifValueLineBase()
42     {
43     }
44
45
46     public LdifValueLineBase( int offset, String JavaDoc rawLineStart, String JavaDoc rawValueType, String JavaDoc rawValue, String JavaDoc rawNewLine )
47     {
48         super( offset, rawLineStart, rawNewLine );
49
50         this.rawValueType = rawValueType;
51         this.rawValue = rawValue;
52     }
53
54
55     public String JavaDoc getRawValueType()
56     {
57         return getNonNull( this.rawValueType );
58     }
59
60
61     public String JavaDoc getUnfoldedValueType()
62     {
63         return unfold( this.getRawValueType() );
64     }
65
66
67     public String JavaDoc getRawValue()
68     {
69         return getNonNull( this.rawValue );
70     }
71
72
73     public String JavaDoc getUnfoldedValue()
74     {
75         return unfold( this.getRawValue() );
76     }
77
78
79     public String JavaDoc toRawString()
80     {
81         return this.getRawLineStart() + this.getRawValueType() + this.getRawValue() + this.getRawNewLine();
82     }
83
84
85     public boolean isValid()
86     {
87         return super.isValid() && this.rawValueType != null && this.rawValue != null;
88     }
89
90
91     public String JavaDoc getInvalidString()
92     {
93         if ( this.rawValueType == null )
94         {
95             return "Missing value type ':', '::' or ':<'";
96         }
97         else if ( this.rawValue == null )
98         {
99             return "Missing value";
100         }
101         else
102         {
103             return super.getInvalidString();
104         }
105     }
106
107
108     /**
109      *
110      * @return the string representation of the value, non-base64, unfolded
111      */

112     public final String JavaDoc getValueAsString()
113     {
114         Object JavaDoc o = getValueAsObject();
115         if ( o instanceof String JavaDoc )
116         {
117             return ( String JavaDoc ) o;
118         }
119         else if ( o instanceof byte[] )
120         {
121             return LdifUtils.utf8decode( ( byte[] ) o );
122         }
123         else
124         {
125             return "";
126         }
127     }
128
129
130     /**
131      *
132      * @return the binary representation of the real value, non-base64,
133      * unfolded
134      */

135     public final byte[] getValueAsBinary()
136     {
137         Object JavaDoc o = getValueAsObject();
138         if ( o instanceof String JavaDoc )
139         {
140             return LdifUtils.utf8encode( ( String JavaDoc ) o );
141         }
142         else if ( o instanceof byte[] )
143         {
144             return ( byte[] ) o;
145         }
146         else
147         {
148             return new byte[0];
149         }
150     }
151
152
153     /**
154      * Returns the real data:
155      * <ul>
156      * <li>The unfolded String if value is a safe value.
157      * </li>
158      * <li>A byte array if value is base64 encoded.
159      * </li>
160      * <li>A byte array if value references an URL.
161      * </li>
162      * </ul>
163      *
164      * @return the real value or null
165      */

166     public final Object JavaDoc getValueAsObject()
167     {
168         if ( this.isValueTypeSafe() )
169         {
170             return this.getUnfoldedValue();
171         }
172         else if ( this.isValueTypeBase64() )
173         {
174             return LdifUtils.base64decodeToByteArray( this.getUnfoldedValue() );
175         }
176         else if ( this.isValueTypeURL() )
177         {
178             try
179             {
180                 File JavaDoc file = new File JavaDoc( this.getUnfoldedValue() );
181                 byte[] data = new byte[( int ) file.length()];
182                 FileInputStream JavaDoc fis = new FileInputStream JavaDoc( file );
183                 fis.read( data );
184                 return data;
185             }
186             catch ( IOException JavaDoc ioe )
187             {
188                 return null;
189             }
190         }
191         else
192         {
193             return null;
194         }
195     }
196
197
198     public boolean isValueTypeURL()
199     {
200         return this.getUnfoldedValueType().startsWith( ":<" );
201     }
202
203
204     public boolean isValueTypeBase64()
205     {
206         return this.getUnfoldedValueType().startsWith( "::" );
207     }
208
209
210     public boolean isValueTypeSafe()
211     {
212         return this.getUnfoldedValueType().startsWith( ":" ) && !this.isValueTypeBase64() && !this.isValueTypeURL();
213     }
214
215 }
216
Popular Tags