datasetutils.java
来自「java实现浏览器等本地桌面的功能」· Java 代码 · 共 645 行 · 第 1/2 页
JAVA
645 行
/* * $Id: DataSetUtils.java,v 1.8 2005/10/10 17:00:59 rbair 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.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;import java.io.StringReader;import java.math.BigDecimal;import java.util.Date;import java.util.Set;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.jdesktop.dataset.provider.sql.JDBCDataConnection;import org.jdesktop.dataset.provider.sql.SQLCommand;import org.jdesktop.dataset.provider.sql.SQLDataProvider;import org.jdesktop.dataset.provider.sql.TableCommand;import org.xml.sax.Attributes;import org.xml.sax.InputSource;import org.xml.sax.helpers.DefaultHandler;/** * * @author rbair */public class DataSetUtils { /** Creates a new instance of DataSetUtils */ private DataSetUtils() { } /** * Checks to see if the given name is valid. If not, then return false.<br> * A valid name is one that follows the Java naming rules for * indentifiers, <b>except</b> that Java reserved words can * be used, and the name may begin with a number. */ static boolean isValidName(String name) { return !name.matches(".*[\\s]"); } public static String getXmlSchema(DataSet ds) { StringBuilder buffer = new StringBuilder(); buffer.append("<?xml version=\"1.0\" standalone=\"yes\" ?>\n"); buffer.append("<xs:schema id=\""); buffer.append(ds.getName()); buffer.append("\" targetNamespace=\"http://jdesktop.org/tempuri/"); buffer.append(ds.getName()); buffer.append(".xsd\" xmlns=\"http://javadesktop.org/tempuri/"); buffer.append(ds.getName()); buffer.append(".xsd\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" attributeFormDefault=\"qualified\" elementFormDefault=\"qualified\">\n"); buffer.append("\t<xs:element name=\""); buffer.append(ds.getName()); buffer.append("\">\n"); buffer.append("\t\t<xs:complexType>\n"); buffer.append("\t\t\t<xs:choice maxOccurs=\"unbounded\">\n"); for (DataTable table : ds.getTables()) { if (!(table instanceof DataRelationTable)) { buffer.append("\t\t\t\t<xs:element name=\""); buffer.append(table.getName()); buffer.append("\" appendRowSupported=\""); buffer.append(table.isAppendRowSupported()); buffer.append("\" deleteRowSupported=\""); buffer.append(table.isDeleteRowSupported()); buffer.append("\">\n"); buffer.append("\t\t\t\t\t<xs:complexType>\n"); buffer.append("\t\t\t\t\t\t<xs:sequence>\n"); for (DataColumn col : table.getColumns()) { buffer.append("\t\t\t\t\t\t\t<xs:element name=\""); buffer.append(col.getName()); buffer.append("\" type=\""); if (col.getType() == String.class || col.getType() == Character.class) { buffer.append("xs:string"); } else if (col.getType() == BigDecimal.class) { buffer.append("xs:decimal"); } else if (col.getType() == Integer.class) { buffer.append("xs:integer"); } else if (col.getType() == Boolean.class) { buffer.append("xs:boolean"); } else if (col.getType() == Date.class) { buffer.append("xs:dateTime"); } else if (col.getType() == Byte.class) { buffer.append("xs:unsignedByte"); } else { System.out.println("Couldn't find type for xsd for Class " + col.getType()); } if (col.getDefaultValue() != null) { buffer.append("\" default=\""); buffer.append(col.getDefaultValue()); } if (!col.isRequired()) { buffer.append("\" minOccurs=\"0"); } buffer.append("\" keyColumn=\""); buffer.append(col.isKeyColumn()); buffer.append("\" readOnly=\""); buffer.append(col.isReadOnly()); if (col.getExpression() != null && !(col.getExpression().trim().equals(""))) { buffer.append("\" expression=\""); buffer.append(col.getExpression()); } buffer.append("\" />\n"); } buffer.append("\t\t\t\t\t\t</xs:sequence>\n"); buffer.append("\t\t\t\t\t</xs:complexType>\n"); buffer.append("\t\t\t\t</xs:element>\n"); } } buffer.append("\t\t\t</xs:choice>\n"); buffer.append("\t\t</xs:complexType>\n"); buffer.append("\t\t<xs:annotation>\n"); buffer.append("\t\t\t<xs:appinfo>\n"); //write the relations out for (DataRelation r : ds.getRelations()) { buffer.append("\t\t\t\t<dataRelation name=\""); buffer.append(r.getName()); buffer.append("\" parentColumn=\""); DataColumn col = r.getParentColumn(); if (col != null) { buffer.append(col.getTable().getName()); buffer.append("."); buffer.append(col.getName()); } buffer.append("\" childColumn=\""); col = r.getChildColumn(); if (col != null) { buffer.append(col.getTable().getName()); buffer.append("."); buffer.append(col.getName()); } buffer.append("\" />\n"); } //write the data relation tables out for (DataTable table : ds.getTables()) { if (table instanceof DataRelationTable) { DataRelationTable drt = (DataRelationTable)table; buffer.append("\t\t\t\t<dataRelationTable name=\""); buffer.append(drt.getName()); buffer.append("\" relation=\""); DataRelation dr = drt.getRelation(); buffer.append(dr == null ? "" : dr.getName()); buffer.append("\" parentSelector=\""); DataSelector sel = drt.getParentSelector(); buffer.append(sel == null ? "" : sel.getName()); buffer.append("\" parentTable=\""); DataTable parent = drt.getParentTable(); buffer.append(parent == null ? "" : parent.getName()); buffer.append("\" />\n"); } } //write the data values out for (DataValue value : ds.getValues()) { buffer.append("\t\t\t\t<dataValue name=\""); buffer.append(value.getName()); buffer.append("\" expression=\""); if (value.getExpression() != null) { buffer.append(value.getExpression()); } buffer.append("\" />\n"); } //close the annotation section buffer.append("\t\t\t</xs:appinfo>\n"); buffer.append("\t\t</xs:annotation>\n"); //close the document down buffer.append("\t</xs:element>\n"); buffer.append("</xs:schema>\n"); return buffer.toString(); } public static DataSet createFromXmlSchema(String schema) { return createFromXmlSchema(new StringReader(schema)); } public static DataSet createFromXmlSchema(File f) throws FileNotFoundException { return createFromXmlSchema(new FileInputStream(f)); } public static DataSet createFromXmlSchema(InputStream is) { return createFromXmlSchema(new InputStreamReader(is)); } public static DataSet createFromXmlSchema(Reader schema) { DataSet ds = new DataSet(); //set up an XML parser to parse the schema try { SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); InputSource is = new InputSource(schema); parser.parse(is, new DataSetParser(ds)); } catch (Exception e) { e.printStackTrace(); } return ds; } /** * parses the document. * * The xs:element tag is used repeatedly. With a depth of 0, it is the * DataSet element. With a depth of 1, it is a DataTable, and with a depth of * 2 it is a DataColumn */ private static final class DataSetParser extends DefaultHandler { public int elementDepth = 0; private Attributes attrs; private DataSet ds; private DataTable table; private DataColumn column; private DataProvider dataProvider; public DataSetParser(DataSet ds) { this.ds = ds == null ? new DataSet() : ds; } public DataSet getDataSet() { return ds; } public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes atts) throws org.xml.sax.SAXException { this.attrs = atts; if (qName.equals("xs:element")) { elementDepth++; switch(elementDepth) { case 1: //this is a DataSet ds.setName(attrs.getValue("name")); break; case 2: //this is a table tag table = ds.createTable(attrs.getValue("name")); String val = attrs.getValue("appendRowSupported"); table.setAppendRowSupported(val == null || val.equalsIgnoreCase("true")); val = attrs.getValue("deleteRowSupported"); table.setDeleteRowSupported(val == null || val.equalsIgnoreCase("true")); break; case 3: //this is a column tag column = table.createColumn(attrs.getValue("name")); //set the required flag val = attrs.getValue("minOccurs"); if (val != null && val.equals("")) { column.setRequired(true); } //find out if this is a keycolumn val = attrs.getValue("keyColumn"); column.setKeyColumn(val == null ? false : val.equalsIgnoreCase("true")); //find out if this column is readOnly val = attrs.getValue("readOnly"); column.setReadOnly(val == null ? false : val.equalsIgnoreCase("true")); //grab the default, if one is supplied String defaultValue = attrs.getValue("default"); //TODO This will require some kind of type conversion //get the type. Convert from XSD types to java types val = attrs.getValue("type"); if (val.equals("xs:string")) { column.setType(String.class); if (defaultValue != null && !defaultValue.equals("")) { column.setDefaultValue(defaultValue); } } else if (val.equals("xs:decimal")) { column.setType(BigDecimal.class); if (defaultValue != null && !defaultValue.equals("")) { column.setDefaultValue(new BigDecimal(defaultValue)); } } else if (val.equals("xs:integer") || val.equals("xs:int")) { column.setType(Integer.class); if (defaultValue != null && !defaultValue.equals("")) { column.setDefaultValue(new Integer(defaultValue)); } } else if (val.equals("xs:boolean")) { column.setType(Boolean.class); if (defaultValue != null && !defaultValue.equals("")) { column.setDefaultValue(Boolean.parseBoolean(defaultValue)); } } else if (val.equals("xs:date") || val.equals("xs:time") || val.equals("xs.dateTime")) { column.setType(Date.class); if (defaultValue != null && !defaultValue.equals("")) { column.setDefaultValue(new Date(Date.parse(defaultValue))); } } else if (val.equals("xs:unsignedByte")) { column.setType(Byte.class); if (defaultValue != null && !defaultValue.equals("")) { column.setDefaultValue(new Byte(defaultValue)); } } else { System.err.println("unexpected classType: '" + val + "'"); } //set the column expression val = attrs.getValue("expression");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?