1

我有一个 OWLOntology,我需要使用 RDFXMLDocumentFormat 将其保存到文件中,并且我想将其编码为 UTF-8。具体来说,我希望文件在顶部有以下内容:

<?xml version="1.0" encoding="UTF-8"?>

当然,我可以将 OWLOntology(使用 RDFXMLDocumentFormat)保存到 ByteArrayOutputStream,使用该输出流中的字符串创建 XML 文档,然后使用编码设置为 UTF-8 的转换器将该 XML 文档写入文件;但是,这在大型本体上表现不佳,因为它将被写入输出流,然后读回然后再次写出。

在 API 中,我确实查看了允许我设置编码的 RDFXMLWriter,并且似乎 RDFXMLStorer 在存储本体时使用了它。但是,我看不到如何访问 RDFXMLWriter 来指定所需的编码。

有没有办法做到这一点,我错过了?

4

1 回答 1

1

XMLWriter接口具有所需编码属性的设置器,但当前的实现RDFXMLRenderer不允许设置此属性。(你可以称之为错误——如果你想提出问题,跟踪器就在这里——修复就在这里

正如您所说,使用 XSLT 的一种解决方法是矫枉过正,最终可能会很慢。

由于更改的范围非常有限,我要做的是编写一个拦截器来覆盖一行。像这样的东西(未经测试):

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);
        }
    }
}
于 2017-03-09T21:57:25.697 回答