generating html links from plain text
How can I scan a text (a String) and add links when a word is an URL?
I tried the following which works but it's too slow:
String text = rs.getString("text");
text2scan=text.split("[\n ]")
for(int i = 0; i < text2scan.length; i++){
String replacement =null;
String word = text2scan[i];
log.info(word);
if(word.indexOf(".com")!=-1){
replacement="<a href=\""+word+"\">"+word+"</a>";
text=text.replaceAll(word, replacement);
text=text.replaceAll("www","http://www");
text=text.replaceAll("http://http://","http://");
text=text.replaceAll(">http://",">");
}
}
Isn't this already implemented somewhere?
Thanks
Niklas
[1290 byte] By [
Niklasa] at [2007-11-26 15:50:12]

# 3
Because I was semi interested by this issue, and how best to solve it, heres a quick and dirty example program.
It does a very basic job, but should be pretty much equivalent to what you had originally.
package mypackage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpMakeHyperlinks {
/**
* Loads a file into a StringBuffer
* @param f File to read
* @return StringBuffer with contents of text file.
* @throws IOException
*/
public StringBuffer loadFile(File f) throws IOException {
System.out.println("Reading file " + f.getAbsolutePath());
BufferedReader in = new BufferedReader(new FileReader(f));
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(new BufferedWriter(sw));
String line = null;
while ((line = in.readLine()) != null) {
pw.println(line);
}
pw.flush();
in.close();
return sw.getBuffer();
}
/**
* Writes the contents of a StringBuffer to file
* @param f File to write to
* @param sb Text to write to file
* @throws IOException
*/
public void writeToFile(File f, StringBuffer sb) throws IOException{
System.out.println("Outputting to " + f.getAbsolutePath());
BufferedReader in = new BufferedReader(new StringReader(sb.toString()));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
String line = null;
while ((line = in.readLine()) != null) {
out.println(line);
}
out.flush();
in.close();
}
/**
* Converts all urls like "www.google.com" into hyperlinks in the text.
*
* @param initialText The text to convert
* @return Converted text.
*/
public StringBuffer convertText(StringBuffer initialText) {
StringBuffer result = new StringBuffer(initialText.length());
Pattern p = Pattern.compile("(href=\")?(http://)?www\\..*?.com\\b");
Matcher m = p.matcher(initialText);
while (m.find()) {
String href = m.group();
// ignore links that are already hyperlinks
if (href.startsWith("href")){
continue;
}
// add on the http:// if necessary
if (!href.startsWith("http://")) {
href = "http://" + href;
}
// add the new text into the output.
m.appendReplacement(result, "<a href=\"" + href + "\">" + href + "</a>");
}
m.appendTail(result);
return result;
}
/**
* Testing method for experimenting with the patternmatching
* @param text
*/
public void matchLink(String text) {
System.out.println("Analyzing text " + text);
Pattern p = Pattern.compile("(http://)?www\\..*?.com\\b");
Matcher m = p.matcher(text);
while (m.find()) {
System.out.println("Found: " + m.group());
}
}
public static void main(String[] args) {
RegexpMakeHyperlinks hyper = new RegexpMakeHyperlinks();
String[] testStrings = { "www.google.com", "www.myspace.net", "make sure you visit www.yahoo.com for details", "Two sites: http://www.java.com and http://www.javabeans.com" };
for (int i = 0; i < testStrings.length; i++) {
hyper.matchLink(testStrings[i]);
}
System.out.println();
System.out.println("-");
System.out.println();
File inFile = new File("makeHyperlinkTest.txt");
File outFile = new File("makeHyperlinkTest.output");
try{
StringBuffer sb = hyper.loadFile(inFile);
StringBuffer result = hyper.convertText(sb);
hyper.writeToFile(outFile, result);
}
catch(Exception e){
System.out.println("Error " + e.getMessage());
e.printStackTrace(System.out);
}
}
}
And a test file makeHyperlinkTest.txt
This file just contains plain text with hyperlinks to places like www.google.com and http://www.yahoo.com.
The program I have written will convert these links like www.google.com into hyperlinks for displaying as html.
Make sure you visit http://www.java.sun.com and read all about Java.
And if I already have an <a href="www.google.com">Hyperlink</a> in this file it should leave it alone right?