【问题标题】:In OWLAPI, how to write RDF/XML including UTF-8 encoding in header在 OWLAPI 中,如何在标头中编写包含 UTF-8 编码的 RDF/XML
【发布时间】:2017-03-08 03:33:01
【问题描述】:

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

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

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

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

有没有我想念的方法来做到这一点?

【问题讨论】:

  • manager.saveOntology(ontology, (new RDFXMLDocumentFormatFactory()).createFormat(), new WriterOutputStream(writer, Charset.forName("UTF-8")));
  • 这不会在输出 XML 的标头中添加 'encoding="UTF-8"'。
  • 添加属性不会使输出文件成为 UTF-8,它只是声明它。 OWLAPI 已经默认使用 UTF-8 编码保存。
  • 我很抱歉,让编码出现在标题中是我的目标(现在反映在问题标题中)。

标签: utf-8 semantic-web ontology owl-api rdf-xml


【解决方案1】:

XMLWriter 接口具有所需编码属性的设置器,但RDFXMLRenderer 的当前实现不允许设置此属性。 (您可以将此称为错误 - 如果您想提出问题,跟踪器是 here - 修复是 here

如您所说,使用 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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-15
    • 2011-05-09
    • 2015-05-12
    • 2012-11-07
    • 2018-05-11
    • 2010-12-26
    • 1970-01-01
    • 2021-04-04
    相关资源
    最近更新 更多