Search in sources :

Example 1 with NullInputStream

use of org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream in project webtools.sourceediting by eclipse.

the class CodedReaderCreator method getResettableStream.

private InputStream getResettableStream() throws CoreException, IOException {
    InputStream resettableStream = null;
    if (fIFile != null) {
        InputStream inputStream = null;
        try {
            // note we always get contents, even if out of synch
            inputStream = fIFile.getContents(true);
        } catch (CoreException e) {
            // SHOULD actually check for existence of
            // fIStorage, but
            // for now will just assume core exception
            // means it
            // doesn't exist on file system, yet.
            // and we'll log, just in case its a noteable error
            Logger.logException(e);
            inputStream = new NullInputStream();
        }
        resettableStream = new BufferedInputStream(inputStream, CodedIO.MAX_BUF_SIZE);
    } else {
        if (fInputStream != null) {
            if (fInputStream.markSupported()) {
                resettableStream = fInputStream;
                // try {
                resettableStream.reset();
            // }
            // catch (IOException e) {
            // // assumed just hasn't been marked yet, so ignore
            // }
            } else {
                resettableStream = new BufferedInputStream(fInputStream, CodedIO.MAX_BUF_SIZE);
            }
        }
    }
    if (resettableStream == null) {
        resettableStream = new NullInputStream();
    }
    // mark this once, stream at "zero" position
    resettableStream.mark(MAX_MARK_SIZE);
    return resettableStream;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream) InputStream(java.io.InputStream) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream)

Example 2 with NullInputStream

use of org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream in project webtools.sourceediting by eclipse.

the class CodedReaderCreator method findMementoFromStreamCase.

/**
 * The primary method which contains the highest level rules for how to
 * decide appropriate decoding rules: 1. first check for unicode stream 2.
 * then looked for encoding specified in content (according to the type of
 * content that is it ... xml, html, jsp, etc. 3. then check for various
 * settings: file settings first, if null check project settings, if null,
 * check user preferences. 4. lastly (or, what is the last user
 * preference) is to use "workbench defaults".
 *
 * @throws IOException
 * @throws CoreException
 */
private EncodingMemento findMementoFromStreamCase() throws CoreException, IOException {
    EncodingMemento result = null;
    InputStream resettableLimitedStream = null;
    try {
        resettableLimitedStream = getLimitedStream(getResettableStream());
        if (resettableLimitedStream != null) {
            // first check for unicode stream
            result = checkStreamForBOM(resettableLimitedStream);
            // if not that, then check contents
            if (result == null) {
                resettableLimitedStream.reset();
                result = checkForEncodingInContents(resettableLimitedStream);
            }
        } else {
            // stream null, may name's not.
            if (fFilename != null) {
                // filename not null
                IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
                IContentDescription contentDescription = contentTypeManager.getDescriptionFor(new NullInputStream(), fFilename, IContentDescription.ALL);
                if (contentDescription != null) {
                    result = createMemento(contentDescription);
                }
            }
        }
    } finally {
        if (resettableLimitedStream != null) {
            handleStreamClose(resettableLimitedStream);
        }
    }
    return result;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream) InputStream(java.io.InputStream) IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream)

Example 3 with NullInputStream

use of org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream in project webtools.sourceediting by eclipse.

the class ModelModifications method createXMLModel.

/**
 * Be sure to release any models obtained from this method.
 *
 * @return
 * @throws IOException
 * @throws UnsupportedEncodingException
 */
private static IDOMModel createXMLModel() throws UnsupportedEncodingException, IOException {
    IStructuredModel model = null;
    IModelManager modelManager = StructuredModelManager.getModelManager();
    model = modelManager.getModelForEdit("test.xml", new NullInputStream(), null);
    // always use the same line delimiter for these tests, regardless
    // of
    // plaform or preference settings
    model.getStructuredDocument().setLineDelimiter(TestWriter.commonEOL);
    return (IDOMModel) model;
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream)

Example 4 with NullInputStream

use of org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream in project webtools.sourceediting by eclipse.

the class RegionChangedAdapterNotificationTests method testReplaceTrailingSpaceofEqualSignWithTwoSpaces.

public void testReplaceTrailingSpaceofEqualSignWithTwoSpaces() throws IOException {
    IDOMModel model = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(getName() + ".xml", new NullInputStream(), null);
    try {
        Document document = model.getDocument();
        IStructuredDocument structuredDocument = model.getStructuredDocument();
        structuredDocument.setText(this, "<a b= c></a>");
        Node before = document.getFirstChild();
        final int[] changed = new int[] { -1 };
        INodeAdapter adapter = new INodeAdapter() {

            public boolean isAdapterForType(Object type) {
                return type.equals(RegionChangedAdapterNotificationTests.class);
            }

            public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
                changed[0] = eventType;
            }
        };
        ((INodeNotifier) before).addAdapter(adapter);
        Object[] originalRegions = structuredDocument.getFirstStructuredDocumentRegion().getRegions().toArray();
        StructuredDocumentEvent fmEvent = structuredDocument.replaceText(null, 5, 0, "  ");
        assertTrue("Region instances changed", Arrays.equals(originalRegions, structuredDocument.getFirstStructuredDocumentRegion().getRegions().toArray()));
        assertTrue(fmEvent instanceof RegionChangedEvent);
        Node after = document.getFirstChild();
        assertEquals("Node replaced", before, after);
        assertEquals("unexpected adapter notification event sent " + structuredDocument.get(), -1, changed[0]);
        assertEquals("unexpected document content", "<a b=   c></a>", structuredDocument.get());
    } finally {
        model.releaseFromEdit();
    }
}
Also used : StructuredDocumentEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent) INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) RegionChangedEvent(org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Node(org.w3c.dom.Node) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Document(org.w3c.dom.Document) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream)

Example 5 with NullInputStream

use of org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream in project webtools.sourceediting by eclipse.

the class RegionChangedAdapterNotificationTests method testAppendWhitespaceToEqualSign.

public void testAppendWhitespaceToEqualSign() throws IOException {
    IDOMModel model = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(getName() + ".xml", new NullInputStream(), null);
    try {
        Document document = model.getDocument();
        IStructuredDocument structuredDocument = model.getStructuredDocument();
        structuredDocument.setText(this, "<a b= c></a>");
        Node before = document.getFirstChild();
        final int[] changed = new int[] { -1 };
        INodeAdapter adapter = new INodeAdapter() {

            public boolean isAdapterForType(Object type) {
                return type.equals(RegionChangedAdapterNotificationTests.class);
            }

            public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
                changed[0] = eventType;
            }
        };
        ((INodeNotifier) before).addAdapter(adapter);
        Object[] originalRegions = structuredDocument.getFirstStructuredDocumentRegion().getRegions().toArray();
        StructuredDocumentEvent fmEvent = structuredDocument.replaceText(null, 5, 0, " ");
        assertTrue("Region instances changed", Arrays.equals(originalRegions, structuredDocument.getFirstStructuredDocumentRegion().getRegions().toArray()));
        assertTrue(fmEvent instanceof RegionChangedEvent);
        Node after = document.getFirstChild();
        assertEquals("Node replaced", before, after);
        assertEquals("unexpected adapter notification event sent " + structuredDocument.get(), -1, changed[0]);
        assertEquals("unexpected document content", "<a b=  c></a>", structuredDocument.get());
    } finally {
        model.releaseFromEdit();
    }
}
Also used : StructuredDocumentEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent) INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) RegionChangedEvent(org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Node(org.w3c.dom.Node) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Document(org.w3c.dom.Document) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream)

Aggregations

NullInputStream (org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream)24 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)22 INodeAdapter (org.eclipse.wst.sse.core.internal.provisional.INodeAdapter)18 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)18 RegionChangedEvent (org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent)18 StructuredDocumentEvent (org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent)18 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)18 Document (org.w3c.dom.Document)18 Node (org.w3c.dom.Node)18 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)4 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)4 BufferedInputStream (java.io.BufferedInputStream)2 InputStream (java.io.InputStream)2 IOException (java.io.IOException)1 CoreException (org.eclipse.core.runtime.CoreException)1 IContentDescription (org.eclipse.core.runtime.content.IContentDescription)1 IContentTypeManager (org.eclipse.core.runtime.content.IContentTypeManager)1