forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExport.java
More file actions
executable file
·89 lines (77 loc) · 2.58 KB
/
Export.java
File metadata and controls
executable file
·89 lines (77 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.mxgraph.examples.web;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.URLDecoder;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import org.mortbay.jetty.Request;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.mxgraph.reader.mxGraphViewImageReader;
/**
* This servlet may be used to create bitmap versions of the graphs
* using a high-level description of the visual appearance so there
* is no need to create an object representation of the model on
* the server-side. The description even allows the server to create
* the bitmaps using a SAX parser. A DOM parser is not required.
*
* To integrate the image handler with a client application, the client
* application must be setup to use this servlet. This can be done by
* setting mxEditor.urlImage programmatically or using a config file.
*/
public class Export extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = -4951624126588618796L;
/**
*
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
String xml = URLDecoder.decode(request.getParameter("xml"), "UTF-8").replace("\n", "
");
try
{
// Set the clip on the mxGraphViewImageReader or use
// image.getSubimage to create multiple image files
response.setContentType("image/png");
response.setHeader("Content-Disposition",
"attachment; filename=diagram.png");
response.setStatus(HttpServletResponse.SC_OK);
streamImage(Color.WHITE, xml, response.getOutputStream());
((Request) request).setHandled(true);
}
catch (Exception e)
{
throw new ServletException(e);
}
}
/**
* Streams the given XML string as a PNG image into the given stream.
*
* @param xml
* @param stream
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
protected void streamImage(Color bg, String xml, OutputStream stream)
throws ParserConfigurationException, SAXException, IOException
{
mxGraphViewImageReader reader = new mxGraphViewImageReader(bg, 4, true,
true);
InputSource inputSource = new InputSource(new StringReader(xml));
BufferedImage image = mxGraphViewImageReader.convert(inputSource,
reader);
ImageIO.write(image, "png", stream);
}
}