Ignore chains and states default directories.
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / filters / HtmlFilter.java
1 /*
2  * Reactor - HtmlFilter.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.reactor.filters;
19
20 import static com.google.common.base.Preconditions.checkState;
21 import net.pterodactylus.reactor.Filter;
22 import net.pterodactylus.reactor.State;
23 import net.pterodactylus.reactor.states.FailedState;
24 import net.pterodactylus.reactor.states.HtmlState;
25 import net.pterodactylus.reactor.states.HttpState;
26
27 import org.jsoup.Jsoup;
28 import org.jsoup.nodes.Document;
29
30 /**
31  * {@link Filter} that converts a {@link HttpState} into an {@link HtmlState}.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class HtmlFilter implements Filter {
36
37         /**
38          * {@inheritDoc}
39          */
40         @Override
41         public State filter(State state) {
42                 if (!state.success()) {
43                         return FailedState.from(state);
44                 }
45                 checkState(state instanceof HttpState, "state is not a HttpState but a %s", state.getClass().getName());
46                 Document document = Jsoup.parse(((HttpState) state).content(), ((HttpState) state).uri());
47                 return new HtmlState(((HttpState) state).uri(), document);
48         }
49
50 }