|
What this is
Other links
The source code/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.editor;
import java.io.IOException;
import java.io.Reader;
import javax.swing.text.Segment;
/**
* Converters handling the various line separators.
*
* @author Miloslav Metelka
* @version 1.00
*/
public class LineSeparatorConversion {
/**
* Default size of the conversion buffers.
*/
private static final int DEFAULT_CONVERSION_BUFFER_SIZE = 16384;
private LineSeparatorConversion() {
// no instances
}
/**
* Convert all the occurrences of '\r' and '\r\n' in the text to '\n'.
* @param text text being converted
* @return converted text with '\n' instead of '\r' and '\r\n'.
*/
public static String convertToLineFeed(String text) {
StringBuffer output = new StringBuffer();
convertToLineFeed(text, 0, text.length(), output);
return output.toString();
}
/**
* Convert all the occurrences of '\r' and '\r\n' in the text to '\n'.
* @param text text being converted
* @param offset offset of the first character in the text to be converted.
* @param length number of characters to be converted.
* @param output output buffer to which the converted characters are added.
*/
public static void convertToLineFeed(String text, int offset, int length,
StringBuffer output) {
int endOffset = offset + length;
boolean lastCharCR = false; // whether last char was '\r'
while (offset < endOffset) {
char ch = text.charAt(offset++);
if (lastCharCR && ch == '\n') { // found CRLF sequence
lastCharCR = false;
} else { // not CRLF sequence
if (ch == '\r') {
output.append('\n');
lastCharCR = true;
} else { // current char not '\r'
lastCharCR = false;
output.append(ch);
}
}
}
}
/**
* Convert all the occurrences of '\n' in the given text
* to the requested line separator.
* @param text text being converted
* @param lineFeedReplace characters that replace the '\n' character
* in the converted text.
* @return converted text with replaced '\n' by characters from lineFeedReplace string
*/
public static String convertFromLineFeed(String text, String lineFeedReplace) {
StringBuffer output = new StringBuffer();
convertFromLineFeed(text, 0, text.length(), lineFeedReplace, output);
return output.toString();
}
/**
* Convert all the occurrences of '\n' in the given text
* to the requested line separator.
* @param text text being converted
* @param offset offset of the first character in the text to be converted.
* @param length number of characters to be converted.
* @param lineFeedReplace characters that replace the '\n' character in the output
* @param output output buffer to which the converted characters are added.
*/
public static void convertFromLineFeed(String text, int offset, int length,
String lineFeedReplace, StringBuffer output) {
int lineFeedReplaceLength = lineFeedReplace.length();
int endOffset = offset + length;
while (offset < endOffset) {
char ch = text.charAt(offset++);
if (ch == '\n') {
for (int i = 0; i < lineFeedReplaceLength; i++) {
output.append(lineFeedReplace.charAt(i));
}
} else {
output.append(ch);
}
}
}
/**
* Convert all the occurrences of '\r' and '\r\n' in the text to '\n'.
* This class does conversion in chunks of fixed size
* and is therefore suitable for conversion of readers
* where the size is unknown.
*/
public static class ToLineFeed {
private Reader reader;
private Segment convertedText;
private boolean lastCharCR;
public ToLineFeed(Reader reader) {
this(reader, DEFAULT_CONVERSION_BUFFER_SIZE);
}
public ToLineFeed(Reader reader, int convertBufferSize) {
this.reader = reader;
convertedText = new Segment();
convertedText.array = new char[convertBufferSize];
}
public Segment nextConverted() throws IOException {
if (reader == null) { // no more chars to read
return null;
}
int readOffset = 0;
int readSize = readBuffer(reader, convertedText.array, readOffset, true);
if (readSize == 0) { // no more chars in reader
reader.close();
reader = null;
return null;
}
if (lastCharCR && readSize > 0 && convertedText.array[readOffset] == '\n') {
/* the preceding '\r' was already converted to '\n'
* in the previous buffer so here just skip initial '\n'
*/
readOffset++;
readSize--;
}
convertedText.offset = readOffset;
convertedText.count = readSize;
lastCharCR = convertSegmentToLineFeed(convertedText);
return convertedText;
}
/**
* Convert all the '\r\n' or '\r' to '\n' (linefeed).
* This method
* @param text the text to be converted. Text is converted
* in the original array of the given segment.
* The
|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.