Package com.redcareditor.plist

Source Code of com.redcareditor.plist.PlistPropertyLoader

package com.redcareditor.plist;

import java.lang.reflect.Field;

import com.redcareditor.onig.Rx;

/**
* This class will load properties from the given {@link Dict} into the object.
* It uses reflection, so beware of them typos. :D
*
* @author kungfoo
*/
public class PlistPropertyLoader {
  private Dict dict;
  private Object object;

  public PlistPropertyLoader(Dict dict, Object obj) {
    this.dict = dict;
    this.object = obj;
  }

  public void loadStringProperty(String propertyName) {
    String value = dict.getString(propertyName);
    trySettingProperty(propertyName, value);
  }

  public void loadRegexProperty(String propertyName) {
    String value = dict.getString(propertyName);
    Rx regex = Rx.createRx(value);
    trySettingProperty(propertyName, regex);
  }

  private void trySettingProperty(String propertyName, Object value) {
    try {
      Field prop = object.getClass().getDeclaredField(propertyName);
      if (value != null) {
        prop.set(object, value);
      }
    } catch (Exception e) {
      System.out.println(String.format("Can't set %s = %s on object %s", propertyName, value, object.toString()));
      e.printStackTrace();
    }
  }
}
TOP

Related Classes of com.redcareditor.plist.PlistPropertyLoader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.