All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencms.util.ant.CmsAntTaskReadXMLProperty Maven / Gradle / Ivy

Go to download

OpenCms is an enterprise-ready, easy to use website content management system based on Java and XML technology. Offering a complete set of features, OpenCms helps content managers worldwide to create and maintain beautiful websites fast and efficiently.

The newest version!
/*
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.util.ant;

import java.io.File;

import org.apache.commons.digester.Digester;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;

/**
 * Ant task for reading a property from a XML attribute or element.

* * @since 6.0.0 */ public class CmsAntTaskReadXMLProperty extends Task { /** target attribute. */ private String m_attribute; // optional /** target element. */ private String m_element; // required /** Destination property. */ private String m_property; // required /** return value. */ private String m_value = ","; /** absoulte path to the xml file. */ private String m_xmlFile; // required /** * Default constructor.

*/ public CmsAntTaskReadXMLProperty() { super(); } /** * Run the task.

* * Sets the given property to __ABORT__ if canceled, or to a list of selected * modules if not.

* * @throws BuildException if something goes wrong * * @see org.apache.tools.ant.Task#execute() */ @Override public void execute() throws BuildException { boolean isAttr = ((m_attribute != null) && (m_attribute.trim().length() > 0)); // instantiate Digester and enable XML validation Digester digester = new Digester(); digester.setValidating(false); digester.setEntityResolver(null); digester.setRuleNamespaceURI(null); digester.setErrorHandler(new ErrorHandler() { /** * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException) */ public void error(SAXParseException exception) { log(exception.getMessage(), exception.getLineNumber()); } /** * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException) */ public void fatalError(SAXParseException exception) { log(exception.getMessage(), exception.getLineNumber()); } /** * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException) */ public void warning(SAXParseException exception) { log(exception.getMessage(), exception.getLineNumber()); } }); // add this class to the Digester digester.push(this); if (!isAttr) { digester.addCallMethod(m_element, "setValue", 0); } else { digester.addCallMethod(m_element, "setValue", 1); digester.addCallParam(m_element, 0, m_attribute); } // start the parsing process try { digester.parse(new File(getXmlFile())); } catch (Exception e) { throw new BuildException(e); } getProject().setProperty(m_property, m_value.substring(1)); } /** * Returns the optional XML attribute.

* * @return the optional XML attribute */ public String getAttribute() { return m_attribute; } /** * Returns the XML element path.

* * @return the XML element path */ public String getElement() { return m_element; } /** * Returns the property to store the user selection.

* * @return Returns the property */ public String getProperty() { return m_property; } /** * Returns the return value.

* * @return the return value */ public String getValue() { return m_value; } /** * Returns the xmlFile absolute path.

* * @return the xmlFile absolute path */ public String getXmlFile() { return m_xmlFile; } /** * Sets the optional XML attribute.

* * @param attribute the optional XML attribute to set */ public void setAttribute(String attribute) { m_attribute = attribute; } /** * Sets the XML element path.

* * @param element the XML element path to set */ public void setElement(String element) { m_element = element; } /** * Sets the property for storing the selected value.

* * @param property The property to set */ public void setProperty(String property) { m_property = property; } /** * Sets the return value.

* * @param value the return value to set */ public void setValue(String value) { if (m_value.length() > 1) { m_value += ","; } m_value += value; } /** * Sets the xmlFile absolute path.

* * @param xmlFile the xmlFile absolute path to set */ public void setXmlFile(String xmlFile) { m_xmlFile = xmlFile; } /** * Test case.

* * @param args not used */ public static void main(String[] args) { CmsAntTaskReadXMLProperty task = new CmsAntTaskReadXMLProperty(); task.setXmlFile("C:\\dev\\workspace\\OpenCms\\src-modules\\org\\opencms\\ade/ADEBase.gwt.xml"); task.setElement("module"); task.setAttribute("rename-to"); task.execute(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy