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

📄 propertiesconverter.java

📁 Vyger offers a D & D and Rogue-like environment in a graphical online roleplay game.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
         for (int i = 0; i < descriptors.length; i++) {            if (DEBUG)               System.out.println("Candidate property:" + prefix + descriptors[i].getName());            writeMethod = null;            if ((descriptors[i].getReadMethod() != null) &&                ((writeMethod = descriptors[i].getWriteMethod()) != null)) {               // Class property or                // Getter and setter are present: check that the property is               // not transient               if (!isTransient(objectClass, descriptors[i].getName())) {                  // Not transient: save it                  if (DEBUG)                     System.out.println("Loadable property:" + prefix + descriptors[i].getName());                  loadProperty(object,                               properties,                               prefix + descriptors[i].getName(),                               writeMethod,                               descriptors[i].getPropertyType(),                               (descriptors[i] instanceof IndexedPropertyDescriptor));               }                              else if (DEBUG) {                 System.out.println("Property:" + prefix + descriptors[i].getName() + " is transient");               }            }            else if (DEBUG) {               if (writeMethod == null)                 System.out.println("Property:" + prefix + descriptors[i].getName() + " has no write method");               else                 System.out.println("Property:" + prefix + descriptors[i].getName() + " has no read method");            }         }      }            return object;   }   /**   * Load a single property of an object.   * @param object        the object to load.   * @param properties    the object properties to load.   * @param propertyName  the name for loading the property.   * @param writeMethod   the method for setting the value.   * @param propertyClass the class of the property.   * @param indexed       shows if the property is indexed or not.   * @return the number of loaded properties.   * @exception PersistenceException when a problem occurs during loading.   **/   private static void loadProperty(Object object, Properties properties,                                    String propertyName,                                    Method writeMethod,                                    Class  propertyClass,                                    boolean indexed)                       throws PersistenceException   {      Object[] arg;            try {               if ((indexed) || (propertyClass.isArray())) {            // This is an indexed/array property: load the number of the cells            Integer   nbCellsObject;            int       nbCells;            Class     actualClass;            actualClass = propertyClass.getComponentType();            if (actualClass == null)               actualClass = propertyClass;            nbCellsObject = (Integer) loadValue(properties,                                                propertyName,                                                Integer.TYPE);            if (nbCellsObject == null)               return; // No cells for this array            nbCells = nbCellsObject.intValue();            if (indexed) {               arg = new Object[2];               // This is an indexed property: write each cell               for (int cell = 0; cell < nbCells; cell++) {                  // Load each cell                  arg[1] = loadValue(properties,                                     propertyName + "." + cell,                                     actualClass);                  arg[0] = new Integer(cell);                  writeMethod.invoke(object, arg);               }            }            else {               // This is an array property: write all cells               Object array;               arg = new Object[1];               array = Array.newInstance(actualClass, nbCells);               for (int cell = 0; cell < nbCells; cell++) {                  // Load each cell                  Object value;                  value = loadValue(properties,                                    propertyName + "." + cell,                                    actualClass);                  Array.set(array, cell, value);               }               arg[0] = array;               writeMethod.invoke(object, arg);            }         }         else {            // Single value            arg = new Object[1];            arg[0] = loadValue(properties,                               propertyName,                               propertyClass);            if (arg[0] != null)               writeMethod.invoke(object, arg);         }      }      catch (InvocationTargetException ex) {         // The read method should be callable         Debug.signal(Debug.WARNING, object, ex);      }            catch (IllegalAccessException ex) {         // The read method should be public         Debug.signal(Debug.WARNING, object, ex);      }            catch (IllegalArgumentException ex) {         // The read method should be callable with no arguments         Debug.signal(Debug.WARNING, object, ex);      }   }   /**   * Load a single value of an object.   * @param properties     the object properties.   * @param propertyName   the name of the property.   * @param propertyClass  the class of the property.   * @return the value of the property.   * @exception PersistenceException when a problem occurs during loading.   **/   private static Object loadValue(Properties properties,                                   String propertyName,                                   Class  propertyClass)                         throws PersistenceException   {      Object result;      String value;      value = properties.getProperty(propertyName);      if (DEBUG)         System.out.println("Loaded:" + propertyName + " = " + value + "/" + propertyClass);      result = null;      if (value != null) {        if ((propertyClass.getName().equals("java.lang.Boolean")) ||            (propertyClass.equals(Boolean.TYPE)))           result = new Boolean(value);        else if ((propertyClass.getName().equals("java.lang.Character")) ||                 (propertyClass.equals(Character.TYPE)))           result = new Character(value.charAt(0));        else if ((propertyClass.getName().equals("java.lang.Byte")) ||                 (propertyClass.equals(Byte.TYPE)))           result = new Byte(value);        else if ((propertyClass.getName().equals("java.lang.Integer")) ||                 (propertyClass.equals(Integer.TYPE)))           result = new Integer(value);        else if ((propertyClass.getName().equals("java.lang.Short")) ||                 (propertyClass.equals(Short.TYPE)))           result = new Short(value);        else if ((propertyClass.getName().equals("java.lang.Long")) ||                 (propertyClass.equals(Long.TYPE)))           result = new Long(value);        else if ((propertyClass.getName().equals("java.lang.Float")) ||                 (propertyClass.equals(Float.TYPE)))           result = new Float(value);        else if ((propertyClass.getName().equals("java.lang.Double")) ||                 (propertyClass.equals(Double.TYPE)))           result = new Double(value);        else if (propertyClass.getName().equals("java.lang.StringBuffer"))           result = new StringBuffer(value);        else if (propertyClass.getName().equals("java.lang.String"))           result = value;        else {           // Abnormal: should not occur           Debug.signal(Debug.WARNING, propertyName,  "IGNORED");           return null;        }      }      else         // Complex object: recursively add its properties         result = fromProperties(properties, propertyName + ".");      return result;   }      /**   * Provide the properties of an object to save or to load.   * @param objectClass the class of object to save or to load.   * @return an array of properties.   * @execption IntrospectionException when an error occurs during introspection.   **/   private static PropertyDescriptor[] getProperties(Class objectClass)   {      BeanInfo objectInfo; // Information on the object to save      try {         objectInfo = Introspector.getBeanInfo(objectClass);         return objectInfo.getPropertyDescriptors();      }      catch (IntrospectionException ex) {         Debug.signal(Debug.ERROR, objectClass, ex);      }      return null;   }   /*------------------------------------------------------------------------   *                         Sorted properties   ------------------------------------------------------------------------*/   private static void sortedStore(OutputStream out, String header, Properties properties)    throws IOException    {        BufferedWriter      awriter;        ArrayList           sortedList;        awriter = new BufferedWriter(new OutputStreamWriter(out, "8859_1"));        if (header != null)            writeln(awriter, "#" + header);        sortedList = new ArrayList(properties.size());        writeln(awriter, "#" + new Date().toString());        for (Enumeration e = properties.keys(); e.hasMoreElements();) {            String key = (String)e.nextElement();            String val = (String)properties.get(key);            key = saveConvert(key);            val = saveConvert(val);            sortedList.add(key + "=" + val);        }        Collections.sort(sortedList);        for (int i = 0; i < sortedList.size(); i++)            writeln(awriter, (String) sortedList.get(i));        awriter.flush();    }    private static final String specialSaveChars = "=: \t\r\n\f#!";    /** A table of hex digits */    private static final char[] hexDigit = {	'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'    };    /*     * Converts unicodes to encoded \\uxxxx     * and writes out any of the characters in specialSaveChars     * with a preceding slash     */    private static String saveConvert(String theString) {        char aChar;        int len = theString.length();        StringBuffer outBuffer = new StringBuffer(len*2);        for(int x=0; x<len; ) {            aChar = theString.charAt(x++);            switch(aChar) {                case '\\':outBuffer.append('\\'); outBuffer.append('\\');                          continue;                case '\t':outBuffer.append('\\'); outBuffer.append('t');                          continue;                case '\n':outBuffer.append('\\'); outBuffer.append('n');                          continue;                case '\r':outBuffer.append('\\'); outBuffer.append('r');                          continue;                case '\f':outBuffer.append('\\'); outBuffer.append('f');                          continue;                default:                    if ((aChar < 20) || (aChar > 127)) {                        outBuffer.append('\\');                        outBuffer.append('u');                        outBuffer.append(toHex((aChar >> 12) & 0xF));                        outBuffer.append(toHex((aChar >> 8) & 0xF));                        outBuffer.append(toHex((aChar >> 4) & 0xF));                        outBuffer.append(toHex((aChar >> 0) & 0xF));                    }                    else {                        if (specialSaveChars.indexOf(aChar) != -1)                            outBuffer.append('\\');                        outBuffer.append(aChar);                    }            }        }        return outBuffer.toString();    }    private static void writeln(BufferedWriter bw, String s) throws IOException {        bw.write(s);        bw.newLine();    }    /**     * Convert a nibble to a hex character     * @param	nibble	the nibble to convert.     */    private static char toHex(int nibble) {  	   return hexDigit[(nibble & 0xF)];    }   /**   * Test method.   **/   public static void main(String[] argv) throws java.io.IOException, PersistenceException, ClassNotFoundException {      testSave(Class.forName(argv[0]), "C:/temp/" + argv[0] + ".txt");/*         wotlas.common.universe.TownMap example[];      wotlas.common.universe.WorldMap world;      world = new wotlas.common.universe.WorldMap();      world.setFullName("An example of world");      world.setShortName("UEoW");      world.setWorldMapID(56);      example = new wotlas.common.universe.TownMap[2];      example[0] = new wotlas.common.universe.TownMap();      example[0].setFullName("TarValon");      example[0].setShortName("TV");      example[1] = new wotlas.common.universe.TownMap();      example[1].setFullName("Ebou Dar");      example[1].setShortName("EB");      world.setTownMaps(example);      save(world, "C:/Temp/world.txt");      world = (wotlas.common.universe.WorldMap) load("C:/Temp/world.txt");      save(world, "C:/Temp/world2.txt");*/      }}

⌨️ 快捷键说明

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