Search in sources :

Example 6 with JSPTranslationExtension

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

the class JSPJavaValidator method adjustIndirectPosition.

/**
 * Assumed the message offset is an indirect position. In other words, an
 * error from an included file.
 *
 * @param m
 * @param translation
 */
private void adjustIndirectPosition(IMessage m, IJSPTranslation translation) {
    if (!(translation instanceof JSPTranslationExtension))
        return;
    IDocument jspDoc = ((JSPTranslationExtension) translation).getJspDocument();
    if (!(jspDoc instanceof IStructuredDocument))
        return;
    IStructuredDocument sDoc = (IStructuredDocument) jspDoc;
    IStructuredDocumentRegion[] regions = sDoc.getStructuredDocumentRegions(0, m.getOffset() + m.getLength());
    // iterate backwards until you hit the include directive
    for (int i = regions.length - 1; i >= 0; i--) {
        IStructuredDocumentRegion region = regions[i];
        if (region.getType() == DOMJSPRegionContexts.JSP_DIRECTIVE_NAME) {
            if (getDirectiveName(region).equals("include")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                ITextRegion fileValueRegion = getAttributeValueRegion(region, "file");
                if (fileValueRegion != null) {
                    m.setOffset(region.getStartOffset(fileValueRegion));
                    m.setLength(fileValueRegion.getTextLength());
                } else {
                    m.setOffset(region.getStartOffset());
                    m.setLength(region.getTextLength());
                }
                /**
                 * Bug 219761 - Syntax error reported at wrong location
                 * (don't forget to adjust the line number, too)
                 */
                m.setLineNo(sDoc.getLineOfOffset(m.getOffset()) + 1);
                break;
            }
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) JSPTranslationExtension(org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDocument(org.eclipse.jface.text.IDocument)

Example 7 with JSPTranslationExtension

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

the class JSPTranslatorPersistenceTest method testCreateTranslationAdapter.

/**
 * <p>This test case follows the general pattern of how a translation is created, then can be
 * externalized when the workspace is closed, then reloaded when its opened again.</p>
 *
 * @throws Exception
 */
public void testCreateTranslationAdapter() throws Exception {
    String outFileName = "testCreateTranslationAdapter.obj";
    IStructuredModel structModel = null;
    try {
        structModel = getModelForRead("Test1.jsp");
        // verify there is not already an existing translation adapter
        IDOMDocument domDoc = ((IDOMModel) structModel).getDocument();
        INodeAdapter existingAdapter = domDoc.getAdapterFor(IJSPTranslation.class);
        assertNull("There should be no existing adapter for IJSPTranslation", existingAdapter);
        // create a translator and externalize it, then load the externalized translator
        JSPTranslator originalTranslator = writeTranslator(structModel, outFileName);
        JSPTranslator externalizedTranslator = (JSPTranslator) readObject(outFileName);
        // create an adaptr from the loaded externalized translator and add it to the doc
        JSPTranslationAdapter restoredAdapter = new JSPTranslationAdapter((IDOMModel) structModel, externalizedTranslator);
        domDoc.addAdapter(restoredAdapter);
        // verify we can retrieve the adapter we just set
        existingAdapter = domDoc.getAdapterFor(IJSPTranslation.class);
        assertNotNull("There should now be an existing adapter for IJSPTranslation", existingAdapter);
        assertTrue("Expected " + existingAdapter + " to be an instance of JSPTranslationAdapter", existingAdapter instanceof JSPTranslationAdapter);
        JSPTranslationAdapter retrievedAdapter = (JSPTranslationAdapter) existingAdapter;
        JSPTranslationExtension jspTranslationExtension = retrievedAdapter.getJSPTranslation();
        /* verify that the original translation is equal to that of the
			 * retrieved adapter created from the previously externalized translator
			 */
        assertEquals("The original translation should be the same as the restored externalized translation", originalTranslator.getTranslation().toString(), jspTranslationExtension.getJavaText());
    } finally {
        if (structModel != null) {
            structModel.releaseFromRead();
        }
    }
}
Also used : INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) JSPTranslationExtension(org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension) 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) JSPTranslator(org.eclipse.jst.jsp.core.internal.java.JSPTranslator)

Aggregations

JSPTranslationExtension (org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension)7 IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)6 JSPTranslationAdapter (org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)6 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)6 IFile (org.eclipse.core.resources.IFile)3 INodeAdapter (org.eclipse.wst.sse.core.internal.provisional.INodeAdapter)3 List (java.util.List)2 IProject (org.eclipse.core.resources.IProject)2 IProblem (org.eclipse.jdt.core.compiler.IProblem)2 ISelection (org.eclipse.jface.viewers.ISelection)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IImportContainer (org.eclipse.jdt.core.IImportContainer)1