Source Code Cross Referenced for XMLFileReader.java in  » Development » jLo » org » jzonic » jlo » reader » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » jLo » org.jzonic.jlo.reader 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * XMLFileReader.java
003:         *
004:         * Created on 25. November 2002, 23:05
005:         */
006:
007:        package org.jzonic.jlo.reader;
008:
009:        import org.jzonic.jlo.*;
010:        import org.jzonic.jlo.error.ErrorHandler;
011:        import org.jzonic.jlo.filter.LogFilter;
012:        import org.jzonic.jlo.formatter.*;
013:        import org.jzonic.jlo.handler.*;
014:        import org.w3c.dom.Document;
015:        import org.w3c.dom.NamedNodeMap;
016:        import org.w3c.dom.Node;
017:        import org.w3c.dom.NodeList;
018:        import org.xml.sax.SAXException;
019:
020:        import javax.xml.parsers.DocumentBuilder;
021:        import javax.xml.parsers.DocumentBuilderFactory;
022:        import javax.xml.parsers.ParserConfigurationException;
023:        import java.io.IOException;
024:        import java.io.InputStream;
025:        import java.util.HashMap;
026:        import java.util.Hashtable;
027:        import java.util.Map;
028:        import java.util.Vector;
029:
030:        /**
031:         *
032:         * @author  Administrator
033:         */
034:        public class XMLFileReader implements  LogConfigurationReader {
035:
036:            private String fileName;
037:            private static final VariableManager variableManager = VariableManager
038:                    .getInstance();
039:
040:            public XMLFileReader() {
041:            }
042:
043:            public void setFileName(String fileName) {
044:                this .fileName = fileName;
045:            }
046:
047:            public LogConfiguration parseConfiguration(String configurationName)
048:                    throws ReaderException {
049:                ResourceLocator rl = new ResourceLocator();
050:                InputStream is = rl.findResource(fileName);
051:                if (is == null) {
052:                    throw new ReaderException("the file " + fileName
053:                            + " cannot be found in the classpath");
054:                }
055:                DocumentBuilderFactory dbf = DocumentBuilderFactory
056:                        .newInstance();
057:                dbf.setValidating(false);
058:                DocumentBuilder db = null;
059:                try {
060:                    db = dbf.newDocumentBuilder();
061:                } catch (ParserConfigurationException pce) {
062:                    ErrorHandler.reportError(
063:                            "Exception during parser configuration", pce);
064:                    throw new ReaderException(
065:                            "Exception during parser configuration:"
066:                                    + pce.getMessage());
067:                }
068:                Document doc = null;
069:                try {
070:                    doc = db.parse(is);
071:                } catch (SAXException se) {
072:                    ErrorHandler.reportError(
073:                            "Exception while parsing the file", se);
074:                    throw new ReaderException(
075:                            "Exception while parsing the file:"
076:                                    + se.getMessage());
077:                } catch (IOException ioe) {
078:                    ErrorHandler.reportError("IO-Exception", ioe);
079:                    throw new ReaderException("IO-Exception:"
080:                            + ioe.getMessage());
081:                }
082:                return processXML(doc, configurationName);
083:            }
084:
085:            private LogConfiguration processXML(Document doc,
086:                    String configurationName) {
087:                LogConfiguration lc = new LogConfiguration(configurationName);
088:                getFilters(doc, lc);
089:                getGenerators(doc, lc);
090:                getLoggers(doc, lc);
091:                getChannels(doc, lc);
092:                //getChannels(doc, lc);
093:                getVariables(doc, lc);
094:                getPipes(doc, lc);
095:                return lc;
096:            }
097:
098:            private void getLoggers(Document doc, LogConfiguration lc) {
099:                String currentLogger;
100:                NodeList nl = doc.getElementsByTagName("logger");
101:                for (int i = 0; i < nl.getLength(); i++) {
102:                    Node n = nl.item(i);
103:                    NamedNodeMap curAtt = n.getAttributes();
104:                    Node curNode = curAtt.getNamedItem("name");
105:                    currentLogger = curNode.getNodeValue();
106:                    String lvl = "INFO";
107:                    Vector lgs = new Vector();
108:                    String filter = null;
109:                    for (Node child = n.getFirstChild(); child != null; child = child
110:                            .getNextSibling()) {
111:                        String generator = null;
112:                        if (child.getNodeName().equals("targets")) {
113:                            lvl = child.getFirstChild().getNodeValue();
114:                        }
115:                        if (child.getNodeName().equals("generator-name")) {
116:                            generator = child.getFirstChild().getNodeValue();
117:                            lgs.add(generator);
118:                        }
119:                        if (child.getNodeName().equals("filter-name")) {
120:                            filter = child.getFirstChild().getNodeValue();
121:                        }
122:                    }
123:                    if (lgs.size() > 0) {
124:                        int targets = Target.parse(lvl);
125:                        if (lvl == null) {
126:                            ErrorHandler
127:                                    .reportError("No targets defined for logger:"
128:                                            + currentLogger
129:                                            + ". Using ALL as targets");
130:                        }
131:                        Logger myLogger = new Logger(currentLogger, targets, lc
132:                                .getName());
133:                        if (filter != null) {
134:                            myLogger.setFilter(lc.getLogFilter(filter));
135:                        }
136:                        for (int k = 0; k < lgs.size(); k++) {
137:                            String lName = (String) lgs.get(k);
138:                            LogGenerator lg = lc.getLogGenerator(lName);
139:                            if (lg != null) {
140:                                myLogger.addLogGenerator(lg);
141:                            }
142:                        }
143:                        lc.addLogger(myLogger);
144:                    } else {
145:                        ErrorHandler
146:                                .reportError("No generator-name found. Using default LogGenerator for "
147:                                        + currentLogger);
148:                        LogGenerator lg = lc.getLogGenerator("Default");
149:                        int targets = Target.parse(lvl);
150:                        Logger myLogger = new Logger(currentLogger, targets, lc
151:                                .getName());
152:                        myLogger.addLogGenerator(lg);
153:                        lc.addLogger(myLogger);
154:                    }
155:                }
156:
157:            }
158:
159:            private void getVariables(Document doc, LogConfiguration lc) {
160:                String varName;
161:                String varValue;
162:                NodeList nl = doc.getElementsByTagName("variable");
163:                for (int i = 0; i < nl.getLength(); i++) {
164:                    Node n = nl.item(i);
165:                    NamedNodeMap curAtt = n.getAttributes();
166:                    Node curNode = curAtt.getNamedItem("name");
167:                    varName = curNode.getNodeValue();
168:                    Node valueNode = curAtt.getNamedItem("value");
169:                    varValue = valueNode.getNodeValue();
170:                    if (varName != null && varValue != null) {
171:                        variableManager.addVariable(varName, varValue, lc
172:                                .getName());
173:                    } else {
174:                        if (varName != null && varValue == null) {
175:                            ErrorHandler
176:                                    .reportError("Variable definition found for:"
177:                                            + varName
178:                                            + " but no value attribute defined");
179:                        } else if (varName == null && varValue != null) {
180:                            ErrorHandler
181:                                    .reportError("Variable definition found but no name attribute");
182:                        } else if (varName == null && varValue == null) {
183:                            ErrorHandler
184:                                    .reportError("Variable tag found but no a name and a value attribute");
185:                        }
186:                    }
187:                }
188:            }
189:
190:            private void getChannels(Document doc, LogConfiguration lc) {
191:                String currentChannel;
192:                NodeList nl = doc.getElementsByTagName("channel");
193:                for (int i = 0; i < nl.getLength(); i++) {
194:                    Node n = nl.item(i);
195:                    NamedNodeMap curAtt = n.getAttributes();
196:                    Node curNode = curAtt.getNamedItem("name");
197:                    currentChannel = curNode.getNodeValue();
198:                    String generator = null;
199:                    String mode = "off";
200:                    boolean running = false;
201:                    for (Node child = n.getFirstChild(); child != null; child = child
202:                            .getNextSibling()) {
203:                        if (child.getNodeName().equals("generator-name")) {
204:                            generator = child.getFirstChild().getNodeValue();
205:                        }
206:                        if (child.getNodeName().equals("mode")) {
207:                            mode = child.getFirstChild().getNodeValue();
208:                        }
209:                    }
210:                    mode = mode.toLowerCase();
211:                    if (mode.equals("on")) {
212:                        running = true;
213:                    }
214:                    if (generator != null) {
215:                        LogGenerator lg = lc.getLogGenerator(generator);
216:                        if (lg != null) {
217:                            Channel myChannel = new Channel(currentChannel, lg,
218:                                    running);
219:                            lc.addChannel(myChannel);
220:                        } else {
221:                            ErrorHandler
222:                                    .reportError("Could not find LogGenerator for:"
223:                                            + currentChannel
224:                                            + ". Using the default one");
225:                            lg = lc.getLogGenerator("Default");
226:                            Channel myChannel = new Channel(currentChannel, lg,
227:                                    running);
228:                            lc.addChannel(myChannel);
229:                        }
230:                    } else {
231:                        ErrorHandler
232:                                .reportError("LogManager getChannels: Using default LogGenerator for "
233:                                        + currentChannel);
234:                        LogGenerator lg = lc.getLogGenerator("Default");
235:                        Channel myChannel = new Channel(currentChannel, lg,
236:                                running);
237:                        lc.addChannel(myChannel);
238:                    }
239:                }
240:            }
241:
242:            private void getPipes(Document doc, LogConfiguration lc) {
243:                NodeList nl = doc.getElementsByTagName("pipe");
244:                for (int i = 0; i < nl.getLength(); i++) {
245:                    LogPipe pipe = null;
246:                    String generator = null;
247:                    String filter = null;
248:                    Node n = nl.item(i);
249:                    for (Node child = n.getFirstChild(); child != null; child = child
250:                            .getNextSibling()) {
251:                        if (child.getNodeName().equals("generator-name")) {
252:                            generator = child.getFirstChild().getNodeValue();
253:                        }
254:                        if (child.getNodeName().equals("filter-name")) {
255:                            filter = child.getFirstChild().getNodeValue();
256:                        }
257:                    }
258:                    if (generator != null) {
259:                        LogGenerator lg = lc.getLogGenerator(generator);
260:                        if (lg != null) {
261:                            pipe = new LogPipe();
262:                            pipe.setFilter(lc.getLogFilter(filter));
263:                            pipe.setGenerator(lg);
264:                            lc.addLogPipe(pipe);
265:                        }
266:                    } else {
267:                        ErrorHandler
268:                                .reportError("LogManager getPipes: Using default LogGenerator");
269:                        LogGenerator lg = lc.getLogGenerator("Default");
270:                        pipe = new LogPipe();
271:                        pipe.setFilter(lc.getLogFilter(filter));
272:                        pipe.setGenerator(lg);
273:                        lc.addLogPipe(pipe);
274:                    }
275:                }
276:            }
277:
278:            /*
279:             *<filter name="ExceptionFilter">
280:            <class>TextFilter</class>
281:            <parameter name="expression" value="Exception"/>
282:            <parameter name="case_sensitiv" value="false"/>
283:            </filter>
284:             */
285:            private void getFilters(Document doc, LogConfiguration lc) {
286:                NodeList nl = doc.getElementsByTagName("filter");
287:                for (int i = 0; i < nl.getLength(); i++) {
288:                    String filterName = null;
289:                    String className = null;
290:                    Map params = new HashMap();
291:                    Node n = nl.item(i);
292:                    NamedNodeMap curAtt = n.getAttributes();
293:                    Node curNode = curAtt.getNamedItem("name");
294:                    if (curNode != null) {
295:                        filterName = curNode.getNodeValue();
296:                        for (Node child = n.getFirstChild(); child != null; child = child
297:                                .getNextSibling()) {
298:                            if (child.getNodeName().equals("class")) {
299:                                className = child.getFirstChild()
300:                                        .getNodeValue();
301:                            }
302:                            if (child.getNodeName().equals("parameter")) {
303:                                String pnName = null;
304:                                String pvName = null;
305:                                NamedNodeMap childAtt = child.getAttributes();
306:                                Node pnNode = childAtt.getNamedItem("name");
307:                                if (pnNode != null) {
308:                                    pnName = pnNode.getNodeValue();
309:                                }
310:                                Node pvNode = childAtt.getNamedItem("value");
311:                                if (pvNode != null) {
312:                                    pvName = pvNode.getNodeValue();
313:                                }
314:                                if (pnName != null && pvName != null) {
315:                                    params.put(pnName, pvName);
316:                                }
317:                            }
318:                        }
319:                        if (className != null) {
320:                            try {
321:                                if (className.indexOf(".") == -1) {
322:                                    className = "org.jzonic.jlo.filter."
323:                                            + className;
324:                                }
325:                                Class clazz = Class.forName(className);
326:                                LogFilter filter = (LogFilter) clazz
327:                                        .newInstance();
328:                                filter.setParameters(params);
329:                                lc.addLogFilter(filterName, filter);
330:                            } catch (Exception e) {
331:                                ErrorHandler
332:                                        .reportError("LogManager getFilters: Cannot instantiate filter:"
333:                                                + filterName);
334:                            }
335:                        } else {
336:                            ErrorHandler
337:                                    .reportError("LogManager getFilters: No class given for filter:"
338:                                            + filterName);
339:                        }
340:                    } else {
341:                        ErrorHandler
342:                                .reportError("No name defined for filter. Ignoring entry");
343:                    }
344:                }
345:            }
346:
347:            private void getGenerators(Document doc, LogConfiguration lc) {
348:                String currentGenerator = "default";
349:                NodeList nl = doc.getElementsByTagName("generator");
350:                for (int i = 0; i < nl.getLength(); i++) {
351:                    Node n = nl.item(i);
352:                    NamedNodeMap curAtt = n.getAttributes();
353:                    Node curNode = curAtt.getNamedItem("name");
354:                    if (curNode != null) {
355:                        currentGenerator = curNode.getNodeValue();
356:                    }
357:                    Hashtable params = new Hashtable();
358:                    Map fParams = new HashMap();
359:                    Formatter formatter = null;
360:                    Handler handler = null;
361:                    String filter = null;
362:                    for (Node child = n.getFirstChild(); child != null; child = child
363:                            .getNextSibling()) {
364:                        if (child.getNodeName().equals("formatter")) {
365:                            formatter = getFormatter(child, lc.getName());
366:                        }
367:                        if (child.getNodeName().equals("handler")) {
368:                            handler = getHandler(child, lc.getName());
369:                        }
370:                        if (child.getNodeName().equals("filter-name")) {
371:                            filter = child.getFirstChild().getNodeValue();
372:                        }
373:                    }
374:                    LogGenerator generator = createLogGenerator(
375:                            currentGenerator, handler, formatter, params, lc
376:                                    .getName());
377:                    if (filter != null) {
378:                        generator.setFilter(lc.getLogFilter(filter));
379:                    }
380:                    lc.addLogGenerator(generator);
381:                }
382:            }
383:
384:            private Handler getHandler(Node n, String configName) {
385:                NamedNodeMap myAtt = n.getAttributes();
386:                Node myNode = myAtt.getNamedItem("class");
387:                if (myNode != null) {
388:                    String handlerName = myNode.getNodeValue();
389:                    if (handlerName != null) {
390:                        // next we set the formatter
391:                        Handler handler = HandlerFactory.getHandler(
392:                                handlerName, configName);
393:                        Map params = getParams(n);
394:                        handler.setParameter(params);
395:                        return handler;
396:                    }
397:                } else {
398:                    ErrorHandler
399:                            .reportError("No class defined for the handler. Ignoring entry");
400:                }
401:                return null;
402:            }
403:
404:            private Formatter getFormatter(Node n, String configName) {
405:                NamedNodeMap myAtt = n.getAttributes();
406:                Node myNode = myAtt.getNamedItem("class");
407:                if (myNode != null) {
408:                    String formatterName = myNode.getNodeValue();
409:                    Formatter formatter = null;
410:                    if (formatterName != null) {
411:                        // next we set the formatter
412:                        formatter = FormatterFactory.getFormatter(
413:                                formatterName, configName);
414:                        Map params = getParams(n);
415:                        formatter.setParameter(params);
416:                        return formatter;
417:                    }
418:                } else {
419:                    ErrorHandler
420:                            .reportError("No class defined for the formatter. Ignoring entry");
421:                }
422:                return null;
423:            }
424:
425:            private Map getParams(Node n) {
426:                Map params = new HashMap();
427:                for (Node child = n.getFirstChild(); child != null; child = child
428:                        .getNextSibling()) {
429:                    if (child.getNodeName().equals("parameter")) {
430:                        String pnName = null;
431:                        String pvName = null;
432:                        NamedNodeMap childAtt = child.getAttributes();
433:                        Node pnNode = childAtt.getNamedItem("name");
434:                        if (pnNode != null) {
435:                            pnName = pnNode.getNodeValue();
436:                        }
437:                        Node pvNode = childAtt.getNamedItem("value");
438:                        if (pvNode != null) {
439:                            pvName = pvNode.getNodeValue();
440:                        }
441:                        if (pnName != null && pvName != null) {
442:                            params.put(pnName, pvName);
443:                        }
444:                    }
445:                }
446:                return params;
447:            }
448:
449:            public LogGenerator createLogGenerator(String name,
450:                    String handlerName, String formatterName, Hashtable params,
451:                    String configName) {
452:                // we create the handler
453:                Handler handler = HandlerFactory.getHandler(handlerName,
454:                        configName);
455:                if (handler != null)
456:                    handler.setParameter(params);
457:                Formatter formatter = null;
458:                if (formatterName != null) {
459:                    // next we set the formatter
460:                    formatter = FormatterFactory.getFormatter(formatterName,
461:                            configName);
462:                }
463:                if (handler != null) {
464:                    return new LogGenerator(name, handler, formatter);
465:                } else {
466:                    return new LogGenerator(name,
467:                            new ConsoleHandler(configName),
468:                            new DefaultFormatter(configName));
469:                }
470:            }
471:
472:            public LogGenerator createLogGenerator(String name,
473:                    Handler handler, Formatter formatter, Hashtable params,
474:                    String configName) {
475:                if (handler != null) {
476:                    return new LogGenerator(name, handler, formatter);
477:                } else {
478:                    return new LogGenerator(name,
479:                            new ConsoleHandler(configName),
480:                            new DefaultFormatter(configName));
481:                }
482:            }
483:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.