1 24 package org.riotfamily.website.css; 25 26 import java.io.BufferedReader ; 27 import java.io.File ; 28 import java.io.FileReader ; 29 import java.io.IOException ; 30 import java.util.HashMap ; 31 import java.util.Map ; 32 33 57 public class IniFile { 58 59 public static final String GLOBAL_SECTION = "global"; 60 61 private File file; 62 63 HashMap sections = new HashMap (); 64 65 Map section; 66 67 private long lastModified; 68 69 public IniFile(File file) throws IOException { 70 this.file = file; 71 load(); 72 } 73 74 public long lastModified() { 75 return file.lastModified(); 76 } 77 78 public synchronized Map getSections() { 79 if (file.lastModified() > lastModified) { 80 try { 81 load(); 82 } 83 catch (IOException e) { 84 throw new RuntimeException (e); 85 } 86 } 87 return sections; 88 } 89 90 private synchronized void load() throws IOException { 91 lastModified = file.lastModified(); 92 sections.clear(); 93 setSection(GLOBAL_SECTION); 94 BufferedReader br = new BufferedReader (new FileReader (file)); 95 for (String ln = br.readLine(); ln != null; ln = br.readLine()) { 96 ln = ln.trim(); 97 if (ln.length() > 0 && ln.charAt(0) != ';') { 98 if (ln.charAt(0) != '[' || ln.indexOf(']') == -1) { 99 int index = ln.indexOf(';'); 100 if (index != -1) { 101 ln = ln.substring(0, index).trim(); 102 } 103 index = ln.indexOf('='); 104 if (index != -1) { 105 String key = ln.substring(0, index).trim(); 106 String value = ln.substring(index + 1).trim(); 107 section.put(key, convertString(value)); 108 } 109 } 110 else { 111 setSection(ln.substring(1, ln.indexOf(']')).trim()); 112 } 113 } 114 } 115 } 116 117 private void setSection(String name) { 118 section = (Map ) sections.get(name); 119 if (section == null) { 120 section = new HashMap (); 121 sections.put(name, section); 122 } 123 } 124 125 private Object convertString(String s) { 126 if (s.equals("true")) { 127 return Boolean.TRUE; 128 } 129 if (s.equals("false")) { 130 return Boolean.FALSE; 131 } 132 try { 133 return Integer.valueOf(s); 134 } 135 catch (NumberFormatException e) { 136 } 137 return s; 138 } 139 140 } 141 | Popular Tags |