package jas.hist; import jas.hist.normalization.Normalizer; import jas.util.xml.XMLNodeTraverser; import java.lang.reflect.Constructor; import java.util.Hashtable; import java.util.StringTokenizer; import java.util.Vector; import org.w3c.dom.Attr; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.Text; /** * Build a DataSource from a DOM node */ class Data1DTraverser extends XMLNodeTraverser { Data1DTraverser(Node node) throws BadXMLException { traverse(node); } protected void handleElement(Element node, String name) throws BadXMLException { if (name.equals("bins1d")) b1d = new Bins1DNodeTraverser(node); else if (name.equals("points")) xy = new XYPointsNodeTraverser(node); else if (name.equals("binnedDataAxisAttributes")) baa = new BinnedDataAxisAttributesNodeTraverser(node); else if (name.equals("pointDataAxisAttributes")) paa = new PointDataAxisAttributesNodeTraverser(node); else if (name.equals("class")) ct = new ClassNodeTraverser(node); else if (name.equals("datasource")) ct = new DataSourceNodeTraverser(node); else if (name.equals("statistics")) stats = new StatisticsTraverser(node); else if (name.equals("axisLabels")) labels = new AxisLabelsTraverser(node); else if (name.equals("style1d")) st.traverse(node,style=new JASHist1DHistogramStyle()); else if (name.equals("normalization")) norm = new NormalizationTraverser(node); else super.handleElement(node,name); } protected void handleAttributeNode(Attr node, String name, String value) throws BadXMLException { if (name.equals("axis")) yAxis = toInt(value.substring(1)); else if (name.equals("name")) refName = value; else super.handleAttributeNode(node,name,value); } DataSource getDataSource() { if (ct != null) return ct.getDataSource(); else if (b1d != null) return new XML1DHistDataSource(b1d,baa,labels,stats); else return new XMLXYDataSource(xy,paa,stats); } int getYAxis() { return yAxis; } JASHistStyle getStyle() { return style; } String getRefName() { return refName; } Normalizer getNormalizer(DataSource data, Hashtable map) throws BadXMLException { return norm == null ? null : norm.getNormalizer(data,map); } private int yAxis; private StatisticsTraverser stats = null; private AxisLabelsTraverser labels = null; private NormalizationTraverser norm = null; private JASHist1DHistogramStyle style = null; private ConstructorNodeTraverser ct = null; private Style1DNodeTraverser st = new Style1DNodeTraverser(); private Bins1DNodeTraverser b1d; private XYPointsNodeTraverser xy; private PointDataAxisAttributesNodeTraverser paa; private BinnedDataAxisAttributesNodeTraverser baa; private String refName; } class ClassNodeTraverser extends ConstructorNodeTraverser { ClassNodeTraverser(Node node) throws BadXMLException { super(node); if (ds instanceof HasDataSource) return; throw new BadXMLException("Class is not a HasDataSource"); } protected void handleAttributeNode(Attr node, String name, String value) throws BadXMLException { if (name.equals("param")) param = value; else super.handleAttributeNode(node,name,value); } DataSource getDataSource() { return ((HasDataSource) ds).getDataSource(param); } private String param; } class DataSourceNodeTraverser extends ConstructorNodeTraverser { DataSourceNodeTraverser(Node node) throws BadXMLException { super(node); if (ds instanceof DataSource) return; throw new BadXMLException("Class is not a DataSource"); } DataSource getDataSource() { return (DataSource) ds; } } abstract class ConstructorNodeTraverser extends XMLNodeTraverser { ConstructorNodeTraverser(Node node) throws BadXMLException { int maxSize = node.getChildNodes().getLength(); types = new String[maxSize]; values = new String[maxSize]; traverse(node); try { Class c = null; ClassLoader ccl = Thread.currentThread().getContextClassLoader(); if (ccl != null) { c = ccl.loadClass(className); } else { c = Class.forName(className); } if (vc == 0) ds = c.newInstance(); else { Class[] argc = new Class[vc]; Object[] args = new Object[vc]; for (int i=0; i