1 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 35 public abstract class LdifLineBase implements LdifPart 36 { 37 38 private String rawNewLine; 39 40 private int offset; 41 42 43 protected LdifLineBase() 44 { 45 } 46 47 48 protected LdifLineBase( int offset, String rawNewLine ) 49 { 50 super(); 51 this.rawNewLine = rawNewLine; 52 this.offset = offset; 53 } 54 55 56 public final String getRawNewLine() 57 { 58 return getNonNull( this.rawNewLine ); 59 } 60 61 62 public String 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 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 toRawString() 106 { 107 return this.getRawNewLine(); 108 } 109 110 111 public final String toFormattedString() 112 { 113 114 Preferences store = BrowserCorePlugin.getDefault().getPluginPreferences(); 115 boolean spaceAfterColon = store.getBoolean( BrowserCoreConstants.PREFERENCE_LDIF_SPACE_AFTER_COLON ); 116 String lineSeparator = store.getString( BrowserCoreConstants.PREFERENCE_LDIF_LINE_SEPARATOR ); 117 118 String raw = toRawString(); 119 String 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 toString() 159 { 160 String 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 getNonNull( String s ) 168 { 169 return s != null ? s : ""; 170 } 171 172 173 protected static String unfold( String 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 fold( String value, int indent ) 184 { 185 Preferences store = BrowserCorePlugin.getDefault().getPluginPreferences(); 186 int lineWidth = store.getInt( BrowserCoreConstants.PREFERENCE_LDIF_LINE_WIDTH ); 187 String lineSeparator = store.getString( BrowserCoreConstants.PREFERENCE_LDIF_LINE_SEPARATOR ); 188 189 StringBuffer formattedLdif = new StringBuffer (); 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 rest = value.substring( endIndex - offset, value.length() ); 201 formattedLdif.append( rest ); 202 203 return formattedLdif.toString(); 205 } 206 207 } 208 | Popular Tags |