Search in sources :

Example 1 with SourceStringReader

use of net.sourceforge.plantuml.SourceStringReader in project markdown-doclet by Abnaxos.

the class UmlTagRenderer method render.

@Override
public void render(Tag tag, StringBuilder target, MarkdownDoclet doclet) {
    if (config == null) {
        if (doclet.getOptions().getPlantUmlConfigFile() != null) {
            try {
                config = Collections.singletonList(Files.toString(doclet.getOptions().getPlantUmlConfigFile(), doclet.getOptions().getEncoding()));
            } catch (IOException e) {
                doclet.printError("Error loading PlantUML configuration file " + doclet.getOptions().getPlantUmlConfigFile() + ": " + e.getLocalizedMessage());
            }
        } else {
            config = Collections.emptyList();
        }
    }
    String packageName;
    if (tag.holder() instanceof ProgramElementDoc) {
        packageName = ((ProgramElementDoc) tag.holder()).containingPackage().name();
    } else if (tag.holder() instanceof PackageDoc) {
        packageName = ((PackageDoc) (tag.holder())).name();
    } else if (tag.holder() instanceof RootDoc) {
        packageName = null;
    } else {
        doclet.printError(tag.position(), "Cannot handle tag for holder " + tag.holder());
        return;
    }
    String source = tag.text().trim();
    int pos = CharMatcher.WHITESPACE.indexIn(source);
    if (pos < 0) {
        doclet.printError(tag.position(), "Invalid @startuml tag: Expected filename and PlantUML source");
        return;
    }
    String fileName = source.substring(0, pos);
    source = "@startuml " + fileName + "\n" + source.substring(pos).trim() + "\n@enduml";
    File outputFile;
    if (packageName == null) {
        outputFile = doclet.getOptions().getDestinationDir();
    } else {
        outputFile = new File(doclet.getOptions().getDestinationDir(), packageName.replace(".", File.separator));
    }
    outputFile.mkdirs();
    outputFile = new File(outputFile, fileName.replace("/", File.separator));
    doclet.printNotice("Generating UML diagram " + outputFile);
    // render
    SourceStringReader reader = new SourceStringReader(new Defines(), source, config);
    try {
        reader.generateImage(outputFile);
    } catch (IOException e) {
        doclet.printError(tag.position(), "Error generating UML image " + outputFile + ": " + e.getLocalizedMessage());
    }
}
Also used : PackageDoc(com.sun.javadoc.PackageDoc) ProgramElementDoc(com.sun.javadoc.ProgramElementDoc) Defines(net.sourceforge.plantuml.preproc.Defines) SourceStringReader(net.sourceforge.plantuml.SourceStringReader) IOException(java.io.IOException) RootDoc(com.sun.javadoc.RootDoc) File(java.io.File)

Example 2 with SourceStringReader

use of net.sourceforge.plantuml.SourceStringReader in project plantuml-server by plantuml.

the class OldProxyServlet method handleImageProxy.

private void handleImageProxy(HttpServletResponse response, String num, String source) throws IOException {
    SourceStringReader reader = new SourceStringReader(getSource(source));
    int n = num == null ? 0 : Integer.parseInt(num);
    reader.generateImage(response.getOutputStream(), n, new FileFormatOption(getOutputFormat(), false));
}
Also used : FileFormatOption(net.sourceforge.plantuml.FileFormatOption) SourceStringReader(net.sourceforge.plantuml.SourceStringReader)

Example 3 with SourceStringReader

use of net.sourceforge.plantuml.SourceStringReader in project plantuml-server by plantuml.

the class DiagramResponse method sendCheck.

void sendCheck(String uml) throws IOException {
    response.setContentType(getContentType());
    SourceStringReader reader = new SourceStringReader(uml);
    DiagramDescription desc = reader.outputImage(new NullOutputStream(), new FileFormatOption(FileFormat.PNG, false));
    PrintWriter httpOut = response.getWriter();
    httpOut.print(desc.getDescription());
}
Also used : FileFormatOption(net.sourceforge.plantuml.FileFormatOption) SourceStringReader(net.sourceforge.plantuml.SourceStringReader) DiagramDescription(net.sourceforge.plantuml.core.DiagramDescription) NullOutputStream(net.sourceforge.plantuml.NullOutputStream) PrintWriter(java.io.PrintWriter)

Example 4 with SourceStringReader

use of net.sourceforge.plantuml.SourceStringReader in project ArTEMiS by ls1intum.

the class PlantUmlResource method generatePng.

/**
 * Generate PNG diagram for given PlantUML commands
 *
 * @param plantuml PlantUML command(s)
 * @return ResponseEntity PNG stream
 * @throws IOException
 */
@GetMapping(value = "/plantuml/png")
public ResponseEntity<byte[]> generatePng(@RequestParam("plantuml") String plantuml) throws IOException {
    // Create PNG output stream
    ByteArrayOutputStream png = new ByteArrayOutputStream();
    // Create input stream reader
    SourceStringReader reader = new SourceStringReader(plantuml);
    // Create PNG
    String dest = reader.generateImage(png);
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.IMAGE_PNG);
    return new ResponseEntity(png.toByteArray(), responseHeaders, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) SourceStringReader(net.sourceforge.plantuml.SourceStringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 5 with SourceStringReader

use of net.sourceforge.plantuml.SourceStringReader in project markdown-doclet by Abnaxos.

the class DocCommentProcessor method generateUmlDiagrams.

/**
 * Generates all PlantUML diagrams in the given `PsiDocComment`. It returns a Map of
 * file names and the URLs where the image for that file has been saved to. Use this
 * URL for the `<img>` tag.
 *
 * @param docComment    The `PsiDocComment`.
 *
 * @return A map mapping the file names to the image URLs.
 *
 * @see TempFileManager#saveTempFile(byte[], String)
 */
private Map<String, URL> generateUmlDiagrams(PsiDocComment docComment) {
    TempFileManager tempFiles = Plugin.tempFileManager();
    Map<String, URL> urls = null;
    for (PsiDocTag tag : docComment.getTags()) {
        if (tag instanceof PsiInlineDocTag) {
            continue;
        } else if (tag.getName().equals("uml") || tag.getName().equals("startuml")) {
            if (urls == null) {
                urls = new HashMap<>();
            }
            String text = stripLead(tag.getText());
            text = stripFirstWord(text)[1];
            String[] stripped = stripFirstWord(text);
            String fileName = stripped[0];
            text = stripped[1];
            String plantUml = "@startuml " + fileName + "\n" + "skinparam backgroundColor transparent\n" + text + "\n@enduml";
            Plugin.print("UML Diagram Source", plantUml);
            ByteArrayOutputStream image = new ByteArrayOutputStream();
            try {
                new SourceStringReader(plantUml).generateImage(image);
            } catch (IOException e) {
                LOG.error("Error generating UML", e, fileName, String.valueOf(docComment.toString()), String.valueOf(docComment.getContainingFile()));
            }
            try {
                urls.put(fileName, tempFiles.saveTempFile(image.toByteArray(), "png"));
            } catch (IOException e) {
                LOG.error("Error generating UML", e, fileName, String.valueOf(docComment.toString()), String.valueOf(docComment.getContainingFile()));
            }
        }
    }
    return MoreObjects.firstNonNull(urls, Collections.<String, URL>emptyMap());
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) PsiInlineDocTag(com.intellij.psi.javadoc.PsiInlineDocTag) HashMap(java.util.HashMap) SourceStringReader(net.sourceforge.plantuml.SourceStringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

SourceStringReader (net.sourceforge.plantuml.SourceStringReader)10 FileFormatOption (net.sourceforge.plantuml.FileFormatOption)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 IOException (java.io.IOException)4 BlockUml (net.sourceforge.plantuml.BlockUml)3 Diagram (net.sourceforge.plantuml.core.Diagram)3 File (java.io.File)2 PrintWriter (java.io.PrintWriter)2 URL (java.net.URL)2 NullOutputStream (net.sourceforge.plantuml.NullOutputStream)2 DiagramDescription (net.sourceforge.plantuml.core.DiagramDescription)2 ImageData (net.sourceforge.plantuml.core.ImageData)2 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)1 PsiInlineDocTag (com.intellij.psi.javadoc.PsiInlineDocTag)1 PackageDoc (com.sun.javadoc.PackageDoc)1 ProgramElementDoc (com.sun.javadoc.ProgramElementDoc)1 RootDoc (com.sun.javadoc.RootDoc)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1