Mercurial > hg > anteater
comparison src/de/mpiwg/anteater/xml/impl/AnalysisXMLManager.java @ 0:036535fcd179
anteater
author | jdamerow |
---|---|
date | Fri, 14 Sep 2012 10:30:43 +0200 |
parents | |
children | 1c2b4f5e2c05 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:036535fcd179 |
---|---|
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 | |
49 public List<String> getSummaryPlacesResults() { | |
50 return getResultsViaParent("/analysis/places/summaries"); | |
51 } | |
52 | |
53 public List<String> getSupplementaryInfoPlacesResults() { | |
54 return getResultsViaParent("/analysis/places/supplementary_information"); | |
55 } | |
56 | |
57 public List<String> getSummaryPersonsResult() { | |
58 return getResults("/analysis/persons/summaries/stanford_ner"); | |
59 } | |
60 | |
61 public List<String> getSupplementaryInfoPersonsResult() { | |
62 return getResults("/analysis/persons/supplementary_information/stanford_ner"); | |
63 } | |
64 | |
65 public List<String> getResults(String xpath) { | |
66 List<Element> results = executeXPath(xpath, null); | |
67 if (results != null && !results.isEmpty()) | |
68 return getResultsString(results); | |
69 return new ArrayList<String>(); | |
70 } | |
71 | |
72 public List<String> getResultsContents(String xpath) { | |
73 List<Element> results = executeXPath(xpath, null); | |
74 if (results != null && !results.isEmpty()) { | |
75 List<String> resultStrings = new ArrayList<String>(); | |
76 for (Element elem : results) { | |
77 resultStrings.add(elem.getText()); | |
78 } | |
79 return resultStrings; | |
80 } | |
81 return new ArrayList<String>(); | |
82 } | |
83 | |
84 public List<String> getResultsViaParent(String xpath) { | |
85 List<Element> results = executeXPath(xpath, null); | |
86 if (results != null && !results.isEmpty()) { | |
87 Element parent = results.get(0); | |
88 List<Element> children = parent.getChildren(); | |
89 return getResultsString(children); | |
90 } | |
91 return new ArrayList<String>(); | |
92 } | |
93 | |
94 protected List<String> getResultsString(List<Element> roots) { | |
95 List<String> results = new ArrayList<String>(); | |
96 | |
97 for (Element rootNode : roots) { | |
98 XMLOutputter output = new XMLOutputter(); | |
99 StringWriter writer = new StringWriter(); | |
100 try { | |
101 output.output(rootNode, writer); | |
102 } catch (IOException e) { | |
103 // TODO Auto-generated catch block | |
104 e.printStackTrace(); | |
105 } | |
106 results.add(writer.toString()); | |
107 } | |
108 | |
109 return results; | |
110 } | |
111 | |
112 public void addSummaryNamesResult(String result) { | |
113 addElementToDoc(result, "/analysis/scientificNames/summaries"); | |
114 } | |
115 | |
116 public void addSupplInfNamesResult(String result) { | |
117 addElementToDoc(result, "/analysis/scientificNames/supplementary_information"); | |
118 } | |
119 | |
120 private void addElementToDoc(String result, String xpath) { | |
121 IXMLParser parser = new JDOMParser(result, false); | |
122 Element root = parser.getRoot(); | |
123 | |
124 List<Element> nodes = executeXPath(xpath, null); | |
125 | |
126 if (nodes != null && nodes.size() > 0) { | |
127 nodes.get(0).addContent(root.clone()); | |
128 } | |
129 | |
130 save(analysisFilepath, true); | |
131 } | |
132 | |
133 public void addSummaryPlacesResult(String result) { | |
134 addElementToDoc(result, "/analysis/places/summaries"); | |
135 } | |
136 | |
137 public void addSupplInfPlacesResult(String result) { | |
138 addElementToDoc(result, "/analysis/places/supplementary_information"); | |
139 } | |
140 | |
141 public void addSummaryPersonsResult(String result) { | |
142 addElementToDoc(result, "/analysis/persons/summaries"); | |
143 } | |
144 | |
145 public void addSupplInfPersonsResult(String result) { | |
146 addElementToDoc(result, "/analysis/persons/supplementary_information"); | |
147 } | |
148 | |
149 public void addSummaryText(String text) { | |
150 addElementToDoc(text, "/analysis/texts/summaries"); | |
151 } | |
152 | |
153 public void addSupplInfText(String text) { | |
154 addElementToDoc(text, "/analysis/texts/supplementary_information"); | |
155 } | |
156 } |