001: /*
002: * Bossa Workflow System
003: *
004: * $Id: EventsXMLHelper.java,v 1.1 2004/03/15 22:20:31 gdvieira Exp $
005: *
006: * Copyright (C) 2004 OpenBR Sistemas S/C Ltda.
007: *
008: * This file is part of Bossa.
009: *
010: * Bossa is free software; you can redistribute it and/or modify it
011: * under the terms of version 2 of the GNU General Public License as
012: * published by the Free Software Foundation.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public
020: * License along with this program; if not, write to the
021: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
022: * Boston, MA 02111-1307, USA.
023: */
024:
025: package com.bigbross.bossa.io;
026:
027: import java.io.BufferedOutputStream;
028: import java.io.FileNotFoundException;
029: import java.io.FileOutputStream;
030: import java.io.IOException;
031: import java.io.OutputStream;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.Map;
035:
036: import javax.xml.transform.OutputKeys;
037: import javax.xml.transform.Transformer;
038: import javax.xml.transform.TransformerConfigurationException;
039: import javax.xml.transform.sax.SAXTransformerFactory;
040: import javax.xml.transform.sax.TransformerHandler;
041: import javax.xml.transform.stream.StreamResult;
042:
043: import org.xml.sax.SAXException;
044: import org.xml.sax.helpers.AttributesImpl;
045:
046: import com.bigbross.bossa.notify.Event;
047:
048: /**
049: * This class provides a static method to convert an event list to a
050: * XML encoded file. <p>
051: *
052: * The XML encoding is as follows:
053: * <pre><events>
054: * <event>
055: * <id>open_work_item</id>
056: * <time>1071696763305</time>
057: * <attribute id="case_type_id">purchase</attribute>
058: * <attribute id="resource_id">joedoe</attribute>
059: * <attribute id="case_id">1</attribute>
060: * <attribute id="work_item_id">check_price</attribute>
061: * </event>
062: * </events></pre>
063: *
064: * The <code>events</code> element is used to group a list of events.
065: * The <code>event</code> element represents a single event and it is
066: * composed by a single <code>id</code> element, a single <code>time</code>
067: * element and zero or more <code>attribute</code> elements. <p>
068: *
069: * The <code>id</code> element contains the id of the event. The
070: * <code>time</code> element contains the time of the event as the
071: * number of milliseconds since January 1, 1970, 00:00:00 GMT. Each
072: * <code>attribute</code> element represents one attribute mapping of
073: * the event. <p>
074: *
075: * @author <a href="http://www.bigbross.com">BigBross Team</a>
076: */
077: public class EventsXMLHelper {
078:
079: /**
080: * Converts the provided event list to a XML file. If the file exists,
081: * it will be overwritten. <p>
082: *
083: * @param events the events to be exported.
084: * @param file the name of the file.
085: * @throws DataTransferException if an error happens during the export.
086: */
087: public static void export(List events, String file)
088: throws DataTransferException {
089: try {
090: SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory
091: .newInstance();
092: TransformerHandler handler = factory
093: .newTransformerHandler();
094: Transformer transformer = handler.getTransformer();
095: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
096: OutputStream out = new BufferedOutputStream(
097: new FileOutputStream(file));
098: StreamResult streamResult = new StreamResult(out);
099: handler.setResult(streamResult);
100:
101: AttributesImpl atb = new AttributesImpl();
102: handler.startDocument();
103: handler.startElement("", "", "events", atb);
104: for (Iterator i = events.iterator(); i.hasNext();) {
105: Event e = (Event) i.next();
106: String id = e.getId();
107: String time = Long.toString(e.getTime().getTime());
108: atb.clear();
109: handler.startElement("", "", "event", atb);
110: handler.startElement("", "", "id", atb);
111: handler.characters(id.toCharArray(), 0, id.length());
112: handler.endElement("", "", "id");
113: handler.startElement("", "", "time", atb);
114: handler
115: .characters(time.toCharArray(), 0, time
116: .length());
117: handler.endElement("", "", "time");
118: for (Iterator j = e.getAttributes().entrySet()
119: .iterator(); j.hasNext();) {
120: Map.Entry entry = (Map.Entry) j.next();
121: String propId = (String) entry.getKey();
122: String propValue = (String) entry.getValue();
123: atb.clear();
124: atb.addAttribute("", "", "id", "CDATA", propId);
125: handler.startElement("", "", "attribute", atb);
126: handler.characters(propValue.toCharArray(), 0,
127: propValue.length());
128: handler.endElement("", "", "attribute");
129: }
130: handler.endElement("", "", "event");
131: }
132: handler.endElement("", "", "events");
133: handler.endDocument();
134: out.close();
135: } catch (FileNotFoundException e) {
136: throw new DataTransferException("File not found.", e);
137: } catch (IOException e) {
138: throw new DataTransferException("I/O error.", e);
139: } catch (TransformerConfigurationException e) {
140: throw new DataTransferException(
141: "Unable to start XML exporter.", e);
142: } catch (SAXException e) {
143: throw new DataTransferException("Error creating XML.", e);
144: }
145: }
146: }
|