Search in sources :

Example 26 with IStructuredModel

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

the class TestHtmlTranslation method testMangleTagAndServerSideInJS.

public void testMangleTagAndServerSideInJS() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script> var a = <%= 4 %>5;\nif(a < <custom:tag/>) {} ; </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\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();
}
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 27 with IStructuredModel

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

the class TestHTMLValidator method testRemoveAndAddBackCommentEndTag.

/**
 * Regression test for Bug 285285
 *
 * @see org.eclipse.wst.xml.ui.internal.validation.TestDelegatingSourceValidatorForXML#testRemoveAndAddBackCommentEndTag
 * @see org.eclipse.wst.html.ui.tests.validation.TestHTMLValidator#testRemoveAndAddBackCommentEndTag
 * @see org.eclipse.jst.jsp.ui.tests.validation.JSPHTMLValidatorTest#testRemoveAndAddBackCommentEndTag
 */
public void testRemoveAndAddBackCommentEndTag() throws Exception {
    IFile testFile = null;
    IStructuredModel model = null;
    try {
        // get test file
        testFile = fProject.getFile("RemoveAndAddBackCommentEndTag.html");
        assertTrue("Test file " + testFile + " does not exist", testFile.exists());
        // get the document
        model = StructuredModelManager.getModelManager().getModelForEdit(testFile);
        IStructuredDocument document = model.getStructuredDocument();
        // set up for fValidator
        WorkbenchContext context = new WorkbenchContext();
        List fileList = new ArrayList();
        fileList.add(testFile.getFullPath().toPortableString());
        context.setValidationFileURIs(fileList);
        // validate clean file
        TestReporter reporter = new TestReporter();
        fValidator.validate(context, reporter);
        assertFalse("There should be no validation errors on " + testFile, reporter.isMessageReported());
        // need to dynamically find where the --> is because
        // its different on unix vs windows because of line endings
        String contents = document.get();
        int endCommentIndex = contents.indexOf("-->");
        // remove -->
        document.replace(endCommentIndex, 3, "");
        // validate file with error
        reporter = new TestReporter();
        fValidator.validate(context, reporter);
        assertTrue("There should be validation errors on " + testFile, reporter.isMessageReported());
        // replace -->
        document.replace(endCommentIndex, 0, "-->");
        // validate clean file
        reporter = new TestReporter();
        fValidator.validate(context, reporter);
        assertFalse("There should be no validation errors on " + testFile, reporter.isMessageReported());
    } finally {
        if (model != null) {
            model.releaseFromEdit();
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) WorkbenchContext(org.eclipse.wst.validation.internal.operations.WorkbenchContext) ArrayList(java.util.ArrayList) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) ArrayList(java.util.ArrayList) List(java.util.List) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 28 with IStructuredModel

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

the class TestHTMLValidator method testInvalidateTagNameThenFix.

/**
 * Regression test for Bug 298472
 */
public void testInvalidateTagNameThenFix() throws Exception {
    IFile testFile = null;
    IStructuredModel model = null;
    try {
        // get test file
        testFile = fProject.getFile("InvalidateTagNameThenFix.html");
        assertTrue("Test file " + testFile + " does not exist", testFile.exists());
        // get the document
        model = StructuredModelManager.getModelManager().getModelForEdit(testFile);
        IStructuredDocument document = model.getStructuredDocument();
        // set up for validator
        WorkbenchContext context = new WorkbenchContext();
        List fileList = new ArrayList();
        fileList.add(testFile.getFullPath().toPortableString());
        context.setValidationFileURIs(fileList);
        // validate clean file
        TestReporter reporter = new TestReporter();
        fValidator.validate(context, reporter);
        assertFalse("There should be no validation errors on " + testFile, reporter.isMessageReported());
        // need to dynamically find where the --> is because
        // its different on unix vs windows because of line endings
        String contents = document.get();
        int endOfStartTagIndex = contents.indexOf("></div>");
        // change to <divx></div>
        document.replace(endOfStartTagIndex, 0, "x");
        // validate file with error
        reporter = new TestReporter();
        fValidator.validate(context, reporter);
        assertTrue("There should be validation errors on " + testFile, reporter.isMessageReported());
        // change back to <div></div>
        document.replace(endOfStartTagIndex, 1, "");
        // validate clean file
        reporter = new TestReporter();
        fValidator.validate(context, reporter);
        assertFalse("There should be no validation errors on " + testFile, reporter.isMessageReported());
    } finally {
        if (model != null) {
            model.releaseFromEdit();
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) WorkbenchContext(org.eclipse.wst.validation.internal.operations.WorkbenchContext) ArrayList(java.util.ArrayList) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) ArrayList(java.util.ArrayList) List(java.util.List) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 29 with IStructuredModel

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

the class TestStructuredPartitionerHTML method getModelForEdit.

/**
 * must release model (from edit) after
 * @param filename relative to this class (TestStructuredPartitioner)
 */
private IStructuredModel getModelForEdit(String filename) {
    IStructuredModel model = null;
    try {
        IModelManager modelManager = StructuredModelManager.getModelManager();
        InputStream inStream = getClass().getResourceAsStream(filename);
        if (inStream == null)
            inStream = new StringBufferInputStream("");
        model = modelManager.getModelForEdit(filename, inStream, null);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return model;
}
Also used : StringBufferInputStream(java.io.StringBufferInputStream) StringBufferInputStream(java.io.StringBufferInputStream) InputStream(java.io.InputStream) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IOException(java.io.IOException)

Example 30 with IStructuredModel

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

the class TestStructuredPartitionerHTML method doComputePartitioningTest.

/**
 * Compute the partitions for the given filename using the default partitioner
 * for that file type.
 *
 * @param filename
 * @return int
 * @throws IOException
 * @throws BadLocationException
 */
protected int doComputePartitioningTest(String filename) throws IOException, BadLocationException {
    IModelManager modelManager = StructuredModelManager.getModelManager();
    InputStream inStream = getClass().getResourceAsStream(filename);
    if (inStream == null)
        inStream = new StringBufferInputStream("");
    IStructuredModel model = modelManager.getModelForEdit(filename, inStream, null);
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    if (DEBUG_PRINT_RESULT && useFormatter) {
        double baseTen = Math.log(10);
        formatter.setMinimumIntegerDigits((int) (Math.log(structuredDocument.getLength()) / baseTen) + 1);
        formatter.setGroupingUsed(false);
    }
    partitions = structuredDocument.computePartitioning(0, structuredDocument.getLength());
    if (DEBUG_PRINT_RESULT) {
        String contents = null;
        System.out.println("\nfilename: " + filename);
        for (int i = 0; i < partitions.length; i++) {
            try {
                contents = structuredDocument.get(partitions[i].getOffset(), partitions[i].getLength());
            } catch (BadLocationException e) {
                contents = "*error*";
            }
            if (useFormatter)
                System.out.println(formatter.format(partitions[i].getOffset()) + ":" + formatter.format(partitions[i].getLength()) + " - " + partitions[i].getType() + " [" + StringUtils.escape(contents) + "]");
            else
                System.out.println(partitions[i] + " [" + StringUtils.escape(contents) + "]");
        }
    }
    checkSeams();
    model.releaseFromEdit();
    inStream.close();
    if (partitions == null)
        return -1;
    return partitions.length;
}
Also used : StringBufferInputStream(java.io.StringBufferInputStream) StringBufferInputStream(java.io.StringBufferInputStream) InputStream(java.io.InputStream) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) BadLocationException(org.eclipse.jface.text.BadLocationException)

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