| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.apache.nutch.parsefilter.debug; |
| |
| import java.io.ByteArrayOutputStream; |
| import java.io.OutputStreamWriter; |
| import java.lang.invoke.MethodHandles; |
| import java.nio.charset.Charset; |
| import java.nio.charset.StandardCharsets; |
| |
| import org.apache.hadoop.conf.Configuration; |
| import org.apache.nutch.metadata.Metadata; |
| import org.apache.nutch.parse.HTMLMetaTags; |
| import org.apache.nutch.parse.HtmlParseFilter; |
| import org.apache.nutch.parse.Parse; |
| import org.apache.nutch.parse.ParseResult; |
| import org.apache.nutch.protocol.Content; |
| import org.apache.nutch.util.DomUtil; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| import org.w3c.dom.DocumentFragment; |
| |
| /** |
| * Adds serialized DOM to parse data, useful for debugging, to understand how |
| * the parser implementation interprets a document (not only HTML). |
| */ |
| public class DebugParseFilter implements HtmlParseFilter { |
| |
| private static final Logger LOG = LoggerFactory |
| .getLogger(MethodHandles.lookup().lookupClass()); |
| |
| private Configuration conf; |
| |
| @Override |
| public ParseResult filter(Content content, ParseResult parseResult, HTMLMetaTags metaTags, DocumentFragment doc) { |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| DomUtil.saveDom(baos, doc); |
| Parse parse = parseResult.get(content.getUrl()); |
| String dom = new String(baos.toByteArray(), StandardCharsets.UTF_8); |
| LOG.debug(dom); |
| parse.getData().getParseMeta().set("DOM", dom); |
| return parseResult; |
| } |
| |
| @Override |
| public void setConf(Configuration conf) { |
| this.conf = conf; |
| } |
| |
| @Override |
| public Configuration getConf() { |
| return conf; |
| } |
| } |