added a builder to parse files with eclipse's build mechanisms
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / builder / ParserBuilder.java
1 package net.sourceforge.phpeclipse.builder;
2
3 import java.util.Map;
4
5 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
6 import net.sourceforge.phpeclipse.phpeditor.PHPParserAction;
7
8 import org.eclipse.core.resources.IFile;
9 import org.eclipse.core.resources.IProject;
10 import org.eclipse.core.resources.IResource;
11 import org.eclipse.core.resources.IResourceDelta;
12 import org.eclipse.core.resources.IResourceVisitor;
13 import org.eclipse.core.resources.IncrementalProjectBuilder;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IConfigurationElement;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.OperationCanceledException;
18
19 /**
20  * Builder for .php files. 
21  * 
22  * 
23  * @see org.eclipse.core.resources.IncrementalProjectBuilder
24  * @see org.eclipse.core.resources.IResourceDelta
25  */
26 public class ParserBuilder extends IncrementalProjectBuilder {
27   private final static int TOTAL_WORK = 100;
28
29   /**
30    * Constructor
31    */
32   public ParserBuilder() {
33   }
34
35   /**
36    * 
37    */
38   protected IProject[] build(int kind, Map args, IProgressMonitor monitor)
39     throws CoreException {
40     monitor.beginTask("Parsing files", TOTAL_WORK);
41
42     if (kind == IncrementalProjectBuilder.FULL_BUILD) {
43       IResourceDelta delta = getDelta(getProject());
44
45       processFull(getProject(), monitor);
46
47     } else { // INCREMENTAL_BUILD or AUTO_BUILD
48
49       IResourceDelta delta = getDelta(getProject());
50       if (delta != null) {
51         delta.accept(new ParserVisitor(monitor));
52       }
53
54     }
55     monitor.done();
56     return null;
57   }
58
59   /**
60    * Performs a <code>FULL_BUILD</code> by visiting all nodes in the resource
61    * tree under the specified project.  
62    * 
63    * @param iProject
64    */
65   public void processFull(IProject iProject, final IProgressMonitor monitor) {
66
67     // Create resource visitor logic
68     IResourceVisitor myVisitor = new IResourceVisitor() {
69       public boolean visit(IResource resource) throws CoreException {
70         if (resource.getType() == IResource.FILE) {
71           if (monitor.isCanceled()) {
72             throw new OperationCanceledException();
73           }
74           if ((resource.getFileExtension() != null)
75             && PHPFileUtil.isPHPFile((IFile) resource)) {
76             monitor.worked(1);
77             monitor.subTask("Parsing: " + resource.getFullPath());
78             PHPParserAction.parseFile((IFile) resource);
79           }
80         }
81
82         return true;
83       }
84     };
85
86     // Process the project using the visitor just created
87     try {
88       iProject.accept(myVisitor);
89     } catch (CoreException e) {
90       e.printStackTrace();
91     }
92
93   }
94
95   /** 
96    * Sets initialization data for this builder.
97    * <p>
98    * This method is part of the <code>IExecutableExtension</code>
99    * interface.
100    * </p>
101    * <p>
102    * Subclasses are free to extend this method to pick up 
103    * initialization parameters from the plug-in plug-in manifest 
104    * (<code>plugin.xml</code>) file,
105    * but should be sure to invoke this method on their superclass.
106    * <p>
107    * For example, the following method looks for a boolean-valued 
108    * parameter named "trace":
109    * <pre>
110    *     public void setInitializationData(IConfigurationElement cfig, 
111    *             String propertyName, Object data) 
112    *                    throws CoreException {
113    *         super.setInitializationData(cfig, propertyName, data);
114    *         if (data instanceof Hashtable) { 
115    *             Hashtable args = (Hashtable) data; 
116    *             String traceValue = (String) args.get("trace"); 
117    *             TRACING = (traceValue!=null && traceValue.equals("true"));
118    *         }
119    *     }
120    * </pre>
121    * </p>
122    */
123   public void setInitializationData(
124     IConfigurationElement config,
125     String propertyName,
126     Object data)
127     throws CoreException {
128     super.setInitializationData(config, propertyName, data);
129
130   }
131
132   /**
133    * Informs this builder that it is being started by the build management
134    * infrastructure.  By the time this method is run, the builder's project
135    * is available and <code>setInitializationData</code> has been called.
136    * The default implementation should be called by all overriding methods.
137    *
138    * @see #setInitializationData
139    */
140   protected void startupOnInitialize() {
141     //   traceMsg("Parse Builder Initialize - startupOnInitialize()");
142   }
143
144   /**
145   * Write trace statements.  
146   * System.out.println with prefix tagging used for simplicity.
147   */
148   //  private void traceMsg(String msg) {
149   //    if (PHPeclipsePlugin.DEBUG | traceEnabled)
150   //      System.out.println(
151   //        buildMode
152   //          + "<"
153   //          + getProject()
154   //          + "> "
155   //          + "\t\t\t"
156   //          + buildMark
157   //          + msg);
158   //  }
159 }