Search in sources :

Example 16 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testLeadingXMLComment.

public void testLeadingXMLComment() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "0<script> <!--12</script>3");
    assertNotNull("missing test model", structuredModel);
    // do translation
    JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
    JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
    IJsTranslation translation = translationAdapter.getJsTranslation(false);
    String translated = translation.getJsText();
    assertEquals("script with leading comment but no new line should be empty", "                ", translated);
    // release model
    structuredModel.releaseFromRead();
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)

Example 17 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testMangleServerSide1InJSCheckProblems.

// public void testMangleOverlappingTagAndServerSideInJS() {
// // get model
// String fileName = getName() + ".html";
// IStructuredModel structuredModel = getSharedModel(fileName, "<script> var a = <%= 4 %>5;\nif(a < <custom:tag attr=\"<%=%>\"/>) {} ; </script>");
// assertNotNull("missing test model", structuredModel);
// 
// // do translation
// JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
// JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
// IJsTranslation translation = translationAdapter.getJsTranslation(false);
// String translated = translation.getJsText();
// assertTrue("server-side script block included\n" + translated, translated.indexOf("<%") < 0);
// assertTrue("server-side script block included\n" + translated, translated.indexOf("%>") < 0);
// assertTrue("tag included\n" + translated, translated.indexOf("custom") < 0);
// assertTrue("tag included\n" + translated, translated.indexOf("/>") < 0);
// assertTrue("var dropped\n" + translated, translated.indexOf("var a = ") > -1);
// assertTrue("if dropped\n" + translated, translated.indexOf("5;\nif(a < ") > -1);
// assertTrue("block dropped\n" + translated, translated.indexOf(") {} ; ") > -1);
// 
// assertTrue("problems found in translation ", translation.getProblems().isEmpty());
// 
// // release model
// structuredModel.releaseFromRead();
// }
public void testMangleServerSide1InJSCheckProblems() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script> var text = <%= javaObject.getText() %>; </script>");
    assertNotNull("missing test model", structuredModel);
    // do translation
    JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
    JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
    IJsTranslation translation = translationAdapter.getJsTranslation(false);
    String translated = translation.getJsText();
    assertTrue("translation empty", translated.length() > 5);
    assertTrue("server-side script block included", translated.indexOf("<%") < 0);
    assertTrue("server-side script block included", translated.indexOf("%>") < 0);
    assertTrue("var dropped", translated.indexOf("var text = ") > -1);
    assertTrue("problems found in translation ", translation.getProblems().isEmpty());
    // release model
    structuredModel.releaseFromRead();
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)

Example 18 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testMangleMultipleServerSide2InJSCheckProblems.

public void testMangleMultipleServerSide2InJSCheckProblems() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script> var text = <? serverObject.getText() ?>;  <? serverObject.getText() ?></script>");
    assertNotNull("missing test model", structuredModel);
    // do translation
    JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
    JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
    IJsTranslation translation = translationAdapter.getJsTranslation(false);
    String translated = translation.getJsText();
    assertTrue("translation empty", translated.length() > 5);
    assertTrue("server-side script block included", translated.indexOf("<?") < 0);
    assertTrue("server-side script block included", translated.indexOf("?>") < 0);
    assertTrue("var dropped", translated.indexOf("var text = ") > -1);
    assertTrue("problems found in translation ", translation.getProblems().isEmpty());
    // release model
    structuredModel.releaseFromRead();
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)

Example 19 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testMangleMultipleMixedServerSideAndClientTagInJS_and_CheckProblems2.

public void testMangleMultipleMixedServerSideAndClientTagInJS_and_CheckProblems2() {
    // get model
    String fileName = getName() + ".jsp";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script> var text = <? serverObject.getText() ?>; <%=\"a\"%> <%=\"b\"%> <server:tag/> <? serverObject.getText() ?><%=\"c\"%> </script>");
    assertNotNull("missing test model", structuredModel);
    // do translation
    JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
    JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
    IJsTranslation translation = translationAdapter.getJsTranslation(false);
    String translated = translation.getJsText();
    assertEquals("translated contents not as expected", "         var text = _$tag_______________________; _$tag___ _$tag___ _$tag________ _$tag________________________$tag___ ", translated);
    assertTrue("translation empty", translated.length() > 5);
    assertTrue("server-side script block included", translated.indexOf("<?") < 0);
    assertTrue("server-side script block included", translated.indexOf("?>") < 0);
    assertTrue("server-side script block included", translated.indexOf("<%") < 0);
    assertTrue("server-side script block included", translated.indexOf("%>") < 0);
    assertTrue("var dropped", translated.indexOf("var text = ") > -1);
    assertTrue("problems found in translation ", translation.getProblems().isEmpty());
    // release model
    structuredModel.releaseFromRead();
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)

Example 20 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method getSharedModel.

private IStructuredModel getSharedModel(String id, String contents) {
    IStructuredModel structuredModel = null;
    InputStream inputStream = null;
    try {
        inputStream = new ByteArrayInputStream(contents.getBytes("UTF8"));
        structuredModel = fModelManager.getModelForRead(id, inputStream, null);
    } catch (Exception exception) {
        StringWriter s = new StringWriter();
        exception.printStackTrace(new PrintWriter(s));
        fail(s.toString());
    } finally {
        try {
            inputStream.close();
        } catch (IOException exception) {
        // should already be closed
        }
    }
    return structuredModel;
}
Also used : StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IOException(java.io.IOException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)482 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)110 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)102 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)81 IFile (org.eclipse.core.resources.IFile)75 IOException (java.io.IOException)69 CoreException (org.eclipse.core.runtime.CoreException)49 IDocument (org.eclipse.jface.text.IDocument)46 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)42 InputStream (java.io.InputStream)40 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)36 Path (org.eclipse.core.runtime.Path)35 BadLocationException (org.eclipse.jface.text.BadLocationException)34 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)32 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)32 IProject (org.eclipse.core.resources.IProject)31 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)31 IPath (org.eclipse.core.runtime.IPath)27 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Document (org.w3c.dom.Document)19