Skip to content

Commit 3447246

Browse files
committed
Add ScriptExtractor with extract-script Flux command
See #312 See hbz/oerindex#3
1 parent 47a5ba7 commit 3447246

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2020 Fabian Steeg, hbz
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.metafacture.html;
17+
18+
import java.io.IOException;
19+
import java.io.Reader;
20+
21+
import org.apache.commons.io.IOUtils;
22+
import org.jsoup.Jsoup;
23+
import org.jsoup.nodes.Document;
24+
import org.jsoup.nodes.Element;
25+
import org.metafacture.framework.FluxCommand;
26+
import org.metafacture.framework.ObjectReceiver;
27+
import org.metafacture.framework.annotations.Description;
28+
import org.metafacture.framework.annotations.In;
29+
import org.metafacture.framework.annotations.Out;
30+
import org.metafacture.framework.helpers.DefaultObjectPipe;
31+
32+
/**
33+
* Extracts the first script from an HTML document
34+
*
35+
* @author Fabian Steeg
36+
*/
37+
@Description("Extracts the first script from an HTML document")
38+
@In(Reader.class)
39+
@Out(String.class)
40+
@FluxCommand("extract-script")
41+
public class ScriptExtractor extends DefaultObjectPipe<Reader, ObjectReceiver<String>> {
42+
@Override
43+
public void process(final Reader reader) {
44+
try {
45+
Document document = Jsoup.parse(IOUtils.toString(reader));
46+
Element firstScript = document.select("script").first();
47+
getReceiver().process(firstScript.data());
48+
} catch (IOException e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
}

metafacture-html/src/main/resources/flux-commands.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
#
1616
html-to-xml org.metafacture.html.HtmlReader
1717
decode-html org.metafacture.html.HtmlDecoder
18+
extract-script org.metafacture.html.ScriptExtractor
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2020 Fabian Steeg, hbz
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.metafacture.html;
17+
18+
import static org.mockito.Mockito.verify;
19+
import static org.mockito.Mockito.verifyNoMoreInteractions;
20+
21+
import java.io.StringReader;
22+
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.metafacture.framework.ObjectReceiver;
27+
import org.mockito.Mock;
28+
import org.mockito.MockitoAnnotations;
29+
30+
/**
31+
* Tests for {@link ScriptExtractor}.
32+
*
33+
* @author Fabian Steeg
34+
*
35+
*/
36+
public final class ScriptExtractorTest {
37+
38+
private static final StringReader IN = new StringReader("<html><script>{\"code\":\"yo\"}");
39+
private static final String OUT = "{\"code\":\"yo\"}";
40+
41+
private ScriptExtractor scriptExtractor;
42+
43+
@Mock
44+
private ObjectReceiver<String> receiver;
45+
46+
@Before
47+
public void setup() {
48+
MockitoAnnotations.initMocks(this);
49+
scriptExtractor = new ScriptExtractor();
50+
scriptExtractor.setReceiver(receiver);
51+
}
52+
53+
@Test
54+
public void testShouldProcessRecordsFollowedbySeparator() {
55+
scriptExtractor.process(IN);
56+
verify(receiver).process(OUT);
57+
verifyNoMoreInteractions(receiver);
58+
}
59+
60+
@After
61+
public void cleanup() {
62+
scriptExtractor.closeStream();
63+
}
64+
}

0 commit comments

Comments
 (0)