comment.java

来自「FuncPlotter is a combined Java applicati」· Java 代码 · 共 289 行

JAVA
289
字号
/*====================================================================*\Comment.javaComment 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.IOException;import java.util.ArrayList;import java.util.List;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;//----------------------------------------------------------------------// COMMENT CLASSpublic class Comment{//////////////////////////////////////////////////////////////////////////  Constants////////////////////////////////////////////////////////////////////////    private interface ElementName    {        String  COMMENT = "comment";    }    private interface AttrName    {        String  INDENT  = "indent";    }//////////////////////////////////////////////////////////////////////////  Enumeration types////////////////////////////////////////////////////////////////////////    // ERROR IDENTIFIERS    private enum ErrorId        implements AppException.Id    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        NO_ATTRIBUTE        ( "The element does not have such an attribute." ),        INVALID_ATTRIBUTE        ( "The attribute is invalid." ),        ATTRIBUTE_OUT_OF_BOUNDS        ( "The attribute value is out of bounds." );    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private ErrorId( String message )        {            this.message = message;        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods : AppException.Id interface    ////////////////////////////////////////////////////////////////////        public String getMessage( )        {            return message;        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance variables    ////////////////////////////////////////////////////////////////////        private String  message;    }    //==================================================================//////////////////////////////////////////////////////////////////////////  Constructors////////////////////////////////////////////////////////////////////////    public Comment( )    {        text = new String( );    }    //------------------------------------------------------------------    public Comment( String text )    {        setText( text );    }    //------------------------------------------------------------------    public Comment( Element element )        throws XmlParseException    {        String elementPath = XmlUtilities.getElementPath( element );        // Attribute: indent        String paramName = elementPath + "." + AttrName.INDENT;        String paramValue = XmlUtilities.getAttribute( element, AttrName.INDENT );        int indent = 0;        if ( paramValue != null )        {            try            {                indent = Integer.parseInt( paramValue );                if ( indent < 0 )                    throw new XmlParseException( ErrorId.ATTRIBUTE_OUT_OF_BOUNDS, paramName, paramValue );            }            catch ( NumberFormatException e )            {                throw new XmlParseException( ErrorId.INVALID_ATTRIBUTE, paramName, paramValue );            }        }        // Merge text nodes        StringBuilder buffer = new StringBuilder( );        NodeList nodes = element.getChildNodes( );        for ( int i = 0; i < nodes.getLength( ); ++i )        {            if ( nodes.item( i ).getNodeType( ) == Node.TEXT_NODE )                buffer.append( nodes.item( i ).getNodeValue( ) );        }        String str = buffer.toString( );        // Remove indent from each line of comment        buffer.setLength( 0 );        int index = 0;        while ( index < str.length( ) )        {            int startIndex = index;            index = str.indexOf( '\n', index );            if ( index < 0 )                index = str.length( );            int indentEndIndex = Math.min( startIndex + indent, index );            while ( startIndex < indentEndIndex )            {                if ( str.charAt( startIndex ) != ' ' )                    break;                ++startIndex;            }            if ( index > startIndex )                buffer.append( str.substring( startIndex, index ) );            buffer.append( '\n' );            ++index;        }        text = buffer.toString( ).trim( );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Class methods////////////////////////////////////////////////////////////////////////    public static String getElementName( )    {        return ElementName.COMMENT;    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods////////////////////////////////////////////////////////////////////////    public String getText( )    {        return text;    }    //------------------------------------------------------------------    public void setText( String text )    {        this.text = text.trim( );    }    //------------------------------------------------------------------    public boolean isEmpty( )    {        return ( text.length( ) == 0 );    }    //------------------------------------------------------------------    public void write( XmlWriter writer,                       int       elementIndent,                       int       textIndent )        throws IOException    {        List<XmlWriter.Attribute> attributes = new ArrayList<XmlWriter.Attribute>( );        attributes.add( new XmlWriter.Attribute( AttrName.INDENT, textIndent ) );        writer.writeElementStart( ElementName.COMMENT, attributes, elementIndent, true, true );        int index = 0;        while ( index < text.length( ) )        {            int startIndex = index;            index = text.indexOf( '\n', startIndex );            if ( index < 0 )                index = text.length( );            if ( startIndex < index )            {                writer.writeSpaces( textIndent );                writer.writeEscaped( text.substring( startIndex, index ) );            }            writer.writeLineSeparator( );            ++index;        }        writer.writeElementEnd( ElementName.COMMENT, elementIndent );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance variables////////////////////////////////////////////////////////////////////////    private String  text;}//----------------------------------------------------------------------

⌨️ 快捷键说明

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