我有java程序,它将从服务器接收纯文本。纯文本可能包含URL。 Java库中是否有任何类将纯文本转换为HTML文本?还是其他任何图书馆?如果没有那么解决方案是什么?
答案 0 :(得分:22)
您应该以编程方式对文本进行一些替换。以下是一些线索:
"<br>\n"
(\ n为了更好的输出可读性)。" "
"<"
"&"
"&#"+((int)myChar)+";"
,以便在每次编码时都可读。"http://[^ ]+"
或"www.[^ ]"
这样的正则表达式,然后像 JB Nizet 那样转换它们。至"<a href=\""+url+"\">"+url+"</a>"
,但仅在完成所有其他替换后。执行此操作的代码如下所示:
public static String escape(String s) {
StringBuilder builder = new StringBuilder();
boolean previousWasASpace = false;
for( char c : s.toCharArray() ) {
if( c == ' ' ) {
if( previousWasASpace ) {
builder.append(" ");
previousWasASpace = false;
continue;
}
previousWasASpace = true;
} else {
previousWasASpace = false;
}
switch(c) {
case '<': builder.append("<"); break;
case '>': builder.append(">"); break;
case '&': builder.append("&"); break;
case '"': builder.append("""); break;
case '\n': builder.append("<br>"); break;
// We need Tab support here, because we print StackTraces as HTML
case '\t': builder.append(" "); break;
default:
if( c < 128 ) {
builder.append(c);
} else {
builder.append("&#").append((int)c).append(";");
}
}
}
return builder.toString();
}
但是,尚未添加链接转换。如果有人这样做,请更新代码。
答案 1 :(得分:7)
我找到了使用模式匹配的解决方案。这是我的代码 -
String str = "(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'\".,<>?«»“”‘’]))";
Pattern patt = Pattern.compile(str);
Matcher matcher = patt.matcher(plain);
plain = matcher.replaceAll("<a href=\"$1\">$1</a>");
以下是输入和输出 -
输入文字是变量plain
:
some text and then the URL http://www.google.com and then some other text.
输出:
some text and then the URL <a href="http://www.google.com">http://www.google.com</a> and then some other text.
答案 2 :(得分:3)
如果您的纯文本 URL (与包含 hyperlink不同,正如您在问题中写的那样),然后将其转换为HTML中的超链接完全由
完成String hyperlink = "<a href='" + url + "'>" + url + "</a>";
答案 3 :(得分:2)
使用此
public static String stringToHTMLString(String string) { StringBuffer sb = new StringBuffer(string.length()); // true if last char was blank boolean lastWasBlankChar = false; int len = string.length(); char c; for (int i = 0; i < len; i++) { c = string.charAt(i); if (c == ' ') { // blank gets extra work, // this solves the problem you get if you replace all // blanks with , if you do that you loss // word breaking if (lastWasBlankChar) { lastWasBlankChar = false; sb.append(" "); } else { lastWasBlankChar = true; sb.append(' '); } } else { lastWasBlankChar = false; // // HTML Special Chars if (c == '"') sb.append("""); else if (c == '&') sb.append("&"); else if (c == '<') sb.append("<"); else if (c == '>') sb.append(">"); else if (c == '\n') // Handle Newline sb.append("<br/>"); else { int ci = 0xffff & c; if (ci < 160) // nothing special only 7 Bit sb.append(c); else { // Not 7 Bit use the unicode system sb.append("&#"); sb.append(new Integer(ci).toString()); sb.append(';'); } } } } return sb.toString(); }
答案 4 :(得分:1)
刚加入所有答案的编码:
private static String txtToHtml(String s) {
StringBuilder builder = new StringBuilder();
boolean previousWasASpace = false;
for (char c : s.toCharArray()) {
if (c == ' ') {
if (previousWasASpace) {
builder.append(" ");
previousWasASpace = false;
continue;
}
previousWasASpace = true;
} else {
previousWasASpace = false;
}
switch (c) {
case '<':
builder.append("<");
break;
case '>':
builder.append(">");
break;
case '&':
builder.append("&");
break;
case '"':
builder.append(""");
break;
case '\n':
builder.append("<br>");
break;
// We need Tab support here, because we print StackTraces as HTML
case '\t':
builder.append(" ");
break;
default:
builder.append(c);
}
}
String converted = builder.toString();
String str = "(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'\".,<>?«»“”‘’]))";
Pattern patt = Pattern.compile(str);
Matcher matcher = patt.matcher(converted);
converted = matcher.replaceAll("<a href=\"$1\">$1</a>");
return converted;
}
答案 5 :(得分:1)
在Android应用程序中,我刚刚实现了内容的HTML化(请参阅https://github.com/andstatus/andstatus/issues/375)。实际转换是使用Android系统库在文学3行代码中完成的。这样可以在每个后续版本的Android库中使用更好的实现。
private static String htmlifyPlain(String textIn) {
SpannableString spannable = SpannableString.valueOf(textIn);
Linkify.addLinks(spannable, Linkify.WEB_URLS);
return Html.toHtml(spannable);
}