2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * 
19   * ========================================================================
20   */
21  package org.apache.cactus.eclipse.quickfix;
22  
23  import java.util.ArrayList;
24  
25  import org.apache.cactus.eclipse.runner.ui.CactusMessages;
26  import org.apache.cactus.eclipse.runner.ui.CactusPlugin;
27  import org.eclipse.core.runtime.CoreException;
28  import org.eclipse.jdt.core.ICompilationUnit;
29  import org.eclipse.jdt.core.IJavaProject;
30  import org.eclipse.jdt.core.JavaModelException;
31  import org.eclipse.jdt.ui.text.java.IInvocationContext;
32  import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
33  import org.eclipse.jdt.ui.text.java.IProblemLocation;
34  import org.eclipse.jdt.ui.text.java.IQuickFixProcessor;
35  /**
36   * QuickFix processor to propose cactifying of the project.
37   * 
38   * @version $Id: CactusQuickFixProcessor.java 238816 2008-03-18 16:36:46Z ptahchiev $
39   */
40  public class CactusQuickFixProcessor implements IQuickFixProcessor {
41      /**
42       * Constant for error code when the import is not found
43       */
44  	private static final int IMPORT_NOT_FOUND = 268435846;
45      /**
46       * Constant for error code when the classpath is not correct
47       */
48  	private static final int IS_CLASSPATH_CORRECT = 16777218;
49      /**
50       * The default relevance constant.
51       */
52  	private static final int DEFAULT_RELEVANCE = 90;
53      /**
54       * A method for getting only the corrections we need.
55       */
56  	public boolean hasCorrections(final ICompilationUnit unit, final int problemId) {
57  		if (problemId == IMPORT_NOT_FOUND || problemId == IS_CLASSPATH_CORRECT) {
58  			return true;
59  		}
60  		return false;
61  	}
62  
63      /**
64       * A method for getting the corrections.
65       */
66  	public IJavaCompletionProposal[] getCorrections(
67  			final IInvocationContext context,
68  			final IProblemLocation[] locations) throws CoreException {
69  		if (locations == null || locations.length == 0) {
70  			return null;
71  		}
72  		final ArrayList resultingCollections = new ArrayList();
73  		for (int i=0;i< locations.length;i++) {
74  			IProblemLocation problemLocation = (IProblemLocation) locations[i];
75  			process(context, problemLocation, resultingCollections);
76  		}
77  		IJavaCompletionProposal[] proposals = new IJavaCompletionProposal[resultingCollections.size()];
78      
79  		for(int i=0;i<resultingCollections.size();i++) {
80  			proposals[i] = (IJavaCompletionProposal) resultingCollections.get(i);
81  		}
82  		return proposals;
83  	}
84  	
85      /**
86       * Process when corrections found.
87       */
88  	private void process(
89  			final IInvocationContext context,
90  			final IProblemLocation problem,
91  			final ArrayList proposals) {
92  		if (problem.getProblemId() == 0) { // no proposals
93  			return;
94  		}
95  		
96  	    final String source;
97  	    try {
98  	      source = context.getCompilationUnit().getSource();
99  	    }
100 	    catch (final JavaModelException e) {
101 	      CactusPlugin.log(e.getMessage());
102 	      return;
103 	    }
104 	    final int offset = problem.getOffset();
105 	    final int length = problem.getLength();
106 
107 	    final String substring = source.substring(offset, offset + length);
108 	    
109 	    
110 	    
111 	    IJavaProject theWorkingProject = context.getCompilationUnit().getJavaProject();
112 	    
113 	    boolean cactusProblem = (problem.getProblemId() == IMPORT_NOT_FOUND && substring.startsWith("org.apache.cactus")) ||
114 	    						(problem.getProblemId() == IS_CLASSPATH_CORRECT && isCactusPrefixesMatch(substring));
115 	    
116 		if(cactusProblem) {
117 			final String name = CactusMessages.getString("Cactus.quickFix.name");
118 			proposals.add(new AddCactusClassesCompletionProposal(name, DEFAULT_RELEVANCE, theWorkingProject));
119 		}
120 	}
121 	
122     /**
123      * If the error prefix matches some of the Cactus 'keywords'
124      * then we have to add the Cactus correction.
125      */
126 	private boolean isCactusPrefixesMatch(String prefix) {
127 		return (prefix.startsWith("ServletTestCase") || 
128 				prefix.startsWith("JspTestCase") || 
129 				prefix.startsWith("EJBTestCase") || 
130 				prefix.startsWith("JettyTestSetup"));
131 	}
132 }