Search in sources :

Example 91 with IModelManager

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

the class TestModelEmbeddedContentType method doStructuredModelEmbeddedJSP.

protected IStructuredModel doStructuredModelEmbeddedJSP() throws IOException {
    // First make (empty) structuredDocument
    IModelManager modelManager = StructuredModelManager.getModelManager();
    IStructuredModel model = modelManager.createUnManagedStructuredModelFor(ContentTypeIdForJSP.ContentTypeID_JSP);
    assertTrue("model could not be created!", model != null);
    // should have an embedded type
    Document doc = ((IDOMModel) model).getDocument();
    PageDirectiveAdapter embeddedHandler = (PageDirectiveAdapter) ((INodeNotifier) doc).getAdapterFor(PageDirectiveAdapter.class);
    assertTrue("embededHanlder should NOT be null for JSP", embeddedHandler != null);
    // embedded type should be HTML since no content
    checkEmbeddedType(model, ModelHandlerUtility.getDefaultEmbeddedType());
    String testContent = "<%@ page contentType=\"text/html\" %>";
    model.getStructuredDocument().replaceText(this, 0, 0, testContent);
    assertTrue("reinit should NOT be needed in this case", !model.isReinitializationNeeded());
    // embedded type should STILL be HTML since no contentType was
    // text/html
    checkEmbeddedType(model, ModelHandlerUtility.getDefaultEmbeddedType());
    return model;
// 
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) PageDirectiveAdapter(org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) Document(org.w3c.dom.Document)

Example 92 with IModelManager

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

the class CreatingJSPExpression method testCreateJSPExpression.

public void testCreateJSPExpression() throws IOException {
    // First make (empty) structuredDocument
    IModelManager modelManager = StructuredModelManager.getModelManager();
    IStructuredModel model = modelManager.createUnManagedStructuredModelFor(ContentTypeIdForJSP.ContentTypeID_JSP);
    assertTrue("model could not be created!", model != null);
    // Now, assigning use a page directive, but leaving embedded type the same as default
    model.getStructuredDocument().setText(this, "<%@ page contentType=\"text/html\" language=\"java\" %>");
    Document doc = ((IDOMModel) model).getDocument();
    Element jspexpression = doc.createElement("jsp:expression");
    ((IDOMElement) jspexpression).setJSPTag(true);
    doc.appendChild(jspexpression);
    Text javacode = doc.createTextNode(" // some java code here; if (x <0) return new String(\"0\") else return new String (\"1\"); ");
    jspexpression.appendChild(javacode);
    Text cdatasection = doc.createCDATASection(" // some cdata java code here; if (x <0) return new String(\"0\") else return new String (\"1\"); ");
    doc.appendChild(cdatasection);
    // format's not needed, just prettier ... not sure why
    // it won't work right on whole document.
    new NodeFormatter().format(jspexpression);
    new NodeFormatter().format(cdatasection);
    System.out.println("document text: ");
    System.out.println(model.getStructuredDocument().get());
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NodeFormatter(org.eclipse.wst.xml.core.internal.provisional.format.NodeFormatter) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) Text(org.w3c.dom.Text) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) Document(org.w3c.dom.Document) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 93 with IModelManager

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

the class TestModelsFromFiles method doTestReload.

/**
 * @param string
 * @param class1
 * @param class2
 * @throws CoreException
 * @throws IOException
 * @throws ResourceInUse
 * @throws ResourceAlreadyExists
 */
private void doTestReload(String filePath, Class expectedDocumentClass, Class expectedPartioner) throws ResourceAlreadyExists, ResourceInUse, IOException, CoreException {
    IModelManager modelManager = StructuredModelManager.getModelManager();
    IFile file = (IFile) fTestProject.findMember(filePath);
    if (file == null) {
        file = fTestProject.getFile(filePath);
    }
    IStructuredModel model = modelManager.getNewModelForEdit(file, true);
    try {
        assertNotNull(model);
        IStructuredDocument document = model.getStructuredDocument();
        assertNotNull(document);
        assertTrue("wrong class of document", expectedDocumentClass.isInstance(document));
        IDocumentPartitioner setupPartitioner = null;
        if (Utilities.contains(expectedDocumentClass.getInterfaces(), IDocumentExtension3.class)) {
            setupPartitioner = ((IDocumentExtension3) document).getDocumentPartitioner(IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING);
        } else {
            setupPartitioner = document.getDocumentPartitioner();
        }
        assertTrue("wrong partitioner in document.", expectedPartioner.isInstance(setupPartitioner));
        InputStream inStream = null;
        try {
            inStream = file.getContents();
            model.reload(inStream);
            assertNotNull(model);
            IStructuredDocument documentReloaded = model.getStructuredDocument();
            // note: NOT same instance of document
            // FIXME: this test has to be changed back, once the reload
            // API is
            // fixed to work with different document.
            // assertFalse(document == documentReloaded);
            assertTrue(document == documentReloaded);
        } finally {
            if (inStream != null) {
                inStream.close();
            }
        }
    } finally {
        if (model != null)
            model.releaseFromEdit();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) 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)

Example 94 with IModelManager

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

the class TestModelsFromFiles method doTestCreateModelWithNonExistIFile.

/**
 * @param string
 * @param class1
 * @param class2
 * @throws CoreException
 * @throws IOException
 * @throws ResourceInUse
 * @throws ResourceAlreadyExists
 */
private String doTestCreateModelWithNonExistIFile(String filePath, Class expectedDocumentClass, Class expectedPartioner) throws ResourceAlreadyExists, ResourceInUse, IOException, CoreException {
    String contents = null;
    IModelManager modelManager = StructuredModelManager.getModelManager();
    IFile file = (IFile) fTestProject.findMember(filePath);
    // file will be null if the resource does not exist
    if (file == null) {
        file = fTestProject.getFile(filePath);
    }
    IStructuredModel model = modelManager.getNewModelForEdit(file, false);
    try {
        assertNotNull(model);
        IStructuredDocument document = model.getStructuredDocument();
        assertNotNull(document);
        contents = document.get();
        assertTrue("wrong class of document", expectedDocumentClass.isInstance(document));
        IDocumentPartitioner setupPartitioner = null;
        if (Utilities.contains(expectedDocumentClass.getInterfaces(), IDocumentExtension3.class)) {
            setupPartitioner = ((IDocumentExtension3) document).getDocumentPartitioner(IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING);
        } else {
            setupPartitioner = document.getDocumentPartitioner();
        }
        assertTrue("wrong partitioner in document.", expectedPartioner.isInstance(setupPartitioner));
    } finally {
        if (model != null)
            model.releaseFromEdit();
    }
    return contents;
}
Also used : IFile(org.eclipse.core.resources.IFile) IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) 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)

Example 95 with IModelManager

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

the class TestFormatProcessorHTML method getModelForEdit.

/**
 * must release model (from edit) after
 *
 * @param filename
 *            relative to this class (TestStructuredPartitioner)
 */
private IStructuredModel getModelForEdit(final String filename) {
    IStructuredModel model = null;
    try {
        IModelManager modelManager = StructuredModelManager.getModelManager();
        InputStream inStream = getClass().getResourceAsStream(filename);
        if (inStream == null)
            throw new FileNotFoundException("Can't file resource stream " + filename);
        final String baseFile = getClass().getResource(filename).toString();
        model = modelManager.getModelForEdit(baseFile, inStream, new URIResolver() {

            String fBase = baseFile;

            public String getFileBaseLocation() {
                return fBase;
            }

            public String getLocationByURI(String uri) {
                return getLocationByURI(uri, fBase);
            }

            public String getLocationByURI(String uri, boolean resolveCrossProjectLinks) {
                return getLocationByURI(uri);
            }

            public String getLocationByURI(String uri, String baseReference) {
                int lastSlash = baseReference.lastIndexOf("/");
                if (lastSlash > 0)
                    return baseReference.substring(0, lastSlash + 1) + uri;
                return baseReference;
            }

            public String getLocationByURI(String uri, String baseReference, boolean resolveCrossProjectLinks) {
                return getLocationByURI(uri, baseReference);
            }

            public IProject getProject() {
                return null;
            }

            public IContainer getRootLocation() {
                return null;
            }

            public InputStream getURIStream(String uri) {
                return getClass().getResourceAsStream(getLocationByURI(uri));
            }

            public void setFileBaseLocation(String newLocation) {
                this.fBase = newLocation;
            }

            public void setProject(IProject newProject) {
            }
        });
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return model;
}
Also used : InputStream(java.io.InputStream) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) FileNotFoundException(java.io.FileNotFoundException) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject)

Aggregations

IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)139 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)102 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)45 IOException (java.io.IOException)37 InputStream (java.io.InputStream)27 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)27 IFile (org.eclipse.core.resources.IFile)23 CoreException (org.eclipse.core.runtime.CoreException)21 BadLocationException (org.eclipse.jface.text.BadLocationException)13 FileNotFoundException (java.io.FileNotFoundException)12 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)11 Document (org.w3c.dom.Document)10 ArrayList (java.util.ArrayList)9 IProject (org.eclipse.core.resources.IProject)9 StringBufferInputStream (java.io.StringBufferInputStream)8 IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)7 IDocument (org.eclipse.jface.text.IDocument)6 PageDirectiveAdapter (org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter)6 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5