XMLWriter
インターフェイスには、目的のエンコーディング属性の設定がありますが、現在の実装ではRDFXMLRenderer
はこの属性を設定できません。 (これをバグと呼ぶかもしれません - 問題を提起したい場合はトラッカーはhereです - 修正はhereです)
XSLTの回避策は過剰なことですが、遅くなる可能性があります。
変更の範囲が非常に限られているので、私は1つの行だけを上書きするためのインターセプタを作成します。このような何か(未テスト):
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import org.semanticweb.owlapi.io.WriterDocumentTarget;
public class TestUTF8 {
public static void main(String[] args) {
try (Writer w = new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8)) {
WriterDocumentTarget t = new WriterDocumentTarget(new InterceptingWriter(w));
// save the ontology here
} catch (IOException e) {
e.printStackTrace();
}
}
}
class InterceptingWriter extends Writer {
private static final String XML_VERSION_1_0 = "<?xml version=\"1.0\"?>\n";
private static final String XML_VERSION_1_0_ENCODING_UTF_8 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
private Writer wrapped;
boolean beginning = true;
public InterceptingWriter(Writer wrapped) {
this.wrapped = wrapped;
}
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
wrapped.write(cbuf, off, len);
}
@Override
public void flush() throws IOException {
wrapped.flush();
}
@Override
public void close() throws IOException {
wrapped.close();
}
@Override
public void write(String str, int off, int len) throws IOException {
if (str.equals(XML_VERSION_1_0) && off == 0 && len == XML_VERSION_1_0.length()) {
wrapped.write(XML_VERSION_1_0_ENCODING_UTF_8, 0, XML_VERSION_1_0_ENCODING_UTF_8.length());
} else {
wrapped.write(str, off, len);
}
}
@Override
public void write(String str) throws IOException {
if (str.equals(XML_VERSION_1_0)) {
super.write(XML_VERSION_1_0_ENCODING_UTF_8);
} else {
super.write(str);
}
}
}
manager.saveOntology(オントロジー、(新しいRDFXMLDocumentFormatFactory())createFormat()、新WriterOutputStream(作家、Charset.forName( "UTF-8"))); – Galigator
出力XMLのヘッダーに 'encoding = "UTF-8"を追加しません。 –
属性を追加しても、出力ファイルUTF-8は作成されません。 OWLAPIはデフォルトでUTF-8エンコーディングで既に保存されています。 – Ignazio