📄 xmlbeans.java
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * XMLBeans.java * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand */package weka.gui.beans.xml;import weka.core.converters.ConverterUtils;import weka.core.xml.XMLBasicSerialization;import weka.core.xml.XMLDocument;import weka.gui.beans.BeanConnection;import weka.gui.beans.BeanInstance;import weka.gui.beans.BeanVisual;import weka.gui.beans.MetaBean;import weka.gui.beans.Visible;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.Point;import java.beans.BeanInfo;import java.beans.EventSetDescriptor;import java.beans.Introspector;import java.beans.beancontext.BeanContextSupport;import java.io.File;import java.util.Enumeration;import java.util.Hashtable;import java.util.StringTokenizer;import java.util.Vector;import javax.swing.JComponent;import javax.swing.JPanel;import javax.swing.plaf.ColorUIResource;import javax.swing.plaf.FontUIResource;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;/** * This class serializes and deserializes a KnowledgeFlow setup to and fro XML. * <br> * * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision: 1.7 $ */public class XMLBeans extends XMLBasicSerialization { /** the value of the id property */ public final static String VAL_ID = "id"; /** the value of the x property */ public final static String VAL_X = "x"; /** the value of the y property */ public final static String VAL_Y = "y"; /** the value of the bean property */ public final static String VAL_BEAN = "bean"; /** the value of the source property */ public final static String VAL_SOURCEID = "source_id"; /** the value of the target property */ public final static String VAL_TARGETID = "target_id"; /** the value of the eventname property */ public final static String VAL_EVENTNAME = "eventname"; /** the value of the hidden property */ public final static String VAL_HIDDEN = "hidden"; /** the value of the file property */ public final static String VAL_FILE = "file"; /** the value of the dir property */ public final static String VAL_DIR = "dir"; /** the value of the prefix property */ public final static String VAL_PREFIX = "prefix"; /** the value of the options property */ public final static String VAL_OPTIONS = "options"; /** the value of the saver property */ public final static String VAL_SAVER = "saver"; /** the value of the loader property */ public final static String VAL_LOADER = "loader"; /** the value of the text property */ public final static String VAL_TEXT = "text"; /** the value of the beanContext property */ public final static String VAL_BEANCONTEXT = "beanContext"; /** the value of the width property */ public final static String VAL_WIDTH = "width"; /** the value of the height property */ public final static String VAL_HEIGHT = "height"; /** the value of the red property */ public final static String VAL_RED = "red"; /** the value of the green property */ public final static String VAL_GREEN = "green"; /** the value of the blue property */ public final static String VAL_BLUE = "blue"; /** the value of the value property */ public final static String VAL_NAME = "name"; /** the value of the style property */ public final static String VAL_STYLE = "style"; /** the value of the location property */ public final static String VAL_LOCATION = "location"; /** the value of the size property */ public final static String VAL_SIZE = "size"; /** the value of the color property */ public final static String VAL_COLOR = "color"; /** the value of the font property */ public final static String VAL_FONT = "font"; /** the value of the iconpath property */ public final static String VAL_ICONPATH = "iconPath"; /** the value of the animatedIconPath property */ public final static String VAL_ANIMATEDICONPATH = "animatedIconPath"; /** the value of the associatedConnections property */ public final static String VAL_ASSOCIATEDCONNECTIONS = "associatedConnections"; /** the value of the input property */ public final static String VAL_INPUTS = "inputs"; /** the value of the input id property */ public final static String VAL_INPUTSID = "inputs_id"; /** the value of the outputs id property */ public final static String VAL_OUTPUTS = "outputs"; /** the value of the outputs property */ public final static String VAL_OUTPUTSID = "outputs_id"; /** the value of the subFlow property */ public final static String VAL_SUBFLOW = "subFlow"; /** the value of the originalCoords property */ public final static String VAL_ORIGINALCOORDS = "originalCoords"; /** the index in the Vector, where the BeanInstances are stored * (Instances and Connections are stored in a Vector and then serialized) */ public final static int INDEX_BEANINSTANCES = 0; /** the index in the Vector, where the BeanConnections are stored * (Instances and Connections are stored in a Vector and then serialized) */ public final static int INDEX_BEANCONNECTIONS = 1; /** the component that manages the layout of the beans */ protected JComponent m_BeanLayout; /** keeps track of the BeanInstances read so far, used for the BeanConnections */ protected Vector m_BeanInstances; /** keeps track of the BeanInstances read so far, used for the BeanConnections */ protected Vector m_BeanInstancesID; /** whether to ignore the BeanConnection */ protected boolean m_IgnoreBeanConnections; /** the current MetaBean (for the BeanConnections) */ protected MetaBean m_CurrentMetaBean; /** the identifier for regular BeanConnections */ protected final static String REGULAR_CONNECTION = "regular_connection"; /** the relation between Bean and connection, MetaBean BeanConnections * are stored under the reference of the MetaBean, regular connections * are stored under REGULAR_CONNECTION. The relation has the following * format (is a string): sourcePos,targetPos,event,hidden * @see #REGULAR_CONNECTION */ protected Hashtable m_BeanConnectionRelation; /** the data that is about to be read/written contains a complete layout * @see #m_DataType */ public final static int DATATYPE_LAYOUT = 0; /** the data that is about to be read/written contains user-components, i.e., * Metabeans * @see #m_DataType */ public final static int DATATYPE_USERCOMPONENTS = 1; /** the type of data that is be read/written * @see #DATATYPE_LAYOUT * @see #DATATYPE_USERCOMPONENTS */ protected int m_DataType = DATATYPE_LAYOUT; /** the beancontext to use for loading from XML and the beancontext is * null in the bean */ protected BeanContextSupport m_BeanContextSupport = null; /** * initializes the serialization for layouts * * @param layout the component that manages the layout * @param context the bean context support to use * @throws Exception if initialization fails */ public XMLBeans(JComponent layout, BeanContextSupport context) throws Exception { this(layout, context, DATATYPE_LAYOUT); } /** * initializes the serialization for different types of data * * @param layout the component that manages the layout * @param context the bean context support to use * @param datatype the type of data to read/write * @throws Exception if initialization fails */ public XMLBeans(JComponent layout, BeanContextSupport context, int datatype) throws Exception { super(); m_BeanLayout = layout; m_BeanContextSupport = context; setDataType(datatype); } /** * sets what kind of data is to be read/written * @param value the type of data * @see #m_DataType */ public void setDataType(int value) { if (value == DATATYPE_LAYOUT) m_DataType = value; else if (value == DATATYPE_USERCOMPONENTS) m_DataType = value; else System.out.println("DataType '" + value + "' is unknown!"); } /** * returns the type of data that is to be read/written * @return the type of data * @see #m_DataType */ public int getDataType() { return m_DataType; } /** * generates internally a new XML document and clears also the IgnoreList and * the mappings for the Read/Write-Methods * * @throws Exception if something goes wrong */ public void clear() throws Exception { Vector<String> classnames; int i; super.clear(); // ignore: suppress unnecessary GUI stuff // needs to be checked for new Java versions (might introduce new properties) // - works with Java 1.5 m_Properties.addIgnored("UI"); m_Properties.addIgnored("actionMap"); m_Properties.addIgnored("alignmentX"); m_Properties.addIgnored("alignmentY"); m_Properties.addIgnored("autoscrolls"); m_Properties.addIgnored("background"); m_Properties.addIgnored("border"); m_Properties.addIgnored("componentPopupMenu"); m_Properties.addIgnored("debugGraphicsOptions"); m_Properties.addIgnored("doubleBuffered"); m_Properties.addIgnored("enabled"); m_Properties.addIgnored("focusCycleRoot"); m_Properties.addIgnored("focusTraversalPolicy"); m_Properties.addIgnored("focusTraversalPolicyProvider"); m_Properties.addIgnored("focusable"); m_Properties.addIgnored("font"); m_Properties.addIgnored("foreground"); m_Properties.addIgnored("inheritsPopupMenu"); m_Properties.addIgnored("inputVerifier"); m_Properties.addIgnored("layout"); m_Properties.addIgnored("locale"); m_Properties.addIgnored("maximumSize"); m_Properties.addIgnored("minimumSize"); m_Properties.addIgnored("nextFocusableComponent"); m_Properties.addIgnored("opaque"); m_Properties.addIgnored("preferredSize"); m_Properties.addIgnored("requestFocusEnabled"); m_Properties.addIgnored("toolTipText"); m_Properties.addIgnored("transferHandler"); m_Properties.addIgnored("verifyInputWhenFocusTarget"); m_Properties.addIgnored("visible"); // special ignore m_Properties.addIgnored("size"); // otherwise you get an endless loop with Dimension! m_Properties.addIgnored("location"); // otherwise you get an endless loop with Point! // allow m_Properties.addAllowed(weka.gui.beans.BeanInstance.class, "x"); m_Properties.addAllowed(weka.gui.beans.BeanInstance.class, "y"); m_Properties.addAllowed(weka.gui.beans.BeanInstance.class, "bean"); m_Properties.addAllowed(weka.gui.beans.Saver.class, "saver"); m_Properties.addAllowed(weka.gui.beans.Loader.class, "loader"); if (getDataType() == DATATYPE_LAYOUT) m_Properties.addAllowed(weka.gui.beans.Loader.class, "beanContext"); else m_Properties.addIgnored(weka.gui.beans.Loader.class, "beanContext"); // TODO: more classes??? m_Properties.addAllowed(weka.gui.beans.Filter.class, "filter"); m_Properties.addAllowed(weka.gui.beans.Classifier.class, "wrappedAlgorithm"); m_Properties.addAllowed(weka.gui.beans.Clusterer.class, "wrappedAlgorithm"); m_Properties.addAllowed(weka.classifiers.Classifier.class, "debug"); m_Properties.addAllowed(weka.classifiers.Classifier.class, "options"); m_Properties.addAllowed(weka.filters.Filter.class, "options"); m_Properties.addAllowed(weka.core.converters.DatabaseSaver.class, "options"); m_Properties.addAllowed(weka.core.converters.DatabaseLoader.class, "options"); m_Properties.addAllowed(weka.core.converters.TextDirectoryLoader.class, "options"); // we assume that classes implementing SplitEvaluator also implement OptionHandler m_Properties.addAllowed(weka.experiment.SplitEvaluator.class, "options"); // we assume that classes implementing ResultProducer also implement OptionHandler m_Properties.addAllowed(weka.experiment.ResultProducer.class, "options"); // read/write methods m_CustomMethods.register(this, Color.class, "Color"); m_CustomMethods.register(this, Dimension.class, "Dimension"); m_CustomMethods.register(this, Font.class, "Font"); m_CustomMethods.register(this, Point.class, "Point"); m_CustomMethods.register(this, ColorUIResource.class, "ColorUIResource"); m_CustomMethods.register(this, FontUIResource.class, "FontUIResource"); m_CustomMethods.register(this, weka.gui.beans.BeanInstance.class, "BeanInstance"); m_CustomMethods.register(this, weka.gui.beans.BeanConnection.class, "BeanConnection"); m_CustomMethods.register(this, weka.gui.beans.BeanVisual.class, "BeanVisual"); m_CustomMethods.register(this, weka.gui.beans.Saver.class, "BeanSaver"); m_CustomMethods.register(this, weka.gui.beans.MetaBean.class, "MetaBean"); classnames = ConverterUtils.getFileLoaders(); for (i = 0; i < classnames.size(); i++) m_CustomMethods.register(this, Class.forName(classnames.get(i)), "Loader"); classnames = ConverterUtils.getFileSavers(); for (i = 0; i < classnames.size(); i++) m_CustomMethods.register(this, Class.forName(classnames.get(i)), "Saver"); // other variables m_BeanInstances = null; m_BeanInstancesID = null; m_CurrentMetaBean = null; m_IgnoreBeanConnections = true; m_BeanConnectionRelation = null; } /** * traverses over all BeanInstances (or MetaBeans) and stores them in a vector * (recurses into MetaBeans, since the sub-BeanInstances are not visible) * @param list the BeanInstances/MetaBeans to traverse */ protected void addBeanInstances(Vector list) { int i; BeanInstance beaninst; for (i = 0; i < list.size(); i++) { if (list.get(i) instanceof BeanInstance) { beaninst = (BeanInstance) list.get(i); m_BeanInstancesID.add(new Integer(m_BeanInstances.size())); m_BeanInstances.add(beaninst); if (beaninst.getBean() instanceof MetaBean) addBeanInstances(((MetaBean) beaninst.getBean()).getBeansInSubFlow()); } else if (list.get(i) instanceof MetaBean) { addBeanInstances(((MetaBean) list.get(i)).getBeansInSubFlow()); } else { System.out.println("addBeanInstances does not support Vectors of class '" + list.get(i) + "'!"); } } } /** * enables derived classes to due some pre-processing on the objects, that's * about to be serialized. Right now it only returns the object. * * @param o the object that is serialized into XML * @return the possibly altered object * @throws Exception if post-processing fails */ protected Object writePreProcess(Object o) throws Exception { o = super.writePreProcess(o); // gather all BeanInstances, also the ones in MetaBeans m_BeanInstances = new Vector(); m_BeanInstancesID = new Vector(); switch (getDataType()) { case DATATYPE_LAYOUT: addBeanInstances(BeanInstance.getBeanInstances()); break; case DATATYPE_USERCOMPONENTS: addBeanInstances((Vector) o); break; default: System.out.println("writePreProcess: data type '" + getDataType() + "' is not recognized!"); break; } return o; } /** * enables derived classes to add other properties to the DOM tree, e.g. * ones that do not apply to the get/set convention of beans. only implemented * with empty method body. * * @param o the object that is serialized into XML * @throws Exception if post-processing fails */ protected void writePostProcess(Object o) throws Exception { Element root; NodeList list; Element conns; Element child; int i; // since not all BeanConnections get saved to XML (e.g., MetaBeans in the // UserToolBar) if one saves a layout, the numbering in the Vector of the // BeanConnections is not correct. The "name" attribute of the nodes has // to be modified if (getDataType() == DATATYPE_LAYOUT) { root = m_Document.getDocument().getDocumentElement(); conns = (Element) root.getChildNodes().item(INDEX_BEANCONNECTIONS); list = conns.getChildNodes(); for (i = 0; i < list.getLength(); i++) { child = (Element) list.item(i); child.setAttribute(ATT_NAME, "" + i); } } } /** * additional pre-processing can happen in derived classes before the * actual reading from XML (working on the raw XML). right now it does * nothing with the document, only empties the help-vector for the * BeanInstances and reads the IDs for the BeanInstances, s.t. the correct * references can be set again * * @param document the document to pre-process * @return the processed object * @throws Exception if post-processing fails * @see #m_BeanInstances */ protected Document readPreProcess(Document document) throws Exception { NodeList list; int i; Element node; String clsName; Vector children;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -