KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > file > monolog > MonologPropertiesIO


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.util.monolog.file.monolog;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.BufferedWriter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.OutputStreamWriter JavaDoc;
27 import java.text.DateFormat JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Calendar JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 /**
34  * Permit to read or write a java.util.Properties from an inputStream.
35  * The interest of this implementation with regards to the
36  * properties.load(InputStream) method is to sort the properties and to take
37  * in account particularity of logging properties.
38  *
39  * @author S.Chassande-Barrioz
40  */

41 public class MonologPropertiesIO {
42     
43     public static final char[] PROP_VAL_SEPS = {'=', ' '};
44     public static final String JavaDoc[] 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 JavaDoc props, OutputStream JavaDoc os) throws IOException JavaDoc {
51         BufferedWriter JavaDoc w = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(os));
52         //Sort properties
53
ArrayList JavaDoc keys = new ArrayList JavaDoc(props.keySet());
54         // Order properties by the alphabetic order
55
Collections.sort(keys);
56         ArrayList JavaDoc loggers = new ArrayList JavaDoc();
57         ArrayList JavaDoc handlers = new ArrayList JavaDoc();
58         ArrayList JavaDoc levels = new ArrayList JavaDoc();
59         for(int i=0; i<keys.size(); i++) {
60             String JavaDoc key = (String JavaDoc) 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 JavaDoc key = (String JavaDoc) 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 JavaDoc key = (String JavaDoc) 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 JavaDoc key = (String JavaDoc) loggers.get(i);
118                 String JavaDoc 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 JavaDoc readProperties(InputStream JavaDoc is) throws IOException JavaDoc {
138         Properties JavaDoc props = new Properties JavaDoc();
139         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
140         while (br.ready()) {
141             String JavaDoc 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 JavaDoc key = p.substring(0, sepIdx);
171                 String JavaDoc value = p.substring(sepIdx+1).trim();
172                 if (isComment) {
173                     if (keywordIdx == 0
174                             && key.endsWith(PropertiesConfAccess.DOT
175                                     + PropertiesConfAccess.LEVEL_FIELD)) { // logger
176
props.put(key, "INHERIT");
177                     }
178                 } else {
179                     props.put(key, value);
180                 }
181             }
182         }
183         return props;
184     }
185 }
186
Popular Tags