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

📄 dom2db.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////                 . . . i n   j a h i a   w e   t r u s t . . .////////  DOM2DB////  NK      25.07.2001//package org.jahia.utils.xml;import java.io.*;import java.sql.*;import java.util.*;import org.w3c.dom.*;import org.xml.sax.*;import javax.xml.parsers.SAXParser;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import javax.xml.transform.*;import javax.xml.transform.sax.*;import javax.xml.transform.dom.*;import javax.xml.transform.stream.*;import org.jahia.exceptions.*;import org.jahia.data.xml.JahiaXmlDocument;import org.jahia.utils.*;               	// JahiaConsole/** * Tools used to load a DOM Document created using DB2DOM. * * @author Khue Nguyen * @see org.jahia.tools.xml.DB2DOM * @version 1.0 */public class DOM2DB extends JahiaXmlDocument {	private static final String CLASS_NAME		= "DOM2DB";	private static final String DATABASE_TAG 	= "database";	private static final String TABLE_TAG 		= "table";	private static final String ROW_TAG 		= "row";	private static final String COLUMN_TAG 		= "column";	private static final String ID_ATTRIB		= "id";	private static final String NAME_ATTRIB		= "name";	private static final String TYPE_ATTRIB		= "type";	Vector mTables = null;   	//--------------------------------------------------------------------------	/**     * Constructor     *     * @param (String) path, the full path to the application.xml file     */    public DOM2DB (String docPath) throws JahiaException    {      super(docPath);      extractDocumentData();    }   	//--------------------------------------------------------------------------   	/**     * Constructor using a gived parser     *     * @param (String) path, the full path to a xml file     * @param (Parser) parser, the parser to use     */	public DOM2DB (String docPath, org.xml.sax.helpers.ParserAdapter parser)	throws JahiaException {		super(docPath,parser);		extractDocumentData();	}   	//--------------------------------------------------------------------------	/**     * Extracts data from the application.xml file. Build the JahiaWebAppsWarPackage object     * to store extracted data     */   	public void extractDocumentData() throws JahiaException {    	//JahiaConsole.println(CLASS_NAME+".extractDocumentData","started");       	if (m_XMLDocument == null) {        	throw new JahiaException( CLASS_NAME,                                    "The Document is null !",                                       JahiaException.ERROR,                                       JahiaException.ERROR);       	}		if (!m_XMLDocument.hasChildNodes()) {	    	throw new JahiaException(	CLASS_NAME,                                    "Main document node has no children",                                    JahiaException.ERROR,                                    JahiaException.ERROR);      	}		Element docElNode = (Element) m_XMLDocument.getDocumentElement();       	if (!docElNode.getNodeName().equalsIgnoreCase(DATABASE_TAG)) {        	throw new JahiaException(  "Invalid Document format",                                        "Tag <"+DATABASE_TAG+"> is not present as starting tag in file",                                        JahiaException.ERROR,                                        JahiaException.ERROR);       	}       	mTables = XMLParser.getChildNodes(docElNode,TABLE_TAG);      	//JahiaConsole.println(CLASS_NAME+".extractDocumentData","done");   }   	//--------------------------------------------------------------------------   	/**     * Returns an enumeration of all table names     *     * @return Vector the list of table names     */	public Vector getTableNames()	throws JahiaException {		Vector v = new Vector();		int size = mTables.size();		for ( int i=0 ; i<size ; i++ ){			Element el = (Element)mTables.get(i);			v.add(XMLParser.getAttributeValue(el,NAME_ATTRIB));		}		return v;	}   	//--------------------------------------------------------------------------   	/**     * Returns an enumeration of the tables     *     * @return Enumeration the list of tables     */	public Enumeration getTables()	throws JahiaException {		return mTables.elements();	}   	//--------------------------------------------------------------------------   	/**     * Write the contents of a table in DB     *	 * @param (Element) the Element table	 * @param (Statement) the db statement to use to insert data in table     */	public static void sqlInsert(Element table, Statement stmt)	throws JahiaException,SQLException {		sqlInsert(table, stmt, null);	}   	//--------------------------------------------------------------------------   	/**     * Write the contents of a table in DB     *	 * @param (Element) the Element table	 * @param (Statement) the db statement to use to insert data in table	 * @param (DBRowDataFilter) an optional data filter. Give a null instance if you want no filtering     */	public static void sqlInsert(Element table, Statement stmt, DBRowDataFilter filter )	throws JahiaException,SQLException {		String tableName = XMLParser.getAttributeValue(table,NAME_ATTRIB);		String insertQuery = "INSERT INTO " + tableName + " (";		JahiaConsole.println(CLASS_NAME+".sqlInsert"," table name=" + tableName );		String colName = null;		String colVal = null;		int colType = 0;		Vector rows = XMLParser.getChildNodes(table,ROW_TAG);		Vector cols = null;		Element r = null;		Element c = null;		int nbRows = rows.size();		int nbCols = 0;		boolean isText = false;		StringBuffer buff = null;		StringBuffer valBuff = null;		StringBuffer colBuff = null;		for ( int i=0 ; i<nbRows ; i++ ){			Hashtable vals = new Hashtable();			r = (Element)rows.get(i);			buff = new StringBuffer(insertQuery);			valBuff = new StringBuffer();			colBuff = new StringBuffer();			cols = XMLParser.getChildNodes(r,COLUMN_TAG);			nbCols = cols.size();			for ( int j=0 ; j<nbCols ; j++ ){				isText = false;				c = (Element)cols.get(j);				colName = XMLParser.getAttributeValue(c,NAME_ATTRIB);				colType = Integer.parseInt(XMLParser.getAttributeValue(c,TYPE_ATTRIB));				//JahiaConsole.println(CLASS_NAME+".sqlInsert"," column node : name=" + colName );                Node textNode = c.getFirstChild();				if ( textNode == null ){					colVal = "";				} else if (textNode.getNodeType() == Node.TEXT_NODE) {                	colVal = textNode.getNodeValue();                } else {	                throw new JahiaException(CLASS_NAME,	                     "Value of column is not in correct format, should only be text",	                     JahiaException.ERROR,	                     JahiaException.ERROR);				}				vals.put(colName.toLowerCase(),colVal);                if( (colType == Types.VARCHAR)                	|| (colType == Types.CHAR)                	|| (colType == Types.BLOB)                	|| (colType == Types.LONGVARCHAR) ) {                    isText = true;                } else if ( colType == 8 ){                	Float f = new Float(colVal);                	colVal = (new Long(f.longValue())).toString();                } else {                    try {                        int testConversion = Integer.parseInt(colVal);                    } catch (NumberFormatException nfe) {                        //isText = true;                        colVal = "0";                    }                }				// build columns list and values                colBuff.append(colName);                if(isText) {                    valBuff.append("'");                }                valBuff.append(JahiaTools.quote(JahiaTools.text2XMLEntityRef(colVal,1)));                if(isText) {                    valBuff.append("'");                }                if(j<(nbCols-1)) {                    valBuff.append(", ");                    colBuff.append(", ");                }			}			buff.append(colBuff.toString().toLowerCase());			buff.append(") VALUES(");			buff.append(valBuff.toString());			buff.append(")");			if ( filter == null || filter.inValue(vals) ){				if ( valBuff.length()>0 ){					stmt.executeUpdate(buff.toString());				}			}			//JahiaConsole.println(CLASS_NAME+".sqlInsert",buff.toString());		}	}}

⌨️ 快捷键说明

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