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

📄 xmlfile.java

📁 FuncPlotter is a combined Java application and applet for displaying two-dimensional plots of explic
💻 JAVA
字号:
/*====================================================================*\XmlFile.javaXML file class.------------------------------------------------------------------------This file is part of FuncPlotter, a combined Java application and appletfor plotting explicit functions in one variable.Copyright 2005-2007 Andy Morgan-Richards.FuncPlotter is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or (at youroption) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith this program.  If not, see <http://www.gnu.org/licenses/>.\*====================================================================*/// PACKAGEpackage xml;//----------------------------------------------------------------------// IMPORTSimport exception.AppException;import exception.FileException;import exception.TempFileException;import exception.UrlException;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.URL;import org.w3c.dom.Document;import org.w3c.dom.Element;//----------------------------------------------------------------------// XML FILE CLASSpublic class XmlFile{//////////////////////////////////////////////////////////////////////////  Constants////////////////////////////////////////////////////////////////////////    private static final    String  XML_VERSION_STR = "1.0";//////////////////////////////////////////////////////////////////////////  Enumeration types////////////////////////////////////////////////////////////////////////    // ERROR IDENTIFIERS    private enum ErrorId        implements AppException.Id    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        FILE_DOES_NOT_EXIST        ( "The %1 does not exist." ),        FAILED_TO_OPEN_FILE        ( "Failed to open the %1." ),        FAILED_TO_CLOSE_FILE        ( "Failed to close the %1." ),        FAILED_TO_LOCK_FILE        ( "Failed to lock the %1." ),        ERROR_READING_FILE        ( "An error occurred while reading the %1." ),        ERROR_WRITING_FILE        ( "An error occurred while writing the %1." ),        FILE_ACCESS_NOT_PERMITTED        ( "Access to the %1 was not permitted." ),        FAILED_TO_CREATE_TEMPORARY_FILE        ( "Failed to create a temporary %1." ),        FAILED_TO_DELETE_FILE        ( "Failed to delete the existing %1." ),        FAILED_TO_RENAME_FILE        ( "Failed to rename the temporary %1." ),        NOT_ENOUGH_MEMORY        ( "There was not enough memory to read the %1." ),        UTF8_ENCODING_NOT_SUPPORTED        ( "This implementation of Java does not support the UTF-8 encoding." );    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private ErrorId( String message )        {            this.message = message;        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods : AppException.Id interface    ////////////////////////////////////////////////////////////////////        public String getMessage( )        {            return message;        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance variables    ////////////////////////////////////////////////////////////////////        private String  message;    }    //==================================================================//////////////////////////////////////////////////////////////////////////  Member interfaces////////////////////////////////////////////////////////////////////////    // ELEMENT WRITER INTERFACE    public interface ElementWriter    {    ////////////////////////////////////////////////////////////////////    //  Methods    ////////////////////////////////////////////////////////////////////        void writeElement( XmlWriter writer,                           Element   element,                           int       indent )            throws IOException;        //--------------------------------------------------------------    }    //==================================================================//////////////////////////////////////////////////////////////////////////  Constructors////////////////////////////////////////////////////////////////////////    private XmlFile( )    {    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Class methods////////////////////////////////////////////////////////////////////////    public static Document read( File file )        throws AppException    {        FileInputStream inStream = null;        try        {            // Test for file            try            {                if ( !file.isFile( ) )                    throw new FileException( ErrorId.FILE_DOES_NOT_EXIST, file );            }            catch ( SecurityException e )            {                throw new FileException( ErrorId.FILE_ACCESS_NOT_PERMITTED, file );            }            // Open input stream on file            try            {                inStream = new FileInputStream( file );            }            catch ( FileNotFoundException e )            {                throw new FileException( ErrorId.FAILED_TO_OPEN_FILE, file );            }            catch ( SecurityException e )            {                throw new FileException( ErrorId.FILE_ACCESS_NOT_PERMITTED, file );            }            // Lock file            try            {                if ( inStream.getChannel( ).tryLock( 0, Long.MAX_VALUE, true ) == null )                    throw new FileException( ErrorId.FAILED_TO_LOCK_FILE, file );            }            catch ( Exception e )            {                throw new FileException( ErrorId.FAILED_TO_LOCK_FILE, file, e );            }            // Read and parse file            Document document = null;            try            {                document = XmlUtilities.createDOMDocument( inStream );            }            catch ( OutOfMemoryError e )            {                throw new FileException( ErrorId.NOT_ENOUGH_MEMORY, file );            }            // Close input stream            try            {                inStream.close( );                inStream = null;            }            catch ( IOException e )            {                throw new FileException( ErrorId.FAILED_TO_CLOSE_FILE, file );            }            // Return document            return document;        }        catch ( AppException e )        {            try            {                if ( inStream != null )                    inStream.close( );            }            catch ( Exception e1 )            {                // ignore            }            throw e;        }    }    //------------------------------------------------------------------    public static Document read( URL url )        throws AppException    {        InputStream inStream = null;        try        {            // Open input stream on URL            try            {                inStream = url.openStream( );            }            catch ( SecurityException e )            {                throw new UrlException( ErrorId.FILE_ACCESS_NOT_PERMITTED, url );            }            catch ( IOException e )            {                throw new UrlException( ErrorId.FAILED_TO_OPEN_FILE, url );            }            // Read and parse file            Document document = null;            try            {                document = XmlUtilities.createDOMDocument( inStream );            }            catch ( OutOfMemoryError e )            {                throw new UrlException( ErrorId.NOT_ENOUGH_MEMORY, url );            }            // Close input stream            try            {                inStream.close( );                inStream = null;            }            catch ( IOException e )            {                throw new UrlException( ErrorId.FAILED_TO_CLOSE_FILE, url );            }            // Return document            return document;        }        catch ( AppException e )        {            try            {                if ( inStream != null )                    inStream.close( );            }            catch ( Exception e1 )            {                // ignore            }            throw e;        }    }    //------------------------------------------------------------------    public static void write( File          file,                              Document      document,                              ElementWriter elementWriter )        throws AppException, IllegalArgumentException    {        write( file, document, XmlWriter.Standalone.NONE, elementWriter );    }    //------------------------------------------------------------------    public static void write( File                 file,                              Document             document,                              XmlWriter.Standalone standalone,                              ElementWriter        elementWriter )        throws AppException, IllegalArgumentException    {        // Validate arguments        if ( (file == null) || (document == null) )            throw new IllegalArgumentException( );        // Write file        File tempFile = null;        XmlWriter writer = null;        boolean oldFileDeleted = false;        try        {            // Create temporary file            try            {                tempFile = File.createTempFile( XmlConstants.TEMP_FILE_PREFIX, null,                                                file.getAbsoluteFile( ).getParentFile( ) );            }            catch ( Exception e )            {                throw new AppException( ErrorId.FAILED_TO_CREATE_TEMPORARY_FILE, e );            }            // Open XML writer on temporary file            try            {                writer = new XmlWriter( tempFile, XmlConstants.UTF8_STR );            }            catch ( FileNotFoundException e )            {                throw new FileException( ErrorId.FAILED_TO_OPEN_FILE, tempFile, e );            }            catch ( SecurityException e )            {                throw new FileException( ErrorId.FILE_ACCESS_NOT_PERMITTED, tempFile, e );            }            catch ( UnsupportedEncodingException e )            {                throw new AppException( ErrorId.UTF8_ENCODING_NOT_SUPPORTED, e );            }            // Lock file            try            {                if ( writer.getFileOutStream( ).getChannel( ).tryLock( ) == null )                    throw new FileException( ErrorId.FAILED_TO_LOCK_FILE, tempFile );            }            catch ( Exception e )            {                throw new FileException( ErrorId.FAILED_TO_LOCK_FILE, tempFile, e );            }            // Write file            try            {                writer.writeXmlDeclaration( XML_VERSION_STR, XmlConstants.UTF8_STR, standalone );                elementWriter.writeElement( writer, document.getDocumentElement( ), 0 );            }            catch ( IOException e )            {                throw new FileException( ErrorId.ERROR_WRITING_FILE, tempFile, e );            }            // Close output stream            try            {                writer.close( );                writer = null;            }            catch ( IOException e )            {                throw new FileException( ErrorId.FAILED_TO_CLOSE_FILE, tempFile, e );            }            // Delete any existing file            try            {                if ( file.exists( ) && !file.delete( ) )                    throw new FileException( ErrorId.FAILED_TO_DELETE_FILE, file );                oldFileDeleted = true;            }            catch ( SecurityException e )            {                throw new FileException( ErrorId.FAILED_TO_DELETE_FILE, file );            }            // Rename temporary file            try            {                if ( !tempFile.renameTo( file ) )                    throw new TempFileException( ErrorId.FAILED_TO_RENAME_FILE, file, tempFile );            }            catch ( SecurityException e )            {                throw new TempFileException( ErrorId.FAILED_TO_RENAME_FILE, file, tempFile );            }        }        catch ( AppException e )        {            try            {                if ( writer != null )                    writer.close( );            }            catch ( Exception e1 )            {                // ignore            }            try            {                if ( !oldFileDeleted && (tempFile != null) && tempFile.exists( ) )                    tempFile.delete( );            }            catch ( Exception e1 )            {                // ignore            }            throw e;        }    }    //------------------------------------------------------------------}//----------------------------------------------------------------------

⌨️ 快捷键说明

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