KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > util > log > XMLLogPropertyReader


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG 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
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.util.log;
19
20 import org.xml.sax.InputSource JavaDoc;
21 import org.w3c.dom.Document JavaDoc;
22 import org.w3c.dom.Element JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24 import org.w3c.dom.NamedNodeMap JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26
27 // jaxp 1.0.1 imports
28
import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30
31 import java.net.URL JavaDoc;
32 import java.util.HashMap JavaDoc;
33
34 import com.finalist.util.PropertyReader;
35
36 /**
37  * This class is an helper class for retrieve log settings from an xml file
38  * @author Ronald Kramp - Finalist IT Group
39  * @version $Revision: 1.1 $, $Date: 2004/11/12 14:06:44 $
40  */

41 public class XMLLogPropertyReader {
42
43    /** name for the appender tag in the xml file */
44    public static final String JavaDoc APPENDER = "appender";
45
46    /** name for the name attribute of the appender in the xml file */
47    public static final String JavaDoc NAME = "name";
48
49    /** name for the logfile attribute of the appender in the xml file */
50    public static final String JavaDoc LOG_FILE = "logfile";
51
52    /** name for the append attribute of the appender in the xml file */
53    public static final String JavaDoc APPEND = "append";
54
55    /** name for the maxbackupindex attribute of the appender in the xml file */
56    public static final String JavaDoc MAX_BACKUP_INDEX = "maxbackupindex";
57
58    /** name for the maxfilesize attribute of the appender in the xml file */
59    public static final String JavaDoc MAX_FILE_SIZE = "maxfilesize";
60
61    /** name for the shownumberoflastpackages attribute of the appender in the xml file */
62    public static final String JavaDoc SHOW_NUMBER_OF_LAST_PACKAGES = "shownumberoflastpackages";
63
64    /** name for the datepattern attribute of the appender in the xml file */
65    public static final String JavaDoc DATE_PATTERN = "datepattern";
66
67    /** name for the messageseparator attribute of the appender in the xml file */
68    public static final String JavaDoc MESSAGE_SEPARATOR = "messageseparator";
69
70    /** name for the loglevel attribute of the appender in the xml file */
71    public static final String JavaDoc LOG_LEVEL = "loglevel";
72
73    /**
74     * Retrieving all appender setting from a configuration file.
75     * @param location the directory + filename where to find the log configuration xml file
76     * @return HashMap all log configurations
77     */

78    public static HashMap JavaDoc getLogConfigs(String JavaDoc location) {
79       HashMap JavaDoc logConfigs = new HashMap JavaDoc();
80       try {
81          //URL url = PropertyReader.getPropertiesURL(location);
82
URL JavaDoc url = PropertyReader.getPropertiesURL(location);
83          InputSource JavaDoc xmlInp = new InputSource JavaDoc(url.openStream());
84          Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlInp);
85          NodeList JavaDoc appenders = doc.getElementsByTagName(APPENDER);
86          for (int i = 0; i < appenders.getLength(); i++) {
87             Node JavaDoc curNode = appenders.item(i);
88             if (curNode.getNodeName().equals(APPENDER)) {
89                NamedNodeMap JavaDoc nnm = curNode.getAttributes();
90                String JavaDoc appenderName = nnm.getNamedItem(NAME).getNodeValue();
91                String JavaDoc logFile = nnm.getNamedItem(LOG_FILE).getNodeValue();
92                String JavaDoc append = nnm.getNamedItem(APPEND).getNodeValue();
93
94                String JavaDoc maxBackupIndexString = nnm.getNamedItem(MAX_BACKUP_INDEX).getNodeValue();
95                int maxBackupIndex = -1;
96                try {
97                   maxBackupIndex = Integer.parseInt(maxBackupIndexString);
98                }
99                catch (Exception JavaDoc e) {
100                   System.out.println("Incorrect number for maxBackupIndex with appender: " + appenderName + ": " + e);
101                }
102
103                String JavaDoc maxFileSizeString = nnm.getNamedItem(MAX_FILE_SIZE).getNodeValue();
104                int maxFileSize = -1;
105                try {
106                   maxFileSize = Integer.parseInt(maxFileSizeString);
107                }
108                catch (Exception JavaDoc e) {
109                   System.out.println("Incorrect number for maxfilesize with appender: " + appenderName + ": " + e);
110                }
111
112                String JavaDoc showNumberOfLastPackagesString = nnm.getNamedItem(SHOW_NUMBER_OF_LAST_PACKAGES).getNodeValue();
113                int showNumberOfLastPackages = -1;
114                try {
115                   showNumberOfLastPackages = Integer.parseInt(showNumberOfLastPackagesString);
116                }
117                catch (Exception JavaDoc e) {
118                   System.out.println("Incorrect number for shownumberoflastpackages with appender: " +
119                         appenderName + ": " + e);
120                }
121
122                String JavaDoc datePattern = nnm.getNamedItem(DATE_PATTERN).getNodeValue();
123                String JavaDoc messageSeparator = nnm.getNamedItem(MESSAGE_SEPARATOR).getNodeValue();
124                String JavaDoc logLevel = nnm.getNamedItem(LOG_LEVEL).getNodeValue();
125                LogConfig lg = new LogConfig(logFile, append.equals("true"), maxBackupIndex, maxFileSize,
126                      showNumberOfLastPackages, datePattern, messageSeparator, logLevel);
127                System.out.println("Logging for filter [" + appenderName + "] with setting\n");
128                System.out.println(lg);
129                logConfigs.put(appenderName, lg);
130             }
131          }
132       }
133       catch (Exception JavaDoc e) {
134          System.out.println("Exception: " + e);
135       }
136       return logConfigs;
137    }
138 }
139
Popular Tags