2017-03-08 4 views
1

J'ai un OWLOntology que je dois enregistrer dans un fichier en utilisant RDFXMLDocumentFormat, et je voudrais le coder comme UTF-8. Plus précisément, je voudrais que le fichier pour que le suivant en haut:Dans OWLAPI, comment écrire RDF/XML, y compris le codage UTF-8 dans l'en-tête

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

Bien sûr, je pourrais sauver le OWLOntology (en utilisant RDFXMLDocumentFormat) à un ByteArrayOutputStream, créez un document XML en utilisant la chaîne à partir de ce flux de sortie, puis écrivez ce document XML dans un fichier à l'aide d'un Transformer sur lequel le codage est défini sur UTF-8; cependant, cela fonctionnerait mal sur une grande ontologie, car il serait écrit dans un flux de sortie, puis relu, puis réécrit.

Dans l'API, j'ai regardé le RDFXMLWriter qui me permettrait de définir le codage, et il semble que cela soit utilisé par le RDFXMLStorer lorsqu'il stocke l'ontologie. Cependant, je ne vois pas comment je peux accéder au RDFXMLWriter pour spécifier le codage désiré.

Y a-t-il un moyen de faire cela qui me manque?

+0

manager.saveOntology (. Ontologie, (nouvelle RDFXMLDocumentFormatFactory()) createFormat(), nouveau WriterOutputStream (écrivain, Charset.forName ("UTF-8"))); – Galigator

+0

Cela n'ajoute pas 'encoding = "UTF-8"' à l'en-tête de la sortie XML. –

+0

Ajout de l'attribut ne fait pas le fichier de sortie UTF-8, il déclare juste. OWLAPI enregistre déjà avec l'encodage UTF-8 par défaut. – Ignazio

Répondre

1

L'interface XMLWriter possède un setter pour l'attribut de codage souhaité, mais l'implémentation actuelle de RDFXMLRenderer ne permet pas de définir cet attribut. (On pourrait appeler cela un bug - si vous souhaitez soulever une question, le tracker est here - le correctif est here)

Une solution de contournement avec XSLT est, comme vous le dites, un surpuissant, et pourrait finir par être lent.

Étant donné que la portée de la modification est très limitée, j'écris un intercepteur pour écraser seulement une ligne. Quelque chose comme ça (non testé):

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); 
     } 
    } 
}