<commit_before>/**   
 * License Agreement for OpenSearchServer
 *
 * Copyright (C) 2012-2013 <NAME> / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of OpenSearchServer.
 *
 * OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * OpenSearchServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenSearchServer. 
 *  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.jaeksoft.searchlib.parser.htmlParser;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Collection;

import javax.xml.parsers.ParserConfigurationException;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.DomSerializer;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.SimpleHtmlSerializer;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.jaeksoft.searchlib.Logging;

public class HtmlCleanerParser extends HtmlDocumentProvider {

	private final HtmlCleaner cleaner;

	private TagNode rootTagNode = null;

	private String charsetCache = null;

	public HtmlCleanerParser() {
		super(HtmlParserEnum.HtmlCleanerParser);
		cleaner = new HtmlCleaner();
		CleanerProperties props = cleaner.getProperties();
		props.setNamespacesAware(true);
	}

	@Override
	protected HtmlNodeAbstract<?> getDocument(String charset,
			InputStream inputStream) throws SAXException, IOException,
			ParserConfigurationException {
		rootTagNode = cleaner.clean(inputStream, charset);
		charsetCache = null;
		return getDomHtmlNode();
	}

	@Override
	protected HtmlNodeAbstract<?> getDocument(String pageSource)
			throws IOException, ParserConfigurationException {
		rootTagNode = cleaner.clean(new StringReader(pageSource));
		charsetCache = null;
		return getDomHtmlNode();
	}

	private DomHtmlNode getDomHtmlNode() throws ParserConfigurationException {
		Document document = new DomSerializer(cleaner.getProperties(), true)
				.createDOM(rootTagNode);
		return new DomHtmlNode(document);
	}

	public String findCharset() {
		if (charsetCache != null)
			return charsetCache;
		String charsetCache = getMetaCharset();
		if (charsetCache == null)
			return null;
		try {
			Charset.forName(charsetCache);
			return charsetCache;
		} catch (UnsupportedCharsetException e1) {
			try {
				charsetCache = charsetCache.toUpperCase();
				Charset.forName(charsetCache);
				return charsetCache;
			} catch (UnsupportedCharsetException e2) {
				Logging.warn(e2);
				charsetCache = null;
				return null;
			}<commit_msg>Fix #739. HTMLCleaner does not preserve the lang attribute on DOM serialization.<commit_after>/**   
 * License Agreement for OpenSearchServer
 *
 * Copyright (C) 2012-2013 <NAME> / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of OpenSearchServer.
 *
 * OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * OpenSearchServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenSearchServer. 
 *  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.jaeksoft.searchlib.parser.htmlParser;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Collection;

import javax.xml.parsers.ParserConfigurationException;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.DomSerializer;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.SimpleHtmlSerializer;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.jaeksoft.searchlib.Logging;

public class HtmlCleanerParser extends HtmlDocumentProvider {

	private final HtmlCleaner cleaner;

	private TagNode rootTagNode = null;

	private String charsetCache = null;

	public HtmlCleanerParser() {
		super(HtmlParserEnum.HtmlCleanerParser);
		cleaner = new HtmlCleaner();
		CleanerProperties props = cleaner.getProperties();
		props.setNamespacesAware(true);
	}

	@Override
	protected HtmlNodeAbstract<?> getDocument(String charset,
			InputStream inputStream) throws SAXException, IOException,
			ParserConfigurationException {
		rootTagNode = cleaner.clean(inputStream, charset);
		charsetCache = null;
		return getDomHtmlNode();
	}

	@Override
	protected HtmlNodeAbstract<?> getDocument(String pageSource)
			throws IOException, ParserConfigurationException {
		rootTagNode = cleaner.clean(new StringReader(pageSource));
		charsetCache = null;
		return getDomHtmlNode();
	}

	private DomHtmlNode getDomHtmlNode() throws ParserConfigurationException {
		Document document = new DomSerializer(cleaner.getProperties(), true)
				.createDOM(rootTagNode);
		String lang = rootTagNode.getAttributeByName("lang");
		if (lang != null)
			document.getDocumentElement().setAttribute("lang", lang);
		return new DomHtmlNode(document);
	}

	public String findCharset() {
		if (charsetCache != null)
			return charsetCache;
		String charsetCache = getMetaCharset();
		if (charsetCache == null)
			return null;
		try {
			Charset.forName(charsetCache);
			return charsetCache;
		} catch (UnsupportedCharsetException e1) {
			try {
				charsetCache = charsetCache.toUpperCase();
				Charset.forName(charsetCache);
				return charsetCache;
			} catch (UnsupportedCharsetException e2) {
				Logging.warn(e2);
				charsetCache = null;
				return null;
			}
{'fraction_non_alphanumeric': 0.08545176110260337, 'fraction_numerical': 0.00444104134762634, 'mean_word_length': 4.820855614973262, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1921489', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1841, 'n_words': 539}