/* Groovy Class #* */
import java.io.IOException;
import java.io.StringReader;
import com.xpn.xwiki.web.Utils;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xwiki.xml.XMLReaderFactory;
import org.xwiki.xml.EntityResolver;
public class TextExtractHandler extends DefaultHandler
{
static StringBuilder textResult = new StringBuilder();
public void characters(char[] ch, int start, int length)
{
textResult.append(ch, start, length);
}
public String getCleanText(String txt)
{
String res = "";
try {
textResult = new StringBuilder();
XMLReader myReader = Utils.getComponent(XMLReaderFactory.class).createXMLReader();
DefaultHandler myHandler = new TextExtractHandler();
myReader.setContentHandler(myHandler);
myReader.setErrorHandler(myHandler);
myReader.setEntityResolver(Utils.getComponent(EntityResolver.class));
InputSource inputSource = new InputSource(new StringReader(txt));
myReader.parse(inputSource);
return textResult.toString();
} catch (Exception e) {
e.printStackTrace();
res += e;
}
return res;
}
}
/* *# */