Search in sources :

Example 1 with IJSPProblem

use of org.eclipse.jst.jsp.core.internal.java.IJSPProblem 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;
}
Also used : INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IJSPProblem(org.eclipse.jst.jsp.core.internal.java.IJSPProblem) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) JSPTranslationExtension(org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension) ISelection(org.eclipse.jface.viewers.ISelection) UIJob(org.eclipse.ui.progress.UIJob) List(java.util.List) PartInitException(org.eclipse.ui.PartInitException) Job(org.eclipse.core.runtime.jobs.Job) UIJob(org.eclipse.ui.progress.UIJob) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) Position(org.eclipse.jface.text.Position) IEditorPart(org.eclipse.ui.IEditorPart) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) IProblem(org.eclipse.jdt.core.compiler.IProblem) Annotation(org.eclipse.jface.text.source.Annotation) AnnotationTypeLookup(org.eclipse.ui.texteditor.AnnotationTypeLookup) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor)

Example 2 with IJSPProblem

use of org.eclipse.jst.jsp.core.internal.java.IJSPProblem in project webtools.sourceediting by eclipse.

the class TaglibHelper method createValidationMessageProblem.

/**
 * @param customTag
 * @param validationMessage
 * @return
 */
private Object createValidationMessageProblem(final IStructuredDocument document, final ITextRegionCollection customTag, final String validationMessage) {
    final int start;
    if (customTag.getNumberOfRegions() > 3) {
        start = customTag.getStartOffset(customTag.getRegions().get(2));
    } else if (customTag.getNumberOfRegions() > 1) {
        start = customTag.getStartOffset(customTag.getRegions().get(1));
    } else {
        start = customTag.getStartOffset();
    }
    final int end;
    if (customTag.getNumberOfRegions() > 3) {
        end = customTag.getTextEndOffset(customTag.getRegions().get(customTag.getNumberOfRegions() - 2)) - 1;
    } else if (customTag.getNumberOfRegions() > 1) {
        end = customTag.getTextEndOffset(customTag.getRegions().get(1)) - 1;
    } else {
        end = customTag.getTextEndOffset();
    }
    final int line = document.getLineOfOffset(start);
    final char[] name;
    IPath location = TaglibController.getLocation(document);
    if (location == null) {
        name = new char[0];
    } else {
        name = location.toString().toCharArray();
    }
    return new IJSPProblem() {

        public void setSourceStart(int sourceStart) {
        }

        public void setSourceLineNumber(int lineNumber) {
        }

        public void setSourceEnd(int sourceEnd) {
        }

        public boolean isInfo() {
            return false;
        }

        public boolean isWarning() {
            return true;
        }

        public boolean isError() {
            return false;
        }

        public int getSourceStart() {
            return start;
        }

        public int getSourceLineNumber() {
            return line;
        }

        public int getSourceEnd() {
            return end;
        }

        public char[] getOriginatingFileName() {
            return name;
        }

        public String getMessage() {
            return validationMessage;
        }

        public int getID() {
            return getEID();
        }

        public String[] getArguments() {
            return new String[0];
        }

        public int getEID() {
            return IJSPProblem.TEIValidationMessage;
        }
    };
}
Also used : IPath(org.eclipse.core.runtime.IPath) IJSPProblem(org.eclipse.jst.jsp.core.internal.java.IJSPProblem)

Example 3 with IJSPProblem

use of org.eclipse.jst.jsp.core.internal.java.IJSPProblem in project webtools.sourceediting by eclipse.

the class TaglibHelper method createJSPProblem.

/**
 * @param customTag
 * @param teiClass
 * @return
 */
private Object createJSPProblem(final IStructuredDocument document, final ITextRegionCollection customTag, final int problemID, final String messageKey, final String argument, boolean preferVars) {
    final String tagname = customTag.getText(customTag.getRegions().get(1));
    final int start;
    if (customTag.getNumberOfRegions() > 1) {
        start = customTag.getStartOffset(customTag.getRegions().get(1));
    } else {
        start = customTag.getStartOffset();
    }
    final int end;
    if (customTag.getNumberOfRegions() > 1) {
        end = customTag.getTextEndOffset(customTag.getRegions().get(1)) - 1;
    } else {
        end = customTag.getTextEndOffset() - 1;
    }
    final int line = document.getLineOfOffset(start);
    final char[] name;
    IPath location = TaglibController.getLocation(document);
    if (location == null) {
        name = new char[0];
    } else {
        name = location.toString().toCharArray();
    }
    /*
		 * Note: these problems would result in translation errors on the
		 * server, so the severity is not meant to be controllable
		 */
    return new IJSPProblem() {

        public void setSourceStart(int sourceStart) {
        }

        public void setSourceLineNumber(int lineNumber) {
        }

        public void setSourceEnd(int sourceEnd) {
        }

        public boolean isWarning() {
            return false;
        }

        public boolean isInfo() {
            return false;
        }

        public boolean isError() {
            return true;
        }

        public int getSourceStart() {
            return start;
        }

        public int getSourceLineNumber() {
            return line;
        }

        public int getSourceEnd() {
            return end;
        }

        public char[] getOriginatingFileName() {
            return name;
        }

        public String getMessage() {
            return MessageFormat.format(messageKey, new String[] { tagname, argument });
        }

        public int getID() {
            return problemID;
        }

        public String[] getArguments() {
            return new String[0];
        }

        public int getEID() {
            return problemID;
        }
    };
}
Also used : IPath(org.eclipse.core.runtime.IPath) IJSPProblem(org.eclipse.jst.jsp.core.internal.java.IJSPProblem)

Example 4 with IJSPProblem

use of org.eclipse.jst.jsp.core.internal.java.IJSPProblem in project webtools.sourceediting by eclipse.

the class JSPJavaValidator method createMessageFromProblem.

/**
 * Creates an IMessage from asn IProblem
 *
 * @param problem
 * @param f
 * @param translation
 * @param structuredDoc
 * @return message representation of the problem, or null if it could not
 *         create one
 */
private IMessage createMessageFromProblem(IProblem problem, IFile f, IJSPTranslation translation, IStructuredDocument structuredDoc) {
    int sev = -1;
    int sourceStart = -1;
    int sourceEnd = -1;
    if (problem instanceof IJSPProblem) {
        sourceStart = problem.getSourceStart();
        sourceEnd = problem.getSourceEnd();
        switch(((IJSPProblem) problem).getEID()) {
            case IJSPProblem.TEIClassNotFound:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TEI_CLASS_NOT_FOUND);
                break;
            case IJSPProblem.TEIValidationMessage:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TEI_VALIDATION_MESSAGE);
                break;
            case IJSPProblem.TEIClassNotInstantiated:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TEI_CLASS_NOT_INSTANTIATED);
                break;
            case IJSPProblem.TEIClassMisc:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TEI_CLASS_RUNTIME_EXCEPTION);
                break;
            case IJSPProblem.TagClassNotFound:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TAG_HANDLER_CLASS_NOT_FOUND);
                break;
            case IJSPProblem.UseBeanInvalidID:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_USEBEAN_INVALID_ID);
                break;
            case IJSPProblem.UseBeanMissingTypeInfo:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_USBEAN_MISSING_TYPE_INFO);
                break;
            case IJSPProblem.UseBeanAmbiguousType:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_USEBEAN_AMBIGUOUS_TYPE_INFO);
                break;
            default:
                sev = problem.isError() ? IMessage.HIGH_SEVERITY : (problem.isWarning() ? IMessage.NORMAL_SEVERITY : ValidationMessage.IGNORE);
        }
    } else {
        sourceStart = translation.getJspOffset(problem.getSourceStart());
        sourceEnd = translation.getJspOffset(problem.getSourceEnd());
        switch(problem.getID()) {
            case IProblem.LocalVariableIsNeverUsed:
                {
                    sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_LOCAL_VARIABLE_NEVER_USED, sourceStart, sourceEnd);
                }
                break;
            case IProblem.NullLocalVariableReference:
                {
                    sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_NULL_LOCAL_VARIABLE_REFERENCE, sourceStart, sourceEnd);
                }
                break;
            case IProblem.ArgumentIsNeverUsed:
                {
                    sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_ARGUMENT_IS_NEVER_USED, sourceStart, sourceEnd);
                }
                break;
            case IProblem.PotentialNullLocalVariableReference:
                {
                    sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_POTENTIAL_NULL_LOCAL_VARIABLE_REFERENCE, sourceStart, sourceEnd);
                }
                break;
            case IProblem.UnusedImport:
                {
                    sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_UNUSED_IMPORT, sourceStart, sourceEnd);
                }
                break;
            case IProblem.UnusedPrivateField:
            case IProblem.MissingSerialVersion:
                {
                    // JSP files don't get serialized...right?
                    sev = ValidationMessage.IGNORE;
                }
                break;
            case IProblem.UnresolvedVariable:
                {
                    try {
                        // If the unresolved variable is in a fragment, post as a warning. The fragment may be included in a page that has declared the variable
                        IContentType contentType = f.getContentDescription().getContentType();
                        IContentType fragmentType = Platform.getContentTypeManager().getContentType(ContentTypeIdForJSP.ContentTypeID_JSPFRAGMENT);
                        if (contentType != null && fragmentType != null && contentType.isKindOf(fragmentType)) {
                            sev = IMessage.NORMAL_SEVERITY;
                            break;
                        }
                    }// Doesn't matter, we'll just fall back to the default
                     catch (CoreException e) {
                    }
                }
            default:
                {
                    if (problem.isError()) {
                        sev = IMessage.HIGH_SEVERITY;
                    } else if (problem.isWarning()) {
                        sev = IMessage.NORMAL_SEVERITY;
                    } else {
                        sev = IMessage.LOW_SEVERITY;
                    }
                }
                if (sev == ValidationMessage.IGNORE) {
                    return null;
                }
                /* problems without JSP positions are in generated code */
                if (sourceStart == -1) {
                    int problemID = problem.getID();
                    /*
						 * Quoting IProblem doc: "When a problem is tagged as
						 * Internal, it means that no change other than a
						 * local source code change can fix the corresponding
						 * problem." Assuming that our generated code is
						 * correct, that should reduce the reported problems
						 * to those the user can correct.
						 */
                    if (((problemID & IProblem.Internal) != 0) && ((problemID & IProblem.Syntax) != 0) && translation instanceof JSPTranslation) {
                        // Attach to the last code scripting section
                        JSPTranslation jspTranslation = ((JSPTranslation) translation);
                        Position[] jspPositions = (Position[]) jspTranslation.getJsp2JavaMap().keySet().toArray(new Position[jspTranslation.getJsp2JavaMap().size()]);
                        for (int i = 0; i < jspPositions.length; i++) {
                            sourceStart = Math.max(sourceStart, jspPositions[i].getOffset());
                        }
                        IMessage m = new LocalizedMessage(sev, problem.getMessage(), f);
                        m.setOffset(sourceStart);
                        m.setLength(1);
                        return m;
                    } else {
                        return null;
                    }
                }
        }
    }
    if (sev == ValidationMessage.IGNORE) {
        return null;
    }
    final boolean isIndirect = translation.isIndirect(problem.getSourceStart());
    if (isIndirect && !FragmentValidationTools.shouldValidateFragment(f)) {
        return null;
    }
    // line number for marker starts @ 1
    // line number from document starts @ 0
    int lineNo = structuredDoc.getLineOfOffset(sourceStart) + 1;
    IMessage m = new LocalizedMessage(sev, problem.getMessage(), f);
    m.setLineNo(lineNo);
    m.setOffset(sourceStart);
    m.setLength((sourceEnd >= sourceStart) ? (sourceEnd - sourceStart + 1) : 0);
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=119633
    if (isIndirect) {
        adjustIndirectPosition(m, translation);
    }
    return m;
}
Also used : IJSPProblem(org.eclipse.jst.jsp.core.internal.java.IJSPProblem) CoreException(org.eclipse.core.runtime.CoreException) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation) Position(org.eclipse.jface.text.Position) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) IContentType(org.eclipse.core.runtime.content.IContentType)

Example 5 with IJSPProblem

use of org.eclipse.jst.jsp.core.internal.java.IJSPProblem in project webtools.sourceediting by eclipse.

the class JSPJavaTranslatorCoreTest method test_389174.

public void test_389174() throws CoreException, IOException {
    IProject j = BundleResourceUtil.createJavaWebProject(getName());
    assertTrue(j.exists());
    String typeName = "List<List<Boolean>>";
    InputStream source = new ByteArrayInputStream(("<jsp:useBean id=\"x\" type=\"" + typeName + "\" />").getBytes());
    IFile file = j.getFile(getName() + "_test.jsp");
    file.create(source, IResource.FORCE, null);
    JSPTranslator translator = new JSPTranslator();
    IDOMModel structuredModel = null;
    try {
        structuredModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForRead(file);
        translator.reset(structuredModel.getDocument(), new NullProgressMonitor());
        translator.translate();
        assertTrue("specified type did not survive translation", translator.getTranslation().indexOf(typeName) >= 0);
        IJSPProblem[] translationProblems = (IJSPProblem[]) translator.getTranslationProblems().toArray(new IJSPProblem[0]);
        for (int i = 0; i < translationProblems.length; i++) {
            assertEquals("expected IJSPProblem type: " + translationProblems[i].getMessage(), Integer.toHexString(IProblem.UndefinedType), Integer.toHexString(translationProblems[i].getID()));
        }
    } finally {
        if (structuredModel != null)
            structuredModel.releaseFromRead();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) IJSPProblem(org.eclipse.jst.jsp.core.internal.java.IJSPProblem) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IProject(org.eclipse.core.resources.IProject) JSPTranslator(org.eclipse.jst.jsp.core.internal.java.JSPTranslator)

Aggregations

IJSPProblem (org.eclipse.jst.jsp.core.internal.java.IJSPProblem)5 IPath (org.eclipse.core.runtime.IPath)2 Position (org.eclipse.jface.text.Position)2 IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)2 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 List (java.util.List)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IContentType (org.eclipse.core.runtime.content.IContentType)1 Job (org.eclipse.core.runtime.jobs.Job)1 IProblem (org.eclipse.jdt.core.compiler.IProblem)1 Annotation (org.eclipse.jface.text.source.Annotation)1 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)1 ISelection (org.eclipse.jface.viewers.ISelection)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1