use of org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension in project webtools.sourceediting by eclipse.
the class JSPSearchDocument method getJSPTranslation.
/**
* It's not recommended for clients to hold on to this JSPTranslation
* since it's kind of large. If possible, hold on to the
* JSPSearchDocument, which is more of a lightweight proxy.
*
* @return the JSPTranslation for the jsp file, or null if it's an
* unsupported file.
*/
public final JSPTranslationExtension getJSPTranslation() {
JSPTranslationExtension translation = null;
IFile jspFile = getFile();
if (!JSPSearchSupport.isJsp(jspFile))
return translation;
IStructuredModel model = null;
try {
// get existing model for read, then get document from it
IModelManager modelManager = getModelManager();
if (modelManager != null) {
jspFile.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
model = modelManager.getModelForRead(jspFile);
}
// handle unsupported
if (model instanceof IDOMModel) {
IDOMModel xmlModel = (IDOMModel) model;
setupAdapterFactory(xmlModel);
IDOMDocument doc = xmlModel.getDocument();
JSPTranslationAdapter adapter = (JSPTranslationAdapter) doc.getAdapterFor(IJSPTranslation.class);
translation = adapter.getJSPTranslation();
}
} catch (IOException e) {
Logger.logException(e);
} catch (CoreException e) {
Logger.logException(e);
} catch (UnsupportedCharsetExceptionWithDetail e) {
// no need to log this. Just consider it an invalid file for our
// purposes.
// Logger.logException(e);
} finally {
if (model != null)
model.releaseFromRead();
}
return translation;
}
use of org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension in project webtools.sourceediting by eclipse.
the class JSPJavaTranslatorCoreTest method testIterationTagsIncomplete.
/**
* Tests that an iteration tag will generate
* @throws Exception
*/
public void testIterationTagsIncomplete() throws Exception {
String testName = "testIterationTags";
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(testName);
if (!project.isAccessible()) {
// Create new project
project = BundleResourceUtil.createSimpleProject(testName, null, null);
assertTrue(project.exists());
BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/" + testName, "/" + testName);
}
/* This test is failing as of 20180213 so until someone can debug and fix it, comment it out */
/* waitForBuildAndValidation(project); */
IFile testFile = project.getFile("/WebContent/test_missing_end_tag.jsp");
assertTrue("test_missing_end_tag.jsp is not accessible", testFile.isAccessible());
IDOMModel model = null;
try {
model = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(testFile);
ModelHandlerForJSP.ensureTranslationAdapterFactory(model);
JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
JSPTranslationExtension translation = translationAdapter.getJSPTranslation();
String javaText = translation.getJavaText();
int startOffset = javaText.indexOf("} // [</plain:loop>]");
assertTrue("Missing end tag was not accounted for.", startOffset != -1);
} finally {
if (model != null)
model.releaseFromEdit();
}
}
use of org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension in project webtools.sourceediting by eclipse.
the class JSPJavaTranslatorCoreTest method testIterationTags.
public void testIterationTags() throws Exception {
String testName = "testIterationTags";
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(testName);
if (!project.isAccessible()) {
// Create new project
project = BundleResourceUtil.createSimpleProject(testName, null, null);
assertTrue(project.exists());
BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/" + testName, "/" + testName);
}
/* This test is failing as of 20180213 so until someone can debug and fix it, comment it out */
/* waitForBuildAndValidation(project); */
IFile testFile = project.getFile("/WebContent/test.jsp");
assertTrue("test.jsp is not accessible", testFile.isAccessible());
IDOMModel model = null;
try {
model = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(testFile);
ModelHandlerForJSP.ensureTranslationAdapterFactory(model);
JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
JSPTranslationExtension translation = translationAdapter.getJSPTranslation();
translation.setProblemCollectingActive(true);
assertNotNull("No Java translation found", translation);
translation.reconcileCompilationUnit();
translation.setProblemCollectingActive(false);
List<IProblem> problems = translation.getProblems();
assertNotNull("Translation had a null problems list.", problems);
Iterator<IProblem> it = problems.iterator();
String javaText = translation.getJavaText();
int startOffset = javaText.indexOf("<plain:simple>");
assertTrue("<plan:simple> scope not found.", startOffset > 0);
int endOffset = javaText.indexOf("</plain:simple>", startOffset);
assertTrue("</plan:simple> scope not found.", endOffset > 0);
// Finds all errors caused by "continue cannot be used outside of a loop" - should only occur between <plain:simple></plain:simple>
while (it.hasNext()) {
IProblem problem = it.next();
if (problem.isError()) {
if ("continue cannot be used outside of a loop".equals(problem.getMessage())) {
assertTrue("'continue cannot be used outside of a loop' outside of iteration tag: ", problem.getSourceStart() > startOffset && problem.getSourceEnd() < endOffset);
}
}
}
} finally {
if (model != null)
model.releaseFromEdit();
}
}
use of org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension in project webtools.sourceediting by eclipse.
the class ShowTranslationHandler method execute.
/*
* (non-Javadoc)
*
* @see
* org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands
* .ExecutionEvent)
*/
public Object execute(final ExecutionEvent event) throws ExecutionException {
ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection instanceof IStructuredSelection) {
List list = ((IStructuredSelection) selection).toList();
if (!list.isEmpty()) {
if (list.get(0) instanceof IDOMNode) {
final IDOMModel model = ((IDOMNode) list.get(0)).getModel();
INodeAdapter adapter = model.getDocument().getAdapterFor(IJSPTranslation.class);
if (adapter != null) {
Job opener = new UIJob("Opening JSP Java Translation") {
public IStatus runInUIThread(IProgressMonitor monitor) {
JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
final JSPTranslationExtension translation = translationAdapter.getJSPTranslation();
// create an IEditorInput for the Java editor
final IStorageEditorInput input = new JSPTranslationEditorInput(model);
try {
IEditorPart editor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), input, JavaUI.ID_CU_EDITOR, true);
// Now add the problems we found
if (editor instanceof ITextEditor) {
IAnnotationModel annotationModel = ((ITextEditor) editor).getDocumentProvider().getAnnotationModel(input);
translation.reconcileCompilationUnit();
List problemsList = translation.getProblems();
IProblem[] problems = (IProblem[]) problemsList.toArray(new IProblem[problemsList.size()]);
AnnotationTypeLookup lookup = new AnnotationTypeLookup();
for (int i = 0; i < problems.length; i++) {
if (problems[i] instanceof IJSPProblem)
continue;
int length = problems[i].getSourceEnd() - problems[i].getSourceStart() + 1;
Position position = new Position(problems[i].getSourceStart(), length);
Annotation annotation = null;
String type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_INFO);
if (problems[i].isError()) {
type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_ERROR);
} else if (problems[i].isWarning()) {
type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_WARNING);
}
annotation = new Annotation(type, false, problems[i].getMessage());
if (annotation != null) {
annotationModel.addAnnotation(annotation, position);
}
}
}
} catch (PartInitException e) {
e.printStackTrace();
Display.getCurrent().beep();
}
return Status.OK_STATUS;
}
};
opener.setSystem(false);
opener.setUser(true);
opener.schedule();
}
}
}
}
return null;
}
use of org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension in project webtools.sourceediting by eclipse.
the class AddImportHandler method execute.
/* (non-Javadoc)
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
final IEditorSite site = HandlerUtil.getActiveEditor(event).getEditorSite();
final ISelectionProvider provider = site.getSelectionProvider();
final ISelection selection = provider != null ? provider.getSelection() : null;
if (selection instanceof IStructuredSelection && selection instanceof ITextSelection) {
final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
final int offset = ((ITextSelection) selection).getOffset();
final Object firstElement = structuredSelection.getFirstElement();
if (firstElement instanceof IDOMNode) {
final IDOMModel model = ((IDOMNode) firstElement).getModel();
INodeAdapter adapter = model.getDocument().getAdapterFor(IJSPTranslation.class);
if (adapter != null) {
JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
final JSPTranslationExtension translation = translationAdapter.getJSPTranslation();
translation.reconcileCompilationUnit();
final ICompilationUnit cu = translation.getCompilationUnit();
CompilationUnit astRoot = SharedASTProvider.getAST(cu, SharedASTProvider.WAIT_YES, null);
if (astRoot != null) {
final ASTNode node = NodeFinder.perform(astRoot, translation.getJavaOffset(offset), 0);
if (node != null) {
SimpleName name = null;
if (node.getNodeType() == ASTNode.SIMPLE_NAME) {
name = (SimpleName) node;
} else if (node.getNodeType() == ASTNode.QUALIFIED_NAME) {
name = ((QualifiedName) node).getName();
}
if (name != null) {
IBinding binding = name.resolveBinding();
if (binding instanceof ITypeBinding && (binding.getJavaElement() == null || !binding.getJavaElement().exists())) {
// Look it up!
ITypeBinding typeBinding = (ITypeBinding) binding;
final IImportContainer importContainer = cu.getImportContainer();
if (!importContainer.getImport(typeBinding.getQualifiedName()).exists()) {
final List typesFound = new ArrayList();
final TypeNameMatchRequestor collector = new TypeNameMatcher(typesFound);
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { cu.getJavaProject() });
final int mode = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
try {
new SearchEngine().searchAllTypeNames(null, mode, name.getIdentifier().toCharArray(), mode, IJavaSearchConstants.CLASS_AND_INTERFACE, scope, collector, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
final int length = typesFound.size();
final List elements = new ArrayList();
for (int i = 0; i < length; i++) {
final TypeNameMatch match = (TypeNameMatch) typesFound.get(i);
final int modifiers = match.getModifiers();
if (!Flags.isPrivate(modifiers) && !Flags.isPackageDefault(modifiers)) {
elements.add(match);
}
}
TypeNameMatch match = null;
// If there's only one match, insert it; otherwise, open the dialog to choose from the list
if (elements.size() == 1) {
match = (TypeNameMatch) elements.get(0);
} else if (elements.size() > 1) {
ElementListSelectionDialog dialog = new ElementListSelectionDialog(site.getShell(), LABEL_PROVIDER);
dialog.setElements(elements.toArray(new TypeNameMatch[elements.size()]));
dialog.setTitle(JSPUIMessages.AddImportHandler_title);
dialog.setMessage(JSPUIMessages.AddImportHandler_label);
if (dialog.open() == Window.OK) {
final Object result = dialog.getFirstResult();
if (result instanceof TypeNameMatch) {
match = (TypeNameMatch) result;
}
}
}
addImport(match, model.getStructuredDocument());
} catch (JavaModelException e) {
// $NON-NLS-1$
Logger.logException("Exception while determining import.", e);
}
}
}
}
}
}
}
}
}
return null;
}
Aggregations