Search in sources :

Example 1 with XMLResourceEncodingDetector

use of org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector in project webtools.sourceediting by eclipse.

the class ContentDescriberForDTD method handleCalculations.

/**
 * @param description
 * @param detector
 * @throws IOException
 */
private void handleCalculations(IContentDescription description, IResourceCharsetDetector detector) throws IOException {
    // note: if we're asked for one, we set them all. I need to be sure if
    // called
    // mulitiple times (one for each, say) that we don't waste time
    // processing same
    // content again.
    EncodingMemento encodingMemento = ((XMLResourceEncodingDetector) detector).getEncodingMemento();
    // TODO: I need to verify to see if this BOM work is always done
    // by text type.
    Object detectedByteOrderMark = encodingMemento.getUnicodeBOM();
    if (detectedByteOrderMark != null) {
        Object existingByteOrderMark = description.getProperty(IContentDescription.BYTE_ORDER_MARK);
        // need to "push" up into base.
        if (!detectedByteOrderMark.equals(existingByteOrderMark))
            description.setProperty(IContentDescription.BYTE_ORDER_MARK, detectedByteOrderMark);
    }
    if (!encodingMemento.isValid()) {
        // note: after setting here, its the mere presence of
        // IContentDescriptionExtended.UNSUPPORTED_CHARSET
        // in the resource's description that can be used to determine if
        // invalid
        // in those cases, the "detected" property contains an
        // "appropriate default" to use.
        description.setProperty(IContentDescriptionExtended.UNSUPPORTED_CHARSET, encodingMemento.getInvalidEncoding());
        description.setProperty(IContentDescriptionExtended.APPROPRIATE_DEFAULT, encodingMemento.getAppropriateDefault());
    }
    Object detectedCharset = encodingMemento.getDetectedCharsetName();
    Object javaCharset = encodingMemento.getJavaCharsetName();
    // we always include detected, if its different than java
    handleDetectedSpecialCase(description, detectedCharset, javaCharset);
    if (javaCharset != null) {
        Object existingCharset = description.getProperty(IContentDescription.CHARSET);
        if (javaCharset.equals(existingCharset)) {
            handleDetectedSpecialCase(description, detectedCharset, javaCharset);
        } else {
            // we may need to add what we found, but only need to add
            // if different from default.the
            Object defaultCharset = getDetector().getSpecDefaultEncoding();
            if (defaultCharset != null) {
                if (!defaultCharset.equals(javaCharset)) {
                    description.setProperty(IContentDescription.CHARSET, javaCharset);
                }
            } else {
                // assuming if there is no spec default, we always need to
                // add, I'm assuming
                description.setProperty(IContentDescription.CHARSET, javaCharset);
            }
        }
    }
}
Also used : XMLResourceEncodingDetector(org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector) EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)

Example 2 with XMLResourceEncodingDetector

use of org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector in project webtools.sourceediting by eclipse.

the class XMLEncodingTests method testUTF16LEMalformed.

public void testUTF16LEMalformed() throws IOException {
    String filename = fileLocation + "utf16leMalformed.xml";
    doTestFileStream(filename, "UTF-16LE", new XMLResourceEncodingDetector());
}
Also used : XMLResourceEncodingDetector(org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector)

Example 3 with XMLResourceEncodingDetector

use of org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector in project webtools.sourceediting by eclipse.

the class XMLEncodingTests method testUTF16.

/**
 * Caution, when this file prints out in console (when debug set to true, it appears
 * incorrect (due to font problems in console).
 */
public void testUTF16() throws IOException {
    String filename = fileLocation + "utf16WithJapaneseChars.xml";
    doTestFileStream(filename, "UTF-16", new XMLResourceEncodingDetector());
}
Also used : XMLResourceEncodingDetector(org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector)

Example 4 with XMLResourceEncodingDetector

use of org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector in project webtools.sourceediting by eclipse.

the class XMLEncodingTests method testUtf16UnicodeStreamWithNoEncodingInHeaderBE.

/**
 * This test shows unicode BOM should take priority over settings/defaults
 * Note: UTF-16 == UTF-16BE
 */
public void testUtf16UnicodeStreamWithNoEncodingInHeaderBE() throws IOException {
    String filename = fileLocation + "utf16UnicodeStreamWithNoEncodingInHeaderBE.xml";
    doTestFileStream(filename, "UTF-16", new XMLResourceEncodingDetector());
}
Also used : XMLResourceEncodingDetector(org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector)

Example 5 with XMLResourceEncodingDetector

use of org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector in project webtools.sourceediting by eclipse.

the class XMLEncodingTests method testUTF16LE.

public void testUTF16LE() throws IOException {
    String filename = fileLocation + "utf16le.xml";
    doTestFileStream(filename, "UTF-16LE", new XMLResourceEncodingDetector());
}
Also used : XMLResourceEncodingDetector(org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector)

Aggregations

XMLResourceEncodingDetector (org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector)15 EncodingMemento (org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)3 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Charset (java.nio.charset.Charset)1 CharsetDecoder (java.nio.charset.CharsetDecoder)1 MalformedInputException (java.nio.charset.MalformedInputException)1 MalformedInputExceptionWithDetail (org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail)1