KickJava   Java API By Example, From Geeks To Geeks.

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


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.BrowserCorePlugin;
26 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart;
27 import org.eclipse.core.runtime.Preferences;
28
29
30 /**
31  * Base class for all lines in a LDIF file.
32  *
33  *
34  */

35 public abstract class LdifLineBase implements LdifPart
36 {
37
38     private String JavaDoc rawNewLine;
39
40     private int offset;
41
42
43     protected LdifLineBase()
44     {
45     }
46
47
48     protected LdifLineBase( int offset, String JavaDoc rawNewLine )
49     {
50         super();
51         this.rawNewLine = rawNewLine;
52         this.offset = offset;
53     }
54
55
56     public final String JavaDoc getRawNewLine()
57     {
58         return getNonNull( this.rawNewLine );
59     }
60
61
62     public String JavaDoc getUnfoldedNewLine()
63     {
64         return unfold( this.getRawNewLine() );
65     }
66
67
68     public final void adjustOffset( int adjust )
69     {
70         this.offset += adjust;
71     }
72
73
74     public final int getOffset()
75     {
76         return this.offset;
77     }
78
79
80     public final int getLength()
81     {
82         return this.toRawString().length();
83     }
84
85
86     public boolean isValid()
87     {
88         return this.rawNewLine != null;
89     }
90
91
92     public String JavaDoc getInvalidString()
93     {
94         if ( this.rawNewLine == null )
95         {
96             return "Missing new line";
97         }
98         else
99         {
100             return null;
101         }
102     }
103
104
105     public String JavaDoc toRawString()
106     {
107         return this.getRawNewLine();
108     }
109
110
111     public final String JavaDoc toFormattedString()
112     {
113
114         Preferences store = BrowserCorePlugin.getDefault().getPluginPreferences();
115         boolean spaceAfterColon = store.getBoolean( BrowserCoreConstants.PREFERENCE_LDIF_SPACE_AFTER_COLON );
116         String JavaDoc lineSeparator = store.getString( BrowserCoreConstants.PREFERENCE_LDIF_LINE_SEPARATOR );
117
118         String JavaDoc raw = toRawString();
119         String JavaDoc unfolded = unfold( raw );
120
121         if ( this instanceof LdifValueLineBase )
122         {
123             if ( unfolded.indexOf( "::" ) > -1 )
124             {
125                 unfolded = unfolded.replaceFirst( "::[ ]*", spaceAfterColon ? ":: " : "::" );
126             }
127             else if ( unfolded.indexOf( ":<" ) > -1 )
128             {
129                 unfolded = unfolded.replaceFirst( ":<[ ]*", spaceAfterColon ? ":< " : ":<" );
130             }
131             else if ( unfolded.indexOf( ":" ) > -1 )
132             {
133                 unfolded = unfolded.replaceFirst( ":[ ]*", spaceAfterColon ? ": " : ":" );
134             }
135         }
136
137         if ( rawNewLine != null )
138         {
139             int index = unfolded.lastIndexOf( rawNewLine );
140             if ( index > -1 )
141             {
142                 unfolded = unfolded.substring( 0, unfolded.length() - rawNewLine.length() );
143                 unfolded = unfolded + lineSeparator;
144             }
145         }
146
147         if ( this instanceof LdifValueLineBase )
148         {
149             return fold( unfolded, 0 );
150         }
151         else
152         {
153             return unfolded;
154         }
155     }
156
157
158     public final String JavaDoc toString()
159     {
160         String JavaDoc text = toRawString();
161         text = text.replaceAll( "\n", "\\\\n" );
162         text = text.replaceAll( "\r", "\\\\r" );
163         return getClass().getName() + " (" + getOffset() + "," + getLength() + "): '" + text + "'";
164     }
165
166
167     protected static String JavaDoc getNonNull( String JavaDoc s )
168     {
169         return s != null ? s : "";
170     }
171
172
173     protected static String JavaDoc unfold( String JavaDoc s )
174     {
175         s = s.replaceAll( "\n\r ", "" );
176         s = s.replaceAll( "\r\n ", "" );
177         s = s.replaceAll( "\n ", "" );
178         s = s.replaceAll( "\r ", "" );
179         return s;
180     }
181
182
183     protected static String JavaDoc fold( String JavaDoc value, int indent )
184     {
185         Preferences store = BrowserCorePlugin.getDefault().getPluginPreferences();
186         int lineWidth = store.getInt( BrowserCoreConstants.PREFERENCE_LDIF_LINE_WIDTH );
187         String JavaDoc lineSeparator = store.getString( BrowserCoreConstants.PREFERENCE_LDIF_LINE_SEPARATOR );
188
189         StringBuffer JavaDoc formattedLdif = new StringBuffer JavaDoc();
190         int offset = lineWidth - indent;
191         int endIndex = 0 + offset;
192         while ( endIndex + lineSeparator.length() < value.length() )
193         {
194             formattedLdif.append( value.substring( endIndex - offset, endIndex ) );
195             formattedLdif.append( lineSeparator );
196             formattedLdif.append( ' ' );
197             offset = lineWidth - 1;
198             endIndex += offset;
199         }
200         String JavaDoc rest = value.substring( endIndex - offset, value.length() );
201         formattedLdif.append( rest );
202
203         // return
204
return formattedLdif.toString();
205     }
206
207 }
208
Popular Tags