xmlutilities.java

来自「FuncPlotter is a combined Java applicati」· Java 代码 · 共 450 行 · 第 1/2 页

JAVA
450
字号
/*====================================================================*\XmlUtilities.javaXML utilities 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 java.io.ByteArrayInputStream;import java.io.CharArrayReader;import java.io.InputStream;import java.io.IOException;import java.io.StringReader;import java.net.URI;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.FactoryConfigurationError;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.ErrorHandler;import org.xml.sax.InputSource;import org.xml.sax.SAXException;//----------------------------------------------------------------------// XML UTILITIES CLASSpublic class XmlUtilities{//////////////////////////////////////////////////////////////////////////  Constants////////////////////////////////////////////////////////////////////////    private interface AttrName    {        String  INDEX   = "index";        String  XMLNS   = "xmlns";    }//////////////////////////////////////////////////////////////////////////  Enumeration types////////////////////////////////////////////////////////////////////////    // ERROR IDENTIFIERS    private enum ErrorId        implements AppException.Id    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        FAILED_TO_INSTANTIATE_DOCUMENT_BUILDER_FACTORY        ( "Failed to instantiate a DOM document builder factory." ),        FAILED_TO_CREATE_DOCUMENT_BUILDER        ( "Failed to create a DOM document builder." ),        ERROR_PARSING_FILE        ( "An error occurred while parsing the file." );    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private ErrorId( String message )        {            this.message = message;        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods : AppException.Id interface    ////////////////////////////////////////////////////////////////////        public String getMessage( )        {            return message;        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance variables    ////////////////////////////////////////////////////////////////////        private String  message;    }    //==================================================================//////////////////////////////////////////////////////////////////////////  Constructors////////////////////////////////////////////////////////////////////////    private XmlUtilities( )    {    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Class methods////////////////////////////////////////////////////////////////////////    public static String getElementPath( Element element )    {        List<String> strs = new ArrayList<String>( );        while ( element != null )        {            strs.add( element.getNodeName( ) );            Node parent = element.getParentNode( );            element = (parent instanceof Element) ? (Element)parent : null;        }        StringBuilder buffer = new StringBuilder( strs.size( ) << 4 );        for ( int i = strs.size( ) - 1; i >= 0; --i )        {            buffer.append( strs.get( i ) );            if ( i > 0 )                buffer.append( '.' );        }        return buffer.toString( );    }    //------------------------------------------------------------------    public static DocumentBuilder createDocumentBuilder( boolean validate )        throws AppException    {        try        {            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance( );            docBuilderFactory.setValidating( validate );            docBuilderFactory.setNamespaceAware( true );            return docBuilderFactory.newDocumentBuilder( );        }        catch ( FactoryConfigurationError e )        {            throw new AppException( ErrorId.FAILED_TO_INSTANTIATE_DOCUMENT_BUILDER_FACTORY, e );        }        catch ( ParserConfigurationException e )        {            throw new AppException( ErrorId.FAILED_TO_CREATE_DOCUMENT_BUILDER, e );        }    }    //------------------------------------------------------------------    public static Document createDOMDocument( byte[] data )        throws AppException    {        return createDOMDocument( data, null, null );    }    //------------------------------------------------------------------    public static Document createDOMDocument( byte[]       data,                                              URI          systemId,                                              ErrorHandler errorHandler )        throws AppException    {        InputSource inputSource = new InputSource( new ByteArrayInputStream( data ) );        inputSource.setEncoding( XmlConstants.UTF8_STR );        return createDOMDocument( inputSource, systemId, errorHandler );    }    //------------------------------------------------------------------    public static Document createDOMDocument( char[] data )        throws AppException    {        return createDOMDocument( data, null, null );    }    //------------------------------------------------------------------

⌨️ 快捷键说明

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