1 package de.uni_hamburg.corpora.conversion;
3 import com.opencsv.CSVReader;
8 import org.exmaralda.partitureditor.jexmaralda.JexmaraldaException;
9 import org.xml.sax.SAXException;
10 import java.io.BufferedWriter;
11 import java.io.FileNotFoundException;
12 import java.io.FileOutputStream;
13 import java.io.FileReader;
14 import java.io.IOException;
15 import java.io.OutputStreamWriter;
16 import java.io.Writer;
17 import java.net.URISyntaxException;
18 import java.security.NoSuchAlgorithmException;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.List;
22 import javax.xml.parsers.ParserConfigurationException;
23 import javax.xml.transform.TransformerException;
24 import javax.xml.xpath.XPathExpressionException;
25 import org.exmaralda.partitureditor.fsm.FSMException;
26 import org.jdom.Document;
27 import org.jdom.Element;
28 import org.jdom.JDOMException;
29 import org.jdom.output.XMLOutputter;
30 import org.jdom.xpath.XPath;
43 private String comaFile;
44 private String csvFile;
45 private Document coma;
46 private String SpeakerOrCommunication;
47 private Boolean IsSpeaker;
62 this.comaFile = corpusPath;
63 this.csvFile = csvPath;
64 this.SpeakerOrCommunication = SpeakerOrCommunication;
65 if (SpeakerOrCommunication.equals(
"speaker")) {
68 if (SpeakerOrCommunication.equals(
"communication")) {
82 }
catch (ParserConfigurationException pce) {
83 stats.
addException(pce,
function, cd,
"Unknown parsing error");
84 }
catch (IOException ioe) {
85 stats.
addException(ioe,
function, cd,
"Unknown file reading error");
86 }
catch (JDOMException ex) {
87 stats.
addException(ex,
function, cd,
"Unknown JDOM error");
97 throws SAXException, IOException, ParserConfigurationException, JexmaraldaException, JDOMException {
99 this.comaFile = cd.
getURL().getPath();
100 List<String[]> allElements =
readData();
102 for (String[] row : allElements) {
103 System.out.println(Arrays.toString(row));
104 stats.
addNote(
function, cd, Arrays.toString(row));
106 System.out.println(Arrays.toString(allElements.get(0)));
107 stats.
addNote(
function, cd, Arrays.toString(allElements.get(0)));
108 System.out.println(allElements.get(0)[0]);
109 stats.
addNote(
function, cd, allElements.get(0)[0]);
111 coma = org.exmaralda.common.jdomutilities.IOUtilities.readDocumentFromLocalFile(comaFile);
113 for (
int i = 1; i < allElements.size(); i++) {
114 for (
int a = 1; a < allElements.get(i).length; a++) {
117 String place =
"//Speaker[Sigle/text()=\"" + allElements.get(i)[0] +
"\"]/Description";
118 System.out.println(place);
119 stats.
addNote(
function, cd, place);
120 XPath p = XPath.newInstance(place);
122 Object o = p.selectSingleNode(coma);
124 Element desc = (Element) o;
126 Element key =
new Element(
"Key");
127 desc.addContent(key);
128 key.setAttribute(
"Name", allElements.get(0)[a]);
129 System.out.println(desc.getAttributes());
130 stats.
addNote(
function, cd, Arrays.toString(desc.getAttributes().toArray()));
131 key.setText(allElements.get(i)[a]);
136 String place =
"//Communication[@Name=\"" + allElements.get(i)[0] +
"\"]/Description";
137 System.out.println(place);
138 stats.
addNote(
function, cd, place);
139 XPath p = XPath.newInstance(place);
140 System.out.println(p.selectSingleNode(coma));
141 stats.
addNote(
function, cd, p.selectSingleNode(coma).toString());
142 Object o = p.selectSingleNode(coma);
144 Element desc = (Element) o;
146 Element key =
new Element(
"Key");
147 desc.addContent(key);
148 key.setAttribute(
"Name", allElements.get(0)[a]);
149 System.out.println(desc.getAttributes());
150 stats.
addNote(
function, cd, Arrays.toString(desc.getAttributes().toArray()));
151 key.setText(allElements.get(i)[a]);
157 Writer fileWriter =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(comaFile),
"UTF8"));
158 XMLOutputter serializer =
new XMLOutputter();
159 serializer.output(coma, fileWriter);
160 stats.
addNote(
function, cd,
"The data in the csv file has been added into the coma.");
167 public void inputData() throws IOException, JDOMException {
174 public List<String[]>
readData() throws FileNotFoundException, IOException {
175 CSVReader reader =
new CSVReader(
new FileReader(csvFile),
';');
176 List<String[]> allElements = null;
177 allElements = reader.readAll();
186 for (String[] row : allElements) {
187 System.out.println(Arrays.toString(row));
193 System.out.println(Arrays.toString(allElements.get(0)));
195 System.out.println(allElements.get(0)[0]);
196 coma = org.exmaralda.common.jdomutilities.IOUtilities.readDocumentFromLocalFile(comaFile);
198 for (
int i = 1; i < allElements.size(); i++) {
199 for (
int a = 1; a < allElements.get(i).length; a++) {
202 String place =
"//Speaker[Sigle/text()=\"" + allElements.get(i)[0] +
"\"]/Description";
203 System.out.println(place);
204 XPath p = XPath.newInstance(place);
206 Object o = p.selectSingleNode(coma);
208 Element desc = (Element) o;
210 Element key =
new Element(
"Key");
211 desc.addContent(key);
212 key.setAttribute(
"Name", allElements.get(0)[a]);
213 System.out.println(desc.getAttributes());
214 key.setText(allElements.get(i)[a]);
219 String place =
"//Communication[@Name=\"" + allElements.get(i)[0] +
"\"]/Description";
220 System.out.println(place);
221 XPath p = XPath.newInstance(place);
222 System.out.println(p.selectSingleNode(coma));
223 Object o = p.selectSingleNode(coma);
225 Element desc = (Element) o;
227 Element key =
new Element(
"Key");
228 desc.addContent(key);
229 key.setAttribute(
"Name", allElements.get(0)[a]);
230 System.out.println(desc.getAttributes());
231 key.setText(allElements.get(i)[a]);
237 Writer fileWriter =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(comaFile),
"UTF8"));
238 XMLOutputter serializer =
new XMLOutputter();
239 serializer.output(coma, fileWriter);
244 throw new UnsupportedOperationException(
"Not supported yet.");
249 public void process(String filename)
throws JexmaraldaException, SAXException {
250 throw new UnsupportedOperationException(
"Not supported yet.");
261 if (spOrCommInput.equals(
"speaker")) {
264 if (spOrCommInput.equals(
"communication")) {
277 Class cl = Class.forName(
"de.uni_hamburg.corpora.ComaData");
279 }
catch (ClassNotFoundException ex) {
280 report.
addException(ex,
"unknown class not found error");
287 String description =
"this class can be used from the command line to insert data in a csv file " 288 +
" into an existing coma file there needs to be a header with information of the " 289 +
" information in the columns the first line has to consist of the sigle of the " 290 +
" speaker or name of the communication the metadata should be assigned to";
295 public Report function(
Corpus c)
throws Exception, NoSuchAlgorithmException, ClassNotFoundException, FSMException, URISyntaxException, SAXException, IOException, ParserConfigurationException, JexmaraldaException, TransformerException, XPathExpressionException, JDOMException {
296 throw new UnsupportedOperationException(
"Not supported yet.");
void addNote(String statId, String description)
void addException(Throwable e, String description)