📄 propertyfile.java
字号:
/*====================================================================*\PropertyFile.javaProperty 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.UrlException;import java.io.File;import java.io.IOException;import java.net.URL;import org.w3c.dom.Document;import org.w3c.dom.Element;//----------------------------------------------------------------------// PROPERTY FILE CLASSpublic class PropertyFile implements XmlFile.ElementWriter{////////////////////////////////////////////////////////////////////////// Constants//////////////////////////////////////////////////////////////////////// private interface AttrName { String VALUE = "value"; String VERSION = "version"; } private static final String FILE_STR = "file";////////////////////////////////////////////////////////////////////////// Enumeration types//////////////////////////////////////////////////////////////////////// // ERROR IDENTIFIERS private enum ErrorId implements AppException.Id { //////////////////////////////////////////////////////////////////// // Constants //////////////////////////////////////////////////////////////////// INCORRECT_FILE_FORMAT ( "The %1 is not in the correct format." ); //////////////////////////////////////////////////////////////////// // 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 PropertyFile( ) { } //------------------------------------------------------------------ public PropertyFile( String rootElementName ) throws AppException { document = XmlUtilities.createDocumentBuilder( false ).newDocument( ); document.appendChild( document.createElement( rootElementName ) ); } //------------------------------------------------------------------ public PropertyFile( String rootElementName, String versionStr ) throws AppException { this( rootElementName ); document.getDocumentElement( ).setAttribute( AttrName.VERSION, versionStr ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : XmlFile.ElementWriter interface//////////////////////////////////////////////////////////////////////// public void writeElement( XmlWriter writer, Element element, int indent ) throws IOException { writer.writeElementNoText( element, indent ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// public void read( File file, String rootElementName ) throws AppException { try { // Read document document = XmlFile.read( file ); // Validate name of root element if ( !document.getDocumentElement( ).getNodeName( ).equals( rootElementName ) ) throw new FileException( ErrorId.INCORRECT_FILE_FORMAT, file ); } catch ( AppException e ) { e.setSubstitutionString( getFileTypeString( ) ); throw e; } } //------------------------------------------------------------------ public void read( URL url, String rootElementName ) throws AppException { try { // Read document document = XmlFile.read( url ); // Validate name of root element if ( !document.getDocumentElement( ).getNodeName( ).equals( rootElementName ) ) throw new UrlException( ErrorId.INCORRECT_FILE_FORMAT, url ); } catch ( AppException e ) { e.setSubstitutionString( getFileTypeString( ) ); throw e; } } //------------------------------------------------------------------ public void write( File file ) throws AppException, IllegalStateException { // Test for document if ( document == null ) throw new IllegalStateException( ); // Write file try { XmlFile.write( file, document, this ); } catch ( AppException e ) { e.setSubstitutionString( getFileTypeString( ) ); throw e; } } //------------------------------------------------------------------ public String getVersionString( ) { return XmlUtilities.getAttribute( document.getDocumentElement( ), AttrName.VERSION ); } //------------------------------------------------------------------ public String getValue( String elementPath ) { return getValue( elementPath, -1 ); } //------------------------------------------------------------------ public String getValue( String elementPath, int index ) { Element element = XmlUtilities.findElement( document, elementPath, index ); return ( (element == null ) ? null : XmlUtilities.getAttribute( element, AttrName.VALUE ) ); } //------------------------------------------------------------------ public boolean setValue( String elementPath, String value ) { return setValue( elementPath, -1, value ); } //------------------------------------------------------------------ public boolean setValue( String elementPath, int index, String value ) { boolean result = false; if ( value != null ) { Element element = XmlUtilities.findElement( document, elementPath, index, true ); if ( element != null ) { element.setAttribute( AttrName.VALUE, value ); result = true; } } return result; } //------------------------------------------------------------------ protected String getFileTypeString( ) { return FILE_STR; } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private Document document;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -