001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.core.engine.log.monitoring;
034:
035: import org.libresource.so6.core.engine.log.LogPrinter;
036: import org.libresource.so6.core.engine.log.MessageWriter;
037:
038: import org.xml.sax.Attributes;
039: import org.xml.sax.SAXException;
040: import org.xml.sax.helpers.DefaultHandler;
041:
042: import java.io.IOException;
043: import java.io.InputStream;
044: import java.io.PipedInputStream;
045: import java.io.PipedOutputStream;
046:
047: import java.util.Iterator;
048: import java.util.Vector;
049: import java.util.logging.LogRecord;
050:
051: import javax.xml.parsers.SAXParser;
052: import javax.xml.parsers.SAXParserFactory;
053:
054: /**
055: * @author smack
056: */
057: public class XMLLogHandler extends DefaultHandler implements LogPrinter {
058: private PipedOutputStream out;
059: private PipedInputStream in;
060:
061: //
062: private StringBuffer buffer;
063: private String currentTag;
064: private String rootName;
065: private boolean isRoot = true;
066: private boolean globalComment = false;
067:
068: //
069: private TreeContext context;
070:
071: //
072: private Vector messagesListeners;
073:
074: public XMLLogHandler() throws Exception {
075: out = new PipedOutputStream();
076: in = new PipedInputStream(out);
077: context = new TreeContext();
078: messagesListeners = new Vector();
079: isRoot = true;
080: }
081:
082: public void publish(LogRecord record) {
083: try {
084: out.write(record.getMessage().getBytes());
085: out.flush();
086: } catch (Exception e) {
087: e.printStackTrace();
088: }
089: }
090:
091: public void addMessageListener(MessageWriter writer) {
092: messagesListeners.add(writer);
093: }
094:
095: public void removeMessageListener(MessageWriter writer) {
096: messagesListeners.remove(writer);
097: }
098:
099: public void printMessage(String message) {
100: for (Iterator i = messagesListeners.iterator(); i.hasNext();) {
101: ((MessageWriter) i.next()).printMessage(globalComment,
102: message);
103: }
104: }
105:
106: public InputStream getInputStream() {
107: return in;
108: }
109:
110: public void characters(char[] buff, int offset, int len)
111: throws SAXException {
112: buffer.append(buff, offset, len);
113: }
114:
115: public void endElement(String namespaceuri, String sname,
116: String qname) throws SAXException {
117: //System.out.println("endElement");
118: if (qname.equals("COMMENT")) {
119: printMessage(buffer.toString());
120: }
121:
122: if (qname.equals("SUBCALL")) {
123: context.endPart();
124: }
125:
126: // Notify readers that the stream is finished.
127: if (rootName.equals(qname)) {
128: try {
129: out.flush();
130: out.close();
131: } catch (IOException e) {
132: e.printStackTrace();
133: }
134: }
135: }
136:
137: public void startElement(String namespaceuri, String sname,
138: String qname, Attributes attr) throws SAXException {
139: //System.out.println("startElement");
140: currentTag = qname;
141: buffer = new StringBuffer();
142:
143: //
144: if (isRoot) {
145: rootName = currentTag;
146:
147: //System.out.println("Root: " + rootName);
148: }
149:
150: isRoot = false;
151:
152: // General elements
153: if (qname.equals("COMMENT")) {
154: if (attr.getValue("global") != null) {
155: globalComment = Boolean
156: .valueOf(attr.getValue("global"))
157: .booleanValue();
158: } else {
159: globalComment = false;
160: }
161: } else {
162: globalComment = false;
163: }
164:
165: if (qname.equals("SUBCALL")) {
166: String nbCall = attr.getValue("nbCall");
167:
168: if (nbCall == null) {
169: context.startPart();
170: } else {
171: context.startPart(Integer.parseInt(nbCall));
172: }
173: }
174:
175: if (qname.equals("STATE")) {
176: double from = Double.parseDouble(attr.getValue("from"));
177: double to = Double.parseDouble(attr.getValue("to"));
178: double current = Double.parseDouble(attr
179: .getValue("current"));
180: context.setLocalState(from, to, current);
181: }
182:
183: if (attr.getValue("comment") != null) {
184: printMessage(attr.getValue("comment"));
185: }
186: }
187:
188: //
189: public TreeContext getContext() {
190: return context;
191: }
192:
193: public static void main(String[] args) throws Exception {
194: SAXParserFactory factory = SAXParserFactory.newInstance();
195: SAXParser saxParser = factory.newSAXParser();
196: XMLLogHandler xmlLogger = new XMLLogHandler();
197: saxParser.parse(args[0], xmlLogger);
198: }
199: }
|