Java tutorial
/* * Copyright (c) 2007 NTT DATA Corporation * * 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 jp.terasoluna.fw.web.struts.plugins; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import javax.servlet.ServletException; import jp.terasoluna.fw.util.ClassLoadException; import jp.terasoluna.fw.util.ClassUtil; import jp.terasoluna.fw.web.struts.reset.Resetter; import jp.terasoluna.fw.web.struts.reset.ResetterImpl; import jp.terasoluna.fw.web.struts.reset.ResetterResources; import org.apache.commons.digester.Digester; import org.apache.commons.digester.xmlrules.DigesterLoader; import org.apache.commons.digester.xmlrules.XmlLoadException; 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.xml.sax.SAXException; /** * tH?[Zbg???[hvOC?B * * PlugIn@\gp?AT?[ubg?tH?[Zbg@\ * ???AT?[ubgReLXg?B * * <p> * tH?[Zbg`t@C(reset.xml)?? * {@link jp.terasoluna.fw.web.struts.reset.ResetterResources} * CX^XT?[ubgReLXg?B<br> * ?s?A * {@link jp.terasoluna.fw.web.struts.form.FormEx}#reset() * ?\bhC * {@link jp.terasoluna.fw.web.struts.reset.Resetter} * ?o?A * {@link jp.terasoluna.fw.web.struts.reset.ResetterResources} * i[???]Zbg???s?B<br> * Zbg???NX ?A * {@link jp.terasoluna.fw.web.struts.reset.Resetter} * CNX?B<br> * <br><br> * </p> * * <strong>gp@</strong><br> * @\gp Struts?t@C(struts-config.xml) * ??B<br> * resetter Zbg???sNX * ?i? resetter ??B?ftHggp?B * ftHg="jp.terasoluna.fw.web.struts.reset.ResetterImpl") * resources tH?[Zbg`t@C(reset.xml)?A * digesterRules?AtH?[Zbg?[`t@C(reset-rules.xml) * w?B * ?idigesterRules??B?j<br> * <code><pre> * <plug-in className="jp.terasoluna.fw.web.struts.plugins.ResetterPlugIn"> * <set-property * property="resetter" * value="jp.terasoluna.fw.web.struts.reset.ResetterImpl"/> * <set-property * property="resources" * value="/WEB-INF/reset.xml"/> * <set-property * property="digesterRules" * value="/WEB-INF/reset-rules.xml"/> * </plug-in> * </pre></code><br> * tH?[Zbg`t@C(reset.xml)ANV * Zbg?tB?[h??B<br> * wZbg@\gp???Aselect ? * true ??B(?? * {@link jp.terasoluna.fw.web.struts.reset.Resetter} * Q??B) * <code><pre> * <reset> * <action path="/resetAction"> * <property-reset name="field1" /> * <property-reset name="field2" select="true" /> * </action> * ?E?E?E * </reset> * </pre></code> * <br> * <p> * Jq?ADTDURL?X???A<br> * getPublicIdentifier()getDtdUrl()I?[o?[Ch?B * </p> * * @see jp.terasoluna.fw.web.struts.reset.ActionReset * @see jp.terasoluna.fw.web.struts.reset.FieldReset * @see jp.terasoluna.fw.web.struts.reset.Resetter * @see jp.terasoluna.fw.web.struts.reset.ResetterResources * @see jp.terasoluna.fw.web.struts.form.FormEx * @see * jp.terasoluna.fw.web.struts.form.DynaValidatorActionFormEx * @see jp.terasoluna.fw.web.struts.form.ValidatorActionFormEx * */ public class ResetterPlugIn implements PlugIn { /** * ?ONX?B */ private static Log log = LogFactory.getLog(ResetterPlugIn.class); /** * ftHg tH?[Zbg`t@C(reset.xml) pX?B */ private static final String DIGESTER_RULES_PATH = "/WEB-INF/reset-rules.xml"; /** * tH?[Zbg?[`t@C(reset-rules.xml) ??? * DigesterCX^XQ? */ private static Digester digester = null; /** * ftHgZb^NXS?C?NX */ private static final String DEFAULT_RESETTER = ResetterImpl.class.getName(); /** * DTDJq?B */ private String publicIdentifier = "-//NTTDATA//DTD TERASOLUNA for Spring reset 1.0//JA"; /** * DTDpX?B */ private String dtdUrl = "/jp/terasoluna/fw/web/struts/plugins/dtd/reset.dtd"; /** * Zb^NXS?C?NX?B */ private String resetter = null; /** * tH?[Zbg`t@C(reset.xml) pX?B */ private String resourcesPath = null; /** * tH?[Zbg?[`t@C(reset-rules.xml) pX?B */ private String digesterRules = null; /** * ?I???B */ public void destroy() { // } /** * ????B * * @param servlet PlugInN ActionServlet?B * @param config PlugIn ModuleConfig?B * @exception ServletException ??ObvO?B */ public void init(ActionServlet servlet, ModuleConfig config) throws ServletException { if (log.isDebugEnabled()) { log.debug("init() called."); } // Resetter??? initResetter(servlet, config); // ResetterResources??? initResources(servlet, config); } /** * Zbg??NX????B * * Struts?t@C(struts-config.xml) * ?Zbg??NX?B * ???ftHgZbg??NX?B * @param servlet vOCNT?[ubg?B * @param config W?[RtBO * @exception ServletException Zbg??NX????O */ protected void initResetter(ActionServlet servlet, ModuleConfig config) throws ServletException { // ResetterCX^Xi[? Resetter resetterObj = null; try { if (this.resetter == null || "".equals(this.resetter)) { resetterObj = (Resetter) ClassUtil.create(DEFAULT_RESETTER); } else { resetterObj = (Resetter) ClassUtil.create(this.resetter); } } catch (ClassLoadException e) { log.error("", e); throw new ServletException(e); } // ResetterT?[ubgReLXg servlet.getServletContext().setAttribute(Resetter.RESETTER_KEY + config.getPrefix(), resetterObj); } /** * Zbg????B * * tH?[Zbg`t@C(reset.xml)?A * tH?[Zbg?[`t@C(reset-rules.xml)p?A * ?? * {@link * jp.terasoluna.fw.web.struts.reset.ResetterResources} * ??B * * @param servlet vOCNT?[ubg?B * @param config W?[RtBO * @exception ServletException \?[Xt@C * ?O */ protected void initResources(ActionServlet servlet, ModuleConfig config) throws ServletException { if (this.resourcesPath == null || "".equals(this.resourcesPath)) { log.error("resources file location is not specified"); throw new ServletException("resources file location is not specified"); } StringTokenizer st = new StringTokenizer(resourcesPath, ","); List<String> pathList = new ArrayList<String>(); while (st.hasMoreTokens()) { String path = st.nextToken(); path = path.trim(); if (log.isDebugEnabled()) { log.debug("reset file=" + path); } pathList.add(path); } if (digester == null) { if (digesterRules == null) { // ?[t@C??AftHg?[t@C // ??B digesterRules = DIGESTER_RULES_PATH; } // Digester?? try { digester = DigesterLoader.createDigester(servlet.getServletContext().getResource(digesterRules)); digester.setValidating(true); } catch (MalformedURLException e) { log.error("", e); throw new ServletException(e); } catch (XmlLoadException e) { log.error("", e); throw new ServletException(e); } } ResetterResources resetterResources = new ResetterResources(); try { for (int i = 0; i < pathList.size(); i++) { digester.push(resetterResources); URL url = this.getClass().getResource(getDtdUrl()); if (url != null) { digester.register(getPublicIdentifier(), url.toString()); } digester.parse(servlet.getServletContext().getResourceAsStream(pathList.get(i))); } } catch (IOException e) { log.error("", e); throw new ServletException(e); } catch (SAXException e) { log.error("", e); throw new ServletException(e); } // ResetterResourcesT?[ubgReLXg servlet.getServletContext().setAttribute(ResetterResources.RESETTER_RESOURCES_KEY + config.getPrefix(), resetterResources); } /** * Jqp?B * Jq?X???A?\bhI?[o?[Ch?B * @return Jq */ public String getPublicIdentifier() { return publicIdentifier; } /** * DTDURLp?B * DTDURL?X???A?\bhI?[o?[Ch?B * @return DTDURL */ public String getDtdUrl() { return dtdUrl; } /** * Struts?t@C(struts-config.xml) * ?Zb^S?C? * ??B * * @param string * Zb^S?C? */ public void setResetter(String string) { resetter = string; } /** * tH?[Zbg`t@C(reset.xml)pX??B * * @param string * tH?[Zbg`t@C(reset.xml)pX */ public void setResources(String string) { resourcesPath = string; } /** * tH?[Zbg?[`t@C(reset-rules.xml)??B * * @param digesterRules tH?[Zbg?[`t@C(reset-rules.xml) * pX */ public void setDigesterRules(String digesterRules) { this.digesterRules = digesterRules; } /** * Jq??B * @param publicIdentifier Jq */ public void setPublicIdentifier(String publicIdentifier) { this.publicIdentifier = publicIdentifier; } /** * DTDURL??B * @param dtdUrl DTDURL */ public void setDtdUrl(String dtdUrl) { this.dtdUrl = dtdUrl; } }