Search in sources :

Example 1 with StructuredTextPartitionerForXML

use of org.eclipse.wst.xml.core.internal.text.rules.StructuredTextPartitionerForXML in project webtools.sourceediting by eclipse.

the class StructuredTextPartitionerForJSP method createStructuredTextPartitioner.

private IStructuredTextPartitioner createStructuredTextPartitioner(IStructuredDocument structuredDocument) {
    IStructuredTextPartitioner result = null;
    // this same detector should underly content describer, to have consistent results
    JSPDocumentHeadContentDetector jspHeadContentDetector = new JSPDocumentHeadContentDetector();
    jspHeadContentDetector.set(structuredDocument);
    String contentType = null;
    try {
        contentType = jspHeadContentDetector.getContentType();
        // if XHTML or WML, that trumps what is in the page directive
        if (jspHeadContentDetector.isXHTML() || jspHeadContentDetector.isWML()) {
            contentType = "text/html";
        }
    } catch (IOException e) {
        // impossible in this context, since working with document stream
        throw new Error(e);
    }
    // null or empty, treat as "default"
    if (contentType == null || contentType.length() == 0) {
        // $NON-NLS-1$
        contentType = "text/html";
    }
    // make or tie-in to existing registry.
    if (contentType.equalsIgnoreCase(HTML_MIME_TYPE) || contentType.equalsIgnoreCase(VND_WAP_WML)) {
        result = new StructuredTextPartitionerForHTML();
        result.connect(structuredDocument);
    } else if (contentType.equalsIgnoreCase(XHTML_MIME_TYPE)) {
        result = new StructuredTextPartitionerForHTML();
        result.connect(structuredDocument);
    } else if (contentType.equalsIgnoreCase(XML_MIME_TYPE) || contentType.endsWith("+xml")) {
        // $NON-NLS-1$
        result = new StructuredTextPartitionerForXML();
        result.connect(structuredDocument);
    } else {
        result = new StructuredTextPartitioner();
        result.connect(structuredDocument);
    }
    return result;
}
Also used : JSPDocumentHeadContentDetector(org.eclipse.jst.jsp.core.internal.encoding.JSPDocumentHeadContentDetector) StructuredTextPartitionerForXML(org.eclipse.wst.xml.core.internal.text.rules.StructuredTextPartitionerForXML) StructuredTextPartitionerForHTML(org.eclipse.wst.html.core.internal.text.StructuredTextPartitionerForHTML) IOException(java.io.IOException) IStructuredTextPartitioner(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredTextPartitioner) StructuredTextPartitioner(org.eclipse.wst.sse.core.internal.text.rules.StructuredTextPartitioner) IStructuredTextPartitioner(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredTextPartitioner)

Example 2 with StructuredTextPartitionerForXML

use of org.eclipse.wst.xml.core.internal.text.rules.StructuredTextPartitionerForXML in project webtools.sourceediting by eclipse.

the class TransitionTests method testSoftRevert.

public void testSoftRevert() throws CoreException, IOException {
    // $NON-NLS-1$
    String filePath = "testfiles/xml/EmptyFile.xml";
    IFile file = (IFile) fTestProject.findMember(filePath);
    // $NON-NLS-1$
    assertNotNull("Test Case in error. Could not find file " + filePath, file);
    IPath locationPath = file.getLocation();
    ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    bufferManager.connect(locationPath, null);
    ITextFileBuffer buffer = bufferManager.getTextFileBuffer(locationPath);
    IDocument document = buffer.getDocument();
    assertNotNull(document);
    // $NON-NLS-1$
    assertTrue("wrong class of document", document instanceof BasicStructuredDocument);
    // $NON-NLS-1$
    assertTrue("wrong partitioner in document.", ((IDocumentExtension3) document).getDocumentPartitioner(IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING) instanceof StructuredTextPartitionerForXML);
    IStructuredModel model = StructuredModelManager.getModelManager().getModelForEdit((IStructuredDocument) document);
    try {
        try {
            // $NON-NLS-1$
            document.replace(0, 0, "__");
            // $NON-NLS-1$
            document.replace(2, 0, "<a");
            // $NON-NLS-1$
            document.replace(4, 0, ">");
            // $NON-NLS-1$
            document.replace(5, 0, "  ");
        } catch (BadLocationException e) {
            assertNull(e);
        }
        // $NON-NLS-1$
        document.set("");
    } finally {
        model.releaseFromEdit();
        bufferManager.disconnect(locationPath, null);
    }
}
Also used : StructuredTextPartitionerForXML(org.eclipse.wst.xml.core.internal.text.rules.StructuredTextPartitionerForXML) IFile(org.eclipse.core.resources.IFile) BasicStructuredDocument(org.eclipse.wst.sse.core.internal.text.BasicStructuredDocument) IPath(org.eclipse.core.runtime.IPath) IDocumentExtension3(org.eclipse.jface.text.IDocumentExtension3) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with StructuredTextPartitionerForXML

use of org.eclipse.wst.xml.core.internal.text.rules.StructuredTextPartitionerForXML in project webtools.sourceediting by eclipse.

the class JSPModelLoader method reInitializeEmbeddedType.

/**
 * This is "reinitialize" since there should always be at least the
 * default one assigned, before we start checking the stream
 */
private void reInitializeEmbeddedType(IStructuredModel model, EmbeddedTypeHandler oldEmbeddedContentType, EmbeddedTypeHandler newEmbeddedContentType) {
    // check program logic
    // $NON-NLS-1$
    Assert.isNotNull(oldEmbeddedContentType, "Program error: invalid call during model initialization");
    // once we know the embedded content type, we need to set it in the
    // PageDirectiveAdapter ... the order of initialization is
    // critical here, the doc must have been created, but its contents not
    // set yet,
    // and all factories must have been set up also.
    IDOMModel domModel = (IDOMModel) model;
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    IDOMDocument document = domModel.getDocument();
    PageDirectiveAdapter pageDirectiveAdapter = (PageDirectiveAdapter) document.getExistingAdapter(PageDirectiveAdapter.class);
    // ==> // PropagatingAdapter propagatingAdapter = (PropagatingAdapter)
    // ((INodeNotifier)
    // document).getExistingAdapter(PropagatingAdapter.class);
    // ==> // ModelQueryAdapter modelQueryAdapter = (ModelQueryAdapter)
    // ((INodeNotifier)
    // document).getExistingAdapter(ModelQueryAdapter.class);
    oldEmbeddedContentType.uninitializeFactoryRegistry(model.getFactoryRegistry());
    oldEmbeddedContentType.uninitializeParser(structuredDocument.getParser());
    // since 'document' is not recreated in this
    // reinit path, we need to remove all adapters,
    // except for the propagated adapters (including page
    // directive adapter, and model query adapter).
    // to accomplish this, we'll just remove all, then
    // add back with a call to pre-load adapt.
    // let clients decide to unload adapters from document
    // Collection oldAdapters = document.getAdapters();
    // Iterator oldAdaptersIterator = oldAdapters.iterator();
    // while (oldAdaptersIterator.hasNext()) {
    // INodeAdapter oldAdapter = (INodeAdapter)
    // oldAdaptersIterator.next();
    // if (oldAdapter != pageDirectiveAdapter && oldAdapter !=
    // propagatingAdapter && oldAdapter != modelQueryAdapter) {
    // // DO NOT remove directly!
    // // can change contents while in notifity loop!
    // //oldAdaptersIterator.remove();
    // document.removeAdapter(oldAdapter);
    // }
    // }
    // DMW: I believe something like the following is needed,
    // since releases cached adapters
    // if (document instanceof DocumentImpl) {
    // ((DocumentImpl) document).releaseDocumentType();
    // ((DocumentImpl) document).releaseStyleSheets();
    // }
    // remember, embedded type factories are automatically cleared when
    // embededType changed
    pageDirectiveAdapter.setEmbeddedType(newEmbeddedContentType);
    // pageDirectiveAdapter.clearPageWatchers();
    if (newEmbeddedContentType != null) {
        // need to null out or else ModelParserAdapter
        // won't get reinitialized
        ((DOMModelImpl) model).setModelParser(null);
        newEmbeddedContentType.initializeFactoryRegistry(model.getFactoryRegistry());
        newEmbeddedContentType.initializeParser(structuredDocument.getParser());
        // partitioner setup is the responsibility of this loader
        IDocumentPartitioner documentPartitioner = structuredDocument.getDocumentPartitioner();
        // added null/type checks for safety.
        if (documentPartitioner != null && documentPartitioner instanceof StructuredTextPartitionerForJSP) {
            if (newEmbeddedContentType.getFamilyId().equals(ContentTypeIdForXML.ContentTypeID_XML)) {
                ((StructuredTextPartitionerForJSP) documentPartitioner).setEmbeddedPartitioner(new StructuredTextPartitionerForXML());
            } else if (newEmbeddedContentType.getFamilyId().equals(ContentTypeIdForHTML.ContentTypeID_HTML)) {
                ((StructuredTextPartitionerForJSP) documentPartitioner).setEmbeddedPartitioner(new StructuredTextPartitionerForHTML());
            }
        }
    }
    // adding language here, in this convienent central
    // location, but some obvious renaming or refactoring
    // wouldn't hurt, in future.
    // I needed to add this language setting for JSP Fragment support
    // Note: this is the one that counts, since at this point,
    // the model has an ID, so we can look up IFile, etc.
    String language = getLanguage(model);
    if (language != null && language.length() > 0) {
        pageDirectiveAdapter.setLanguage(language);
    }
}
Also used : StructuredTextPartitionerForJSP(org.eclipse.jst.jsp.core.internal.text.StructuredTextPartitionerForJSP) StructuredTextPartitionerForXML(org.eclipse.wst.xml.core.internal.text.rules.StructuredTextPartitionerForXML) IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) DOMModelImpl(org.eclipse.wst.xml.core.internal.document.DOMModelImpl) StructuredTextPartitionerForHTML(org.eclipse.wst.html.core.internal.text.StructuredTextPartitionerForHTML) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) PageDirectiveAdapter(org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter)

Aggregations

StructuredTextPartitionerForXML (org.eclipse.wst.xml.core.internal.text.rules.StructuredTextPartitionerForXML)3 StructuredTextPartitionerForHTML (org.eclipse.wst.html.core.internal.text.StructuredTextPartitionerForHTML)2 IOException (java.io.IOException)1 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)1 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)1 IFile (org.eclipse.core.resources.IFile)1 IPath (org.eclipse.core.runtime.IPath)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 IDocumentExtension3 (org.eclipse.jface.text.IDocumentExtension3)1 IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)1 PageDirectiveAdapter (org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter)1 JSPDocumentHeadContentDetector (org.eclipse.jst.jsp.core.internal.encoding.JSPDocumentHeadContentDetector)1 StructuredTextPartitionerForJSP (org.eclipse.jst.jsp.core.internal.text.StructuredTextPartitionerForJSP)1 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)1 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)1 IStructuredTextPartitioner (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredTextPartitioner)1 BasicStructuredDocument (org.eclipse.wst.sse.core.internal.text.BasicStructuredDocument)1 StructuredTextPartitioner (org.eclipse.wst.sse.core.internal.text.rules.StructuredTextPartitioner)1 DOMModelImpl (org.eclipse.wst.xml.core.internal.document.DOMModelImpl)1