📄 digestingplugin.java
字号:
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/plugins/DigestingPlugIn.java,v 1.12 2004/03/14 06:23:53 sraeburn Exp $
* $Revision: 1.12 $
* $Date: 2004/03/14 06:23:53 $
*
* Copyright 2003,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.struts.plugins;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.servlet.ServletException;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.RuleSet;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.util.RequestUtils;
import org.xml.sax.SAXException;
/**
* <p>An implementation of <code>PlugIn</code> which
* can be configured to instantiate a graph of objects
* using the Commons Digester and place the root object
* of that graph into the Application context.</p>
*
* @version $Revision: 1.12 $
* @see org.apache.struts.action.PlugIn
* @since Struts 1.2
*/
public class DigestingPlugIn implements PlugIn {
/**
* Commons Logging instance.
*/
private static Log log = LogFactory.getLog(DigestingPlugIn.class);
protected static final String SOURCE_CLASSPATH = "classpath";
protected static final String SOURCE_FILE = "file";
protected static final String SOURCE_SERVLET = "servlet";
protected String configPath = null;
protected String configSource = SOURCE_SERVLET;
protected String digesterPath = null;
protected String digesterSource = SOURCE_SERVLET;
protected String key = null;
protected ModuleConfig moduleConfig = null;
protected String rulesets = null;
protected ActionServlet servlet = null;
protected boolean push = false;
/**
* Constructor for DigestingPlugIn.
*/
public DigestingPlugIn() {
super();
}
/**
* Receive notification that our owning module is being shut down.
*/
public void destroy() {
this.servlet = null;
this.moduleConfig = null;
}
/**
* <p>Initialize a <code>Digester</code> and use it to parse a
* configuration file, resulting in a root object which will be placed into
* the ServletContext.</p>
*
* @param servlet ActionServlet that is managing all the
* modules in this web application
* @param config ModuleConfig for the module with which
* this plug-in is associated
*
* @throws ServletException if this <code>PlugIn</code> cannot
* be successfully initialized
*/
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
this.servlet = servlet;
this.moduleConfig = config;
Object obj = null;
Digester digester = this.initializeDigester();
if (this.push) {
log.debug("push == true; pushing plugin onto digester stack");
digester.push(this);
}
try {
log.debug("XML data file: [path: "
+ this.configPath
+ ", source: "
+ this.configSource + "]");
URL configURL = this.getConfigURL(this.configPath, this.configSource);
obj = digester.parse(configURL.openStream());
} catch (IOException e) {
// TODO Internationalize msg
log.error("Exception processing config", e);
throw new ServletException(e);
} catch (SAXException e) {
// TODO Internationalize msg
log.error("Exception processing config", e);
throw new ServletException(e);
}
this.storeGeneratedObject(obj);
}
/**
* Initialize the <code>Digester</code> which will be used to process the
* main configuration.
* @return a Digester, ready to use.
* @throws ServletException
*/
protected Digester initializeDigester() throws ServletException {
Digester digester = null;
if (this.digesterPath != null && this.digesterSource != null) {
try {
log.debug("Initialize digester from XML [path: "
+ this.digesterPath
+ "; source: "
+ this.digesterSource + "]");
digester =
this.digesterFromXml(this.digesterPath, this.digesterSource);
} catch (IOException e) {
// TODO Internationalize msg
log.error("Exception instantiating digester from XML ", e);
throw new ServletException(e);
}
} else {
log.debug("No XML rules for digester; call newDigesterInstance()");
digester = this.newDigesterInstance();
}
this.applyRuleSets(digester);
return digester;
}
/**
* <p>Instantiate a <code>Digester</code>.</p>
* <p>Subclasses may wish to override this to provide a subclass of Digester,
* or to configure the Digester using object methods.</p>
* @return a basic instance of <code>org.apache.commons.digester.Digester</code>
*/
protected Digester newDigesterInstance() {
return new Digester();
}
/**
* <p>Instantiate a Digester from an XML input stream using the Commons
* <code>DigesterLoader</code>.</p>
* @param path the path to the digester rules XML to be found using <code>source</code>
* @param source a string indicating the lookup method to be used with <code>path</code>
* @return a configured Digester
* @throws FileNotFoundException
* @throws MalformedURLException
* @see #getConfigURL(String, String)
*/
protected Digester digesterFromXml(String path, String source)
throws IOException {
URL configURL = this.getConfigURL(path, source);
if (configURL == null)
{
throw new NullPointerException("No resource '" + path + "' found in '" + source + "'");
}
return DigesterLoader.createDigester(configURL);
}
/**
* Instantiate any <code>RuleSet</code> classes defined in the
* <code>rulesets</code> property and use them to add rules to our
* <code>Digester</code>.
* @param digester the Digester instance to add RuleSet objects to.
* @throws ServletException
*/
protected void applyRuleSets(Digester digester) throws ServletException {
if (this.rulesets == null || this.rulesets.trim().length() == 0) {
return;
}
rulesets = rulesets.trim();
String ruleSet = null;
while (rulesets.length() > 0) {
int comma = rulesets.indexOf(",");
if (comma < 0) {
ruleSet = rulesets.trim();
rulesets = "";
} else {
ruleSet = rulesets.substring(0, comma).trim();
rulesets = rulesets.substring(comma + 1).trim();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -