📄 configparsehandler.java
字号:
/* * Copyright (C) butor.com. All rights reserved. * * This software is published under the terms of the GNU Library General * Public License (GNU LGPL), a copy of which has been included with this * distribution in the LICENSE.txt file. */package org.butor.config.lowlevel;import java.util.Stack;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.Locator;import org.xml.sax.SAXException;/** * @author sawanai * * This is a first working handler for Butor Config. */public class ConfigParseHandler implements ContentHandler { protected Stack f_configList = null; protected IConfigLoader config = null; /** * */ public ConfigParseHandler(IConfigLoader config) { super(); this.config = config; f_configList = new Stack(); f_configList.push(config); } /* (non-Javadoc) * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator) */ public void setDocumentLocator(Locator locator) { } /* (non-Javadoc) * @see org.xml.sax.ContentHandler#startDocument() */ public void startDocument() throws SAXException { } /* (non-Javadoc) * @see org.xml.sax.ContentHandler#endDocument() */ public void endDocument() throws SAXException { } /* (non-Javadoc) * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String) */ public void startPrefixMapping(String prefix, String uri) throws SAXException { } /* (non-Javadoc) * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String) */ public void endPrefixMapping(String prefix) throws SAXException { } /* (non-Javadoc) * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) */ public void startElement( String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (localName.equalsIgnoreCase("list")) { PropertyList list = new PropertyList(); list.setName(checkValue(atts.getValue("name"))); /* * No need to check depth! The only way the * stack could be empty and we are beginning * a "list" element is if the XML document is * not well-formed, which is impossible because * the XML parser would have already barfed... */ PropertyContainer parent = (PropertyContainer)f_configList.peek(); list.setConfDir(parent.getConfDir()); list.setAppDir(parent.getAppDir()); list.setRepositoryConfDir(parent.getRepositoryConfDir()); list.setLogDir(parent.getLogDir()); parent.addProperty(list.getName(), list); // This list is becoming the "parent" of // any next list until we close it... f_configList.push(list); } else if (localName.equalsIgnoreCase("item")) { // No problem possible with stack depth, see above PropertyContainer parent = (PropertyContainer)f_configList.peek(); parent.addProperty(checkValue(atts.getValue("name")), checkValue(atts.getValue("value"))); } else if (localName.equalsIgnoreCase("property")) { // No problem possible with stack depth, see above PropertyContainer parent = (PropertyContainer)f_configList.peek(); parent.addProperty(checkValue(atts.getValue("name")), checkValue(atts.getValue("value"))); } } protected String checkValue(String value) { if (value == null) { return value; } int spos = value.indexOf("${"); while (spos > -1) { int epos = value.indexOf("}", spos); if (epos == -1) { break; } // check first for the macros app_dir et conf_dir. They are defined // in this config object String var = value.substring(spos, epos+1); PropertyContainer parent = (PropertyContainer)f_configList.peek(); if (var.equalsIgnoreCase(PropertyContainer.MACRO_APP_DIR)) { var = parent.getAppDir(); } else if (var.equalsIgnoreCase(PropertyContainer.MACRO_CONF_DIR)) { var = parent.getConfDir(); } else if (var.equalsIgnoreCase(PropertyContainer.MACRO_LOG_DIR)) { var = parent.getLogDir(); } else if (var.equalsIgnoreCase(PropertyContainer.MACRO_REPOSITORY_CONF_DIR)) { var = parent.getRepositoryConfDir(); } else { // check then as system variables var = value.substring(spos+2, epos); var = System.getProperty(var); } if (var != null) { value = value.substring(0, spos) +var +value.substring(epos+1); } spos = value.indexOf("${", epos); } return value; } /* (non-Javadoc) * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String) */ public void endElement(String namespaceURI, String localName, String qName) throws SAXException { if (localName.equalsIgnoreCase("list")) { /* * No need to check depth! The only way the * stack could be empty and we are ending * a "list" element is if the XML document is * not well-formed, which is impossible because * the XML parser would have already barfed... */ f_configList.pop(); } } /* (non-Javadoc) * @see org.xml.sax.ContentHandler#characters(char[], int, int) */ public void characters(char[] ch, int start, int length) throws SAXException { } /* (non-Javadoc) * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int) */ public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { } /* (non-Javadoc) * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String) */ public void processingInstruction(String target, String data) throws SAXException { } /* (non-Javadoc) * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String) */ public void skippedEntity(String name) throws SAXException { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -