blob: 4db9c75f3c8289feb7c45494d0b78ddbce701a01 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004, 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ant.internal.ui.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.eclipse.ant.core.AntSecurityException;
import org.eclipse.ant.internal.ui.AntUIImages;
import org.eclipse.ant.internal.ui.IAntUIConstants;
import org.eclipse.ant.internal.ui.editor.AntEditorCompletionProcessor;
import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.IRegion;
import org.xml.sax.Attributes;
public class AntPropertyNode extends AntTaskNode {
private String fValue= null;
private String fReferencedName;
private String fOccurrencesStartingPoint= IAntModelConstants.ATTR_VALUE;
/*
* The set of properties defined by this node
* name-> value mapping
*/
private Map fProperties= null;
public AntPropertyNode(Task task, Attributes attributes) {
super(task);
String label = attributes.getValue(IAntModelConstants.ATTR_NAME);
if (label == null) {
label = attributes.getValue(IAntModelConstants.ATTR_FILE);
if (label != null) {
fReferencedName= label;
label= "file="+label; //$NON-NLS-1$
} else {
label = attributes.getValue(IAntModelConstants.ATTR_RESOURCE);
if (label != null) {
fReferencedName= label;
label= "resource="+label; //$NON-NLS-1$
} else {
label = attributes.getValue(IAntModelConstants.ATTR_ENVIRONMENT);
if (label != null) {
label= "environment=" + label; //$NON-NLS-1$
}
}
}
} else {
fValue= attributes.getValue(IAntModelConstants.ATTR_VALUE);
if (fValue == null) {
fOccurrencesStartingPoint= IAntModelConstants.ATTR_LOCATION;
fValue= attributes.getValue(fOccurrencesStartingPoint); //$NON-NLS-1$
}
}
setBaseLabel(label);
}
public String getProperty(String propertyName) {
if (fProperties != null) {
return (String)fProperties.get(propertyName);
}
return null;
}
/* (non-Javadoc)
* @see org.eclipse.ant.internal.ui.editor.model.AntElementNode#getBaseImageDescriptor()
*/
protected ImageDescriptor getBaseImageDescriptor() {
return AntUIImages.getImageDescriptor(IAntUIConstants.IMG_PROPERTY);
}
/**
* Sets the properties in the project.
*/
public boolean configure(boolean validateFully) {
if (fConfigured) {
return false;
}
try {
getProjectNode().setCurrentConfiguringProperty(this);
getTask().maybeConfigure();
getTask().execute();
fConfigured= true;
} catch (BuildException be) {
handleBuildException(be, AntEditorPreferenceConstants.PROBLEM_PROPERTIES);
} catch (LinkageError le) {
//A classpath problem with the property task. Known cause:
//<property name= "hey" refId= "classFileSetId"/> where
//classFileSetId refs a ClassFileSet which is an optional type that requires
//BCEL JAR. Currently it is not possible to set these types of properties within the Ant Editor.
//see bug 71888
handleBuildException(new BuildException(AntModelMessages.AntPropertyNode_0), AntEditorPreferenceConstants.PROBLEM_PROPERTIES); //$NON-NLS-1$
} catch (AntSecurityException se) {
//either a system exit or setting of system property was attempted
handleBuildException(new BuildException(AntModelMessages.AntPropertyNode_1), AntEditorPreferenceConstants.PROBLEM_SECURITY); //$NON-NLS-1$
} finally {
getProjectNode().setCurrentConfiguringProperty(null);
}
return false;
}
/**
* Adds this property name and value as being created by this property node declaration.
* @param propertyName the name of the property
* @param value the value of the property
*/
public void addProperty(String propertyName, String value) {
if (fProperties == null) {
fProperties= new HashMap(1);
}
fProperties.put(propertyName, value);
}
/* (non-Javadoc)
* @see org.eclipse.ant.internal.ui.model.AntElementNode#getReferencedElement(int)
*/
public String getReferencedElement(int offset) {
if (fReferencedName != null) {
String textToSearch= getAntModel().getText(getOffset(), offset - getOffset());
if (textToSearch != null && textToSearch.length() != 0) {
String attributeString = AntEditorCompletionProcessor.getAttributeStringFromDocumentStringToPrefix(textToSearch);
if ("file".equals(attributeString) || "resource".equals(attributeString)) { //$NON-NLS-1$//$NON-NLS-2$
return fReferencedName;
}
}
}
return null;
}
/* (non-Javadoc)
* @see org.eclipse.ant.internal.ui.model.AntElementNode#containsOccurrence(java.lang.String)
*/
public boolean containsOccurrence(String identifier) {
if (fBaseLabel != null) {
if (fBaseLabel.equals(identifier)) {
return true;
}
}
if (fValue != null) {
return fValue.indexOf('{' + identifier + '}') != -1;
}
return false;
}
/* (non-Javadoc)
* @see org.eclipse.ant.internal.ui.model.AntElementNode#getOccurrencesIdentifier()
*/
public String getOccurrencesIdentifier() {
return fBaseLabel;
}
/* (non-Javadoc)
* @see org.eclipse.ant.internal.ui.model.AntElementNode#isRegionPotentialReference(org.eclipse.jface.text.IRegion)
*/
public boolean isRegionPotentialReference(IRegion region) {
if (super.isRegionPotentialReference(region)) {
String textToSearch= getAntModel().getText(getOffset(), getLength());
int valueOffset= textToSearch.indexOf(fOccurrencesStartingPoint); //$NON-NLS-1$
if (valueOffset > -1) {
valueOffset= textToSearch.indexOf('"', valueOffset);
if (valueOffset > -1) {
boolean inValue= region.getOffset() >= (getOffset() + valueOffset);
if (inValue) {
if ("{".equals(getAntModel().getText(region.getOffset() - 1, 1)) || "}".equals(getAntModel().getText(region.getOffset() + region.getLength(), 1))) { //$NON-NLS-1$ //$NON-NLS-2$
return true;
}
//reference is not in the value and not within a property de-reference
return false;
}
return true;
}
}
}
return false;
}
public List computeIdentifierOffsets(String identifier) {
String textToSearch= getAntModel().getText(getOffset(), getLength());
List results= new ArrayList();
if (fBaseLabel != null) {
if (fBaseLabel.equals(identifier)) {
int nameOffset= textToSearch.indexOf("name"); //$NON-NLS-1$
nameOffset= textToSearch.indexOf(identifier, nameOffset);
results.add(new Integer(getOffset() + nameOffset));
}
}
if (fValue != null) {
int valueOffset= textToSearch.indexOf(fOccurrencesStartingPoint); //$NON-NLS-1$
int endOffset= getOffset() + getLength();
identifier= new StringBuffer("{").append(identifier).append('}').toString(); //$NON-NLS-1$
while(valueOffset < endOffset) {
valueOffset= textToSearch.indexOf(identifier, valueOffset);
if (valueOffset == -1 || valueOffset > endOffset) {
break;
}
results.add(new Integer(getOffset() + valueOffset + 1));
valueOffset+= identifier.length();
}
}
return results;
}
}