public static void readExternal()

in struts/src/main/java/com/intellij/struts/core/JDOMClassExternalizer.java [111:244]


  public static void readExternal(Object data, Element parentNode) throws InvalidDataException {
    if (parentNode == null) return;

    for (Element e : parentNode.getChildren("option")) {
      String fieldName = e.getAttributeValue("name");
      if (fieldName == null) {
        throw new InvalidDataException();
      }
      try {
        Field field = data.getClass().getField(fieldName);
        Class type = field.getType();
        int modifiers = field.getModifiers();
        if ((modifiers & Modifier.PUBLIC) == 0 || (modifiers & Modifier.STATIC) != 0) {
          continue;
        }
        field.setAccessible(true); // class might be non-public
        String value = e.getAttributeValue("value");
        if (type.isPrimitive()) {
          if (value == null) {
            throw new InvalidDataException();
          }
          if (type.equals(byte.class)) {
            try {
              field.setByte(data, Byte.parseByte(value));
            }
            catch (NumberFormatException ex) {
              throw new InvalidDataException();
            }
          }
          else if (type.equals(short.class)) {
            try {
              field.setShort(data, Short.parseShort(value));
            }
            catch (NumberFormatException ex) {
              throw new InvalidDataException();
            }
          }
          else if (type.equals(int.class)) {
            try {
              field.setInt(data, Integer.parseInt(value));
            }
            catch (NumberFormatException ex) {
              throw new InvalidDataException();
            }
          }
          else if (type.equals(long.class)) {
            try {
              field.setLong(data, Long.parseLong(value));
            }
            catch (NumberFormatException ex) {
              throw new InvalidDataException();
            }
          }
          else if (type.equals(float.class)) {
            try {
              field.setFloat(data, Float.parseFloat(value));
            }
            catch (NumberFormatException ex) {
              throw new InvalidDataException();
            }
          }
          else if (type.equals(double.class)) {
            try {
              field.setDouble(data, Double.parseDouble(value));
            }
            catch (NumberFormatException ex) {
              throw new InvalidDataException();
            }
          }
          else if (type.equals(char.class)) {
            if (value.length() != 1) {
              throw new InvalidDataException();
            }
            field.setChar(data, value.charAt(0));
          }
          else if (type.equals(boolean.class)) {
            if (value.equals("true")) {
              field.setBoolean(data, true);
            }
            else if (value.equals("false")) {
              field.setBoolean(data, false);
            }
            else {
              throw new InvalidDataException();
            }
          }
          else {
            throw new InvalidDataException();
          }
        }
        else if (type.equals(String.class)) {
          field.set(data, value);
        }
        else if (type.equals(Color.class)) {
          if (value != null) {
            try {
              int rgb = Integer.parseInt(value, 16);
              field.set(data, new Color(rgb));
            }
            catch (NumberFormatException ex) {
              System.out.println("value=" + value);
              throw new InvalidDataException();
            }
          }
          else {
            field.set(data, null);
          }
        }
        else if (Collection.class.isAssignableFrom(type)) {
          Collection<String> coll = (Collection)field.get(data);
          List<Element> options = e.getChildren("option");
          for (Element val : options) {
            String text = val.getText();
            coll.add(text);
            //                            element
          }
        }
        else {
          Element val = e.getChild("value");

          readExternal(field.get(data), val);
        }
      }
      catch (NoSuchFieldException ignored) {
      }
      catch (SecurityException ex) {
        throw new InvalidDataException();
      }
      catch (IllegalAccessException ex) {
        ex.printStackTrace();
        throw new InvalidDataException();
      }
    }
  }