RdfXmlMessageBodyWriter extends AbstractRDFMessageBodyReaderWriter { @Override public void writeTo(Iterable<Node[]> t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { XMLStreamWriter xmlwriter; try { xmlwriter = _factory.createXMLStreamWriter(entityStream, UTF_8.name()); xmlwriter.writeStartDocument("UTF-8", "1.0"); xmlwriter.writeStartElement("rdf:RDF"); xmlwriter.writeNamespace("rdf", "http: } catch (XMLStreamException e) { _log.log(Level.WARNING, e.getMessage()); throw new ServerErrorException(500, e.getCause()); } for (Node[] na : t) { if (!(na[1] instanceof Resource)) { continue; } if (na[0] instanceof Literal) { continue; } try { xmlwriter.writeStartElement("rdf:Description"); if (na[0] instanceof Resource) { URI u = ((Resource)na[0]).toURI(); xmlwriter.writeAttribute("rdf:about", u.toString()); } else if (na[0] instanceof BNode) { xmlwriter.writeAttribute("rdf:nodeID", na[0].getLabel()); } String r = ((Resource) na[1]).getLabel(); String namespace = null, localname = null; int i = r.indexOf('#'); if (i > 0) { namespace = r.substring(0, i + 1); localname = r.substring(i + 1, r.length()); } else { i = r.lastIndexOf('/'); if (i > 0) { namespace = r.substring(0, i + 1); localname = r.substring(i + 1, r.length()); } } if (namespace == null || localname == null) { _log.log(Level.WARNING, "could not separate namespace and localname for {0}", na[1]); xmlwriter.writeEndElement(); continue; } xmlwriter.writeStartElement(namespace, localname); if (na[2] instanceof BNode) { xmlwriter.writeAttribute("rdf:nodeID", na[2].getLabel()); } else if (na[2] instanceof Resource) { URI u = ((Resource)na[2]).toURI(); xmlwriter.writeAttribute("rdf:resource", u.toString()); } else if (na[2] instanceof Literal) { Literal l = (Literal) na[2]; if (l.getLanguageTag() != null) { xmlwriter .writeAttribute("xml:lang", l.getLanguageTag()); } else if (l.getDatatype() != null) { xmlwriter.writeAttribute("rdf:datatype", l .getDatatype().getLabel()); } try { xmlwriter.writeCharacters(l.getLabel()); } catch (RuntimeException e) { _log.log(Level.WARNING, e.getMessage()); throw new ServerErrorException(500, e.getCause()); } } xmlwriter.writeEndElement(); xmlwriter.writeEndElement(); } catch (XMLStreamException e) { _log.log(Level.WARNING, e.getMessage()); throw new ServerErrorException(500, e.getCause()); } catch (URISyntaxException e) { _log.log(Level.WARNING, e.getMessage()); throw new ServerErrorException(500, e.getCause()); } } try { xmlwriter.writeEndElement(); xmlwriter.writeEndDocument(); xmlwriter.close(); } catch (XMLStreamException e) { _log.log(Level.WARNING, e.getMessage()); throw new ServerErrorException(500, e.getCause()); } }  @Override void writeTo(Iterable<Node[]> t, Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mediaType,
			MultivaluedMap<String, Object> httpHeaders,
			OutputStream entityStream); @Override Iterable<Node[]> readFrom(Class<Iterable<Node[]>> type,
			Type genericType, Annotation[] annotations, MediaType mediaType,
			MultivaluedMap<String, String> httpHeaders, InputStream entityStream); static final MediaType RDF_XML_MEDIATYPE; }
@Test public void test() throws WebApplicationException, IOException { log.log(Level.INFO, "Testing behaviour when having non-http uris or broken http uris as predicates."); String[] tripleStrings = new String[] { "_:bn1 <http: "_:bn2 <hmi-vr:x1> \"1.7726401064446645E-1\"^^<xsd:float> ." }; List<String> tripleStringList = Arrays.asList(tripleStrings); NxParser nxp = new NxParser(tripleStringList); RdfXmlMessageBodyWriter rxmbr = new RdfXmlMessageBodyWriter(); rxmbr.uriinfo = new UriInfo() { @Override public String getPath() { return null; } @Override public String getPath(boolean decode) { return null; } @Override public List<PathSegment> getPathSegments() { return null; } @Override public List<PathSegment> getPathSegments(boolean decode) { return null; } @Override public URI getRequestUri() { return null; } @Override public UriBuilder getRequestUriBuilder() { return null; } @Override public URI getAbsolutePath() { return null; } @Override public UriBuilder getAbsolutePathBuilder() { return null; } @Override public URI getBaseUri() { return null; } @Override public UriBuilder getBaseUriBuilder() { return null; } @Override public MultivaluedMap<String, String> getPathParameters() { return null; } @Override public MultivaluedMap<String, String> getPathParameters(boolean decode) { return null; } @Override public MultivaluedMap<String, String> getQueryParameters() { return null; } @Override public MultivaluedMap<String, String> getQueryParameters(boolean decode) { return null; } @Override public List<String> getMatchedURIs() { return null; } @Override public List<String> getMatchedURIs(boolean decode) { return null; } @Override public List<Object> getMatchedResources() { return null; } @Override public URI resolve(URI uri) { return null; } @Override public URI relativize(URI uri) { return URI.create("http: }}; rxmbr.writeTo(nxp, null, null, null, null, null, System.out); }
@Test public void numberTwoTest() throws WebApplicationException, IOException { log.log(Level.INFO, "Testing weird empty string resources."); Node[][] triples = new Node[][]{{new Resource(""), RDF.TYPE, RDFS.CLASS}}; RdfXmlMessageBodyWriter rxmbr = new RdfXmlMessageBodyWriter(); rxmbr.uriinfo = new UriInfo() { @Override public String getPath() { return null; } @Override public String getPath(boolean decode) { return null; } @Override public List<PathSegment> getPathSegments() { return null; } @Override public List<PathSegment> getPathSegments(boolean decode) { return null; } @Override public URI getRequestUri() { return URI.create("http: } @Override public UriBuilder getRequestUriBuilder() { return null; } @Override public URI getAbsolutePath() { return URI.create("http: } @Override public UriBuilder getAbsolutePathBuilder() { return null; } @Override public URI getBaseUri() { return URI.create("http: } @Override public UriBuilder getBaseUriBuilder() { return null; } @Override public MultivaluedMap<String, String> getPathParameters() { return null; } @Override public MultivaluedMap<String, String> getPathParameters(boolean decode) { return null; } @Override public MultivaluedMap<String, String> getQueryParameters() { return null; } @Override public MultivaluedMap<String, String> getQueryParameters(boolean decode) { return null; } @Override public List<String> getMatchedURIs() { return null; } @Override public List<String> getMatchedURIs(boolean decode) { return null; } @Override public List<Object> getMatchedResources() { return null; } @Override public URI resolve(URI uri) { return UriTemplate.resolve(getBaseUri(), uri); } @Override public URI relativize(URI uri) { if (!uri.isAbsolute()) { uri = resolve(uri); } return UriTemplate.relativize(getRequestUri(), uri); } }; final Resource emptyStringResource = new Resource(""); Resource requestResource = new Resource("<" + rxmbr.uriinfo.getAbsolutePath().toString() + ">", true); for (Node[] nx : triples) { if (emptyStringResource.equals(nx[0])) nx[0] = requestResource; } rxmbr.writeTo(Arrays.asList(triples), null, null, null, null, null, System.out); }