Como baixar ou exportar arquivo xhtml gexf como XML em JSF?
Pergunta
Usei o formato de arquivo gexf na biblioteca JavaScript sigma.js para desenhar gráficos em JSF.
Salvei o modelo gexf na tag ui:fragment no arquivo graph.xhtml desta forma:
<?xml version="1.0" encoding="UTF-8"?>
<ui:fragment xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets">
<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2">
A biblioteca sigma.js usa o graph.xhtml desta forma:
sigInst.parseGexf('#{request.contextPath}/user/graph/graph.jsf');
O parograma usa um ManagedBean para inicializar valores de nós e arestas no arquivo graph.xhtml desta forma:
<ui:repeat value="#{graphInfoBean.edges}" var="edge" varStatus="indexVar">
<edge id="#{indexVar.index}" source="#{edge.source}" target="#{edge.target}"
Tudo está funcionando corretamente e o gráfico está desenhado.Quero que o usuário possa baixar/salvar o arquivo graph.xhtml no formato XML para poder usá-lo em outro software utilitário como o Gephi.
Como faço isso para converter o arquivo xhtml em xml?
Obrigado.
Solução
usar preRenderView
evento para alterar o cabeçalho da página:
<f:event type="preRenderView" listener="#{graphInfoBean.changeHeader}"/>
e a função changeHeader é:
public void changeHeader() {
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.setHeader("content-disposition", "attachment;filename=testFile.xml");
response.setContentType("text/xml");
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow