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

📄 xmlconverter.java

📁 发泄网! 发泄网! 发泄网! 发泄网! 发泄网! 发泄网! 发泄网! 发泄网!
💻 JAVA
字号:
    /* CRMS, customer relationship management system    Copyright (C) 2003  Service To Youth Council    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA    For further information contact the SYC ICT department on GPL@syc.net.au    98 Kermode Street    North Adelaide    South Australia    SA 5006     +61 (0)8 8367 0755    *//* * XMLConverter.java * * Created on 13 June 2003, 14:49 */package crms.util;import java.sql.*;import org.apache.log4j.Logger;import java.util.*;import crms.dao.*;/** * * @author  Administrator */public class XMLConverter {    public static Logger logger = Logger.getLogger(XMLConverter.class);        /** Creates a new instance of XMLConverter */    public XMLConverter() {    }        /**     * Creates an XML document in a StringBuffer that represents the ResultSet     * returned from the database.     *     * @param collectionName Sets the tag text to use if this query will return a collection of objects. This     * XML tag will be the parent of any item nodes of the collection.     * @param elementName The XML tag to use to name each element that is created as a result of     * the data produced.     * @param rs Resultset produced from sql query that will be converted to XML.     * @return A StringBuffer object containing the automatically created XML from the result     * of the database query. Note that database field names are used to create     * the inner tag names used in this XML.     */        public static StringBuffer createXMLFromResultset(String collectionName, String elementName, ResultSet rs) {        StringBuffer buf = new StringBuffer();                try {                        if (rs !=  null) {                /**                 * A note about the ResultSetMetaData:                 * - only call each method once - subsequent                 * calls will cause a Column Index out of Range                 * Postgres error.                 * Also, column numbers are indexed from 1, not 0.                 */                ResultSetMetaData meta = rs.getMetaData();                int count = 0;                                if (collectionName != null) {                    buf.append("    <" + collectionName + ">\n");                }                int columnCount = meta.getColumnCount();                                while (rs.next()) {                    if (elementName != null) {                        buf.append("        <" + elementName + ">\n");                    }                        for (int i=0; i < columnCount; i++) {                        String colName = meta.getColumnName(i+1);                        String tableName = meta.getTableName(i+1);                                        buf.append("            <column name='" + colName + "'>");                                                String columnClass = meta.getColumnClassName(i+1);                        if (tableName.equals("DataStore")) {                            buf.append( AbstractDAO.decode((String) rs.getObject(i+1)));                        } else {                            buf.append(rs.getObject(i+1));                        }                                                buf.append("</column>\n");                    }                    if (elementName != null) {                        buf.append("    </" + elementName + ">\n");                    }                    ++count;                }                if (collectionName != null) {                    buf.append("    </" + collectionName + ">\n");                }            }        }        catch (SQLException ex) {            ex.printStackTrace();            throw new RuntimeException( ex.getMessage() ) ;        }        finally {            try {                if (rs != null) {                    rs.close();                }            }            catch (SQLException ex) {                ex.printStackTrace();                throw new RuntimeException( ex.getMessage() ) ;            }                   }        logger.debug("Returning XML from query...");        return buf;    }                /**     * Creates suitable XML for a supplied Map Array. A map array is an array     * of Map objects. This is used to encapsulate tabular XML data where the     * Maps themselves can be considered rows, and the elements within the     * map are the column values indexed by the map keys that correspond to      * column names.     *     * @param collectionName Enclosing XML tag name.     * @param elementName Tag name given to individual XML elements.     * @param source Array of Maps.     * @return StringBuffer containing XML representation.     */    public static StringBuffer createXMLFromMapArray(String collectionName, String elementName, Map[] source, TypeConverter[] converters) {        StringBuffer buf = new StringBuffer();                if (source !=  null && source.length > 0) {            int count = 0;            if (collectionName != null) {                buf.append("    <" + collectionName + ">\n");            }            Map firstEntry = source[0];            for (int i=0; i < source.length; i++ ) {                Map entry = source[i];                if (elementName != null) {                    buf.append("        <" + elementName + ">\n");                }                Iterator it = entry.keySet().iterator();                while (it.hasNext()) {                    String colName = (String) it.next();                        buf.append("            <column name='" + colName + "'>");                                        Object initialObject = entry.get(colName);                    Object converted = initialObject;                                        if (converters != null) {                        for (int j=0; j < converters.length; j++ ) {                            TypeConverter tc = converters[j];                            if (tc.getOriginalClass().isAssignableFrom(initialObject.getClass())) {                                converted = tc.convert(initialObject);                            }                        }                    }                                        buf.append(converted);                    buf.append("</column>\n");                }                if (elementName != null) {                    buf.append("    </" + elementName + ">\n");                }            }            if (collectionName != null) {                buf.append("    </" + collectionName + ">\n");            }        }        logger.debug("Returning XML from Map Array...");        return buf;    }        public static StringBuffer createXMLFromMapArray(String collectionName, String elementName, Map[] source) {        return createXMLFromMapArray(collectionName, elementName, source, null);    }    public static Map[] createMapArrayFromResultset(ResultSet rs) {                ArrayList mapList = new ArrayList();                try {                        if (rs !=  null) {                /**                 * A note about the ResultSetMetaData:                 * - only call each method once - subsequent                 * calls will cause a Column Index out of Range                 * Postgres error.                 * Also, column numbers are indexed from 1, not 0.                 */                ResultSetMetaData meta = rs.getMetaData();                int count = 0;                                int columnCount = meta.getColumnCount();                                while (rs.next()) {                                        Map thisMap = new HashMap();                                        for (int i=0; i < columnCount; i++) {                        String colName = meta.getColumnName(i+1);                        thisMap.put(colName, rs.getObject(i+1));                    }                    mapList.add(thisMap);                }            }        }        catch (SQLException ex) {            ex.printStackTrace();            throw new RuntimeException( ex.getMessage() ) ;        }        finally {            try {                if (rs != null) {                    rs.close();                }            }            catch (SQLException ex) {                ex.printStackTrace();                throw new RuntimeException( ex.getMessage() ) ;            }                   }        logger.debug("Returning MapArray from query...");        return (Map[]) mapList.toArray();            }}

⌨️ 快捷键说明

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