1 18 package org.objectweb.util.monolog.file.monolog; 19 20 import java.io.BufferedReader ; 21 import java.io.BufferedWriter ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.io.OutputStream ; 26 import java.io.OutputStreamWriter ; 27 import java.text.DateFormat ; 28 import java.util.ArrayList ; 29 import java.util.Calendar ; 30 import java.util.Collections ; 31 import java.util.Properties ; 32 33 41 public class MonologPropertiesIO { 42 43 public static final char[] PROP_VAL_SEPS = {'=', ' '}; 44 public static final String [] keywords = { 45 PropertiesConfAccess.LOGGER_FIELD + PropertiesConfAccess.DOT, 46 PropertiesConfAccess.HANDLER_FIELD + PropertiesConfAccess.DOT, 47 PropertiesConfAccess.LEVEL_FIELD + PropertiesConfAccess.DOT 48 }; 49 50 public static void writeProperties(Properties props, OutputStream os) throws IOException { 51 BufferedWriter w = new BufferedWriter (new OutputStreamWriter (os)); 52 ArrayList keys = new ArrayList (props.keySet()); 54 Collections.sort(keys); 56 ArrayList loggers = new ArrayList (); 57 ArrayList handlers = new ArrayList (); 58 ArrayList levels = new ArrayList (); 59 for(int i=0; i<keys.size(); i++) { 60 String key = (String ) keys.get(i); 61 if (key.startsWith(PropertiesConfAccess.LOGGER_FIELD)) { 62 if (key.startsWith(PropertiesConfAccess.LOGGER_FIELD 63 + PropertiesConfAccess.DOT 64 + "root" + PropertiesConfAccess.DOT)) { 65 loggers.add(0,key); 66 } else { 67 loggers.add(key); 68 } 69 } else if (key.startsWith(PropertiesConfAccess.HANDLER_FIELD)) { 70 handlers.add(key); 71 } else if (key.startsWith(PropertiesConfAccess.LEVEL_FIELD)) { 72 levels.add(key); 73 } 74 } 75 keys = null; 76 77 w.write("#Generated by Monolog the "); 78 w.write(DateFormat.getInstance().format( 79 Calendar.getInstance().getTime())); 80 81 if (levels.size() > 0) { 82 w.newLine(); 83 w.newLine(); 84 w.write("#Level configuration"); 85 w.newLine(); 86 for(int i=0; i<levels.size();i++) { 87 String key = (String ) levels.get(i); 88 w.write(key); 89 w.write(PROP_VAL_SEPS[0]); 90 w.write(props.getProperty(key)); 91 w.newLine(); 92 } 93 } 94 95 if (handlers.size() > 0) { 96 w.newLine(); 97 w.newLine(); 98 w.write("#Handler configuration"); 99 w.newLine(); 100 for(int i=0; i<handlers.size();i++) { 101 String key = (String ) handlers.get(i); 102 if (!key.endsWith(PropertiesConfAccess.DOT 103 + PropertiesConfAccess.ACTIVATION)) { 104 w.write(key); 105 w.write(PROP_VAL_SEPS[0]); 106 w.write(props.getProperty(key)); 107 w.newLine(); 108 } 109 } 110 } 111 if (loggers.size() > 0) { 112 w.newLine(); 113 w.newLine(); 114 w.write("#Logger configuration"); 115 w.newLine(); 116 for(int i=0; i<loggers.size();i++) { 117 String key = (String ) loggers.get(i); 118 String value = props.getProperty(key); 119 if (key.endsWith(PropertiesConfAccess.DOT 120 + PropertiesConfAccess.ADDITIVITY_FIELD) 121 && Boolean.valueOf(value).booleanValue()) { 122 continue; 123 } else if (key.endsWith(PropertiesConfAccess.DOT 124 + PropertiesConfAccess.LEVEL_FIELD) 125 && "INHERIT".equalsIgnoreCase(value)) { 126 key = '#' + key; 127 } 128 w.write(key); 129 w.write(PROP_VAL_SEPS[0]); 130 w.write(value); 131 w.newLine(); 132 } 133 } 134 w.flush(); 135 } 136 137 public static Properties readProperties(InputStream is) throws IOException { 138 Properties props = new Properties (); 139 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 140 while (br.ready()) { 141 String p = br.readLine(); 142 if (p == null || p.length() == 0) { 143 continue; 144 } 145 p = p.replace((char) Character.LINE_SEPARATOR, ' ').trim(); 146 boolean isComment = false; 147 int nc; 148 for(nc=0; nc<p.length() && p.charAt(nc) == '#'; nc++) { 149 isComment = true; 150 } 151 if (nc == p.length()) { 152 continue; 153 } 154 if (isComment) { 155 p = p.substring(nc).trim(); 156 } 157 int keywordIdx; 158 for(keywordIdx=0; keywordIdx<keywords.length; keywordIdx++) { 159 if(p.startsWith(keywords[keywordIdx])) { 160 break; 161 } 162 } 163 int sepIdx = -1; 164 if (keywordIdx < keywords.length) { 165 for(int i = 0; sepIdx == -1 && i<PROP_VAL_SEPS.length; i++) { 166 sepIdx = p.indexOf(PROP_VAL_SEPS[i]); 167 } 168 } 169 if (sepIdx != -1) { 170 String key = p.substring(0, sepIdx); 171 String value = p.substring(sepIdx+1).trim(); 172 if (isComment) { 173 if (keywordIdx == 0 174 && key.endsWith(PropertiesConfAccess.DOT 175 + PropertiesConfAccess.LEVEL_FIELD)) { props.put(key, "INHERIT"); 177 } 178 } else { 179 props.put(key, value); 180 } 181 } 182 } 183 return props; 184 } 185 } 186 | Popular Tags |