dataset.java

来自「java实现浏览器等本地桌面的功能」· Java 代码 · 共 1,035 行 · 第 1/3 页

JAVA
1,035
字号
/* * $Id: DataSet.java,v 1.21 2005/10/15 11:43:19 pdoubleya Exp $ * * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. *  * This library 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 * Lesser General Public License for more details. *  * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */package org.jdesktop.dataset;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeSupport;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import java.math.BigDecimal;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.logging.Logger;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.xpath.XPath;import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathFactory;import org.jdesktop.dataset.event.TableChangeEvent;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.NodeList;/** * <p>A DataSet is the top-level class for managing multiple {@link DataTable}s as a  * group, allowing explicit support for master-detail relationships. A DataSet * contains one or more DataTables, one or more {@link DataRelation}s, and one or more  * {@link DataValue}s. Through the DataSet API one has a sort of unified access to all * these elements in a single group. As a canonical example, a single DataSet could * have DataTables for Customers, Orders, and OrderItems, and the relationships * between them. * * <p>The DataSet internal structure read from a DataSet XML  * schema using the {@link #createFromSchema(String)} method, which also supports * reading from an {@link InputStream} or {@link File}. The internal structure * can then be persisted back as an XML schema by retrieving its String form * using {@link #getSchema()} and then saving that. * * <p>Separately, all the data in a DataSet (e.g. rows in the internal DataTables) * can be read ({@link #readXml(String)}) or written ({@link #writeXml()}). *  * <p>DataSet has standard methods for adding or removing single DataTables, * DataRelationTables, and DataValues. Note that these instances are automatically * tracked, so if their names are changed through their own API, that name change * is reflected automatically in the DataSet. *  * @see DataTable * @see DataRelationTable * @see DataValue * @see DataRelation * * @author rbair */public class DataSet {    /**      * Flag used for {@link #writeXML(OutputControl)} to indicate whether     * all rows, or just modified rows should be spit out.     */    // TODO: replace with some kind of filter; maybe a functor (PWW 04/29/05)    public enum OutputControl { ALL_ROWS, MODIFIED_ONLY };        /**     * The Logger     */    private static final Logger LOG = Logger.getLogger(DataSet.class.getName());        //protected for testing    /** Prefix used by the name generator for automatically-generated DataSet names. */    protected static final String DEFAULT_NAME_PREFIX = "DataSet";        /** The shared instance of the NameGenerator for DataSets not assigned a name. */    private static final NameGenerator NAMEGEN = new NameGenerator(DEFAULT_NAME_PREFIX);        //used for communicating changes to this JavaBean, especially necessary for    //IDE tools, but also handy for any other component listening to this dataset    private PropertyChangeSupport pcs = new PropertyChangeSupport(this);    /** The name of this DataSet; should be unique among all instantiated DataSets. */    private String name;        /** DataTables in this set, keyed by table name. */    private Map<String,DataTable> tables = new HashMap<String,DataTable>();        /** DataRelations in this set, keyed by relation name. */    private Map<String,DataRelation> relations = new HashMap<String,DataRelation>();    /** DataValues in this set, keyed by value name. */    private Map<String,DataValue> values = new HashMap<String,DataValue>();    private Parser parser = new Parser();            /**      * A PropertyChangeListener instance--inner class--used to track changes     * to table, relation and value names.     */    private NameChangeListener nameChangeListener = new NameChangeListener();        /**     * Instantiates a DataSet with an automatically-generated name.     */    public DataSet() {        parser.bindThis(this);        parser.setUndecoratedDecimal(true);        setName(NAMEGEN.generateName(this));    }        /**     * Instantiates a DataSet with a specific name; the name can be useful     * when persisting the DataSet or when working with multiple DataSets.     * See {@link #setName(String)}     * for comments about DataSet naming.     *     * @param name The name for the DataSet.     */    public DataSet(String name) {        parser.bindThis(this);        parser.setUndecoratedDecimal(true);        setName(name);    }    /**     * Assigns a new name to this DataSet. Names are not controlled for     * uniqueness, so the caller must take care to not use     * the same name for more than one DataSet. Any String that is a valid     * Java identifier can be used; see comments on {@link DataSetUtils#isValidName(String)}..     *     * @param name The new name for the DataSet.     */    public void setName(String name) {        if (this.name != name) {            assert DataSetUtils.isValidName(name);            String oldName = this.name;            this.name = name;            pcs.firePropertyChange("name", oldName, name);        }    }        /**     * Returns the current name for the DataSet.     *     * @return the current name for the DataSet.     */    // TODO: should we check for (static) uniqueness of names? (PWW 04/28/04)    public String getName() {        return name;    }        /**     * Creates a new DataTable with an automatically-generated name,      * and adds it to this DataSet; the      * table will belong to this DataSet.     *     * @return a new DataTable assigned to this DataSet.     */    public DataTable createTable() {        return createTable(null);    }    /**     * Creates a new, named DataTable, and adds it to this DataSet; the      * table will belong to this DataSet. If the DataSet already has a table     * with that name, the new, empty DataTable will take its place.     *     * @param name The new DataTable's name.     * @return a new unnamed DataTable assigned to this DataSet.     */    // TODO: error if table already exists--can orphan an existing table for the same name (PWW 04/27/05)    public DataTable createTable(String name) {        DataTable table = new DataTable(this, name);        table.addPropertyChangeListener("name",  nameChangeListener);        tables.put(table.getName(), table);        return table;    }        /**     * Creates a new DataRelationTable with an automatically-generated name,      * and adds it to this DataSet; the      * table will belong to this DataSet.     *     * @return a new DataRelationTable assigned to this DataSet.     */    public DataRelationTable createRelationTable() {        return createRelationTable(null);    }        /**     * Creates a new, named DataRelationTable, and adds it to this DataSet; the      * table will belong to this DataSet. If the DataSet already has a table     * with that name, the new, empty DataRelationTable will take its place.     *     * @param name The new DataRelationTable's name.     * @return a new unnamed DataRelationTable assigned to this DataSet.     */    // TODO: error if table already exists--can orphan an existing table for the same name (PWW 04/27/05)    public DataRelationTable createRelationTable(String name) {        DataRelationTable table = new DataRelationTable(this, name);        table.addPropertyChangeListener("name",  nameChangeListener);        tables.put(table.getName(), table);        return table;    }        /**     * Creates a new DataRelation with an automatically-generated name,      * and adds it to this DataSet; the      * table will belong to this DataSet.     *     * @return a new DataRelation assigned to this DataSet.     */    public DataRelation createRelation() {        return createRelation(null);    }        /**     * Creates a new, named DataRelation, and adds it to this DataSet; the      * table will belong to this DataSet. If the DataSet already has a table     * with that name, the new, empty DataRelation will take its place.     *     * @param name The new DataRelation's name.     * @return a new unnamed DataRelation assigned to this DataSet.     */    // TODO: error if relation already exists--can orphan an existing relation for the same name (PWW 04/27/05)    public DataRelation createRelation(String name) {        DataRelation relation = new DataRelation(this, name);        relation.addPropertyChangeListener("name",  nameChangeListener);        relations.put(relation.getName(), relation);        return relation;    }        /**     * Creates a new DataValue with an automatically-generated name,      * and adds it to this DataSet; the      * table will belong to this DataSet.     *     * @return a new DataValue assigned to this DataSet.     */    public DataValue createValue() {        return createValue(null);    }            /**     * Creates a new, named DataValue, and adds it to this DataSet; the      * table will belong to this DataSet. If the DataSet already has a table     * with that name, the new, empty DataValue will take its place.     *     * @param name The new DataValue's name.     * @return a new unnamed DataValue assigned to this DataSet.     */    // TODO: error if value already exists--can orphan an existing value for the same name (PWW 04/27/05)    public DataValue createValue(String name) {        DataValue value = new DataValue(this, name);        value.addPropertyChangeListener("name", nameChangeListener);        values.put(value.getName(), value);        return value;    }    /**      * Removes a given DataTable from this DataSet.      * @param table The DataTable instance to remove.     */    public void dropTable(DataTable table) {        dropTable(table.getName());    }        /**      * Removes a given DataTable from this DataSet.      * @param tableName The name of the table to remove.     */    public void dropTable(String tableName) {        //drop the DataTable. Remove any remaining references to it by the        //DataRelations or DataRelationTables        DataTable table = tables.remove(tableName);        if (table != null) {            table.removePropertyChangeListener("name",  nameChangeListener);            for (DataRelation r : relations.values()) {                DataColumn col = r.getChildColumn();                if (col != null && col.getTable() == table) {                    r.setChildColumn(null);                }                col = r.getParentColumn();                if (col != null && col.getTable() == table) {                    r.setParentColumn(null);                }            }            for (DataTable t : tables.values()) {                if (t instanceof DataRelationTable) {                    DataRelationTable drt = (DataRelationTable)t;                    if (drt.getParentTable() == table) {                        drt.setParentTable(null);                    }                    if (drt.getParentSelector() != null && drt.getParentSelector().getTable() == table) {                        drt.setParentSelector(null);                    }                }            }        }    }        /**      * Removes a given DataRelationTable from this DataSet.      * @param table The DataRelationTable instance to remove.     */    public void dropRelationTable(DataRelationTable table) {        dropTable(table.getName());    }        /**      * Removes a given DataRelationTable from this DataSet.      * @param tableName The name of the table to remove.     */    public void dropRelationTable(String tableName) {        dropTable(tableName);    }        /**      * Removes a given DataRelation from this DataSet.      * @param relation The DataRelation instance to remove.     */    public void dropRelation(DataRelation relation) {        dropRelation(relation.getName());

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?