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 com.artenum.so6.dataflow.xml;
034:
035: import com.artenum.so6.dataflow.graph.So6SimpleGraphModel;
036:
037: import org.xml.sax.Attributes;
038: import org.xml.sax.SAXException;
039: import org.xml.sax.helpers.DefaultHandler;
040:
041: import java.io.File;
042: import java.io.InputStream;
043:
044: import java.util.Properties;
045:
046: import javax.xml.parsers.SAXParser;
047: import javax.xml.parsers.SAXParserFactory;
048:
049: /**
050: * @author smack
051: */
052: public class So6NetworkHandler extends DefaultHandler {
053: public static final String TAG_NAME_QUEUE = "Queue";
054: public static final String TAG_NAME_WORKSPACE = "Ws";
055: public static final String TAG_NAME_WS_CONNECTION = "WsConnection";
056: public static final String ATTR_LABEL = "label";
057: public static final String ATTR_URL = "url";
058: private String tag;
059: private StringBuffer buffer;
060: private So6SimpleGraphModel model = null;
061:
062: public So6NetworkHandler() {
063: model = new So6SimpleGraphModel();
064: }
065:
066: public So6SimpleGraphModel getModel() {
067: return model;
068: }
069:
070: public void parseXmlFile(File file) throws Exception {
071: model.clear();
072:
073: SAXParserFactory factory = SAXParserFactory.newInstance();
074: SAXParser saxParser = factory.newSAXParser();
075: saxParser.parse(file, this );
076: }
077:
078: public void parseStream(InputStream in) throws Exception {
079: model.clear();
080:
081: SAXParserFactory factory = SAXParserFactory.newInstance();
082: SAXParser saxParser = factory.newSAXParser();
083: saxParser.parse(in, this );
084: }
085:
086: public void characters(char[] buff, int offset, int len)
087: throws SAXException {
088: System.out.println(new String(buff, offset, len));
089: buffer.append(buff, offset, len);
090: }
091:
092: public void endElement(String namespaceuri, String sname,
093: String qname) throws SAXException {
094: model.endElement();
095: }
096:
097: public void startElement(String namespaceuri, String sname,
098: String qname, Attributes attr) throws SAXException {
099: tag = qname;
100: buffer = new StringBuffer();
101:
102: Properties attributes = new Properties();
103:
104: for (int i = 0; i < attr.getLength(); i++) {
105: attributes.setProperty(attr.getQName(i), attr.getValue(i));
106: }
107:
108: String label = attr.getValue(ATTR_LABEL);
109:
110: if (label == null) {
111: label = new String();
112: }
113:
114: if (tag.equals(TAG_NAME_QUEUE)) {
115: model.createSynchronizer(attributes);
116: }
117:
118: if (tag.equals(TAG_NAME_WORKSPACE)) {
119: model.createWorkspace(attributes);
120: }
121:
122: if (tag.equals(TAG_NAME_WS_CONNECTION)) {
123: model.createConnection(attributes);
124: }
125: }
126:
127: public static void main(String[] args) throws Exception {
128: SAXParserFactory factory = SAXParserFactory.newInstance();
129: SAXParser saxParser = factory.newSAXParser();
130: So6NetworkHandler so6Tree = new So6NetworkHandler();
131: saxParser.parse(args[0], so6Tree);
132: }
133: }
|