krysalis-viprom/src/java/ant/org/krysalis/viprom/ant ViPrOMPropertyHelper.java,NONE,1.1 ViPrOMTask.java,NONE,1.1

[email protected]
Newsgroups gmane.comp.krysalis.metamorphosis.cvs
Message-ID <[email protected]>
Update of /cvsroot/metamorphosis/krysalis-viprom/src/java/ant/org/krysalis/viprom/ant
In directory sc8-pr-cvs1:/tmp/cvs-serv9823/src/java/ant/org/krysalis/viprom/ant

Added Files:
	ViPrOMPropertyHelper.java ViPrOMTask.java 
Log Message:
Some first tinkerings...

--- NEW FILE: ViPrOMPropertyHelper.java ---
/*
The Krysalis Patchy Software License, Version 1.1_01

Copyright (c) 2002-2003 Nicola Ken Barozzi.  All rights reserved.

This Licence is compatible with the BSD licence as described and 
approved by http://www.opensource.org/, and is based on the
Apache Software Licence Version 1.1.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in
   the documentation and/or other materials provided with the
   distribution.

3. The end-user documentation included with the redistribution,
   if any, must include the following acknowledgment:
	  "This product includes software developed for project 
	Krysalis(http : //www.krysalis.org/)."
   Alternately, this acknowledgment may appear in the software itself,
   if and wherever such third-party acknowledgments normally appear.

4. The names "Krysalis" and "Nicola Ken Barozzi" and
   "Krysalis Centipede" must not be used to endorse or promote products
   derived from this software without prior written permission. For
   written permission, please contact [email protected].

5. Products derived from this software may not be called "Krysalis",
   "Krysalis Centipede", nor may "Krysalis" appear in their name, without
	prior written permission of Nicola Ken Barozzi.

6. This software may contain voluntary contributions made by many 
   individuals, who decided to donate the code to this project in respect 
   of this licence.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
====================================================================
*/
package org.krysalis.viprom.ant;

import java.util.Iterator;
import java.util.Map;

import org.apache.commons.jxpath.Variables;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.PropertyHelper;
import org.krysalis.version.log.Logger;
import org.krysalis.viprom.Engine;
import org.w3c.dom.Document;

class ViPrOMPropertyHelper extends PropertyHelper {
	public static String VIPROM_PREFIX = "viprom:";

	private boolean m_usePrefix = false;

	private Project m_project = null;

	private Engine m_engine = null;

	static void addPropertyHelper(
		Project project,
		Document docRoot,
		Map aliases,
		boolean usePrefix) {
		//
		// Create the property helper, around document root
		//
		PropertyHelper helper =
			new ViPrOMPropertyHelper(project, docRoot, aliases, usePrefix);
			
		//
		// Link PropertyHelper into chain
		//
		PropertyHelper phelper = PropertyHelper.getPropertyHelper(project);
		helper.setProject(project);
		helper.setNext(phelper.getNext());
		phelper.setNext(helper);
	}

	private ViPrOMPropertyHelper(
		Project project,
		Document docRoot,
		Map aliases,
		boolean usePrefix) {
		m_project = project;

		m_usePrefix = false;

		m_engine =
			new Engine(
				docRoot,
				aliases,
				new ViPrOMPropertyHelperAntVariables());
	}

	public boolean setPropertyHook(
		String ns,
		String name,
		Object v,
		boolean inh,
		boolean user,
		boolean isNew) {

		//:TODO: Namespace: leverage somehow?

		// If prefixed...
		if (!m_usePrefix || name.startsWith(VIPROM_PREFIX)) {
			name = name.substring(VIPROM_PREFIX.length());
			m_engine.setValue(name, v);
			return true;
		}

		return super.setPropertyHook(ns, name, v, inh, user, isNew);
	}

	public Object getPropertyHook(String ns, String name, boolean user) {

		//:TODO: Namespace: leverage somehow?

		//
		//	viprom: properties...
		//
		if (m_usePrefix && name.startsWith(VIPROM_PREFIX))
			name = name.substring(VIPROM_PREFIX.length());

		Logger.getLogger().verbose(
			"PropertyHelper - VIPROM: Trying to resolve and deliver property '"
				+ name
				+ "'");

		String result = null;
		Iterator iter = m_engine.iterate(name);
		if (iter != null) {
			result = iter.next().toString();
			while (iter.hasNext()) {
				Object o = iter.next();
				result += ", " + o;
			}
		}

		if (null == result) {
			Logger.getLogger().verbose(
				"PropertyHelper: Passing the ball to the next hook");
			return super.getPropertyHook(ns, name, user);
		}

		return result;
	}

	public class ViPrOMPropertyHelperAntVariables implements Variables {
		protected ViPrOMPropertyHelperAntVariables() {
		}
		public void declareVariable(String varName, Object value) {
			Logger.getLogger().verbose(
				"AntVariables - VIPROM: declare '"
					+ varName
					+ "' -> '"
					+ value.toString()
					+ "'");
			m_project.setNewProperty(varName, value.toString());
		}
		public Object getVariable(String varName) {
			Object value = m_project.getProperty(varName);
			Logger.getLogger().verbose(
				"AntVariables - VIPROM: get '"
					+ varName
					+ "' as '"
					+ ((null != value) ? value.toString() : null)
					+ "'");
			return value;
		}
		public boolean isDeclaredVariable(String varName) {
			boolean declared =
				(m_project.getProperty(varName) == null ? false : true);
			Logger.getLogger().debug(
				"AntVariables - VIPROM: get '" + varName + "' is " + declared);
			return declared;
		}

		public void undeclareVariable(String varName) {
			throw new UnsupportedOperationException("Cannot undeclare variables in Ant.");
		}
	}
}

--- NEW FILE: ViPrOMTask.java ---
/*
 * Created on Sep 9, 2003
 */
package org.krysalis.viprom.ant;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.krysalis.version.ant.util.AntLogListener;
import org.krysalis.version.log.Logger;
import org.krysalis.version.util.dom.DOMUtils;
import org.krysalis.viprom.Aliases;
import org.w3c.dom.Document;

/**
 * @author ajack
 */
public class ViPrOMTask extends Task {

	private File m_metadata = null;
	private String m_type = null;
	private boolean m_usePrefix = false;

	/* (non-Javadoc)
	 * @see org.apache.tools.ant.Task#execute()
	 */
	public void execute() throws BuildException {

		log("Execute ViPrOMTask...");

		// Set logging context..
		Logger logger = Logger.pushContext(new AntLogListener(this));

		try {
			//
			// Read the XML into memory
			//
			Document document =
				DOMUtils.deserialize(new FileInputStream(m_metadata));

			//
			// Get type map
			//
			Map aliases = Aliases.get(m_type);

			// Register the property helper...
			ViPrOMPropertyHelper.addPropertyHelper(
				getProject(),
				document,
				aliases,
				m_usePrefix);
		}
		catch (Exception e) {
			if (e instanceof BuildException)
				throw (BuildException)e;
			else
				throw new BuildException(e);
		}
		finally {
			Logger.popContext(logger);
		}
	}
	/**
	 * @return
	 */
	public File getMetadata() {
		return m_metadata;
	}

	/**
	 * @return
	 */
	public String getType() {
		return m_type;
	}

	/**
	 * @return
	 */
	public boolean isUsePrefix() {
		return m_usePrefix;
	}

	/**
	 * @param file
	 */
	public void setMetadata(File file) {
		m_metadata = file;
	}

	/**
	 * @param string
	 */
	public void setType(String string) {
		m_type = string;
	}

	/**
	 * @param b
	 */
	public void setUsePrefix(boolean b) {
		m_usePrefix = b;
	}

}




-------------------------------------------------------
This SF.net email is sponsored by: The SF.net Donation Program.
Do you like what SourceForge.net is doing for the Open
Source Community?  Make a contribution, and help us add new
features and functionality. Click here: http://sourceforge.net/donate/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.