Guest User

Untitled

a guest
Jan 17th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.nio.file.*;
  4. import java.util.regex.*;
  5.  
  6. public class CBFIni {
  7.  
  8. public ArrayList<SectionFields> sectionData = new ArrayList<SectionFields>();
  9. Pattern patternParam = Pattern.compile("^[A-Za-z_0-9]+");
  10. Matcher matchParam, matchValue;
  11. String nameParam;
  12.  
  13. public CBFIni() {
  14.  
  15. try {
  16.  
  17. List<String> lines = Files.readAllLines(Paths.get("backups.conf"));
  18. boolean checkSection = false;
  19. int i = -1;
  20.  
  21. for(String line: lines){
  22. if (line.length() > 0 && line.charAt(0) != '#') {
  23. if (!checkSection) {
  24. checkSection = true; // Отметка, что начались параметры секции
  25. sectionData.add(new SectionFields()); // Создаем новый элемент-объект списка
  26. }
  27. i++;
  28. matchParam = patternParam.matcher(line); // Выуживаем имя параметра
  29. if (matchParam.matches()) {
  30. nameParam = line.substring(matchParam.start(),matchParam.end()); // Имя параметра
  31. sectionData.get(sectionData.size()-1).(nameParam) = line.substring(matchParam.end()+1);
  32. }
  33. } else {
  34. checkSection = false;
  35. i = -1;
  36. }
  37. }
  38. } catch (IOException e) {
  39. }
  40. }
  41.  
  42. public class SectionFields {
  43. String backup, description, type, server, folder;
  44. int days, weeks, monthes, years, masterDay;
  45. }
  46.  
  47. }
  48.  
  49. sectionData.get(sectionData.size()-1).(nameParam) = line.substring(matchParam.end()+1);
  50.  
  51. private static void setValue(Class<?> type,
  52. Object object,
  53. String paramName,
  54. String value) throws Exception {
  55.  
  56. Field field;
  57. do {
  58. field = type.getDeclaredField(paramName);
  59. type = type.getSuperclass();
  60. } while (field != null && type != null);
  61.  
  62. if (field == null)
  63. throw new IllegalArgumentException(
  64. "can't find the field with name: " + paramName);
  65.  
  66. field.setAccessible(true);
  67. field.set(object, value);
  68. }
  69.  
  70. private static Map<String, MethodHandle> getSetters(Class<?> type) throws Exception {
  71. Map<String, MethodHandle> setters = new HashMap<>();
  72. while (type != null) {
  73. for (Field field : type.getDeclaredFields()) {
  74. field.setAccessible(true);
  75. setters.put(field.getName(), MethodHandles
  76. .lookup()
  77. .unreflectSetter(field));
  78. }
  79. type = type.getSuperclass();
  80. }
  81.  
  82. return setters;
  83. }
  84.  
  85. Map<String, MethodHandle> setters = getSetters(SectionFields.class);
  86. SectionFields obj = new SectionFields();
  87. setters.get("backup").invoke(obj, "hello world");
Add Comment
Please, Sign In to add comment