jdom解析windows下回车换行符的的trap

如果在windows下的编辑器中获取了一个编辑器的输入,则将编辑器的输入内容转换为Element的Text存储,在jdom中会做一个转换,里面主要做了一步操作,就是将原来文本的内容中回车换行转换为回车换行。(实际上是将“\n”转换为换行符,但是他这里没有考虑windows下平台的问题,因为windows前面还多了一个"\r")

看jdom中换行转换的代码:

public String escapeElementEntities(String str) {
if (escapeOutput == false) return str;

StringBuffer buffer;
int ch, pos;
String entity;
EscapeStrategy strategy = currentFormat.escapeStrategy;

buffer = null;
for (int i = 0; i < str.length(); i++) {
ch = str.charAt(i);
pos = i;
switch(ch) {
case '<' :
entity = "<";
break;
case '>' :
entity = ">";
break;
case '&' :
entity = "&";
break;
case '\r' :
entity = " ";
break;
case '\n' :
entity = currentFormat.lineSeparator;
break;
default :

if (strategy.shouldEscape((char) ch)) {

//make sure what we are escaping is not the
//beginning of a multi-byte character.
if(Verifier.isHighSurrogate((char) ch)) {
//this is a the high of a surrogate pair
i++;
if (i < str.length()) {
char low = str.charAt(i);
if(!Verifier.isLowSurrogate(low)) {
throw new IllegalDataException("Could not decode surrogate pair 0x" +
Integer.toHexString(ch) + " / 0x" + Integer.toHexString(low));
}
ch = Verifier.decodeSurrogatePair((char) ch, low);
} else {
throw new IllegalDataException("Surrogate pair 0x" +
Integer.toHexString(ch) + " truncated");
}
}
entity = "&#x" + Integer.toHexString(ch) + ";";
}
else {
entity = null;
}
break;
}
if (buffer == null) {
if (entity != null) {
// An entity occurred, so we'll have to use StringBuffer
// (allocate room for it plus a few more entities).
buffer = new StringBuffer(str.length() + 20);
// Copy previous skipped characters and fall through
// to pickup current character
buffer.append(str.substring(0, pos));
buffer.append(entity);
}
}
else {
if (entity == null) {
buffer.append((char) ch);
}
else {
buffer.append(entity);
}
}
}

// If there were any entities, return the escaped characters
// that we put in the StringBuffer. Otherwise, just return
// the unmodified input string.
return (buffer == null) ? str : buffer.toString();
}


看以上的代码可以看到,jdom只对"\n"认为是换行,但是'\r'却不做任何处理,这一点很奇怪。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值