0
|
1 package de.mpiwg.anteater.xml.impl;
|
|
2
|
|
3 import java.io.File;
|
|
4 import java.io.IOException;
|
|
5 import java.io.InputStream;
|
|
6 import java.io.StringWriter;
|
|
7 import java.util.ArrayList;
|
|
8 import java.util.List;
|
|
9
|
|
10 import org.jdom2.Element;
|
|
11 import org.jdom2.output.XMLOutputter;
|
|
12
|
|
13 import de.mpiwg.anteater.xml.IXMLParser;
|
|
14
|
|
15
|
|
16 public class AnalysisXMLManager extends JDOMParser {
|
|
17
|
|
18 private String analysisFilepath;
|
|
19
|
|
20 public AnalysisXMLManager(String analysisFilepath) {
|
|
21 super();
|
|
22 this.analysisFilepath = analysisFilepath;
|
|
23
|
|
24 File analysisFile = new File(analysisFilepath);
|
|
25 if (analysisFile.exists())
|
|
26 init(analysisFilepath);
|
|
27 else {
|
|
28 InputStream stream = getClass().getResourceAsStream("templates/analysisFile.xml");
|
|
29 init(stream);
|
|
30 }
|
|
31 }
|
|
32
|
|
33 public List<String> getSummaryTexts() {
|
|
34 return getResults("/analysis/texts/summaries/summary");
|
|
35 }
|
|
36
|
|
37 public List<String> getSupplementaryInfoTexts() {
|
|
38 return getResults("/analysis/texts/supplementary_information/supplInfo");
|
|
39 }
|
|
40
|
|
41 public List<String> getSummaryNamesResults() {
|
|
42 return getResults("/analysis/scientificNames/summaries/result");
|
|
43 }
|
|
44
|
|
45 public List<String> getSupplementaryInfoNamesResults() {
|
|
46 return getResults("/analysis/scientificNames/supplementary_information/result");
|
|
47 }
|
|
48
|
2
|
49 public List<String> getSummaryCommonNamesResults() {
|
|
50 return getResults("/analysis/commonNames/summaries/result");
|
|
51 }
|
|
52
|
|
53 public List<String> getSupplementaryInfoCommonNamesResults() {
|
|
54 return getResults("/analysis/commonNames/supplementary_information/result");
|
|
55 }
|
|
56
|
0
|
57 public List<String> getSummaryPlacesResults() {
|
|
58 return getResultsViaParent("/analysis/places/summaries");
|
|
59 }
|
|
60
|
|
61 public List<String> getSupplementaryInfoPlacesResults() {
|
|
62 return getResultsViaParent("/analysis/places/supplementary_information");
|
|
63 }
|
|
64
|
|
65 public List<String> getSummaryPersonsResult() {
|
|
66 return getResults("/analysis/persons/summaries/stanford_ner");
|
|
67 }
|
|
68
|
|
69 public List<String> getSupplementaryInfoPersonsResult() {
|
|
70 return getResults("/analysis/persons/supplementary_information/stanford_ner");
|
|
71 }
|
|
72
|
|
73 public List<String> getResults(String xpath) {
|
|
74 List<Element> results = executeXPath(xpath, null);
|
|
75 if (results != null && !results.isEmpty())
|
|
76 return getResultsString(results);
|
|
77 return new ArrayList<String>();
|
|
78 }
|
|
79
|
|
80 public List<String> getResultsContents(String xpath) {
|
|
81 List<Element> results = executeXPath(xpath, null);
|
|
82 if (results != null && !results.isEmpty()) {
|
|
83 List<String> resultStrings = new ArrayList<String>();
|
|
84 for (Element elem : results) {
|
|
85 resultStrings.add(elem.getText());
|
|
86 }
|
|
87 return resultStrings;
|
|
88 }
|
|
89 return new ArrayList<String>();
|
|
90 }
|
|
91
|
|
92 public List<String> getResultsViaParent(String xpath) {
|
|
93 List<Element> results = executeXPath(xpath, null);
|
|
94 if (results != null && !results.isEmpty()) {
|
|
95 Element parent = results.get(0);
|
|
96 List<Element> children = parent.getChildren();
|
|
97 return getResultsString(children);
|
|
98 }
|
|
99 return new ArrayList<String>();
|
|
100 }
|
|
101
|
|
102 protected List<String> getResultsString(List<Element> roots) {
|
|
103 List<String> results = new ArrayList<String>();
|
|
104
|
|
105 for (Element rootNode : roots) {
|
|
106 XMLOutputter output = new XMLOutputter();
|
|
107 StringWriter writer = new StringWriter();
|
|
108 try {
|
|
109 output.output(rootNode, writer);
|
|
110 } catch (IOException e) {
|
|
111 // TODO Auto-generated catch block
|
|
112 e.printStackTrace();
|
|
113 }
|
|
114 results.add(writer.toString());
|
|
115 }
|
|
116
|
|
117 return results;
|
|
118 }
|
|
119
|
|
120 public void addSummaryNamesResult(String result) {
|
|
121 addElementToDoc(result, "/analysis/scientificNames/summaries");
|
|
122 }
|
|
123
|
|
124 public void addSupplInfNamesResult(String result) {
|
|
125 addElementToDoc(result, "/analysis/scientificNames/supplementary_information");
|
|
126 }
|
|
127
|
|
128 private void addElementToDoc(String result, String xpath) {
|
|
129 IXMLParser parser = new JDOMParser(result, false);
|
|
130 Element root = parser.getRoot();
|
|
131
|
|
132 List<Element> nodes = executeXPath(xpath, null);
|
|
133
|
|
134 if (nodes != null && nodes.size() > 0) {
|
|
135 nodes.get(0).addContent(root.clone());
|
|
136 }
|
|
137
|
|
138 save(analysisFilepath, true);
|
|
139 }
|
|
140
|
|
141 public void addSummaryPlacesResult(String result) {
|
|
142 addElementToDoc(result, "/analysis/places/summaries");
|
|
143 }
|
|
144
|
|
145 public void addSupplInfPlacesResult(String result) {
|
|
146 addElementToDoc(result, "/analysis/places/supplementary_information");
|
|
147 }
|
|
148
|
|
149 public void addSummaryPersonsResult(String result) {
|
|
150 addElementToDoc(result, "/analysis/persons/summaries");
|
|
151 }
|
|
152
|
|
153 public void addSupplInfPersonsResult(String result) {
|
|
154 addElementToDoc(result, "/analysis/persons/supplementary_information");
|
|
155 }
|
|
156
|
|
157 public void addSummaryText(String text) {
|
|
158 addElementToDoc(text, "/analysis/texts/summaries");
|
|
159 }
|
|
160
|
|
161 public void addSupplInfText(String text) {
|
|
162 addElementToDoc(text, "/analysis/texts/supplementary_information");
|
|
163 }
|
|
164 }
|