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

📄 jahiaxmldocument.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////                 . . . i n   j a h i a   w e   t r u s t . . .////////  JahiaXmlDocument////  NK      29.01.2001////package org.jahia.data.xml;import java.io.*;import java.util.*;import org.w3c.dom.*;import org.xml.sax.*;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;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.registries.ServicesRegistry;import org.jahia.utils.xml.*;       // JahiaConsole, XMLParserimport org.jahia.utils.*;           // JahiaConsole/** * An abstract class to handle Xml documents * * @author Khue ng * @version 1.0 */public abstract class JahiaXmlDocument {    /** The xml Document **/    //protected XmlDocument m_XMLDocument;    protected Document m_XMLDocument;    /** The Full Path to the xml file **/    protected String m_DocPath;    private static final String CANT_READ_FILE_MSG = "Can't read XML file";    private static final String ERROR_READING_FILE_MSG = "Error reading file";    private static final String PARAMETER_TAG = "parameter";    private static final String PARAMETER_TAG_NAME_ATTRIBUTE = "name";    /**     * Handle xml document using default parser behavior     *     * @param (String) path, the full path to a xml file     */    public JahiaXmlDocument (String docPath) throws JahiaException    {        m_DocPath = docPath;        //File doc = new File(m_DocPath);        try {                loadFile(m_DocPath);        } catch ( Throwable t ){              t.printStackTrace();              throw new JahiaException(  "JahiaXmlDocument",                                            "Exception while loading to the file" + m_DocPath + t.getMessage(),                                            JahiaException.ERROR,                                            JahiaException.SERVICE_ERROR);        }    }    /**     * Handle xml document using a gived parser     *     * @param (String) path, the full path to a xml file     * @param (Parser) parser, the parser to use     */    public JahiaXmlDocument (String docPath, org.xml.sax.helpers.ParserAdapter parser)    //com.sun.xml.parser.Parser parser)    throws JahiaException {        m_DocPath = docPath;        //File doc = new File(m_DocPath);        try {                loadFile(m_DocPath);        } catch ( Throwable t ){              throw new JahiaException(  "JahiaXmlDocument",                                            "Exception while loading to the file" + m_DocPath + t.getMessage(),                                            JahiaException.ERROR,                                            JahiaException.SERVICE_ERROR);        }    }   /**    * To be override method. Codes to extract data from xml document.    */   public abstract void extractDocumentData() throws JahiaException ;   /**    * Write Document to the xml text file    *    */   public void write() throws JahiaException {        try {                saveFile(m_DocPath);        } catch ( Throwable t ){              throw new JahiaException(  "JahiaXmlDocument",                                            "Exception while writing to the file" + m_DocPath + t.getMessage(),                                            JahiaException.ERROR,                                            JahiaException.SERVICE_ERROR);        }   }    private void loadFile(String sourceFileName)    throws ParserConfigurationException, IOException, SAXException {        DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();        //dfactory.setValidating(true); // create only parsers that are validating        EntityResolver et = ServicesRegistry.getInstance().getJahiaWebAppsDeployerService().getDtdEntityResolver();        DocumentBuilder docBuilder = dfactory.newDocumentBuilder();        if ( et != null ){            docBuilder.setEntityResolver(et);        }        //docBuilder.setEntityResolver();        FileInputStream sourceStream = new FileInputStream(sourceFileName);        m_XMLDocument = docBuilder.parse(new InputSource(sourceStream));        m_XMLDocument.normalize(); // clean up DOM tree a little        //JahiaConsole.println("JahiaXmlDocument.loadFile",sourceFileName + " loaded successfully");    }    private void saveFile(String destinationFileName)    throws TransformerConfigurationException, FileNotFoundException,           TransformerException    {            m_XMLDocument.normalize(); // cleanup DOM tree a little            TransformerFactory tfactory = TransformerFactory.newInstance();            // This creates a transformer that does a simple identity transform,            // and thus can be used for all intents and purposes as a serializer.            Transformer serializer = tfactory.newTransformer();            Properties oprops = new Properties();            oprops.put("method", "html");            oprops.put("indent-amount", "2");            serializer.setOutputProperties(oprops);            FileOutputStream fileStream = new FileOutputStream(destinationFileName);            serializer.transform(new DOMSource(m_XMLDocument),                                 new StreamResult(fileStream));        try {            fileStream.flush();            fileStream.close();            fileStream = null;        } catch ( IOException ioe ) {        }    }} // end JahiaXmlDocument

⌨️ 快捷键说明

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