[Bprocessor-commit] model/src/net/sourceforge/bprocessor/model/sense/brunata/xml Persistence.java,
Status: Pre-Alpha
Brought to you by:
henryml
From: Michael L. <he...@us...> - 2010-08-31 13:48:10
|
Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model/sense/brunata/xml In directory sfp-cvsdas-2.v30.ch3.sourceforge.com:/tmp/cvs-serv20256/src/net/sourceforge/bprocessor/model/sense/brunata/xml Added Files: Persistence.java Properties.java DocumentParser.java Document.java Property.java Atom.java Element.java Log Message: --- NEW FILE: Document.java --- package net.sourceforge.bprocessor.model.sense.brunata.xml; import java.io.PrintStream; public class Document { Element root; public Document(Element root) { this.root = root; } public Element getRoot() { return root; } public void display(PrintStream out) { root.display(out, 0); } } --- NEW FILE: Persistence.java --- //--------------------------------------------------------------------------------- // $Id: Persistence.java,v 1.1 2010/08/31 13:48:02 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.model.sense.brunata.xml; import java.util.LinkedList; import java.util.List; public class Persistence { private class Internalizer { private Document document; public Internalizer(Document document) { this.document = document; } public List<Serie> internalize() { return internalizeDataseries(document.getRoot()); } public List<Serie> internalizeDataseries(Element xml) { List<Serie> series = new LinkedList(); for (Atom atom : xml.getChildren()) { if (atom instanceof Element) { Element element = (Element) atom; series.add(internalizeDataserie(element)); } } return series; } private Serie internalizeDataserie(Element xml) { Serie serie = new Serie(xml.getValue("regType")); for (Atom atom : xml.getChildren()) { if (atom instanceof Element) { Element element = (Element) atom; serie.add(internalizeDataitem(element)); } } return serie; } private DataItem internalizeDataitem(Element xml) { DataItem item = new DataItem(); item.timestamp = Long.valueOf(xml.getValue("timestamp")); item.value = Double.valueOf(xml.getValue("value")); return item; } } public class Serie { String type; List<DataItem> items; public Serie(String t) { type = t; items = new LinkedList(); } public void add(DataItem item) { items.add(item); } public List<DataItem> items() { return items; } public int size() { return items.size(); } public String getType() { return type; } } public class DataItem { public double value; public long timestamp; } public List<Serie> internalize(Document document) { Internalizer internalizer = new Internalizer(document); return internalizer.internalize(); } } --- NEW FILE: DocumentParser.java --- package net.sourceforge.bprocessor.model.sense.brunata.xml; import java.io.IOException; import java.io.InputStream; import java.util.LinkedList; import java.util.List; import java.util.Stack; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; public class DocumentParser { public Document parse(InputStream input) throws SAXException, IOException { XMLReader reader = XMLReaderFactory.createXMLReader(); DocumentHandler handler = new DocumentHandler(); reader.setContentHandler(handler); reader.setErrorHandler(handler); InputSource source = new InputSource(input); source.setEncoding("UTF8"); reader.parse(source); return document; } Document document; private class DocumentHandler extends DefaultHandler { Stack<Element> stack; Element root; private Properties asProperties(Attributes attributes) { List<Property> content = new LinkedList(); for (int i = 0; i < attributes.getLength(); i++) { String name = attributes.getLocalName(i); String value = attributes.getValue(i); Property property = new Property(name, value); content.add(property); } Properties properties = new Properties(content); return properties; } public void startDocument() { stack = new Stack(); } public void endDocument() { document = new Document(root); } public void startElement(String uri, String name, String cue, Attributes attributes) { Properties properties = asProperties(attributes); Element element = new Element(name, properties); stack.push(element); } public void endElement(String uri, String name, String cue) { Element element = stack.pop(); if (!stack.isEmpty()) { Element top = stack.peek(); top.add(element); } else { root = element; } } } } --- NEW FILE: Properties.java --- package net.sourceforge.bprocessor.model.sense.brunata.xml; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; public class Properties { List<Property> content; public Properties(List<Property> content) { this.content = content; } public String getValue(String key) { for (Property current : content) { if (current.key.equals(key)) { return current.value; } } return null; } public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append(" | "); DateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); for (Property current : content) { String value = current.value; if (current.key.equals("timestamp")) { value = format.format(new Date(Long.valueOf(value))); } buffer.append(current.key + "=" + "\"" + value + "\""); buffer.append(" "); } return buffer.toString(); } } --- NEW FILE: Property.java --- package net.sourceforge.bprocessor.model.sense.brunata.xml; public class Property { String key; String value; public Property(String key, String value) { this.key = key; this.value = value; } } --- NEW FILE: Element.java --- package net.sourceforge.bprocessor.model.sense.brunata.xml; import java.io.PrintStream; import java.util.LinkedList; import java.util.List; public class Element extends Atom { String tag; List<Atom> children; Properties properties; public Element(String tag, Properties properties) { this.tag = tag; this.properties = properties; } public void add(Element element) { if (children == null) { children = new LinkedList(); } children.add(element); } public String getTag() { return tag; } public List<Atom> getChildren() { return children; } public String getValue(String key) { return properties.getValue(key); } public void display(PrintStream out, int level) { for (int i = 0; i < level; i++) { System.out.print(" "); } out.print(tag); out.println(properties); if (children != null) { for (Atom current : children) { current.display(out, level + 1); } } } } --- NEW FILE: Atom.java --- package net.sourceforge.bprocessor.model.sense.brunata.xml; import java.io.PrintStream; public class Atom { public void display(PrintStream out, int i) { // nothing } } |