|
|
@ -11,12 +11,12 @@
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program in the file lgpl21.txt
|
|
|
|
* along with this program in the file lgpl21.txt
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
@ -34,8 +34,6 @@ import java.nio.charset.IllegalCharsetNameException;
|
|
|
|
import java.nio.charset.UnsupportedCharsetException;
|
|
|
|
import java.nio.charset.UnsupportedCharsetException;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
|
|
import com.ibm.icu.text.CharsetDetector;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import net.yacy.cora.document.MultiProtocolURI;
|
|
|
|
import net.yacy.cora.document.MultiProtocolURI;
|
|
|
|
import net.yacy.cora.protocol.ClientIdentification;
|
|
|
|
import net.yacy.cora.protocol.ClientIdentification;
|
|
|
|
import net.yacy.document.AbstractParser;
|
|
|
|
import net.yacy.document.AbstractParser;
|
|
|
@ -47,47 +45,49 @@ import net.yacy.document.parser.html.ScraperInputStream;
|
|
|
|
import net.yacy.document.parser.html.TransformerWriter;
|
|
|
|
import net.yacy.document.parser.html.TransformerWriter;
|
|
|
|
import net.yacy.kelondro.util.FileUtils;
|
|
|
|
import net.yacy.kelondro.util.FileUtils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.ibm.icu.text.CharsetDetector;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class htmlParser extends AbstractParser implements Parser {
|
|
|
|
public class htmlParser extends AbstractParser implements Parser {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Pattern patternUnderline = Pattern.compile("_");
|
|
|
|
private static final Pattern patternUnderline = Pattern.compile("_");
|
|
|
|
|
|
|
|
|
|
|
|
public htmlParser() {
|
|
|
|
public htmlParser() {
|
|
|
|
super("HTML Parser");
|
|
|
|
super("HTML Parser");
|
|
|
|
SUPPORTED_EXTENSIONS.add("htm");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("htm");
|
|
|
|
SUPPORTED_EXTENSIONS.add("html");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("html");
|
|
|
|
SUPPORTED_EXTENSIONS.add("phtml");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("phtml");
|
|
|
|
SUPPORTED_EXTENSIONS.add("shtml");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("shtml");
|
|
|
|
SUPPORTED_EXTENSIONS.add("xhtml");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("xhtml");
|
|
|
|
SUPPORTED_EXTENSIONS.add("php");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("php");
|
|
|
|
SUPPORTED_EXTENSIONS.add("php3");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("php3");
|
|
|
|
SUPPORTED_EXTENSIONS.add("php4");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("php4");
|
|
|
|
SUPPORTED_EXTENSIONS.add("php5");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("php5");
|
|
|
|
SUPPORTED_EXTENSIONS.add("cfm");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("cfm");
|
|
|
|
SUPPORTED_EXTENSIONS.add("asp");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("asp");
|
|
|
|
SUPPORTED_EXTENSIONS.add("aspx");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("aspx");
|
|
|
|
SUPPORTED_EXTENSIONS.add("tex");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("tex");
|
|
|
|
SUPPORTED_EXTENSIONS.add("txt");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("txt");
|
|
|
|
//SUPPORTED_EXTENSIONS.add("js");
|
|
|
|
//SUPPORTED_EXTENSIONS.add("js");
|
|
|
|
SUPPORTED_EXTENSIONS.add("jsp");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("jsp");
|
|
|
|
SUPPORTED_EXTENSIONS.add("mf");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("mf");
|
|
|
|
SUPPORTED_EXTENSIONS.add("pl");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("pl");
|
|
|
|
SUPPORTED_EXTENSIONS.add("py");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("py");
|
|
|
|
SUPPORTED_MIME_TYPES.add("text/html");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("text/html");
|
|
|
|
SUPPORTED_MIME_TYPES.add("text/xhtml+xml");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("text/xhtml+xml");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/xhtml+xml");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("application/xhtml+xml");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/x-httpd-php");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("application/x-httpd-php");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/x-tex");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("application/x-tex");
|
|
|
|
SUPPORTED_MIME_TYPES.add("text/plain");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("text/plain");
|
|
|
|
SUPPORTED_MIME_TYPES.add("text/sgml");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("text/sgml");
|
|
|
|
SUPPORTED_MIME_TYPES.add("text/csv");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("text/csv");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static ContentScraper parseToScraper(
|
|
|
|
public static ContentScraper parseToScraper(
|
|
|
|
final MultiProtocolURI location,
|
|
|
|
final MultiProtocolURI location,
|
|
|
|
final String documentCharset,
|
|
|
|
final String documentCharset,
|
|
|
|
InputStream sourceStream) throws Parser.Failure, IOException {
|
|
|
|
InputStream sourceStream) throws Parser.Failure, IOException {
|
|
|
|
|
|
|
|
|
|
|
|
// make a scraper
|
|
|
|
// make a scraper
|
|
|
|
String charset = null;
|
|
|
|
String charset = null;
|
|
|
|
|
|
|
|
|
|
|
@ -95,72 +95,72 @@ public class htmlParser extends AbstractParser implements Parser {
|
|
|
|
if (documentCharset != null) {
|
|
|
|
if (documentCharset != null) {
|
|
|
|
charset = patchCharsetEncoding(documentCharset);
|
|
|
|
charset = patchCharsetEncoding(documentCharset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// nothing found: try to find a meta-tag
|
|
|
|
// nothing found: try to find a meta-tag
|
|
|
|
if (charset == null) {
|
|
|
|
if (charset == null) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
final ScraperInputStream htmlFilter = new ScraperInputStream(sourceStream,documentCharset,location,null,false);
|
|
|
|
final ScraperInputStream htmlFilter = new ScraperInputStream(sourceStream,documentCharset,location,null,false);
|
|
|
|
sourceStream = htmlFilter;
|
|
|
|
sourceStream = htmlFilter;
|
|
|
|
charset = htmlFilter.detectCharset();
|
|
|
|
charset = htmlFilter.detectCharset();
|
|
|
|
} catch (IOException e1) {
|
|
|
|
} catch (final IOException e1) {
|
|
|
|
throw new Parser.Failure("Charset error:" + e1.getMessage(), location);
|
|
|
|
throw new Parser.Failure("Charset error:" + e1.getMessage(), location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// the author didn't tell us the encoding, try the mozilla-heuristic
|
|
|
|
// the author didn't tell us the encoding, try the mozilla-heuristic
|
|
|
|
if (charset == null) {
|
|
|
|
if (charset == null) {
|
|
|
|
CharsetDetector det = new CharsetDetector();
|
|
|
|
final CharsetDetector det = new CharsetDetector();
|
|
|
|
det.enableInputFilter(true);
|
|
|
|
det.enableInputFilter(true);
|
|
|
|
InputStream detStream = new BufferedInputStream(sourceStream);
|
|
|
|
final InputStream detStream = new BufferedInputStream(sourceStream);
|
|
|
|
det.setText(detStream);
|
|
|
|
det.setText(detStream);
|
|
|
|
charset = det.detect().getName();
|
|
|
|
charset = det.detect().getName();
|
|
|
|
sourceStream = detStream;
|
|
|
|
sourceStream = detStream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// wtf? still nothing, just take system-standard
|
|
|
|
// wtf? still nothing, just take system-standard
|
|
|
|
if (charset == null) {
|
|
|
|
if (charset == null) {
|
|
|
|
charset = Charset.defaultCharset().name();
|
|
|
|
charset = Charset.defaultCharset().name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Charset c;
|
|
|
|
Charset c;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
c = Charset.forName(charset);
|
|
|
|
c = Charset.forName(charset);
|
|
|
|
} catch (IllegalCharsetNameException e) {
|
|
|
|
} catch (final IllegalCharsetNameException e) {
|
|
|
|
c = Charset.defaultCharset();
|
|
|
|
c = Charset.defaultCharset();
|
|
|
|
} catch (UnsupportedCharsetException e) {
|
|
|
|
} catch (final UnsupportedCharsetException e) {
|
|
|
|
c = Charset.defaultCharset();
|
|
|
|
c = Charset.defaultCharset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// parsing the content
|
|
|
|
// parsing the content
|
|
|
|
final ContentScraper scraper = new ContentScraper(location);
|
|
|
|
final ContentScraper scraper = new ContentScraper(location);
|
|
|
|
final TransformerWriter writer = new TransformerWriter(null,null,scraper,null,false);
|
|
|
|
final TransformerWriter writer = new TransformerWriter(null,null,scraper,null,false);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
FileUtils.copy(sourceStream, writer, c);
|
|
|
|
FileUtils.copy(sourceStream, writer, c);
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (final IOException e) {
|
|
|
|
throw new Parser.Failure("IO error:" + e.getMessage(), location);
|
|
|
|
throw new Parser.Failure("IO error:" + e.getMessage(), location);
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
sourceStream.close();
|
|
|
|
sourceStream.close();
|
|
|
|
writer.close();
|
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//OutputStream hfos = new htmlFilterOutputStream(null, scraper, null, false);
|
|
|
|
//OutputStream hfos = new htmlFilterOutputStream(null, scraper, null, false);
|
|
|
|
//serverFileUtils.copy(sourceFile, hfos);
|
|
|
|
//serverFileUtils.copy(sourceFile, hfos);
|
|
|
|
//hfos.close();
|
|
|
|
//hfos.close();
|
|
|
|
if (writer.binarySuspect()) {
|
|
|
|
if (writer.binarySuspect()) {
|
|
|
|
final String errorMsg = "Binary data found in resource";
|
|
|
|
final String errorMsg = "Binary data found in resource";
|
|
|
|
throw new Parser.Failure(errorMsg, location);
|
|
|
|
throw new Parser.Failure(errorMsg, location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scraper;
|
|
|
|
return scraper;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Document[] parse(
|
|
|
|
public Document[] parse(
|
|
|
|
final MultiProtocolURI location,
|
|
|
|
final MultiProtocolURI location,
|
|
|
|
final String mimeType,
|
|
|
|
final String mimeType,
|
|
|
|
final String documentCharset,
|
|
|
|
final String documentCharset,
|
|
|
|
final InputStream sourceStream) throws Parser.Failure, InterruptedException {
|
|
|
|
final InputStream sourceStream) throws Parser.Failure, InterruptedException {
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return transformScraper(location, mimeType, documentCharset, parseToScraper(location, documentCharset, sourceStream));
|
|
|
|
return transformScraper(location, mimeType, documentCharset, parseToScraper(location, documentCharset, sourceStream));
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (final IOException e) {
|
|
|
|
throw new Parser.Failure("IOException in htmlParser: " + e.getMessage(), location);
|
|
|
|
throw new Parser.Failure("IOException in htmlParser: " + e.getMessage(), location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -197,7 +197,7 @@ public class htmlParser extends AbstractParser implements Parser {
|
|
|
|
scraper.getRSS(),
|
|
|
|
scraper.getRSS(),
|
|
|
|
scraper.getImages(),
|
|
|
|
scraper.getImages(),
|
|
|
|
scraper.indexingDenied())};
|
|
|
|
scraper.indexingDenied())};
|
|
|
|
//scraper.close();
|
|
|
|
//scraper.close();
|
|
|
|
for (final Document ppd: ppds) {
|
|
|
|
for (final Document ppd: ppds) {
|
|
|
|
ppd.setFavicon(scraper.getFavicon());
|
|
|
|
ppd.setFavicon(scraper.getFavicon());
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -214,10 +214,10 @@ public class htmlParser extends AbstractParser implements Parser {
|
|
|
|
* @return patched encoding name
|
|
|
|
* @return patched encoding name
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static String patchCharsetEncoding(String encoding) {
|
|
|
|
public static String patchCharsetEncoding(String encoding) {
|
|
|
|
|
|
|
|
|
|
|
|
// do nothing with null
|
|
|
|
// do nothing with null
|
|
|
|
if ((encoding == null) || (encoding.length() < 3)) return null;
|
|
|
|
if ((encoding == null) || (encoding.length() < 3)) return null;
|
|
|
|
|
|
|
|
|
|
|
|
// trim encoding string
|
|
|
|
// trim encoding string
|
|
|
|
encoding = encoding.trim();
|
|
|
|
encoding = encoding.trim();
|
|
|
|
|
|
|
|
|
|
|
@ -228,7 +228,7 @@ public class htmlParser extends AbstractParser implements Parser {
|
|
|
|
// all other names but such with "windows" use uppercase
|
|
|
|
// all other names but such with "windows" use uppercase
|
|
|
|
if (encoding.startsWith("WINDOWS")) encoding = "windows" + encoding.substring(7);
|
|
|
|
if (encoding.startsWith("WINDOWS")) encoding = "windows" + encoding.substring(7);
|
|
|
|
if (encoding.startsWith("MACINTOSH")) encoding = "MacRoman";
|
|
|
|
if (encoding.startsWith("MACINTOSH")) encoding = "MacRoman";
|
|
|
|
|
|
|
|
|
|
|
|
// fix wrong fill characters
|
|
|
|
// fix wrong fill characters
|
|
|
|
encoding = patternUnderline.matcher(encoding).replaceAll("-");
|
|
|
|
encoding = patternUnderline.matcher(encoding).replaceAll("-");
|
|
|
|
|
|
|
|
|
|
|
@ -236,7 +236,7 @@ public class htmlParser extends AbstractParser implements Parser {
|
|
|
|
if (encoding.matches(".*UTF[-_]?8.*")) return "UTF-8";
|
|
|
|
if (encoding.matches(".*UTF[-_]?8.*")) return "UTF-8";
|
|
|
|
if (encoding.startsWith("US")) return "US-ASCII";
|
|
|
|
if (encoding.startsWith("US")) return "US-ASCII";
|
|
|
|
if (encoding.startsWith("KOI")) return "KOI8-R";
|
|
|
|
if (encoding.startsWith("KOI")) return "KOI8-R";
|
|
|
|
|
|
|
|
|
|
|
|
// patch missing '-'
|
|
|
|
// patch missing '-'
|
|
|
|
if (encoding.startsWith("windows") && encoding.length() > 7) {
|
|
|
|
if (encoding.startsWith("windows") && encoding.length() > 7) {
|
|
|
|
final char c = encoding.charAt(7);
|
|
|
|
final char c = encoding.charAt(7);
|
|
|
@ -244,7 +244,7 @@ public class htmlParser extends AbstractParser implements Parser {
|
|
|
|
encoding = "windows-" + encoding.substring(7);
|
|
|
|
encoding = "windows-" + encoding.substring(7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (encoding.startsWith("ISO")) {
|
|
|
|
if (encoding.startsWith("ISO")) {
|
|
|
|
// patch typos
|
|
|
|
// patch typos
|
|
|
|
if (encoding.length() > 3) {
|
|
|
|
if (encoding.length() > 3) {
|
|
|
@ -256,11 +256,11 @@ public class htmlParser extends AbstractParser implements Parser {
|
|
|
|
if (encoding.length() > 8) {
|
|
|
|
if (encoding.length() > 8) {
|
|
|
|
final char c = encoding.charAt(8);
|
|
|
|
final char c = encoding.charAt(8);
|
|
|
|
if ((c >= '0') && (c <= '9')) {
|
|
|
|
if ((c >= '0') && (c <= '9')) {
|
|
|
|
encoding = encoding.substring(0, 8) + "-" + encoding.substring(8);
|
|
|
|
encoding = encoding.substring(0, 8) + "-" + encoding.substring(8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// patch wrong name
|
|
|
|
// patch wrong name
|
|
|
|
if (encoding.startsWith("ISO-8559")) {
|
|
|
|
if (encoding.startsWith("ISO-8559")) {
|
|
|
|
// popular typo
|
|
|
|
// popular typo
|
|
|
@ -279,26 +279,26 @@ public class htmlParser extends AbstractParser implements Parser {
|
|
|
|
|
|
|
|
|
|
|
|
return encoding;
|
|
|
|
return encoding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
public static void main(final String[] args) {
|
|
|
|
// test parsing of a url
|
|
|
|
// test parsing of a url
|
|
|
|
MultiProtocolURI url;
|
|
|
|
MultiProtocolURI url;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
url = new MultiProtocolURI(args[0]);
|
|
|
|
url = new MultiProtocolURI(args[0]);
|
|
|
|
byte[] content = url.get(ClientIdentification.getUserAgent(), 3000);
|
|
|
|
final byte[] content = url.get(ClientIdentification.getUserAgent(), 3000);
|
|
|
|
Document[] document = new htmlParser().parse(url, "text/html", null, new ByteArrayInputStream(content));
|
|
|
|
final Document[] document = new htmlParser().parse(url, "text/html", null, new ByteArrayInputStream(content));
|
|
|
|
String title = document[0].dc_title();
|
|
|
|
final String title = document[0].dc_title();
|
|
|
|
System.out.println(title);
|
|
|
|
System.out.println(title);
|
|
|
|
System.out.println(CharacterCoding.unicode2html(title, false));
|
|
|
|
System.out.println(CharacterCoding.unicode2html(title, false));
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
} catch (final MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (final IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (Parser.Failure e) {
|
|
|
|
} catch (final Parser.Failure e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
} catch (final InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|