Search in sources :

Example 6 with JSPTranslator

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

the class JSPTranslatorPersistenceTest method writeTranslator.

private static JSPTranslator writeTranslator(IStructuredModel jspModel, String externalizedFileName) throws IOException, CoreException {
    JSPTranslator translator = new JSPTranslator();
    translator.reset((IDOMNode) jspModel.getIndexedRegion(0), new NullProgressMonitor());
    translator.translate();
    writeObject(externalizedFileName, translator);
    return translator;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JSPTranslator(org.eclipse.jst.jsp.core.internal.java.JSPTranslator)

Example 7 with JSPTranslator

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslator 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)

Example 8 with JSPTranslator

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

the class JSPJavaTranslatorTest method testJSPInJavascript.

void testJSPInJavascript(String filename) throws UnsupportedEncodingException, IOException {
    IStructuredModel sm = getStructuredModelForRead(filename);
    if (sm != null) {
        IDOMNode xmlNode = (IDOMNode) sm.getIndexedRegion(0);
        if (xmlNode != null) {
            JSPTranslator jspt = new JSPTranslator();
            jspt.reset(xmlNode, null);
            // int sourceTextPos = text.indexOf("<%= testJspString") + 17;
            // jspt.setSourceCursor(sourceTextPos); // right after the
            // text
            jspt.translate();
            String translation = jspt.getTranslation().toString();
            // offsets are found using JSPTranslation now
            // int translatedCursorPosition = jspt.getCursorPosition();
            // assertEquals("incorrect cursor position >" +
            // translatedCursorPosition, 519, translatedCursorPosition);
            // assertEquals("translation was incorrect", "testJspString",
            // translation.substring(519, 532));
            int cursorStart = translation.indexOf("out.print( testJspString );") + 10;
            assertEquals("incorrect cursor position >" + cursorStart, 914, cursorStart);
        }
        sm.releaseFromRead();
    }
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JSPTranslator(org.eclipse.jst.jsp.core.internal.java.JSPTranslator)

Aggregations

JSPTranslator (org.eclipse.jst.jsp.core.internal.java.JSPTranslator)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)3 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)3 IFile (org.eclipse.core.resources.IFile)2 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)2 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 IProject (org.eclipse.core.resources.IProject)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IJSPProblem (org.eclipse.jst.jsp.core.internal.java.IJSPProblem)1 IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)1 JSPTranslationAdapter (org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)1 JSPTranslationExtension (org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension)1 INodeAdapter (org.eclipse.wst.sse.core.internal.provisional.INodeAdapter)1