|
Ant example source code file (PropertyHelper.java)
The PropertyHelper.java source code
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.tools.ant;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
/* ISSUES:
- ns param. It could be used to provide "namespaces" for properties, which
may be more flexible.
- Object value. In ant1.5 String is used for Properties - but it would be nice
to support generic Objects (the property remains immutable - you can't change
the associated object). This will also allow JSP-EL style setting using the
Object if an attribute contains only the property (name="${property}" could
avoid Object->String->Object conversion)
- Currently we "chain" only for get and set property (probably most users
will only need that - if they need more they can replace the top helper).
Need to discuss this and find if we need more.
*/
/** NOT FINAL. API MAY CHANGE
*
* Deals with properties - substitution, dynamic properties, etc.
*
* This is the same code as in Ant1.5. The main addition is the ability
* to chain multiple PropertyHelpers and to replace the default.
*
* @since Ant 1.6
*/
public class PropertyHelper {
private Project project;
private PropertyHelper next;
/** Project properties map (usually String to String). */
private Hashtable properties = new Hashtable();
/**
* Map of "user" properties (as created in the Ant task, for example).
* Note that these key/value pairs are also always put into the
* project properties, so only the project properties need to be queried.
* Mapping is String to String.
*/
private Hashtable userProperties = new Hashtable();
/**
* Map of inherited "user" properties - that are those "user"
* properties that have been created by tasks and not been set
* from the command line or a GUI tool.
* Mapping is String to String.
*/
private Hashtable inheritedProperties = new Hashtable();
/**
* Default constructor.
*/
protected PropertyHelper() {
}
//override facility for subclasses to put custom hashtables in
// -------------------- Hook management --------------------
/**
* Set the project for which this helper is performing property resolution
*
* @param p the project instance.
*/
public void setProject(Project p) {
this.project = p;
}
/** There are 2 ways to hook into property handling:
* - you can replace the main PropertyHelper. The replacement is required
* to support the same semantics (of course :-)
*
* - you can chain a property helper capable of storing some properties.
* Again, you are required to respect the immutability semantics (at
* least for non-dynamic properties)
*
* @param next the next property helper in the chain.
*/
public void setNext(PropertyHelper next) {
this.next = next;
}
/**
* Get the next property helper in the chain.
*
* @return the next property helper.
*/
public PropertyHelper getNext() {
return next;
}
/**
* Factory method to create a property processor.
* Users can provide their own or replace it using "ant.PropertyHelper"
* reference. User tasks can also add themselves to the chain, and provide
* dynamic properties.
*
* @param project the project fro which the property helper is required.
*
* @return the project's property helper.
*/
public static synchronized
PropertyHelper getPropertyHelper(Project project) {
PropertyHelper helper
= (PropertyHelper) project.getReference(MagicNames.REFID_PROPERTY_HELPER);
if (helper != null) {
return helper;
}
helper = new PropertyHelper();
helper.setProject(project);
project.addReference(MagicNames.REFID_PROPERTY_HELPER, helper);
return helper;
}
// -------------------- Methods to override --------------------
/**
* Sets a property. Any existing property of the same name
* is overwritten, unless it is a user property. Will be called
* from setProperty().
*
* If all helpers return false, the property will be saved in
* the default properties table by setProperty.
*
* @param ns The namespace that the property is in (currently
* not used.
* @param name The name of property to set.
* Must not be <code>null.
* @param value The new value of the property.
* Must not be <code>null.
* @param inherited True if this property is inherited (an [sub]ant[call] property).
* @param user True if this property is a user property.
* @param isNew True is this is a new property.
* @return true if this helper has stored the property, false if it
* couldn't. Each helper should delegate to the next one (unless it
* has a good reason not to).
*/
public boolean setPropertyHook(String ns, String name,
Object value,
boolean inherited, boolean user,
boolean isNew) {
if (getNext() != null) {
boolean subst = getNext().setPropertyHook(ns, name, value,
inherited, user, isNew);
// If next has handled the property
if (subst) {
return true;
}
}
return false;
}
/** Get a property. If all hooks return null, the default
* tables will be used.
*
* @param ns namespace of the sought property.
* @param name name of the sought property.
* @param user True if this is a user property.
* @return The property, if returned by a hook, or null if none.
*/
public Object getPropertyHook(String ns, String name, boolean user) {
if (getNext() != null) {
Object o = getNext().getPropertyHook(ns, name, user);
if (o != null) {
return o;
}
}
// Experimental/Testing, will be removed
if (name.startsWith("toString:")) {
name = name.substring("toString:".length());
Object v = project.getReference(name);
return (v == null) ? null : v.toString();
}
return null;
}
// -------------------- Optional methods --------------------
// You can override those methods if you want to optimize or
// do advanced things (like support a special syntax).
// The methods do not chain - you should use them when embedding ant
// (by replacing the main helper)
/**
* Parses a string containing <code>${xxx} style property
* references into two lists. The first list is a collection
* of text fragments, while the other is a set of string property names.
* <code>null entries in the first list indicate a property
* reference from the second list.
*
* It can be overridden with a more efficient or customized version.
*
* @param value Text to parse. Must not be <code>null.
* @param fragments List to add text fragments to.
* Must not be <code>null.
* @param propertyRefs List to add property names to.
* Must not be <code>null.
*
* @exception BuildException if the string contains an opening
* <code>${ without a closing
* <code>}
*/
public void parsePropertyString(String value, Vector fragments,
Vector propertyRefs)
throws BuildException {
parsePropertyStringDefault(value, fragments, propertyRefs);
}
/**
* Replaces <code>${xxx} style constructions in the given value
* with the string value of the corresponding data types.
*
* @param ns The namespace for the property.
* @param value The string to be scanned for property references.
* May be <code>null, in which case this
* method returns immediately with no effect.
* @param keys Mapping (String to String) of property names to their
* values. If <code>null, only project properties will
* be used.
*
* @exception BuildException if the string contains an opening
* <code>${ without a closing
* <code>}
* @return the original string with the properties replaced, or
* <code>null if the original string is
Other Ant examples (source code examples)Here is a short list of links related to this Ant PropertyHelper.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.