KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > reader > XMLFileReader


1 /*
2  * XMLFileReader.java
3  *
4  * Created on 25. November 2002, 23:05
5  */

6
7 package org.jzonic.jlo.reader;
8
9 import org.jzonic.jlo.*;
10 import org.jzonic.jlo.error.ErrorHandler;
11 import org.jzonic.jlo.filter.LogFilter;
12 import org.jzonic.jlo.formatter.*;
13 import org.jzonic.jlo.handler.*;
14 import org.w3c.dom.Document JavaDoc;
15 import org.w3c.dom.NamedNodeMap JavaDoc;
16 import org.w3c.dom.Node JavaDoc;
17 import org.w3c.dom.NodeList JavaDoc;
18 import org.xml.sax.SAXException JavaDoc;
19
20 import javax.xml.parsers.DocumentBuilder JavaDoc;
21 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
22 import javax.xml.parsers.ParserConfigurationException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Vector JavaDoc;
29 /**
30  *
31  * @author Administrator
32  */

33 public class XMLFileReader implements LogConfigurationReader {
34     
35     private String JavaDoc fileName;
36     private static final VariableManager variableManager = VariableManager.getInstance();
37     
38     public XMLFileReader() {
39     }
40     
41     public void setFileName(String JavaDoc fileName) {
42         this.fileName = fileName;
43     }
44     
45     public LogConfiguration parseConfiguration(String JavaDoc configurationName) throws ReaderException {
46         ResourceLocator rl = new ResourceLocator();
47         InputStream JavaDoc is = rl.findResource(fileName);
48         if ( is == null ) {
49             throw new ReaderException("the file "+fileName+" cannot be found in the classpath");
50         }
51         DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
52         dbf.setValidating(false);
53         DocumentBuilder JavaDoc db = null;
54         try {
55             db = dbf.newDocumentBuilder();
56         } catch (ParserConfigurationException JavaDoc pce) {
57             ErrorHandler.reportError("Exception during parser configuration",pce);
58             throw new ReaderException("Exception during parser configuration:" + pce.getMessage());
59         }
60         Document JavaDoc doc = null;
61         try {
62             doc = db.parse(is);
63         } catch (SAXException JavaDoc se) {
64             ErrorHandler.reportError("Exception while parsing the file", se);
65             throw new ReaderException(
66             "Exception while parsing the file:" + se.getMessage());
67         } catch (IOException JavaDoc ioe) {
68             ErrorHandler.reportError("IO-Exception", ioe);
69             throw new ReaderException("IO-Exception:" + ioe.getMessage());
70         }
71         return processXML(doc,configurationName);
72     }
73     
74     private LogConfiguration processXML(Document JavaDoc doc,String JavaDoc configurationName) {
75         LogConfiguration lc = new LogConfiguration(configurationName);
76         getFilters(doc, lc);
77         getGenerators(doc, lc);
78         getLoggers(doc, lc);
79         getChannels(doc, lc);
80         //getChannels(doc, lc);
81
getVariables(doc, lc);
82         getPipes(doc, lc);
83         return lc;
84     }
85     
86     private void getLoggers(Document JavaDoc doc, LogConfiguration lc) {
87         String JavaDoc currentLogger;
88         NodeList JavaDoc nl = doc.getElementsByTagName("logger");
89         for (int i = 0; i < nl.getLength(); i++) {
90             Node JavaDoc n = nl.item(i);
91             NamedNodeMap JavaDoc curAtt = n.getAttributes();
92             Node JavaDoc curNode = curAtt.getNamedItem("name");
93             currentLogger = curNode.getNodeValue();
94             String JavaDoc lvl = "INFO";
95             Vector JavaDoc lgs = new Vector JavaDoc();
96             String JavaDoc filter = null;
97             for (Node JavaDoc child = n.getFirstChild();child != null;child = child.getNextSibling()) {
98                 String JavaDoc generator = null;
99                 if (child.getNodeName().equals("targets")) {
100                     lvl = child.getFirstChild().getNodeValue();
101                 }
102                 if (child.getNodeName().equals("generator-name")) {
103                     generator = child.getFirstChild().getNodeValue();
104                     lgs.add(generator);
105                 }
106                 if (child.getNodeName().equals("filter-name")) {
107                     filter = child.getFirstChild().getNodeValue();
108                 }
109             }
110             if (lgs.size() > 0 ) {
111                 int targets = Target.parse(lvl);
112                 if ( lvl == null ) {
113                     ErrorHandler.reportError("No targets defined for logger:"+currentLogger+". Using ALL as targets");
114                 }
115                 Logger myLogger = new Logger(currentLogger, targets,lc.getName());
116                 if ( filter != null ) {
117                     myLogger.setFilter(lc.getLogFilter(filter));
118                 }
119                 for ( int k = 0; k < lgs.size();k++) {
120                     String JavaDoc lName = (String JavaDoc)lgs.get(k);
121                     LogGenerator lg = lc.getLogGenerator(lName);
122                     if (lg != null) {
123                         myLogger.addLogGenerator(lg);
124                     }
125                 }
126                 lc.addLogger(myLogger);
127             } else {
128                 ErrorHandler.reportError("No generator-name found. Using default LogGenerator for "+ currentLogger);
129                 LogGenerator lg = lc.getLogGenerator("Default");
130                 int targets = Target.parse(lvl);
131                 Logger myLogger = new Logger(currentLogger, targets,lc.getName());
132                 myLogger.addLogGenerator(lg);
133                 lc.addLogger(myLogger);
134             }
135         }
136         
137     }
138     
139     private void getVariables(Document JavaDoc doc, LogConfiguration lc) {
140         String JavaDoc varName;
141         String JavaDoc varValue;
142         NodeList JavaDoc nl = doc.getElementsByTagName("variable");
143         for (int i = 0; i < nl.getLength(); i++) {
144             Node JavaDoc n = nl.item(i);
145             NamedNodeMap JavaDoc curAtt = n.getAttributes();
146             Node JavaDoc curNode = curAtt.getNamedItem("name");
147             varName = curNode.getNodeValue();
148             Node JavaDoc valueNode = curAtt.getNamedItem("value");
149             varValue = valueNode.getNodeValue();
150             if (varName != null && varValue != null) {
151                 variableManager.addVariable(varName, varValue, lc.getName());
152             }
153             else {
154                 if ( varName != null && varValue == null ) {
155                     ErrorHandler.reportError("Variable definition found for:"+varName+" but no value attribute defined");
156                 }
157                 else if ( varName == null && varValue != null ) {
158                     ErrorHandler.reportError("Variable definition found but no name attribute");
159                 }
160                 else if ( varName == null && varValue == null ) {
161                     ErrorHandler.reportError("Variable tag found but no a name and a value attribute");
162                 }
163             }
164         }
165     }
166     
167     private void getChannels(Document JavaDoc doc, LogConfiguration lc) {
168         String JavaDoc currentChannel;
169         NodeList JavaDoc nl = doc.getElementsByTagName("channel");
170         for (int i = 0; i < nl.getLength(); i++) {
171             Node JavaDoc n = nl.item(i);
172             NamedNodeMap JavaDoc curAtt = n.getAttributes();
173             Node JavaDoc curNode = curAtt.getNamedItem("name");
174             currentChannel = curNode.getNodeValue();
175             String JavaDoc generator = null;
176             String JavaDoc mode = "off";
177             boolean running = false;
178             for (Node JavaDoc child = n.getFirstChild();child != null;child = child.getNextSibling()) {
179                 if (child.getNodeName().equals("generator-name")) {
180                     generator = child.getFirstChild().getNodeValue();
181                 }
182                 if (child.getNodeName().equals("mode")) {
183                     mode = child.getFirstChild().getNodeValue();
184                 }
185             }
186             mode = mode.toLowerCase();
187             if (mode.equals("on")) {
188                 running = true;
189             }
190             if (generator != null) {
191                 LogGenerator lg = lc.getLogGenerator(generator);
192                 if (lg != null) {
193                     Channel myChannel =
194                     new Channel(currentChannel, lg, running);
195                     lc.addChannel(myChannel);
196                 }
197                 else {
198                     ErrorHandler.reportError("Could not find LogGenerator for:"+currentChannel+". Using the default one");
199                     lg = lc.getLogGenerator("Default");
200                     Channel myChannel = new Channel(currentChannel, lg, running);
201                     lc.addChannel(myChannel);
202                 }
203             }
204             else {
205                 ErrorHandler.reportError("LogManager getChannels: Using default LogGenerator for "+ currentChannel);
206                 LogGenerator lg = lc.getLogGenerator("Default");
207                 Channel myChannel = new Channel(currentChannel, lg, running);
208                 lc.addChannel(myChannel);
209             }
210         }
211     }
212     
213     private void getPipes(Document JavaDoc doc, LogConfiguration lc) {
214         NodeList JavaDoc nl = doc.getElementsByTagName("pipe");
215         for (int i = 0; i < nl.getLength(); i++) {
216             LogPipe pipe = null;
217             String JavaDoc generator = null;
218             String JavaDoc filter = null;
219             Node JavaDoc n = nl.item(i);
220             for (Node JavaDoc child = n.getFirstChild();child != null;child = child.getNextSibling()) {
221                 if (child.getNodeName().equals("generator-name")) {
222                     generator = child.getFirstChild().getNodeValue();
223                 }
224                 if (child.getNodeName().equals("filter-name")) {
225                     filter = child.getFirstChild().getNodeValue();
226                 }
227             }
228             if (generator != null) {
229                 LogGenerator lg = lc.getLogGenerator(generator);
230                 if (lg != null) {
231                     pipe = new LogPipe();
232                     pipe.setFilter(lc.getLogFilter(filter));
233                     pipe.setGenerator(lg);
234                     lc.addLogPipe(pipe);
235                 }
236             }
237             else {
238                 ErrorHandler.reportError("LogManager getPipes: Using default LogGenerator");
239                 LogGenerator lg = lc.getLogGenerator("Default");
240                 pipe = new LogPipe();
241                 pipe.setFilter(lc.getLogFilter(filter));
242                 pipe.setGenerator(lg);
243                 lc.addLogPipe(pipe);
244             }
245         }
246     }
247     
248     /*
249      *<filter name="ExceptionFilter">
250     <class>TextFilter</class>
251     <parameter name="expression" value="Exception"/>
252     <parameter name="case_sensitiv" value="false"/>
253   </filter>
254      */

255     private void getFilters(Document JavaDoc doc, LogConfiguration lc) {
256         NodeList JavaDoc nl = doc.getElementsByTagName("filter");
257         for (int i = 0; i < nl.getLength(); i++) {
258             String JavaDoc filterName = null;
259             String JavaDoc className = null;
260             Map JavaDoc params = new HashMap JavaDoc();
261             Node JavaDoc n = nl.item(i);
262             NamedNodeMap JavaDoc curAtt = n.getAttributes();
263             Node JavaDoc curNode = curAtt.getNamedItem("name");
264             if ( curNode != null ) {
265                 filterName = curNode.getNodeValue();
266                 for (Node JavaDoc child = n.getFirstChild();child != null;child = child.getNextSibling()) {
267                     if (child.getNodeName().equals("class")) {
268                         className = child.getFirstChild().getNodeValue();
269                     }
270                     if (child.getNodeName().equals("parameter")) {
271                         String JavaDoc pnName = null;
272                         String JavaDoc pvName = null;
273                         NamedNodeMap JavaDoc childAtt = child.getAttributes();
274                         Node JavaDoc pnNode = childAtt.getNamedItem("name");
275                         if ( pnNode != null) {
276                             pnName = pnNode.getNodeValue();
277                         }
278                         Node JavaDoc pvNode = childAtt.getNamedItem("value");
279                         if ( pvNode != null) {
280                             pvName = pvNode.getNodeValue();
281                         }
282                         if ( pnName != null && pvName != null ) {
283                             params.put(pnName,pvName);
284                         }
285                     }
286                 }
287                 if ( className != null) {
288                     try {
289                         if ( className.indexOf(".") == -1 ) {
290                             className = "org.jzonic.jlo.filter."+className;
291                         }
292                         Class JavaDoc clazz = Class.forName(className);
293                         LogFilter filter = (LogFilter)clazz.newInstance();
294                         filter.setParameters(params);
295                         lc.addLogFilter(filterName,filter);
296                     }
297                     catch (Exception JavaDoc e) {
298                         ErrorHandler.reportError("LogManager getFilters: Cannot instantiate filter:"+filterName);
299                     }
300                 }
301                 else {
302                     ErrorHandler.reportError("LogManager getFilters: No class given for filter:"+filterName);
303                 }
304             }
305             else {
306                 ErrorHandler.reportError("No name defined for filter. Ignoring entry");
307             }
308         }
309     }
310     
311     
312     private void getGenerators(Document JavaDoc doc, LogConfiguration lc) {
313         String JavaDoc currentGenerator = "default";
314         NodeList JavaDoc nl = doc.getElementsByTagName("generator");
315         for (int i = 0; i < nl.getLength(); i++) {
316             Node JavaDoc n = nl.item(i);
317             NamedNodeMap JavaDoc curAtt = n.getAttributes();
318             Node JavaDoc curNode = curAtt.getNamedItem("name");
319             if (curNode != null) {
320                 currentGenerator = curNode.getNodeValue();
321             }
322             Hashtable JavaDoc params = new Hashtable JavaDoc();
323             Map JavaDoc fParams = new HashMap JavaDoc();
324             Formatter formatter = null;
325             Handler handler = null;
326             String JavaDoc filter = null;
327             for (Node JavaDoc child = n.getFirstChild();child != null;child = child.getNextSibling()) {
328                 if (child.getNodeName().equals("formatter")) {
329                     formatter = getFormatter(child,lc.getName());
330                 }
331                 if (child.getNodeName().equals("handler")) {
332                     handler = getHandler(child,lc.getName());
333                 }
334                 if (child.getNodeName().equals("filter-name")) {
335                     filter = child.getFirstChild().getNodeValue();
336                 }
337             }
338             LogGenerator generator = createLogGenerator(currentGenerator, handler, formatter, params,lc.getName());
339             if ( filter != null ) {
340                 generator.setFilter(lc.getLogFilter(filter));
341             }
342             lc.addLogGenerator(generator);
343         }
344     }
345
346     private Handler getHandler(Node JavaDoc n,String JavaDoc configName) {
347         NamedNodeMap JavaDoc myAtt = n.getAttributes();
348         Node JavaDoc myNode = myAtt.getNamedItem("class");
349         if ( myNode != null ) {
350             String JavaDoc handlerName = myNode.getNodeValue();
351             if (handlerName != null) {
352                 // next we set the formatter
353
Handler handler = HandlerFactory.getHandler(handlerName,configName);
354                 Map JavaDoc params = getParams(n);
355                 handler.setParameter(params);
356                 return handler;
357             }
358         }
359         else {
360             ErrorHandler.reportError("No class defined for the handler. Ignoring entry");
361         }
362         return null;
363     }
364
365     private Formatter getFormatter(Node JavaDoc n,String JavaDoc configName) {
366         NamedNodeMap JavaDoc myAtt = n.getAttributes();
367         Node JavaDoc myNode = myAtt.getNamedItem("class");
368         if ( myNode != null ) {
369             String JavaDoc formatterName = myNode.getNodeValue();
370             Formatter formatter = null;
371             if (formatterName != null) {
372                 // next we set the formatter
373
formatter = FormatterFactory.getFormatter(formatterName,configName);
374                 Map JavaDoc params = getParams(n);
375                 formatter.setParameter(params);
376                 return formatter;
377             }
378         }
379         else {
380             ErrorHandler.reportError("No class defined for the formatter. Ignoring entry");
381         }
382         return null;
383     }
384
385     private Map JavaDoc getParams(Node JavaDoc n) {
386         Map JavaDoc params = new HashMap JavaDoc();
387         for (Node JavaDoc child = n.getFirstChild();child != null;child = child.getNextSibling()) {
388             if (child.getNodeName().equals("parameter")) {
389                 String JavaDoc pnName = null;
390                 String JavaDoc pvName = null;
391                 NamedNodeMap JavaDoc childAtt = child.getAttributes();
392                 Node JavaDoc pnNode = childAtt.getNamedItem("name");
393                 if ( pnNode != null) {
394                     pnName = pnNode.getNodeValue();
395                 }
396                 Node JavaDoc pvNode = childAtt.getNamedItem("value");
397                 if ( pvNode != null) {
398                     pvName = pvNode.getNodeValue();
399                 }
400                 if ( pnName != null && pvName != null ) {
401                     params.put(pnName,pvName);
402                 }
403             }
404         }
405         return params;
406     }
407
408     public LogGenerator createLogGenerator(String JavaDoc name,String JavaDoc handlerName,String JavaDoc formatterName,Hashtable JavaDoc params,String JavaDoc configName) {
409         // we create the handler
410
Handler handler = HandlerFactory.getHandler(handlerName,configName);
411         if (handler != null)
412             handler.setParameter(params);
413         Formatter formatter = null;
414         if (formatterName != null) {
415             // next we set the formatter
416
formatter = FormatterFactory.getFormatter(formatterName,configName);
417         }
418         if (handler != null) {
419             return new LogGenerator(name, handler, formatter);
420         }
421         else {
422             return new LogGenerator(name, new ConsoleHandler(configName), new DefaultFormatter(configName));
423         }
424     }
425
426     public LogGenerator createLogGenerator(String JavaDoc name,Handler handler,Formatter formatter,Hashtable JavaDoc params,String JavaDoc configName) {
427         if (handler != null) {
428             return new LogGenerator(name, handler, formatter);
429         }
430         else {
431             return new LogGenerator(name, new ConsoleHandler(configName), new DefaultFormatter(configName));
432         }
433     }
434 }
435
Popular Tags