|
-
-
- package org.apache.tools.ant;
-
- import org.apache.tools.ant.helper.*;
- import java.util.Enumeration;
- import java.util.Locale;
- import java.util.Vector;
- import java.util.Hashtable;
- import org.xml.sax.AttributeList;
- import org.xml.sax.Attributes;
- import org.xml.sax.helpers.AttributeListImpl;
- import org.xml.sax.helpers.AttributesImpl;
-
-
- public class RuntimeConfigurable2 extends RuntimeConfigurable {
-
-
- private String elementTag = null;
-
- private Vector children = new Vector();
-
- private Object wrappedObject = null;
-
- private Attributes attributes;
-
- private StringBuffer characters = new StringBuffer();
-
-
-
- public RuntimeConfigurable2(Object proxy, String elementTag) {
- super( proxy, elementTag );
- wrappedObject = proxy;
- this.elementTag = elementTag;
- if( proxy instanceof Task )
- ((Task)proxy).setRuntimeConfigurableWrapper( this );
- }
-
-
-
- void setProxy(Object proxy) {
- wrappedObject = proxy;
- }
-
-
-
- public void setAttributes(AttributeList attributes) {
-
- }
-
- public void setAttributes2(Attributes attributes) {
- this.attributes=new AttributesImpl( attributes );
- }
-
-
-
- public AttributeList getAttributes() {
- return sax1Attributes( attributes );
- }
-
- public Attributes getAttributes2() {
- return attributes;
- }
-
- public static AttributeList sax1Attributes( Attributes sax2Att ) {
- AttributeListImpl sax1Att=new AttributeListImpl();
- int length = sax2Att.getLength();
- if (length > 0) {
- for (int i = 0; i < length; i++) {
-
-
- sax1Att.addAttribute( sax2Att.getQName(i),
- sax2Att.getType(i),
- sax2Att.getValue(i));
- }
- }
- return sax1Att;
- }
-
-
-
- public void addChild(RuntimeConfigurable child) {
- children.addElement(child);
- }
-
-
-
- RuntimeConfigurable getChild(int index) {
- return (RuntimeConfigurable) children.elementAt(index);
- }
-
-
-
- public void addText(String data) {
- characters.append(data);
- }
-
-
-
- public void addText(char[] buf, int start, int count) {
- addText(new String(buf, start, count));
- }
-
-
-
- public String getElementTag() {
- return elementTag;
- }
-
-
-
- public void maybeConfigure(Project p) throws BuildException {
- String id = null;
-
- if (attributes != null) {
- configure(wrappedObject, attributes, p);
- id = attributes.getValue("id");
- attributes = null;
- }
-
- if (characters.length() != 0) {
- ProjectHelper.addText(p, wrappedObject, characters.toString());
- characters.setLength(0);
- }
- Enumeration enum = children.elements();
- while (enum.hasMoreElements()) {
- RuntimeConfigurable2 child
- = (RuntimeConfigurable2) enum.nextElement();
- if (child.wrappedObject instanceof Task) {
- Task childTask = (Task) child.wrappedObject;
- childTask.setRuntimeConfigurableWrapper(child);
- childTask.maybeConfigure();
- } else {
- child.maybeConfigure(p);
- }
- ProjectHelper.storeChild(p, wrappedObject, child.wrappedObject,
- child.getElementTag().toLowerCase(Locale.US));
- }
-
- if (id != null) {
- p.addReference(id, wrappedObject);
- }
- }
-
-
- public static void configure( Object target, Attributes attrs, Project project )
- throws BuildException
- {
- if (target instanceof TaskAdapter) {
- target = ((TaskAdapter) target).getProxy();
- }
-
- IntrospectionHelper ih =
- IntrospectionHelper.getHelper(target.getClass());
-
- project.addBuildListener(ih);
-
- for (int i = 0; i < attrs.getLength(); i++) {
-
- String value = RuntimeConfigurable2.replaceProperties(project, attrs.getValue(i));
-
- try {
- ih.setAttribute(project, target,
- attrs.getQName(i).toLowerCase(Locale.US), value);
- } catch (BuildException be) {
-
- if (!attrs.getQName(i).equals("id")) {
- throw be;
- }
- }
- }
- }
-
- public static String replaceProperties( Project project ,String value ) {
- if (value == null) {
- return null;
- }
-
- Vector fragments = new Vector();
- Vector propertyRefs = new Vector();
-
- ProjectHelper.parsePropertyString(value, fragments, propertyRefs);
-
- StringBuffer sb = new StringBuffer();
- Enumeration i = fragments.elements();
- Enumeration j = propertyRefs.elements();
- while (i.hasMoreElements()) {
-
- String fragment = (String) i.nextElement();
- if (fragment == null) {
- String propertyName = (String) j.nextElement();
- Object repl=project.getProperty( propertyName );
-
- if( repl==null) {
-
- repl=processReference( project, propertyName );
- }
-
- if (repl==null ) {
- project.log("Property ${" + propertyName
- + "} has not been set", Project.MSG_VERBOSE);
- fragment="${" + propertyName + "}";
- } else {
- fragment = (String) repl;
- }
- }
- sb.append(fragment);
- }
-
- return sb.toString();
-
- }
-
- static Hashtable propertySources=new Hashtable();
-
- public static interface ProjectPropertySource {
-
- public String getProperty( Project project, String key );
-
- }
-
- public static void addPropertySource( String ns, ProjectPropertySource src ) {
- propertySources.put( ns, src );
- }
-
-
-
-
- static String processReference( Project project, String name ) {
- if( name.startsWith( "toString:" )) {
- name=name.substring( "toString:".length());
- Object v=project.getReference( name );
- if( v==null ) return null;
- return v.toString();
- }
-
- int idx=name.indexOf(":");
- if( idx<0 ) return null;
-
- String ns=name.substring( 0, idx );
- String path=name.substring( idx );
-
- ProjectPropertySource ps=(ProjectPropertySource)propertySources.get( ns );
- if( ps == null )
- return null;
-
- return ps.getProperty( project, path );
- }
- }
|