2009-04-03 7 views
0

javaのxmlファイルにxml:base宣言を追加します。私は現在いくつかのサードパーティのコードによって生成されたOutputStreamにxml出力を持っています。xml:baseをjavaのxmlファイルに追加します

<rdf:RDF 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:owl="http://www.w3.org/2002/07/owl#" 
    xmlns="http://www.mycompany.com/myNS#" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#" 
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"> 

そして私は、それは次のようになりたい::

ファイルは次のようにアウトを開始

<rdf:RDF 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:owl="http://www.w3.org/2002/07/owl#" 
    xmlns="http://www.mycompany.com/myNS#" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#" 
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" 
    xml:base="http://www.mycompany.com/myNS"> 

私は考えることができないので、私は、脳のおならか何かを持っなければなりませんこれを実用的に行う良い方法があります。

アイデア?

答えて

-1

ByteArrayInputStreamは大きなファイルではスケールされません。私は一時ファイルを使用する考えが嫌いでした。私はまた、​​タグを追加するだけでファイル全体をDOMにロードするのは大変だと考えました。

ここでは、パイプと単純な手書きの解析コードを使用してタグを追加する代わりの方法を示します。

PipedInputStream pipedInput = new PipedInputStream(); 
PipedOutputStream pipedOutput = new PipedOutputStream(pipedInput); 
new Thread(new ModelExportThread(model, pipedOutput)).start(); 
int bufferSize = 1024; 
byte[] bytes = new byte[bufferSize];    
StringBuffer stringBuffer = new StringBuffer(); 
int bytesRead = pipedInput.read(bytes, 0, bufferSize); 
boolean done = false; 
String startRDF = "<rdf:RDF"; 
while (bytesRead > 0) { 
    if (!done) { 
     stringBuffer.append(new String(bytes, 0, bytesRead)); 
     int startIndex = stringBuffer.indexOf(startRDF); 
     if ((startIndex >= 0)) { 
      stringBuffer.insert(startIndex + startRDF.length(), " xml:base=\"" + namespace + "\""); 
      outputStream.write(stringBuffer.toString().getBytes()); 
      stringBuffer.setLength(0); 
      done = true; 
     } 
    } else { 
     outputStream.write(bytes, 0, bytesRead); 
    } 
    bytesRead = pipedInput.read(bytes, 0, bufferSize); 
} 
outputStream.flush(); 

出力パイプに書き込むためのスレッドコードです。

public class ModelExportThread implements Runnable { 

    private final OntModel model; 
    private final OutputStream outputStream; 

    public ModelExportThread(OntModel model, OutputStream outputStream) { 
     this.model = model; 
     this.outputStream = outputStream; 
    } 

    public void run() { 
     try { 
      model.write(outputStream, "RDF/XML-ABBREV"); 
      outputStream.flush(); 
      outputStream.close(); 
     } catch (IOException ex) { 
      Logger.getLogger(OntologyModel.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

} 
-1

いくつかの掘削の後、これは私がやったことです。

注:サードパーティのアプリケーションでは、 'writer'という名前の出力ストリームではなく、StringWriterにxmlを書き込みました。 'outputStream'は結果のXMLが書き込まれるストリームの名前です。

ByteArrayInputStream inputStream = new ByteArrayInputStream(writer.toString().getBytes()); 
Document myXML = 
    DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); 
myXML.getDocumentElement().setAttribute("xml:base", namespace); 
Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
StreamResult result = new StreamResult(outputStream); 
DOMSource source = new DOMSource(myXML); 
transformer.transform(source, result); 

本当にこれは簡単だと思いました。

+0

これはあなたが投稿した他の回答よりも短く、これよりも簡単です。モデルを読み終えたら、ライターの 'xmlbase'プロパティを設定し、ライターを使ってモデルを書きます。私はこれを示す追加[回答](http://stackoverflow.com/a/18987916/1281433)。 –

1

あなたは適切なRDFWriterを取得し、あなたの選ばれたxmlbaseにそのxmlbaseプロパティを設定することにより、RDF/XMLシリアル化で使用される​​を変更することができます。次のコードは、文字列からモデルを読み込みます(この質問の重要な部分は、frmという形式ではなく、モデルを記述する方法です)、RDF/XMLで2回、毎回異なる​​を使用して書き込みます。出力され

import java.io.ByteArrayInputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import com.hp.hpl.jena.rdf.model.Model; 
import com.hp.hpl.jena.rdf.model.ModelFactory; 
import com.hp.hpl.jena.rdf.model.RDFWriter; 

public class ChangeBase { 
    public static void main(String[] args) throws IOException { 
     final String NS = "http://example.org/"; 
     final String text = "" + 
       "@prefix ex: <"+NS+">.\n" + 
       "ex:foo a ex:Foo .\n" + 
       "ex:foo ex:frob ex:bar.\n"; 
     final Model model = ModelFactory.createDefaultModel(); 
     try (final InputStream in = new ByteArrayInputStream(text.getBytes())) { 
      model.read(in, null, "TTL"); 
     } 
     // get a writer for RDF/XML-ABBREV, set its xmlbase to the NS, and write the model 
     RDFWriter writer = model.getWriter("RDF/XML-ABBREV"); 
     writer.setProperty("xmlbase", NS); 
     writer.write(model, System.out, null); 

     // change the base to example.com (.com, not .org) and write again 
     writer.setProperty("xmlbase", "http://example.com"); 
     writer.write(model, System.out, null); 
    } 
} 

(最初のケースでは、塩基はhtttp://example.org/であり、第二に、それは(差は.ORG対.COMである)http://example.comだということに気づく:

<rdf:RDF 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:ex="http://example.org/" 
    xml:base="http://example.org/"> 
    <ex:Foo rdf:about="foo"> 
    <ex:frob rdf:resource="bar"/> 
    </ex:Foo> 
</rdf:RDF> 
<rdf:RDF 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:ex="http://example.org/" 
    xml:base="http://example.com"> 
    <ex:Foo rdf:about="http://example.org/foo"> 
    <ex:frob rdf:resource="http://example.org/bar"/> 
    </ex:Foo> 
</rdf:RDF> 
関連する問題