PackageWriter.java
1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package ipipan.clarin.tei.impl.io.write;
import ipipan.clarin.tei.api.entities.AnnotationLayer;
import ipipan.clarin.tei.api.entities.TEICorpusText;
import ipipan.clarin.tei.api.entities.TEIParagraph;
import ipipan.clarin.tei.api.exceptions.TEIException;
import java.io.Writer;
import javax.xml.stream.XMLStreamException;
/**
*
* @author mlenart
*/
public class PackageWriter {
private final OutWrapper out;
public PackageWriter(Writer writer) throws TEIException {
try {
this.out = new OutWrapper(writer);
} catch (XMLStreamException ex) {
throw new TEIException(ex);
}
}
public void write(TEICorpusText tei) throws TEIException {
try {
doWrite(tei);
} catch (XMLStreamException ex) {
throw new TEIException(ex);
}
}
public void close() throws TEIException {
try {
out.close();
} catch (XMLStreamException ex) {
throw new TEIException(ex);
}
}
private void doWrite(TEICorpusText tei) throws XMLStreamException,
TEIException {
out.rootStart("teiCorpus");
HeaderWriter hwriter = new HeaderWriter();
ParagraphWriter pwriter = new ParagraphWriter(false);
hwriter.writeHeader(out, tei.getCorpusHeader());
for (AnnotationLayer layer : tei.getAnnotationLayers()) {
out.start("TEI");
hwriter.writeHeader(out, tei.getLayerHeader(layer));
out.start("text");
out.start("body");
for (TEIParagraph par : tei.getParagraphs()) {
par.correctSegmentOffsets();
pwriter.writeParagraph(out, par, layer);
}
// special case for coreference layer,
// because it doesn't have paragraph structure
if (layer.equals(AnnotationLayer.COREFERENCE)) {
CoreferenceWriter corefwriter = new CoreferenceWriter(false);
corefwriter.writeCoreference(out, tei);
}
out.end(); // body
out.end(); // text
out.end(); // TEI
}
out.end();
}
}