⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlreader.java

📁 数据仓库工具
💻 JAVA
字号:
/*
    Copyright (C) 2003  Together

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package org.webdocwf.util.xml;

//xml imports
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.enhydra.xml.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import java.sql.*;
import java.io.File;
import java.util.ArrayList;

/**
 * Class load existing XML file, creating DOM from file or creating
 * new DOM.Class has methods for reading data from XML file.
 *
 * @author     Zoran Milakovic
 */
public class XmlReader
{
  private String[] columnNames;
  private String[] columnValues;
  private String tableName;

  /**
   * Document made from XML file, and in which will
   * be made changes.Document will be saved in XML file.
   */
  private SearchElement searchDocument;
  private Document document;
  /**
   * Full path of the XML file.
   */
  private String fileName;
  /**
   * Constructor will build Document from the specified file
   *  if file exist, or will create new Document if file not exist.
   *
   * @param fileName full pathname of the XML file
   * @throws SQLException
   */
  public XmlReader(String fileName) throws SQLException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
        this.fileName = fileName;
        File file = new File( fileName );
        DocumentBuilder builder = factory.newDocumentBuilder();
        try {
          this.document = builder.parse( file );
        } catch( Exception e ) {
          throw new SQLException("Error while parsing XML file ! : "+e.getMessage());
        }
        this.searchDocument = (SearchElement)SearchElement.newInstance( document );
    } catch( Exception e ) { throw new SQLException("Error in creating DOM : "+e.getMessage()); }
  }




  private ArrayList rset = new ArrayList();

  /**
   * Gets data from database.Method will fill array list which will be result set.
   * ArrayList will contain arrays of strings.Every array of string will present
   * one row in database.
   *
   * @param tableName Name of table.
   * @param columnNames Names of columns from which will be select data.
   * @param whereColumnNames Names of columns in where conditions.
   * @param whereColumnValues Values of conditions.
   * @throws SQLException
   */
  public void select(String tableName , String[] columnNames , String[] whereColumnNames , String[] whereColumnValues) throws SQLException {
   try {
    NodeList tableRows = searchDocument.getSubElementsByTagName("dml/"+tableName);
    for(int i = 0; i < tableRows.getLength(); i++) {
      boolean isMatch = true;
      if( whereColumnNames != null && whereColumnValues != null ) {
        for(int k = 0; k < whereColumnNames.length; k++) {
          NodeList columns = ( (SearchElement)tableRows.item(i) ).getSubElementsByCondition(whereColumnNames[k]+"="+whereColumnValues[k]);
          if( columns.getLength() == 0 )
            isMatch = false;
        }
      }
      if( isMatch ) {
        ArrayList colValuesList = new ArrayList();
        colValuesList.clear();
          //columnNames has names of ALL columns, even if some of them are not have tag in xml file.
          //This is posible because all column names are stored in CREATE TABLE statement
        for(int k = 0; k < columnNames.length; k++) {
          NodeList columns = ( (SearchElement)tableRows.item(i) ).getSubElementsByTagName(columnNames[k]);
             //it is posible that variable columns has zero length,if some column tags are missing
             //in that case, column has null value
          if( columns.getLength() != 0 ) {
            Node column = columns.item(0);
            Node textNode = column.getFirstChild();
            if( textNode == null )
              colValuesList.add( "null" );
            else
              colValuesList.add( formatString(textNode.getNodeValue()) );
          } else {
            colValuesList.add( "null" );
          }
        }
        rset.add( colValuesList.toArray(new String[0]) );
        int y = 0;
      }
    }
    }catch(Exception e) {
      throw new SQLException("Error in select : "+e.getMessage());
    }
  }

  /**
   * Gets table names from database.
   *
   * @throws SQLException
   */
  public void selectTableNames() throws SQLException {
    try {
      ArrayList tableNames = new ArrayList();
      ArrayList tableNamesAll = new ArrayList();

      NodeList sqlStatements = searchDocument.getSubElementsByTagName("ddl");
      XmlSqlParser parser = new XmlSqlParser();
      for( int i = 0; i < sqlStatements.getLength(); i++ ) {
        Node node = sqlStatements.item(i);
        parser.parse( node.getFirstChild().toString() );
        String tableName = parser.getTableName();
        if ( !tableNamesAll.contains( tableName ) ) {
          tableNamesAll.add( tableName );
          tableNames.clear();
          tableNames.add( tableName );
          rset.add( tableNames.toArray(new String[0]));
        }
      }

      NodeList allRowTableNames = ((Element)( searchDocument.getSubElementsByTagName("dml").item(0) )).getChildNodes();
      for(int i = 0;i < allRowTableNames.getLength();i++) {
        if( allRowTableNames.item(i).getNodeType() != 3 ) {
          String tableName = allRowTableNames.item(i).getNodeName();
          if( !tableNamesAll.contains( tableName ) ) {
            tableNamesAll.add( tableName );
            tableNames.clear();
            tableNames.add( tableName );
            rset.add( tableNames.toArray(new String[0]));
          }
        }
      }
    }catch(Exception e) {
      throw new SQLException("Error in selectTableNames : "+e.getMessage());
    }
  }

  private String formatString(String str) {
    String retVal = str;
    retVal = Utils.replaceAll( retVal, XmlSqlParser.equalEscape, "=" );
    retVal = Utils.replaceAll( retVal, XmlSqlParser.atEscape, "@" );
    retVal = Utils.replaceAll( retVal, XmlSqlParser.slashEscape, "/" );
    return retVal;
  }

  /**
   *
   * @return list with results
   */
  public ArrayList getResultSet() {
    return this.rset;
  }

}

⌨️ 快捷键说明

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