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

📄 propertiesconverter.java

📁 Vyger offers a D & D and Rogue-like environment in a graphical online roleplay game.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Light And Shadow. A Persistent Universe based on Robert Jordan's Wheel of Time Books. * Copyright (C) 2001-2002 WOTLAS Team *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */package wotlas.libs.persistence;import java.io.*;import java.util.*;import java.beans.*;import java.lang.reflect.*;import wotlas.utils.Debug;/** * Save and Load an object to a "dot-properties" file. * To be saved/loaded a property should: * - have a public getter and setter. * - be not transient. * Null indexed values are saved. * @author Hari, Diego */public class PropertiesConverter{   /**   * Warnings or not.   **/   private static boolean warning = false;   public static void setWarning(boolean warning) { PropertiesConverter.warning = warning; }   public static boolean isWarning() { return PropertiesConverter.warning; }   /**   * Set this value to true to get DEBUG trace   **/   private static final boolean DEBUG = false;      /**   * Save an object into a "dot-properties" format file.   * @param object the object to save.   * @param name the path of the file where to save the object.   * @exception PersistenceException when a problem occurs during saving.   */   public static void save(Object object, String name)                      throws PersistenceException   {      BufferedOutputStream os;      try {         os = new BufferedOutputStream(new FileOutputStream(name));      }      catch (FileNotFoundException ex) {         Debug.signal(Debug.ERROR, name, ex);         throw new PersistenceException(ex);      }      save(object, os);            try{        os.close(); // close stream : ADDED by ALDISS      }catch(IOException e) {      	e.printStackTrace();      }   }      /** save an object BackupReady in a binary file    *     * @param obj    * @return    */	public static void Backup(Object object, String name)					   throws PersistenceException	{	    BufferedOutputStream os;	    try {		    os = new BufferedOutputStream(new FileOutputStream(name));	    } catch (FileNotFoundException ex) {		    Debug.signal(Debug.ERROR, name, ex);		    throw new PersistenceException(ex);	    }	    //save(object, os);	   	try{			ObjectOutputStream nested = new ObjectOutputStream(os);			nested.writeObject(object);			nested.flush();			os.close(); // close stream : ADDED by ALDISS	    } catch(IOException e) {			e.printStackTrace();		}	}      /**   * Save an object into a "dot-properties" format file.   * @param object the object to save.   * @param os     the output stream where to save the object.   * @exception PersistenceException when a problem occurs during saving.   */   public static void save(Object object, OutputStream os)                      throws PersistenceException   {      save(object, object.getClass(), os);   }   /**   * Test the saving of an object class into a "dot-properties" format file.   * @param clazz  the class of object to save.   * @param name the path of the file where to save the object.   * @exception PersistenceException when a problem occurs during saving.   */   public static void testSave(Class clazz, String name)                      throws PersistenceException   {      BufferedOutputStream os;      try {         os = new BufferedOutputStream(new FileOutputStream(name));      }      catch (FileNotFoundException ex) {         Debug.signal(Debug.ERROR, name, ex);         throw new PersistenceException(ex);      }      testSave(clazz, os);                       }   /**   * Test the saving of an object class into a "dot-properties" format file.   * @param clazz  the class of object to save.   * @param os     the output stream where to save the object.   * @exception PersistenceException when a problem occurs during saving.   */   public static void testSave(Class clazz, OutputStream os)                      throws PersistenceException   {      save(null, clazz, os);   }   /**   * Save an object into a "dot-properties" format file.   * @param object the object to save (null for testing class).   * @param clazz  the class of object to save.   * @param os     the output stream where to save the object.   * @exception PersistenceException when a problem occurs during saving.   */   private static void save(Object object, Class clazz, OutputStream os)                       throws PersistenceException   {      Properties toBeSaved;       toBeSaved = new Properties();      // Save the content of the object      if (toProperties(object, clazz, toBeSaved, "") > 0) {         try {            //toBeSaved.store(os, "WotLas 1.0 Saved Object");            sortedStore(os, "WotLas 1.0 Saved Object", toBeSaved);         }         catch (IOException ex) {            Debug.signal(Debug.ERROR, os, ex);            throw new PersistenceException(ex);         }      }      else {         Debug.signal(Debug.ERROR, object, "Nothing saved");      }   }   /**   * Restore an object from a binary file format file.   * @param is     the input stream from where to load the object.   * @return the object loaded from the file   * @exception PersistenceException when a problem occurs during saving.   */   public static Object Restore(InputStream is)					  throws PersistenceException   { 	  Object obj = null;	  try {		 ObjectInputStream nested = new ObjectInputStream(is);		 obj  = nested.readObject();	  } catch(ClassNotFoundException e) {	     e.printStackTrace();		Debug.signal(Debug.ERROR, is, e);	  } catch (IOException ex) {		 Debug.signal(Debug.ERROR, is, ex);		 throw new PersistenceException(ex);	  }      	  // Build the object from its content	  return obj;   }      /**   * Restore an object from a binary format file.   * @param name the path of the file from which to load the object.   * @return the object loaded from the file   * @exception PersistenceException when a problem occurs during saving.   */   public static Object Restore(String name)					  throws PersistenceException   {	  BufferedInputStream is;	  try {		 is = new BufferedInputStream(new FileInputStream(name));	  }	  catch (FileNotFoundException ex) {		 Debug.signal(Debug.ERROR, name, ex);		 throw new PersistenceException(ex);	  }	  Object obj = null;	  try{	    ObjectInputStream nested = new ObjectInputStream(is);	    obj  = nested.readObject();	  	// Object obj = load(is); 		is.close(); // close stream : ADDED by ALDISS	  }catch(ClassNotFoundException e) {		e.printStackTrace();	  }catch(IOException e) {		e.printStackTrace();	  }	  return obj;   }   /**   * Load an object from a "dot-properties" format file.   * @param name the path of the file from which to load the object.   * @return the object loaded from the file   * @exception PersistenceException when a problem occurs during saving.   */   public static Object load(String name)                      throws PersistenceException   {      BufferedInputStream is;      try {         is = new BufferedInputStream(new FileInputStream(name));      }      catch (FileNotFoundException ex) {         Debug.signal(Debug.ERROR, name, ex);         throw new PersistenceException(ex);      }      Object obj = load(is);       try{        is.close(); // close stream : ADDED by ALDISS      }catch(IOException e) {      	e.printStackTrace();      }      return obj;   }   /**   * Load an object from a "dot-properties" format file.   * @param is     the input stream from where to load the object.   * @return the object loaded from the file   * @exception PersistenceException when a problem occurs during saving.   */   public static Object load(InputStream is)                      throws PersistenceException   {      Properties toBeRestored;       toBeRestored = new Properties();      try {         toBeRestored.load(is);      }      catch (IOException ex) {         Debug.signal(Debug.ERROR, is, ex);         throw new PersistenceException(ex);      }            // Build the object from its content      return fromProperties(toBeRestored, "");   }   /**   * Shows if a field is transient or not.   * @param clazz      the class containing the field.   * @param name       the name of the field to check.   * @return true if the field is transient, not otherwise.   **/   private static boolean isTransient(Class clazz, String name) {      // getFields return all the accessible (public) field inherited      // of the class. So we must use getDeclaredFields (which returns all      // the fields, even private) of the class, but which omit the superclasses      // fields: this is the reason for the loop.      for (Class curClass = clazz; curClass != null; curClass = curClass.getSuperclass()) {         Field field;         try {           field = curClass.getDeclaredField(name);           return Modifier.isTransient(field.getModifiers());         }         catch (NoSuchFieldException ex) {           // The field is not found in this class, try in superclass         }      }      // The XPosition bug: if a field is named xPosition, the property      // name is XPosition (though the setter/getter are [s/g]etXPosition()      // => Search not case sensitive if the field is not found      // The field is not found in the class, return that is transient      for (Class curClass = clazz; curClass != null; curClass = curClass.getSuperclass()) {         Field[] fields;         fields = curClass.getDeclaredFields();         for (int i = 0; i < fields.length; i++) {             if (name.equalsIgnoreCase(fields[i].getName()))                return Modifier.isTransient(fields[i].getModifiers());

⌨️ 快捷键说明

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