|
| 1 | +/** |
| 2 | + * Copyright 2005 The Apache Software Foundation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.apache.nutch.parse; |
| 17 | + |
| 18 | +// JDK imports |
| 19 | +import java.util.logging.Logger; |
| 20 | + |
| 21 | +// Nutch Imports |
| 22 | +import org.apache.nutch.protocol.Content; |
| 23 | +import org.apache.nutch.util.LogFormatter; |
| 24 | + |
| 25 | + |
| 26 | +/** |
| 27 | + * A Utility class containing methods to simply perform parsing utilities such |
| 28 | + * as iterating through a preferred list of {@link Parser}s to obtain |
| 29 | + * {@link Parse} objects. |
| 30 | + * |
| 31 | + * @author mattmann |
| 32 | + * @author Jérôme Charron |
| 33 | + * @author Sébastien Le Callonnec |
| 34 | + */ |
| 35 | +public class ParseUtil { |
| 36 | + |
| 37 | + /* our log stream */ |
| 38 | + public static final Logger LOG = LogFormatter.getLogger(ParseUtil.class |
| 39 | + .getName()); |
| 40 | + |
| 41 | + /** No public constructor */ |
| 42 | + private ParseUtil() { } |
| 43 | + |
| 44 | + /** |
| 45 | + * Performs a parse by iterating through a List of preferred {@Parser}s |
| 46 | + * until a successful parse is performed and a {@link Parse} object is |
| 47 | + * returned. If the parse is unsuccessful, a message is logged to the |
| 48 | + * <code>WARNING</code> level, and an empty parse is returned. |
| 49 | + * |
| 50 | + * @param content The content to try and parse. |
| 51 | + * @return A {@link Parse} object containing the parsed data. |
| 52 | + * @throws ParseException If no suitable parser is found to perform the parse. |
| 53 | + */ |
| 54 | + public final static Parse parse(Content content) throws ParseException { |
| 55 | + Parser[] parsers = null; |
| 56 | + |
| 57 | + try { |
| 58 | + parsers = ParserFactory.getParsers(content.getContentType(), ""); |
| 59 | + } catch (ParserNotFound e) { |
| 60 | + LOG.warning("No suitable parser found when trying to parse content " + |
| 61 | + content); |
| 62 | + throw new ParseException(e.getMessage()); |
| 63 | + } |
| 64 | + |
| 65 | + Parse parse = null; |
| 66 | + for (int i=0; i<parsers.length; i++) { |
| 67 | + parse = parsers[i].getParse(content); |
| 68 | + if ((parse != null) && (parse.getData().getStatus().isSuccess())) { |
| 69 | + return parse; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + LOG.warning("Unable to successfully parse content " + content.getUrl() + |
| 74 | + " of type " + content.getContentType()); |
| 75 | + |
| 76 | + return new ParseStatus().getEmptyParse(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Method parses a {@link Content} object using the {@link Parser} specified |
| 81 | + * by the parameter <code>parserId</code>. If a suitable {@link Parser} is not |
| 82 | + * found, then a <code>WARNING</code> level message is logged, and a |
| 83 | + * ParseException is thrown. |
| 84 | + * If the parse is uncessful for any other reason, then a <code>WARNING</code> |
| 85 | + * level message is logged, and a <code>ParseStatus.getEmptyParse() is |
| 86 | + * returned. |
| 87 | + * |
| 88 | + * @param parserId The ID of the {@link Parser} to use to parse the specified |
| 89 | + * content. |
| 90 | + * @param content The content to parse. |
| 91 | + * @return A {@link Parse} object if the parse is successful, otherwise, |
| 92 | + * a <code>ParseStatus.getEmptyParse()</code>. |
| 93 | + * @throws ParseException If there is no suitable {@link Parser} found |
| 94 | + * to perform the parse. |
| 95 | + */ |
| 96 | + public final static Parse parseByParserId(String parserId, Content content) |
| 97 | + throws ParseException { |
| 98 | + Parse parse = null; |
| 99 | + Parser p = null; |
| 100 | + |
| 101 | + try { |
| 102 | + p = ParserFactory.getParserById(parserId); |
| 103 | + } catch (ParserNotFound e) { |
| 104 | + LOG.warning("No suitable parser found when trying to parse content " + |
| 105 | + content); |
| 106 | + throw new ParseException(e.getMessage()); |
| 107 | + } |
| 108 | + |
| 109 | + parse = p.getParse(content); |
| 110 | + |
| 111 | + if (parse != null && parse.getData().getStatus().isSuccess()) { |
| 112 | + return parse; |
| 113 | + } else { |
| 114 | + LOG.warning("Unable to successfully parse content " + content.getUrl() + |
| 115 | + " of type " + content.getContentType()); |
| 116 | + return new ParseStatus().getEmptyParse(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments