KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > model > AntPropertyNode


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ant.internal.ui.model;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.apache.tools.ant.BuildException;
20 import org.apache.tools.ant.Task;
21 import org.eclipse.ant.core.AntSecurityException;
22 import org.eclipse.ant.internal.ui.AntUIImages;
23 import org.eclipse.ant.internal.ui.IAntUIConstants;
24 import org.eclipse.ant.internal.ui.editor.AntEditorCompletionProcessor;
25 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants;
26 import org.eclipse.jface.resource.ImageDescriptor;
27 import org.eclipse.jface.text.IRegion;
28 import org.xml.sax.Attributes JavaDoc;
29
30 public class AntPropertyNode extends AntTaskNode {
31     
32     private String JavaDoc fValue= null;
33     private String JavaDoc fReferencedName;
34     private String JavaDoc fOccurrencesStartingPoint= IAntModelConstants.ATTR_VALUE;
35     private String JavaDoc fOccurrencesIdentifier;
36     
37     /*
38      * The set of properties defined by this node
39      * name-> value mapping
40      */

41     private Map JavaDoc fProperties= null;
42     
43     public AntPropertyNode(Task task, Attributes JavaDoc attributes) {
44         super(task);
45          String JavaDoc label = attributes.getValue(IAntModelConstants.ATTR_NAME);
46          if (label == null) {
47             label = attributes.getValue(IAntModelConstants.ATTR_FILE);
48             if (label != null) {
49                 fReferencedName= label;
50                 label= "file="+label; //$NON-NLS-1$
51
} else {
52                 label = attributes.getValue(IAntModelConstants.ATTR_RESOURCE);
53                 if (label != null) {
54                     fReferencedName= label;
55                     label= "resource="+label; //$NON-NLS-1$
56
} else {
57                     label = attributes.getValue(IAntModelConstants.ATTR_ENVIRONMENT);
58                     if (label != null) {
59                         label= "environment=" + label; //$NON-NLS-1$
60
} else {
61                         label = attributes.getValue("srcFile"); //$NON-NLS-1$
62
if (label != null) {
63                             fReferencedName= label;
64                             label= "srcFile=" + label; //$NON-NLS-1$
65
}
66                     }
67                 }
68             }
69          } else {
70             fValue= attributes.getValue(IAntModelConstants.ATTR_VALUE);
71             if (fValue == null) {
72                 fOccurrencesStartingPoint= IAntModelConstants.ATTR_LOCATION;
73                 fValue= attributes.getValue(fOccurrencesStartingPoint);
74             }
75          }
76          setBaseLabel(label);
77     }
78     
79     public String JavaDoc getProperty(String JavaDoc propertyName) {
80         if (fProperties != null) {
81             return (String JavaDoc)fProperties.get(propertyName);
82         }
83         return null;
84     }
85     
86     /* (non-Javadoc)
87      * @see org.eclipse.ant.internal.ui.editor.model.AntElementNode#getBaseImageDescriptor()
88      */

89     protected ImageDescriptor getBaseImageDescriptor() {
90         return AntUIImages.getImageDescriptor(IAntUIConstants.IMG_PROPERTY);
91     }
92     
93     /**
94      * Sets the properties in the project.
95      */

96     public boolean configure(boolean validateFully) {
97         if (fConfigured) {
98             return false;
99         }
100         try {
101             getProjectNode().setCurrentConfiguringProperty(this);
102             getTask().maybeConfigure();
103             getTask().execute();
104             fConfigured= true;
105         } catch (BuildException be) {
106             handleBuildException(be, AntEditorPreferenceConstants.PROBLEM_PROPERTIES);
107         } catch (LinkageError JavaDoc le) {
108             //A classpath problem with the property task. Known cause:
109
//<property name= "hey" refId= "classFileSetId"/> where
110
//classFileSetId refs a ClassFileSet which is an optional type that requires
111
//BCEL JAR. Currently it is not possible to set these types of properties within the Ant Editor.
112
//see bug 71888
113
handleBuildException(new BuildException(AntModelMessages.AntPropertyNode_0), AntEditorPreferenceConstants.PROBLEM_PROPERTIES);
114         } catch (AntSecurityException se) {
115             //either a system exit or setting of system property was attempted
116
handleBuildException(new BuildException(AntModelMessages.AntPropertyNode_1), AntEditorPreferenceConstants.PROBLEM_SECURITY);
117         } finally {
118             getProjectNode().setCurrentConfiguringProperty(null);
119         }
120         return false;
121     }
122
123     /**
124      * Adds this property name and value as being created by this property node declaration.
125      * @param propertyName the name of the property
126      * @param value the value of the property
127      */

128     public void addProperty(String JavaDoc propertyName, String JavaDoc value) {
129         if (fProperties == null) {
130             fProperties= new HashMap JavaDoc(1);
131         }
132         fProperties.put(propertyName, value);
133     }
134     
135     /* (non-Javadoc)
136      * @see org.eclipse.ant.internal.ui.model.AntElementNode#getReferencedElement(int)
137      */

138     public String JavaDoc getReferencedElement(int offset) {
139         if (fReferencedName != null) {
140             String JavaDoc textToSearch= getAntModel().getText(getOffset(), offset - getOffset());
141             if (textToSearch != null && textToSearch.length() != 0) {
142                 String JavaDoc attributeString = AntEditorCompletionProcessor.getAttributeStringFromDocumentStringToPrefix(textToSearch);
143                 if ("file".equals(attributeString) || "resource".equals(attributeString) || "srcFile".equals(attributeString)) { //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
144
return fReferencedName;
145                 }
146             }
147         }
148         return null;
149     }
150     
151     /* (non-Javadoc)
152      * @see org.eclipse.ant.internal.ui.model.AntElementNode#containsOccurrence(java.lang.String)
153      */

154     public boolean containsOccurrence(String JavaDoc identifier) {
155         if (!getTask().getTaskName().equals("property")) { //$NON-NLS-1$
156
return super.containsOccurrence(identifier);
157         }
158
159         if (fValue != null) {
160             return fValue.indexOf(identifier) != -1;
161         }
162         return false;
163     }
164
165     /* (non-Javadoc)
166      * @see org.eclipse.ant.internal.ui.model.AntElementNode#getOccurrencesIdentifier()
167      */

168     public String JavaDoc getOccurrencesIdentifier() {
169         if (fOccurrencesIdentifier == null) {
170             fOccurrencesIdentifier= new StringBuffer JavaDoc("${").append(fBaseLabel).append('}').toString(); //$NON-NLS-1$
171
}
172         return fOccurrencesIdentifier;
173     }
174
175     /* (non-Javadoc)
176      * @see org.eclipse.ant.internal.ui.model.AntElementNode#isRegionPotentialReference(org.eclipse.jface.text.IRegion)
177      */

178     public boolean isRegionPotentialReference(IRegion region) {
179         boolean superOK= super.isRegionPotentialReference(region);
180         if (!getTask().getTaskName().equals("property") || !superOK) { //$NON-NLS-1$
181
return superOK;
182         }
183         
184         String JavaDoc textToSearch= getAntModel().getText(getOffset(), getLength());
185         if (textToSearch == null) {
186             return false;
187         }
188         int valueOffset= textToSearch.indexOf(fOccurrencesStartingPoint);
189         if (valueOffset > -1) {
190             valueOffset= textToSearch.indexOf('"', valueOffset);
191             if (valueOffset > -1) {
192                 boolean inValue= region.getOffset() >= (getOffset() + valueOffset);
193                 if (inValue) {
194                     if ("{".equals(getAntModel().getText(region.getOffset() - 1, 1)) || "}".equals(getAntModel().getText(region.getOffset() + region.getLength(), 1))) { //$NON-NLS-1$ //$NON-NLS-2$
195
return true;
196                     }
197                     //reference is not in the value and not within a property de-reference
198
return false;
199                 }
200                 return true;
201             }
202         }
203         return false;
204     }
205     
206     public List JavaDoc computeIdentifierOffsets(String JavaDoc identifier) {
207         if (!getTask().getTaskName().equals("property")) { //$NON-NLS-1$
208
return super.computeIdentifierOffsets(identifier);
209         }
210         String JavaDoc textToSearch= getAntModel().getText(getOffset(), getLength());
211         if (textToSearch == null || textToSearch.length() == 0 || identifier.length() == 0) {
212             return null;
213         }
214         List JavaDoc results= new ArrayList JavaDoc();
215         if (fBaseLabel != null) {
216             if (fBaseLabel.equals(identifier)) {
217                 int nameOffset= textToSearch.indexOf("name"); //$NON-NLS-1$
218
nameOffset= textToSearch.indexOf(identifier, nameOffset + 1);
219                 results.add(new Integer JavaDoc(getOffset() + nameOffset));
220             }
221         }
222         if (fValue != null) {
223             int valueOffset= textToSearch.indexOf(fOccurrencesStartingPoint);
224             int endOffset= getOffset() + getLength();
225             while (valueOffset < endOffset) {
226                 valueOffset= textToSearch.indexOf(identifier, valueOffset);
227                 if (valueOffset == -1 || valueOffset > endOffset) {
228                     break;
229                 }
230                 results.add(new Integer JavaDoc(getOffset() + valueOffset));
231                 valueOffset+= identifier.length();
232             }
233         }
234         return results;
235     }
236     
237     /* (non-Javadoc)
238      * @see org.eclipse.ant.internal.ui.model.AntElementNode#isFromDeclaration(org.eclipse.jface.text.IRegion)
239      */

240     public boolean isFromDeclaration(IRegion region) {
241         if (fBaseLabel == null) {
242             return false;
243         }
244         if (fBaseLabel.length() != region.getLength()) {
245             return false;
246         }
247         int offset= getOffset();
248          String JavaDoc textToSearch= getAntModel().getText(getOffset(), getLength());
249          if (textToSearch == null || textToSearch.length() == 0) {
250             return false;
251          }
252          int nameStartOffset= textToSearch.indexOf("name"); //$NON-NLS-1$
253
nameStartOffset= textToSearch.indexOf("\"", nameStartOffset); //$NON-NLS-1$
254
int nameEndOffset= textToSearch.indexOf("\"", nameStartOffset + 1); //$NON-NLS-1$
255
nameEndOffset+=offset;
256          nameStartOffset+=offset;
257          return nameStartOffset <= region.getOffset() && region.getOffset() + region.getLength() <= nameEndOffset;
258     }
259 }
260
Popular Tags