PageRenderTime 273ms CodeModel.GetById 26ms app.highlight 52ms RepoModel.GetById 179ms app.codeStats 1ms

/src/main/java/uk/ac/cam/ch/ami/amievents/log/TimelineXMLWriter.java

https://bitbucket.org/jat45/ami
Java | 200 lines | 141 code | 36 blank | 23 comment | 7 complexity | 07dc73a2da23acc29cb0447d99dd00b3 MD5 | raw file
  1package uk.ac.cam.ch.ami.amievents.log;
  2
  3import org.apache.commons.io.IOUtils;
  4import org.w3c.dom.Document;
  5import org.w3c.dom.Element;
  6import org.w3c.dom.Text;
  7import uk.ac.cam.ch.ami.ColorList;
  8import uk.ac.cam.ch.ami.amievents.AmiEventType;
  9import uk.ac.cam.ch.ami.amievents.events.AmiEvent;
 10import uk.ac.cam.ch.ami.amievents.events.CommentAmiEvent;
 11
 12import javax.xml.parsers.DocumentBuilder;
 13import javax.xml.parsers.DocumentBuilderFactory;
 14import javax.xml.transform.OutputKeys;
 15import javax.xml.transform.Transformer;
 16import javax.xml.transform.TransformerFactory;
 17import javax.xml.transform.dom.DOMSource;
 18import javax.xml.transform.stream.StreamResult;
 19import java.awt.*;
 20import java.io.*;
 21import java.sql.Timestamp;
 22import java.text.SimpleDateFormat;
 23import java.util.HashMap;
 24import java.util.Map;
 25import java.util.TreeMap;
 26
 27/**
 28 * Generate an XML file which can be read by Timeline (http://thetimelineproj.sourceforge.net)
 29 *
 30 * Created by IntelliJ IDEA.
 31 * User: alt36
 32 * Date: Oct 12, 2010
 33 * Time: 1:54:10 PM
 34 */
 35public class TimelineXMLWriter {
 36
 37    public void WriteXML(File saveDir, AmiEventLog eventlog) {
 38        TreeMap<Timestamp, AmiEvent> allEvents = eventlog.getAllEventsLog();
 39
 40        try{
 41            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
 42            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
 43            Document doc = docBuilder.newDocument();
 44
 45            // Creating the XML tree
 46
 47            // create the root element and add it to the document
 48            Element root = doc.createElement("timeline");
 49            doc.appendChild(root);
 50
 51            // elements which are all children of the root element
 52            Element ver = doc.createElement("version");
 53            root.appendChild(ver);
 54            Text vertext = doc.createTextNode("0.10.2");
 55            ver.appendChild(vertext);
 56
 57            Element categories = doc.createElement("categories");
 58            Element events = doc.createElement("events");
 59            Element view = doc.createElement("view");
 60            root.appendChild(categories);
 61            root.appendChild(events);
 62            root.appendChild(view);
 63
 64            Map<AmiEventType, Color> colormap = new HashMap<AmiEventType, Color>();
 65            ColorList c = new ColorList();
 66            colormap.put(AmiEventType.PHYSICAL_EVENT, c.getPhysicalEventColor());
 67            colormap.put(AmiEventType.COMMENT_EVENT, c.getCommentEventColor());
 68            colormap.put(AmiEventType.USER_EVENT, c.getUserEventColor());
 69            colormap.put(AmiEventType.CHEMICAL_EVENT, c.getChemicalEventColor());
 70            colormap.put(AmiEventType.EQUIPMENT_EVENT, c.getEquipmentEventColor());
 71            colormap.put(AmiEventType.SENSOR_EVENT, c.getSensorEventColor());
 72
 73            AmiEventType[] types=AmiEventType.values();
 74
 75            // create list of categories
 76
 77            for(AmiEventType t : types) {
 78                Element category = doc.createElement("category");
 79                categories.appendChild(category);
 80                Element name = doc.createElement("name");
 81                Element color = doc.createElement("color");
 82                category.appendChild(name);
 83                category.appendChild(color);
 84                Text text = doc.createTextNode(t.toString());
 85                name.appendChild(text);
 86                Color col = colormap.get(t);
 87                text = doc.createTextNode(col.getRed()+","+col.getGreen()+","+col.getBlue());
 88                color.appendChild(text);
 89
 90            }
 91
 92            // add all of the events
 93            for(AmiEvent e : allEvents.values()) {
 94                if(!e.isToBeDeleted()) {
 95                    Element event = doc.createElement("event");
 96                    events.appendChild(event);
 97
 98                    Element start=doc.createElement("start");
 99                    Element end=doc.createElement("end");
100                    Element text=doc.createElement("text");
101                    Element category=doc.createElement("category");
102
103                    event.appendChild(start);
104                    event.appendChild(end);
105                    event.appendChild(text);
106                    event.appendChild(category);
107
108                    Text content = doc.createTextNode(e.getTimelineStart());
109                    start.appendChild(content);
110
111                    content = doc.createTextNode(e.getTimelineEnd());
112                    end.appendChild(content);
113
114                    content = doc.createTextNode(e.getTimelineText());
115                    text.appendChild(content);
116
117                    content = doc.createTextNode(e.getAmiEventType().toString());
118                    category.appendChild(content);
119
120                    if(e.getAmiEventType() == AmiEventType.COMMENT_EVENT) {
121                        Element description = doc.createElement("description");
122                        event.appendChild(description);
123                        CommentAmiEvent commentEvent = (CommentAmiEvent)e;
124                        content = doc.createTextNode(commentEvent.getComment());
125                        description.appendChild(content);
126                    }
127
128
129                }
130
131            }
132
133            // set the "displayed period" to cover the range of all the events,
134            // with an (arbitrary) half hour buffer either side
135            Element displayedPeriod = doc.createElement("displayed_period");
136            view.appendChild(displayedPeriod);
137            Element start = doc.createElement("start");
138            Element end = doc.createElement("end");
139            displayedPeriod.appendChild(start);
140            displayedPeriod.appendChild(end);
141
142            long halfHour = 30*60*1000; // milliseconds in half an hour
143            // the allEvents TreeMap is sorted with most recent events first, hence the earliest event is the last in
144            // the log
145            Timestamp earliest = new Timestamp(allEvents.lastKey().getTime()-halfHour);
146            Timestamp latest = new Timestamp(allEvents.firstKey().getTime()+halfHour);
147            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
148
149            Text content = doc.createTextNode(sdf.format(earliest));
150            start.appendChild(content);
151
152            content = doc.createTextNode(sdf.format(latest));
153            end.appendChild(content);
154
155            // hidden categories
156            Element hiddenCategories = doc.createElement("hidden_categories");
157            view.appendChild(hiddenCategories);
158            for(AmiEventType t : types)    {
159                if(!eventlog.isEventTypeHidden(t)) {
160                    Element name = doc.createElement("name");
161                    hiddenCategories.appendChild(name);
162                    content = doc.createTextNode(t.toString());
163                    name.appendChild(content);
164                }
165            }
166            /////////////////
167            //Output the XML
168
169            //set up a transformer
170            TransformerFactory transfac = TransformerFactory.newInstance();
171            Transformer trans = transfac.newTransformer();
172            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
173            trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
174            trans.setOutputProperty(OutputKeys.INDENT, "yes");
175
176            // create string from xml tree
177            StringWriter sw = new StringWriter();
178            StreamResult result = new StreamResult(sw);
179            DOMSource source = new DOMSource(doc);
180            trans.transform(source, result);
181            String xmlString = sw.toString();
182
183            // write XML to file
184            BufferedWriter out=null;
185            try{
186                out = new BufferedWriter(new FileWriter(saveDir.getPath() + IOUtils.DIR_SEPARATOR + "log.timeline"));
187                out.write(xmlString);
188
189            } catch (IOException e) {
190                e.printStackTrace();
191            } finally {
192                out.close();
193            }
194
195        } catch (Exception e) {
196            e.printStackTrace();
197        }
198    }
199
200}