001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.configuration; 029 030import org.apache.commons.digester3.Digester; 031import org.apache.commons.digester3.Rule; 032 033import org.dom4j.Element; 034import org.xml.sax.Attributes; 035 036/** 037* Helper class for parsing an element with several sub-elements which are text-valued (i.e. have no nested sub-elements). 038*/ 039public class CmsElementWithSubElementsParamConfigHelper { 040 041 /** The xpath of the element. */ 042 private String m_basePath; 043 044 /** The class of the configuration object to create (must be subclass of I_CmsConfigurationParameterHandler). */ 045 private Class<?> m_class; 046 047 /** The name of the XML element. */ 048 private String m_name; 049 050 /** The attributes to read / write. */ 051 private String[] m_subElements; 052 053 /** 054 * Creates a new instance.<p> 055 * 056 * @param parentPath the parent XPath 057 * @param name the XML element name 058 * @param cls the class to use for the configuration (must be subclass of I_CmsConfigurationParameterHandler) 059 * @param subElements the attributes to read / write 060 */ 061 public CmsElementWithSubElementsParamConfigHelper( 062 String parentPath, 063 String name, 064 Class<?> cls, 065 String... subElements) { 066 067 m_basePath = parentPath + "/" + name; 068 m_name = name; 069 m_class = cls; 070 m_subElements = subElements; 071 } 072 073 /** 074 * Adds the configuration parsing rules to the digester.<p> 075 * 076 * @param digester the digester to which the rules should be added 077 */ 078 public void addRules(Digester digester) { 079 080 digester.addRule(m_basePath, new Rule() { 081 082 @SuppressWarnings("synthetic-access") 083 @Override 084 public void begin(String namespace, String name, Attributes attributes) throws Exception { 085 086 I_CmsConfigurationParameterHandler config = (I_CmsConfigurationParameterHandler)(m_class.newInstance()); 087 config.initConfiguration(); 088 getDigester().push(config); 089 } 090 091 @Override 092 public void end(String namespace, String name) throws Exception { 093 094 getDigester().pop(); 095 } 096 097 }); 098 099 for (String elem : m_subElements) { 100 digester.addRule(m_basePath + "/" + elem, new Rule() { 101 102 @Override 103 public void body(String namespace, String name, String text) throws Exception { 104 105 I_CmsConfigurationParameterHandler handler = (I_CmsConfigurationParameterHandler)(getDigester().peek()); 106 handler.addConfigurationParameter(name, text); 107 } 108 }); 109 } 110 } 111 112 /** 113 * Generates the XML configuration from the given configuration object.<p> 114 * 115 * @param parent the parent element 116 * @param config the configuration 117 */ 118 public void generateXml(Element parent, I_CmsConfigurationParameterHandler config) { 119 120 if (config != null) { 121 Element elem = parent.addElement(m_name); 122 for (String subElemName : m_subElements) { 123 for (String value : config.getConfiguration().getList(subElemName)) { 124 elem.addElement(subElemName).addText(value); 125 } 126 } 127 } 128 } 129 130 /** 131 * Gets the xPath of the configuration element.<p> 132 * 133 * @return the xPath of the configuration element 134 */ 135 public String getBasePath() { 136 137 return m_basePath; 138 } 139}