{
/* Need to ensure that the configuration object is not shared
* any more; otherwise later changes via factory could be
* visible half-way through output...
*/
WriterConfig cfg = mConfig.createNonShared();
XmlWriter xw;
if (w == null) {
if (enc == null) {
enc = WstxOutputProperties.DEFAULT_OUTPUT_ENCODING;
} else {
enc = CharsetNames.normalize(enc);
}
try {
if (enc == CharsetNames.CS_UTF8) {
/* 16-Aug-2006, TSa: Note: utf8 writer may or may not
* need to close the stream it has, but buffering
* xml writer must call close on utf8 writer. Thus:
*/
w = new UTF8Writer(cfg, out, autoCloseOutput);
xw = new BufferingXmlWriter(w, cfg, enc, true, out);
//xw = new ISOLatin1XmlWriter(out, cfg);
} else if (enc == CharsetNames.CS_ISO_LATIN1) {
xw = new ISOLatin1XmlWriter(out, cfg, autoCloseOutput);
/* 15-Dec-2006, TSa: For now, let's just use the
* default buffering writer for Ascii: while it's
* easy to create one from Latin1, it should be
* done after some refactorings are done to Latin1,
* not right now.
*/
/*
} else if (enc == CharsetNames.CS_US_ASCII) {
// !!! TBI
xw = new ISOLatin1XmlWriter(out, cfg, autoCloseOutput);
*/
} else {
w = new OutputStreamWriter(out, enc);
xw = new BufferingXmlWriter(w, cfg, enc, autoCloseOutput, out);
}
} catch (IOException ex) {
throw new XMLStreamException(ex);
}
} else {
// we may still be able to figure out the encoding:
if (enc == null) {
if (w instanceof OutputStreamWriter) {
enc = ((OutputStreamWriter) w).getEncoding();
}
}
try {
xw = new BufferingXmlWriter(w, cfg, enc, autoCloseOutput, out);
} catch (IOException ex) {
throw new XMLStreamException(ex);
}
}
if (cfg.willSupportNamespaces()) {
if (cfg.automaticNamespacesEnabled()) {
return new RepairingNsStreamWriter(xw, enc, cfg);
}
return new SimpleNsStreamWriter(xw, enc, cfg);
}
return new NonNsStreamWriter(xw, enc, cfg);