Search in sources :

Example 51 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project hale by halestudio.

the class CharsetConfigurationPage method update.

/**
 * Update the page state.
 */
protected void update() {
    String name = charsetCombo.getText();
    if (name != null && !name.isEmpty()) {
        try {
            Charset cs = Charset.forName(name);
            setMessage(successMessage(cs), INFORMATION);
            setPageComplete(true);
            return;
        } catch (UnsupportedCharsetException e) {
            setMessage("Charset not supported", ERROR);
        } catch (IllegalCharsetNameException e) {
            setMessage("Illegal charset name", ERROR);
        }
    } else {
        setMessage("Please specify a character encoding", INFORMATION);
    }
    setPageComplete(false);
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset)

Example 52 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project j2objc by google.

the class Properties method storeToXML.

/**
 * Emits an XML document representing all of the properties contained
 * in this table, using the specified encoding.
 *
 * <p>The XML document will have the following DOCTYPE declaration:
 * <pre>
 * &lt;!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;
 * </pre>
 *
 * <p>If the specified comment is {@code null} then no comment
 * will be stored in the document.
 *
 * <p> An implementation is required to support writing of XML documents
 * that use the "{@code UTF-8}" or "{@code UTF-16}" encoding. An
 * implementation may support additional encodings.
 *
 * <p>The specified stream remains open after this method returns.
 *
 * @param os        the output stream on which to emit the XML document.
 * @param comment   a description of the property list, or {@code null}
 *                  if no comment is desired.
 * @param  encoding the name of a supported
 *                  <a href="../lang/package-summary.html#charenc">
 *                  character encoding</a>
 *
 * @throws IOException if writing to the specified output stream
 *         results in an <tt>IOException</tt>.
 * @throws java.io.UnsupportedEncodingException if the encoding is not
 *         supported by the implementation.
 * @throws NullPointerException if {@code os} is {@code null},
 *         or if {@code encoding} is {@code null}.
 * @throws ClassCastException  if this {@code Properties} object
 *         contains any keys or values that are not
 *         {@code Strings}.
 * @see    #loadFromXML(InputStream)
 * @see    <a href="http://www.w3.org/TR/REC-xml/#charencoding">Character
 *         Encoding in Entities</a>
 * @since 1.5
 */
public void storeToXML(OutputStream os, String comment, String encoding) throws IOException {
    if (os == null) {
        throw new NullPointerException("os == null");
    } else if (encoding == null) {
        throw new NullPointerException("encoding == null");
    }
    /*
         * We can write to XML file using encoding parameter but note that some
         * aliases for encodings are not supported by the XML parser. Thus we
         * have to know canonical name for encoding used to store data in XML
         * since the XML parser must recognize encoding name used to store data.
         */
    String encodingCanonicalName;
    try {
        encodingCanonicalName = Charset.forName(encoding).name();
    } catch (IllegalCharsetNameException e) {
        System.out.println("Warning: encoding name " + encoding + " is illegal, using UTF-8 as default encoding");
        encodingCanonicalName = "UTF-8";
    } catch (UnsupportedCharsetException e) {
        System.out.println("Warning: encoding " + encoding + " is not supported, using UTF-8 as default encoding");
        encodingCanonicalName = "UTF-8";
    }
    PrintStream printStream = new PrintStream(os, false, encodingCanonicalName);
    printStream.print("<?xml version=\"1.0\" encoding=\"");
    printStream.print(encodingCanonicalName);
    printStream.println("\"?>");
    printStream.print("<!DOCTYPE properties SYSTEM \"");
    printStream.print(PROP_DTD_NAME);
    printStream.println("\">");
    printStream.println("<properties>");
    if (comment != null) {
        printStream.print("<comment>");
        printStream.print(substitutePredefinedEntries(comment));
        printStream.println("</comment>");
    }
    for (Map.Entry<Object, Object> entry : entrySet()) {
        String keyValue = (String) entry.getKey();
        String entryValue = (String) entry.getValue();
        printStream.print("<entry key=\"");
        printStream.print(substitutePredefinedEntries(keyValue));
        printStream.print("\">");
        printStream.print(substitutePredefinedEntries(entryValue));
        printStream.println("</entry>");
    }
    printStream.println("</properties>");
    printStream.flush();
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) PrintStream(java.io.PrintStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException)

Example 53 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project j2objc by google.

the class IllegalCharsetNameExceptionTest method testConstructor.

public void testConstructor() {
    IllegalCharsetNameException ex = new IllegalCharsetNameException("impossible");
    assertTrue(ex instanceof IllegalArgumentException);
    assertNull(ex.getCause());
    assertEquals(ex.getCharsetName(), "impossible");
    assertTrue(ex.getMessage().indexOf("impossible") != -1);
    ex = new IllegalCharsetNameException("ascii");
    assertNull(ex.getCause());
    assertEquals(ex.getCharsetName(), "ascii");
    assertTrue(ex.getMessage().indexOf("ascii") != -1);
    ex = new IllegalCharsetNameException("");
    assertNull(ex.getCause());
    assertEquals(ex.getCharsetName(), "");
    ex.getMessage();
    ex = new IllegalCharsetNameException(null);
    assertNull(ex.getCause());
    assertNull(ex.getCharsetName());
    assertTrue(ex.getMessage().indexOf("null") != -1);
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException)

Example 54 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project vinnie by mangstadt.

the class VObjectReader method getCharset.

/**
 * Gets the character set the given property is encoded in.
 * @param property the property
 * @param listener the data listener
 * @return the character set or null if the character is not set or could
 * not be determined
 */
private Charset getCharset(VObjectProperty property, VObjectDataListener listener) {
    Exception thrown;
    try {
        return property.getParameters().getCharset();
    } catch (IllegalCharsetNameException e) {
        // name contains illegal characters
        thrown = e;
    } catch (UnsupportedCharsetException e) {
        // not recognized by the JVM
        thrown = e;
    }
    listener.onWarning(Warning.UNKNOWN_CHARSET, property, thrown, context);
    return null;
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) DecoderException(com.github.mangstadt.vinnie.codec.DecoderException) IOException(java.io.IOException) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException)

Example 55 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project gerrit by GerritCodeReview.

the class Text method charset.

private static Charset charset(byte[] content, String encoding) {
    if (encoding == null) {
        UniversalDetector d = new UniversalDetector(null);
        d.handleData(content, 0, content.length);
        d.dataEnd();
        encoding = d.getDetectedCharset();
    }
    if (encoding == null) {
        return ISO_8859_1;
    }
    try {
        return Charset.forName(encoding);
    } catch (IllegalCharsetNameException err) {
        logger.atSevere().log("Invalid detected charset name '%s': %s", encoding, err.getMessage());
        return ISO_8859_1;
    } catch (UnsupportedCharsetException err) {
        logger.atSevere().log("Detected charset '%s' not supported: %s", encoding, err.getMessage());
        return ISO_8859_1;
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UniversalDetector(org.mozilla.universalchardet.UniversalDetector)

Aggregations

IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)69 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)44 Charset (java.nio.charset.Charset)34 IOException (java.io.IOException)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 File (java.io.File)9 ByteBuffer (java.nio.ByteBuffer)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 CharacterCodingException (java.nio.charset.CharacterCodingException)6 CoreException (org.eclipse.core.runtime.CoreException)6 IStatus (org.eclipse.core.runtime.IStatus)6 Status (org.eclipse.core.runtime.Status)6 HistoricallyNamedCharset (sun.nio.cs.HistoricallyNamedCharset)6 CharsetEncoder (java.nio.charset.CharsetEncoder)5 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)5 SequenceInputStream (java.io.SequenceInputStream)4 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)4 CharArrayWriter (java.io.CharArrayWriter)3 FileInputStream (java.io.FileInputStream)3