corpus-services  1.0
ExmaErrorList.java
Go to the documentation of this file.
1 
7 package de.uni_hamburg.corpora;
8 
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.HashMap;
12 import java.util.Map;
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.parsers.ParserConfigurationException;
16 import javax.xml.transform.TransformerConfigurationException;
17 import javax.xml.transform.TransformerException;
18 import org.w3c.dom.Attr;
19 import org.w3c.dom.Document;
20 import org.w3c.dom.Element;
21 
22 
23 public class ExmaErrorList {
24 
28  private static Map<String, Collection<ExmaErrorListItem>> statistics;
29 
33  public ExmaErrorList() {
34  statistics = new HashMap<String, Collection<ExmaErrorListItem>>();
35  }
36 
40  private Collection<ExmaErrorListItem> getOrCreateStatistic(String statId) {
41  if (!statistics.containsKey(statId)) {
42  statistics.put(statId, new ArrayList<ExmaErrorListItem>());
43  }
44  return statistics.get(statId);
45  }
46 
51  public void merge(ExmaErrorList sr) {
52  for (Map.Entry<String, Collection<ExmaErrorListItem>> kv
53  : sr.statistics.entrySet()) {
54  if (statistics.containsKey(kv.getKey())) {
55  Collection<ExmaErrorListItem> c
56  = statistics.get(kv.getKey());
57  c.addAll(kv.getValue());
58  statistics.put(kv.getKey(), c);
59  } else {
60  statistics.put(kv.getKey(), kv.getValue());
61  }
62  }
63  }
64 
69  public void addError(String statId, String fileName, String tierID, String eventStart, boolean done, String description) {
70  Collection<ExmaErrorListItem> stat = getOrCreateStatistic(statId);
71  stat.add(new ExmaErrorListItem(fileName, tierID, eventStart, done, description));
72  }
73 
77  public static Document createFullErrorList() throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
78  DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
79  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
80  Document doc = docBuilder.newDocument();
81  Element rootElement;
82  rootElement = doc.createElement("error-list");
83  doc.appendChild(rootElement);
84  Element secondElement = doc.createElement("errors");
85  rootElement.appendChild(secondElement);
86  for (Collection<ExmaErrorListItem> col : statistics.values()) {
87  for (ExmaErrorListItem item : col) {
88  Element error = doc.createElement("error");
89  Attr fl = doc.createAttribute("file");
90  fl.setValue(item.getFileName());
91  error.setAttributeNode(fl);
92  Attr tier = doc.createAttribute("tier");
93  tier.setValue(item.getTierID());
94  error.setAttributeNode(tier);
95  Attr start = doc.createAttribute("start");
96  start.setValue(item.getEventStart());
97  error.setAttributeNode(start);
98  Attr done = doc.createAttribute("done");
99  if(item.isDone())
100  done.setValue("yes");
101  else
102  done.setValue("no");
103  error.setAttributeNode(done);
104  error.appendChild(doc.createTextNode(item.getDescription()));
105  secondElement.appendChild(error);
106  }
107  }
108  return doc;
109  /* TransformerFactory transformerFactory = TransformerFactory.newInstance();
110  Transformer transformer = transformerFactory.newTransformer();
111  DOMSource source = new DOMSource(doc);
112  File f = new File(location.getFile());
113  URI u = f.toURI();
114  StreamResult result = new StreamResult(new File(u));
115  transformer.transform(source, result); */
116  }
117 }
void addError(String statId, String fileName, String tierID, String eventStart, boolean done, String description)