public static Document toDocument(StringBuffer content,String root,URL url, HTTPResponse method) throws IOException {
if(method.getStatusCode()!=200)return null;
// get type and charset
Document doc=null;
ContentType ct = method.getContentType();
long len=method.getContentLength();
String charset=ct==null?"iso-8859-1":ct.getCharset();
Runtime rt = Runtime.getRuntime();
if(len>rt.freeMemory()){
Runtime.getRuntime().gc();
if(len>rt.freeMemory()) return null;
}
//print.err("url:"+url+";chr:"+charset+";type:"+type);
if(ct==null || ct.getMimeType()==null) {}
// HTML
else if(ct.getMimeType().indexOf("text/html")!=-1) {
Reader r=null;
try{
r = IOUtil.getReader(method.getContentAsStream(), charset);
doc= HTMLDocument.getDocument(content,r);
}
finally{
IOUtil.closeEL(r);
}
}
// PDF
else if(ct.getMimeType().indexOf("application/pdf")!=-1) {
InputStream is=null;
try{
is=IOUtil.toBufferedInputStream(method.getContentAsStream());
doc= PDFDocument.getDocument(content,is);
}
finally {
IOUtil.closeEL(is);
}
}
// DOC
else if(ct.getMimeType().equals("application/msword")) {
InputStream is=null;
try{
is=IOUtil.toBufferedInputStream(method.getContentAsStream());
doc= WordDocument.getDocument(content,is);
}
finally {
IOUtil.closeEL(is);
}
}
// Plain
else if(ct.getMimeType().indexOf("text/plain")!=-1) {
Reader r=null;
try{
r=IOUtil.toBufferedReader(IOUtil.getReader(method.getContentAsStream(), charset));
doc= FileDocument.getDocument(content,r);
}