⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmllogpropertyreader.java

📁 STRUTS数据库项目开发宝典
💻 JAVA
字号:


package com.helpsoft.util.log;

import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

// jaxp 1.0.1 imports
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import java.net.URL;
import java.util.HashMap;

import com.helpsoft.util.PropertyReader;

/**
 * This class is an helper class for retrieve log settings from an xml file
 * @author caoguangxin- www.relationinfo.com

 */
public class XMLLogPropertyReader {

   /** name for the appender tag in the xml file */
   public static final String APPENDER = "appender";

   /** name for the name attribute of the appender in the xml file */
   public static final String NAME = "name";

   /** name for the logfile attribute of the appender in the xml file */
   public static final String LOG_FILE = "logfile";

   /** name for the append attribute of the appender in the xml file */
   public static final String APPEND = "append";

   /** name for the maxbackupindex attribute of the appender in the xml file */
   public static final String MAX_BACKUP_INDEX = "maxbackupindex";

   /** name for the maxfilesize attribute of the appender in the xml file */
   public static final String MAX_FILE_SIZE = "maxfilesize";

   /** name for the shownumberoflastpackages attribute of the appender in the xml file */
   public static final String SHOW_NUMBER_OF_LAST_PACKAGES = "shownumberoflastpackages";

   /** name for the datepattern attribute of the appender in the xml file */
   public static final String DATE_PATTERN = "datepattern";

   /** name for the messageseparator attribute of the appender in the xml file */
   public static final String MESSAGE_SEPARATOR = "messageseparator";

   /** name for the loglevel attribute of the appender in the xml file */
   public static final String LOG_LEVEL = "loglevel";

   /**
    * Retrieving all appender setting from a configuration file.
    * @param location the directory + filename where to find the log configuration xml file
    * @return HashMap all log configurations
    */
   public static HashMap getLogConfigs(String location) {
      HashMap logConfigs = new HashMap();
      try {
         //URL url = PropertyReader.getPropertiesURL(location);
         URL url = PropertyReader.getPropertiesURL(location);
         InputSource xmlInp = new InputSource(url.openStream());
         Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlInp);
         NodeList appenders = doc.getElementsByTagName(APPENDER);
         for (int i = 0; i < appenders.getLength(); i++) {
            Node curNode = appenders.item(i);
            if (curNode.getNodeName().equals(APPENDER)) {
               NamedNodeMap nnm = curNode.getAttributes();
               String appenderName = nnm.getNamedItem(NAME).getNodeValue();
               String logFile = nnm.getNamedItem(LOG_FILE).getNodeValue();
               String append = nnm.getNamedItem(APPEND).getNodeValue();

               String maxBackupIndexString = nnm.getNamedItem(MAX_BACKUP_INDEX).getNodeValue();
               int maxBackupIndex = -1;
               try {
                  maxBackupIndex = Integer.parseInt(maxBackupIndexString);
               }
               catch (Exception e) {
                  System.out.println("Incorrect number for maxBackupIndex with appender: " + appenderName + ": " + e);
               }

               String maxFileSizeString = nnm.getNamedItem(MAX_FILE_SIZE).getNodeValue();
               int maxFileSize = -1;
               try {
                  maxFileSize = Integer.parseInt(maxFileSizeString);
               }
               catch (Exception e) {
                  System.out.println("Incorrect number for maxfilesize with appender: " + appenderName + ": " + e);
               }

               String showNumberOfLastPackagesString = nnm.getNamedItem(SHOW_NUMBER_OF_LAST_PACKAGES).getNodeValue();
               int showNumberOfLastPackages = -1;
               try {
                  showNumberOfLastPackages = Integer.parseInt(showNumberOfLastPackagesString);
               }
               catch (Exception e) {
                  System.out.println("Incorrect number for shownumberoflastpackages with appender: " +
                        appenderName + ": " + e);
               }

               String datePattern = nnm.getNamedItem(DATE_PATTERN).getNodeValue();
               String messageSeparator = nnm.getNamedItem(MESSAGE_SEPARATOR).getNodeValue();
               String logLevel = nnm.getNamedItem(LOG_LEVEL).getNodeValue();
               LogConfig lg = new LogConfig(logFile, append.equals("true"), maxBackupIndex, maxFileSize,
                     showNumberOfLastPackages, datePattern, messageSeparator, logLevel);
               System.out.println("Logging for filter [" + appenderName + "] with setting\n");
               System.out.println(lg);
               logConfigs.put(appenderName, lg);
            }
         }
      }
      catch (Exception e) {
         System.out.println("Exception: " + e);
      }
      return logConfigs;
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -