Пример #1
0
 /**
  * Parse a properties sub-packet. If any errors occur while de-serializing Java object properties,
  * an exception will be printed and not thrown since a thrown exception will shut down the entire
  * connection. ClassCastExceptions will occur when both the sender and receiver of the packet
  * don't have identical versions of the same class.
  *
  * @param parser the XML parser, positioned at the start of a properties sub-packet.
  * @return a map of the properties.
  * @throws Exception if an error occurs while parsing the properties.
  */
 public static Map<String, Object> parseProperties(XmlPullParser parser) throws Exception {
   Map<String, Object> properties = new HashMap<String, Object>();
   while (true) {
     int eventType = parser.next();
     if (eventType == XmlPullParser.START_TAG && parser.getName().equals("property")) {
       // Parse a property
       boolean done = false;
       String name = null;
       String type = null;
       String valueText = null;
       Object value = null;
       while (!done) {
         eventType = parser.next();
         if (eventType == XmlPullParser.START_TAG) {
           String elementName = parser.getName();
           if (elementName.equals("name")) {
             name = parser.nextText();
           } else if (elementName.equals("value")) {
             type = parser.getAttributeValue("", "type");
             valueText = parser.nextText();
           }
         } else if (eventType == XmlPullParser.END_TAG) {
           if (parser.getName().equals("property")) {
             if ("integer".equals(type)) {
               value = Integer.valueOf(valueText);
             } else if ("long".equals(type)) {
               value = Long.valueOf(valueText);
             } else if ("float".equals(type)) {
               value = Float.valueOf(valueText);
             } else if ("double".equals(type)) {
               value = Double.valueOf(valueText);
             } else if ("boolean".equals(type)) {
               value = Boolean.valueOf(valueText);
             } else if ("string".equals(type)) {
               value = valueText;
             } else if ("java-object".equals(type)) {
               try {
                 byte[] bytes = StringUtils.decodeBase64(valueText);
                 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
                 value = in.readObject();
               } catch (Exception e) {
                 LOGGER.log(Level.SEVERE, "", e);
               }
             }
             if (name != null && value != null) {
               properties.put(name, value);
             }
             done = true;
           }
         }
       }
     } else if (eventType == XmlPullParser.END_TAG) {
       if (parser.getName().equals("properties")) {
         break;
       }
     }
   }
   return properties;
 }
Пример #2
0
 /**
  * Decodes a String into an object of the specified type. If the object type is not supported,
  * null will be returned.
  *
  * @param type the type of the property.
  * @param value the encode String value to decode.
  * @return the String value decoded into the specified type.
  * @throws Exception If decoding failed due to an error.
  */
 private static Object decode(Class type, String value) throws Exception {
   if (type.getName().equals("java.lang.String")) {
     return value;
   }
   if (type.getName().equals("boolean")) {
     return Boolean.valueOf(value);
   }
   if (type.getName().equals("int")) {
     return Integer.valueOf(value);
   }
   if (type.getName().equals("long")) {
     return Long.valueOf(value);
   }
   if (type.getName().equals("float")) {
     return Float.valueOf(value);
   }
   if (type.getName().equals("double")) {
     return Double.valueOf(value);
   }
   if (type.getName().equals("java.lang.Class")) {
     return Class.forName(value);
   }
   return null;
 }