use of net.sourceforge.plantuml.SourceStringReader in project plantuml-server by plantuml.
the class ProxyServlet method doGet.
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
final String fmt = request.getParameter("fmt");
final String source = request.getParameter("src");
final String index = request.getParameter("idx");
final URL srcUrl;
// Check if the src URL is valid
try {
srcUrl = new URL(source);
} catch (MalformedURLException mue) {
mue.printStackTrace();
return;
}
// generate the response
String diagmarkup = getSource(srcUrl);
SourceStringReader reader = new SourceStringReader(diagmarkup);
int n = index == null ? 0 : Integer.parseInt(index);
List<BlockUml> blocks = reader.getBlocks();
BlockUml block = blocks.get(n);
Diagram diagram = block.getDiagram();
UmlSource umlSrc = diagram.getSource();
String uml = umlSrc.getPlainString();
// System.out.println("uml=" + uml);
// generate the response
DiagramResponse dr = new DiagramResponse(response, getOutputFormat(fmt), request);
try {
dr.sendDiagram(uml, 0);
} catch (IIOException iioe) {
// Browser has closed the connection, so the HTTP OutputStream is closed
// Silently catch the exception to avoid annoying log
}
dr = null;
}
use of net.sourceforge.plantuml.SourceStringReader in project plantuml-server by plantuml.
the class DiagramResponse method sendDiagram.
void sendDiagram(String uml, int idx) throws IOException {
response.addHeader("Access-Control-Allow-Origin", "*");
response.setContentType(getContentType());
SourceStringReader reader = new SourceStringReader(uml);
if (format == FileFormat.BASE64) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final DiagramDescription result = reader.outputImage(baos, idx, new FileFormatOption(FileFormat.PNG));
baos.close();
final String encodedBytes = "data:image/png;base64," + Base64Coder.encodeLines(baos.toByteArray()).replaceAll("\\s", "");
response.getOutputStream().write(encodedBytes.getBytes());
return;
}
final BlockUml blockUml = reader.getBlocks().get(0);
if (notModified(blockUml)) {
addHeaderForCache(blockUml);
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
if (StringUtils.isDiagramCacheable(uml)) {
addHeaderForCache(blockUml);
}
final Diagram diagram = blockUml.getDiagram();
final ImageData result = diagram.exportDiagram(response.getOutputStream(), idx, new FileFormatOption(format));
}
use of net.sourceforge.plantuml.SourceStringReader in project plantuml-server by plantuml.
the class DiagramResponse method sendMap.
void sendMap(String uml) throws IOException {
response.setContentType(getContentType());
SourceStringReader reader = new SourceStringReader(uml);
final BlockUml blockUml = reader.getBlocks().get(0);
if (StringUtils.isDiagramCacheable(uml)) {
addHeaderForCache(blockUml);
}
final Diagram diagram = blockUml.getDiagram();
ImageData map = diagram.exportDiagram(new NullOutputStream(), 0, new FileFormatOption(FileFormat.PNG, false));
if (map.containsCMapData()) {
PrintWriter httpOut = response.getWriter();
final String cmap = map.getCMapData("plantuml");
httpOut.print(cmap);
}
}
use of net.sourceforge.plantuml.SourceStringReader in project netbeans-mmd-plugin by raydac.
the class PlantUmlTextEditor method exportAsFile.
private void exportAsFile() {
final JFileChooser fileChooser = new JFileChooser(lastExportedFile);
fileChooser.setAcceptAllFileFilterUsed(false);
final FileFilter fileFiterSVG = new FileFilter() {
@Override
public boolean accept(@Nonnull final File f) {
return f.isDirectory() || f.getName().toLowerCase(Locale.ENGLISH).endsWith(".svg");
}
@Nonnull
@Override
public String getDescription() {
return "SVG images (*.svg)";
}
};
final FileFilter fileFiterPNG = new FileFilter() {
@Override
public boolean accept(@Nonnull final File f) {
return f.isDirectory() || f.getName().toLowerCase(Locale.ENGLISH).endsWith(".png");
}
@Nonnull
@Override
public String getDescription() {
return "PNG images (*.png)";
}
};
final FileFilter fileFiterLTX = new FileFilter() {
@Override
public boolean accept(@Nonnull final File f) {
return f.isDirectory() || f.getName().toLowerCase(Locale.ENGLISH).endsWith(".tex");
}
@Nonnull
@Override
public String getDescription() {
return "LaTeX text files (*.tex)";
}
};
fileChooser.setApproveButtonText("Export");
fileChooser.setDialogTitle("Export PlantUML image as File");
fileChooser.setMultiSelectionEnabled(false);
fileChooser.addChoosableFileFilter(fileFiterPNG);
fileChooser.addChoosableFileFilter(fileFiterSVG);
fileChooser.addChoosableFileFilter(fileFiterLTX);
if (fileChooser.showSaveDialog(this.mainPanel) == JFileChooser.APPROVE_OPTION) {
lastExportedFile = fileChooser.getSelectedFile();
final FileFilter fileFilter = fileChooser.getFileFilter();
final SourceStringReader reader = new SourceStringReader(this.editor.getText());
final FileFormatOption option;
final String ext;
if (fileFilter == fileFiterSVG) {
option = new FileFormatOption(FileFormat.SVG);
ext = ".svg";
} else if (fileFilter == fileFiterPNG) {
option = new FileFormatOption(FileFormat.PNG);
ext = ".png";
} else if (fileFilter == fileFiterLTX) {
option = new FileFormatOption(FileFormat.LATEX);
ext = ".tex";
} else {
throw new Error("Unexpected situation");
}
final ByteArrayOutputStream buffer = new ByteArrayOutputStream(131072);
if (!lastExportedFile.getName().contains(".") || !lastExportedFile.getName().endsWith(ext)) {
lastExportedFile = new File(lastExportedFile.getParent(), lastExportedFile.getName() + ext);
}
try {
reader.outputImage(buffer, option);
FileUtils.writeByteArrayToFile(lastExportedFile, buffer.toByteArray());
LOGGER.info("Exported plant uml image as file : " + lastExportedFile);
} catch (Exception ex) {
LOGGER.error("Can't export plant uml image", ex);
JOptionPane.showMessageDialog(this.mainPanel, "Error during export, see log!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
use of net.sourceforge.plantuml.SourceStringReader in project netbeans-mmd-plugin by raydac.
the class PlantUmlTextEditor method renderCurrentTextInPlantUml.
private void renderCurrentTextInPlantUml() {
final String text = this.editor.getText();
final SourceStringReader reader = new SourceStringReader(text, "UTF-8");
final ByteArrayOutputStream buffer = new ByteArrayOutputStream(131072);
try {
reader.outputImage(buffer, new FileFormatOption(FileFormat.PNG, false));
this.imageComponent.setImage(ImageIO.read(new ByteArrayInputStream(buffer.toByteArray())));
this.renderedScrollPane.revalidate();
} catch (IOException e) {
LOGGER.error("Can't render plant uml", e);
this.renderedScrollPane.setViewportView(new JLabel("Error during rendering"));
}
}
Aggregations