add xml:base to xml file in java
Question
I want to add an xml:base declaration to an xml file in java. I currently have the xml output in an OutputStream that was generated by some third party code.
The file starts out like this:
<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#">
And I want it to look like this:
<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">
I must be having a brain fart or something, because I can't think of a good way to do this pragmatically.
Any ideas?
Solution 2
The ByteArrayInputStream won't scale for large files, and I didn't like the idea of using a temp file. I also thought it was overkill to load the whole file into the DOM just to add the xml:base
tag.
Here's an alternate solution using pipes and a simple hand rolled parsing code to add the tag.
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();
Here's the threaded code to write to the output pipe.
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);
}
}
}
OTHER TIPS
You can change the xml:base
used in RDF/XML serialization by obtaining the appropriate RDFWriter and setting its xmlbase
property to your chosen xmlbase
. The following code reads a model from a string (the important part of this question is about how to write the model, not where it comes frm) and then writes it in RDF/XML twice, each time with a different xml:base
.
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 );
}
}
The output is (notice that in the first case, the base is htttp://example.org/
and in the second, it's http://example.com
(the difference is .org vs. .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>
After some digging, this is what I did.
NOTE: I had the third party app write the xml to a StringWriter instead of an output stream named 'writer'. 'outputStream' is the name of the stream the resulting XML will be written to.
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);
I really thought this would be easier.