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>
* <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
* </pre>
*
*<p>If the specified comment is <code>null</code> then no comment
* will be stored in the document.
*
* <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</code>
* 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 NullPointerException if <code>os</code> is <code>null</code>,
* or if <code>encoding</code> is <code>null</code>.
* @throws ClassCastException if this <code>Properties</code> object
* contains any keys or values that are not
* <code>Strings</code>.
* @see #loadFromXML(InputStream)
* @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();
}
use of java.nio.charset.IllegalCharsetNameException in project robovm by robovm.
the class Properties method storeToXML.
/**
* Writes all properties stored in this instance into the {@code
* OutputStream} in XML representation. The DOCTYPE is
*
* <pre>
* <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
* </pre>
*
* If the comment is null, no comment is added to the output. The parameter
* {@code encoding} defines which encoding should be used. The {@code
* OutputStream} is not closed at the end.
*
* @param os the {@code OutputStream} to write to.
* @param comment the comment to add. If null, no comment is added.
* @param encoding the code identifying the encoding that should be used to
* write into the {@code OutputStream}.
* @throws IOException if an error occurs during writing to the output.
*/
public synchronized 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();
}
use of java.nio.charset.IllegalCharsetNameException in project intellij-community by JetBrains.
the class JdkUtil method appendEncoding.
private static void appendEncoding(SimpleJavaParameters javaParameters, GeneralCommandLine commandLine, ParametersList parametersList) {
// Value of file.encoding and charset of GeneralCommandLine should be in sync in order process's input and output be correctly handled.
String encoding = parametersList.getPropertyValue("file.encoding");
if (encoding == null) {
Charset charset = javaParameters.getCharset();
if (charset == null)
charset = EncodingManager.getInstance().getDefaultCharset();
commandLine.addParameter("-Dfile.encoding=" + charset.name());
commandLine.withCharset(charset);
} else {
try {
Charset charset = Charset.forName(encoding);
commandLine.withCharset(charset);
} catch (UnsupportedCharsetException | IllegalCharsetNameException ignore) {
}
}
}
use of java.nio.charset.IllegalCharsetNameException in project XobotOS by xamarin.
the class Properties method storeToXML.
/**
* Writes all properties stored in this instance into the {@code
* OutputStream} in XML representation. The DOCTYPE is
*
* <pre>
* <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
* </pre>
*
* If the comment is null, no comment is added to the output. The parameter
* {@code encoding} defines which encoding should be used. The {@code
* OutputStream} is not closed at the end.
*
* @param os the {@code OutputStream} to write to.
* @param comment the comment to add. If null, no comment is added.
* @param encoding the code identifying the encoding that should be used to
* write into the {@code OutputStream}.
* @throws IOException if an error occurs during writing to the output.
*/
public synchronized void storeToXML(OutputStream os, String comment, String encoding) throws IOException {
if (os == null || encoding == null) {
throw new NullPointerException();
}
/*
* 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();
}
use of java.nio.charset.IllegalCharsetNameException in project intellij-community by JetBrains.
the class BinaryContent method getDocument.
@Override
@Nullable
public Document getDocument() {
if (myDocument == null) {
if (isBinary())
return null;
String text = null;
try {
Charset charset = ObjectUtils.notNull(myCharset, EncodingProjectManager.getInstance(myProject).getDefaultCharset());
text = CharsetToolkit.bytesToString(myBytes, charset);
} catch (IllegalCharsetNameException ignored) {
}
// Decode a string using the truly default encoding.
if (text == null)
text = new String(myBytes);
text = LineTokenizer.correctLineSeparators(text);
myDocument = EditorFactory.getInstance().createDocument(text);
myDocument.setReadOnly(true);
}
return myDocument;
}
Aggregations