Search in sources :

Example 11 with JSPTranslation

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

the class JSPJavaHyperlinkDetector method getJSPTranslation.

/**
 * Get JSP translation object
 *
 * @return JSPTranslation if one exists, null otherwise
 */
private JSPTranslation getJSPTranslation(IDocument document) {
    JSPTranslation translation = null;
    IDOMModel xmlModel = null;
    try {
        xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(document);
        if (xmlModel != null) {
            IDOMDocument xmlDoc = xmlModel.getDocument();
            JSPTranslationAdapter adapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
            if (adapter != null) {
                translation = adapter.getJSPTranslation();
            }
        }
    } finally {
        if (xmlModel != null)
            xmlModel.releaseFromRead();
    }
    return translation;
}
Also used : IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)

Example 12 with JSPTranslation

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

the class JSPSearchDocument method getJspOffset.

public int getJspOffset(int javaOffset) {
    // copied from JSPTranslation
    int result = -1;
    int offsetInRange = 0;
    Position jspPos, javaPos = null;
    JSPTranslation trans = getJSPTranslation();
    if (trans != null) {
        HashMap java2jspMap = trans.getJava2JspMap();
        // iterate all mapped java ranges
        Iterator it = java2jspMap.keySet().iterator();
        while (it.hasNext()) {
            javaPos = (Position) it.next();
            // need to count the last position as included
            if (!javaPos.includes(javaOffset) && !(javaPos.offset + javaPos.length == javaOffset))
                continue;
            offsetInRange = javaOffset - javaPos.offset;
            jspPos = (Position) java2jspMap.get(javaPos);
            if (jspPos != null)
                result = jspPos.offset + offsetInRange;
            else {
                // $NON-NLS-1$
                Logger.log(Logger.ERROR, "jspPosition was null!" + javaOffset);
            }
            break;
        }
    }
    return result;
}
Also used : IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation) Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) Iterator(java.util.Iterator)

Example 13 with JSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslation 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 14 with JSPTranslation

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

the class JSPJavaTranslatorCoreTest method test_107338.

public void test_107338() throws Exception {
    String projectName = "bug_107338";
    // Create new project
    IProject project = BundleResourceUtil.createSimpleProject(projectName, null, null);
    assertTrue(project.exists());
    BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/" + projectName, "/" + projectName);
    project.open(new NullProgressMonitor());
    IFile file = project.getFile("WebContent/test107338.jsp");
    assertTrue(file.exists());
    IStructuredModel model = StructuredModelManager.getModelManager().getModelForRead(file);
    IDOMModel jspModel = (IDOMModel) model;
    String jspSource = model.getStructuredDocument().get();
    assertTrue("line delimiters have been converted to Windows [CRLF]", jspSource.indexOf("\r\n") < 0);
    assertTrue("line delimiters have been converted to Mac [CR]", jspSource.indexOf("\r") < 0);
    ModelHandlerForJSP.ensureTranslationAdapterFactory(model);
    IDOMDocument xmlDoc = jspModel.getDocument();
    JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
    JSPTranslation translation = translationAdapter.getJSPTranslation();
    // System.err.print(translation.getJavaText());
    assertTrue("new-line beginning scriptlet missing from translation", translation.getJavaText().indexOf("int i = 0;") >= 0);
    model.releaseFromRead();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) IProject(org.eclipse.core.resources.IProject)

Example 15 with JSPTranslation

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

the class BasicJSPSearchRequestor method acceptSearchMatch.

/**
 * Maps java search coordinates to corresponding JSP coordinates.
 * Adds the matches to the Search Results view.
 * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch)
 */
public void acceptSearchMatch(SearchMatch match) throws CoreException {
    if (JSPSearchSupport.getInstance().isCanceled())
        return;
    String matchDocumentPath = match.getResource().getFullPath().toString();
    SearchDocument searchDoc = JSPSearchSupport.getInstance().getSearchDocument(matchDocumentPath);
    if (searchDoc != null && searchDoc instanceof JavaSearchDocumentDelegate) {
        JavaSearchDocumentDelegate javaSearchDoc = (JavaSearchDocumentDelegate) searchDoc;
        int jspStart = javaSearchDoc.getJspOffset(match.getOffset());
        int jspEnd = javaSearchDoc.getJspOffset(match.getOffset() + match.getLength());
        JSPTranslation trans = javaSearchDoc.getJspTranslation();
        String jspText = trans.getJspText();
        String javaText = javaSearchDoc.getJavaText();
        if (DEBUG)
            displayDebugInfo(match, jspStart, jspEnd, jspText, javaText);
        if (jspStart > -1 && jspEnd > -1)
            addSearchMatch(new Document(trans.getJspText()), javaSearchDoc.getFile(), jspStart, jspEnd, jspText);
    }
}
Also used : JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation) JavaSearchDocumentDelegate(org.eclipse.jst.jsp.core.internal.java.search.JavaSearchDocumentDelegate) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) SearchDocument(org.eclipse.jdt.core.search.SearchDocument) SearchDocument(org.eclipse.jdt.core.search.SearchDocument)

Aggregations

JSPTranslation (org.eclipse.jst.jsp.core.internal.java.JSPTranslation)24 IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)21 JSPTranslationAdapter (org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)14 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)12 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)9 IFile (org.eclipse.core.resources.IFile)6 IJavaElement (org.eclipse.jdt.core.IJavaElement)6 IPath (org.eclipse.core.runtime.IPath)5 Path (org.eclipse.core.runtime.Path)5 DOMModelForJSP (org.eclipse.jst.jsp.core.internal.domdocument.DOMModelForJSP)4 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)4 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 CoreException (org.eclipse.core.runtime.CoreException)3 IDocument (org.eclipse.jface.text.IDocument)3 Position (org.eclipse.jface.text.Position)3 ArrayList (java.util.ArrayList)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2