1 | package org.greenstone.atea;
|
---|
2 |
|
---|
3 | import java.io.*;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Map;
|
---|
6 |
|
---|
7 | public class TextDumpPage {
|
---|
8 |
|
---|
9 | public static final String TEXT_START_MARKER="text:start:";
|
---|
10 | public static final String TEXT_END_MARKER="text:end:";
|
---|
11 |
|
---|
12 | private Map tuples;
|
---|
13 |
|
---|
14 | public TextDumpPage(String siteID, String unparsedPageDump) {
|
---|
15 | tuples = new HashMap();
|
---|
16 |
|
---|
17 | try (
|
---|
18 | BufferedReader reader = new BufferedReader(new StringReader(unparsedPageDump));
|
---|
19 | ) {
|
---|
20 |
|
---|
21 | String line = reader.readLine(); // should have at least first line
|
---|
22 |
|
---|
23 | // first line always has a "key:" somewhere after the pageURL
|
---|
24 | int endIndex = line.indexOf("key:");
|
---|
25 | String pageURL = line.substring(endIndex);
|
---|
26 |
|
---|
27 | tuples.add("pageURL", pageURL.trim());
|
---|
28 |
|
---|
29 | String key = line.substring(endIndex);
|
---|
30 | tuples.add("key", key.trim());
|
---|
31 |
|
---|
32 | boolean readingPageText = false;
|
---|
33 | StringBuilder pageText = null;
|
---|
34 |
|
---|
35 | // continue reading all other tuples for this page, if any
|
---|
36 | while((line = reader.readLine()) != null) {
|
---|
37 |
|
---|
38 | if(!readingPageText) {
|
---|
39 | // check if we're dealing with metadata or start/end of pagetext
|
---|
40 | endIndex = line.indexOf(":");
|
---|
41 | if(endIndex != -1) { // dealing with the rest of the page dump's metadata
|
---|
42 | String k = line.substring(0, endIndex);
|
---|
43 | String v = line.substring(endIndex+1);
|
---|
44 | tuples.add(k.trim(), v.trim());
|
---|
45 | }
|
---|
46 |
|
---|
47 | else if(line.equals(TEXT_START_MARKER)) { // dealing with the page body text
|
---|
48 | StringBuilder pageText = new StringBuilder();
|
---|
49 | readingPageText = true;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | else { // we're reading in the page text
|
---|
54 |
|
---|
55 | if(line.equals(TEXT_END_MARKER)) {
|
---|
56 | // finished with a page body
|
---|
57 | // remove any FINAL artificial newline we introduced
|
---|
58 | tuples.add("pageText", pageText.toString().trim());
|
---|
59 | readingPageText = false;
|
---|
60 | pageText = null;
|
---|
61 | }
|
---|
62 | else {
|
---|
63 | pageText.append(line);
|
---|
64 | pageText.append("\n"); // there are no newlines within pageText
|
---|
65 | // but if there were newlines, add them back here as readLine() removes them
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | // If the page had no pageText, add a "pageText" -> "" mapping
|
---|
72 | if(!tuples.containsKey("pageText")) {
|
---|
73 | tuples.add("pageText", "");
|
---|
74 | }
|
---|
75 |
|
---|
76 | } catch (IOException ioe) {
|
---|
77 | error("@@@@@@@@@ Error reading in txtdump of a page.", ioe);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | public String getPageURL() {
|
---|
83 | return tuples.get("url");
|
---|
84 | }
|
---|
85 |
|
---|
86 | public String getPageText() {
|
---|
87 | return tuples.get("pageText");
|
---|
88 | }
|
---|
89 |
|
---|
90 | public String get(String key) {
|
---|
91 | return tuples.get(key);
|
---|
92 | }
|
---|
93 | }
|
---|