Skip to content

Commit

Permalink
[#561] Remove resource leaks in User Guide generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
prmr committed Jan 17, 2025
1 parent b7c7e56 commit fae4801
Showing 1 changed file with 65 additions and 46 deletions.
111 changes: 65 additions & 46 deletions src/org/jetuml/gui/tips/UserGuideGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,48 @@
* JetUML - A desktop application for fast UML diagramming.
*
* Copyright (C) 2021 by McGill University.
*
*
* See: https://github.com/prmr/JetUML
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses.
* You should have received a copy of the GNU General Public License along with
* this program. If not, see http://www.gnu.org/licenses.
*******************************************************************************/
package org.jetuml.gui.tips;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.lines;
import static java.nio.file.Files.write;
import static java.util.stream.Collectors.joining;
import static org.jetuml.application.ApplicationResources.RESOURCES;
import static org.jetuml.gui.tips.TipLoader.loadTip;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.stream.Stream;

import org.jetuml.gui.tips.TipLoader.Tip;

/**
* Script to generate the user guide by collecting all the tips of the day in a single page.
* The user guide is created by expanding a template md file with HTML content
* to show all the tips in collapsible elements.
* Script to generate the user guide by collecting all the tips of the day in a
* single page. The user guide is created by expanding a template md file with
* HTML content to show all the tips in collapsible elements.
*/
public final class UserGuideGenerator
{
Expand All @@ -59,13 +61,15 @@ public final class UserGuideGenerator
private static final String TEMPLATE_TEXT = "<p>$TEXT$</p>";
private static final String TEMPLATE_IMAGE = "<img src=\"../tipdata/tip_images/$TEXT$\">";
private static final String TEMPLATE_DIV_CLOSE = "</div>";

private static final Map<TipCategory, List<String>> TOPICS = new HashMap<>();
private static final Map<TipCategory, List<String>> LEVELS = new HashMap<>();
private static final Map<TipCategory, List<String>> DIAGRAMS = new HashMap<>();

private UserGuideGenerator() {}


private UserGuideGenerator()
{
}

/**
* Run without arguments.
*
Expand All @@ -79,13 +83,25 @@ public static void main(String[] pArgs) throws IOException
generateDiagrams();
System.out.println("The User Guide was generated sucessfully.");
}


private static String getContent(Path pPath)
{
try (Stream<String> stream = Files.lines(pPath, UTF_8))
{
return stream.collect(joining("\n"));
}
catch (IOException exception)
{
throw new UncheckedIOException(exception);
}
}

/*
* Generates the user guide organized by topics.
*/
private static void generateTopics() throws IOException
{
String template = lines(INPUT_FILE_TOPICS, UTF_8).collect(joining("\n"));
String template = getContent(INPUT_FILE_TOPICS);
List<String> tipCategories = new ArrayList<>();
for( TipCategory category : TipCategory.values() )
{
Expand All @@ -95,24 +111,24 @@ private static void generateTopics() throws IOException
}
}
template = template.replace(PLACEHOLDER, tipCategories.stream().collect(joining("\n")));

for( TipCategory category : TipCategory.values() )
{
if( category.getView() == View.TOPIC )
{
template = template.replace("$" + category.toString() + "$",
template = template.replace("$" + category.toString() + "$",
TOPICS.get(category).stream().collect(joining("\n")));
}
}
write(OUTPUT_FILE_TOPICS, template.getBytes(UTF_8));
}

/*
* Generates the user guide organized by levels.
*/
private static void generateLevels() throws IOException
{
String template = lines(INPUT_FILE_LEVELS, UTF_8).collect(joining("\n"));
{
String template = getContent(INPUT_FILE_LEVELS);
List<String> tipCategories = new ArrayList<>();
for( TipCategory category : TipCategory.values() )
{
Expand All @@ -122,24 +138,24 @@ private static void generateLevels() throws IOException
}
}
template = template.replace(PLACEHOLDER, tipCategories.stream().collect(joining("\n")));

for( TipCategory category : TipCategory.values() )
{
if( category.getView() == View.LEVEL )
{
template = template.replace("$" + category.toString() + "$",
template = template.replace("$" + category.toString() + "$",
LEVELS.get(category).stream().collect(joining("\n")));
}
}
write(OUTPUT_FILE_LEVELS, template.getBytes(UTF_8));
}

/*
* Generates the user guide organized by diagram type.
*/
private static void generateDiagrams() throws IOException
{
String template = lines(INPUT_FILE_DIAGRAMS, UTF_8).collect(joining("\n"));
String template = getContent(INPUT_FILE_DIAGRAMS);
List<String> tipCategories = new ArrayList<>();
for( TipCategory category : TipCategory.values() )
{
Expand All @@ -149,21 +165,21 @@ private static void generateDiagrams() throws IOException
}
}
template = template.replace(PLACEHOLDER, tipCategories.stream().collect(joining("\n")));

for( TipCategory category : TipCategory.values() )
{
if( category.getView() == View.DIAGRAM )
{
template = template.replace("$" + category.toString() + "$",
template = template.replace("$" + category.toString() + "$",
DIAGRAMS.get(category).stream().collect(joining("\n")));
}
}
write(OUTPUT_FILE_DIAGRAMS, template.getBytes(UTF_8));
}

/*
* Creates an html representation of all available tips,
* grouped by Views, and their respective categories in each View.
* Creates an html representation of all available tips, grouped by Views,
* and their respective categories in each View.
*/
private static void tipsAsHtml()
{
Expand Down Expand Up @@ -193,32 +209,34 @@ else if( category.getView() == View.LEVEL )
}
}
}
/*

/*
* Obtains the number of tips for the application's properties.
*/
private static int numberOfTips()
{
return Integer.parseInt(RESOURCES.getString("tips.quantity"));
}

/*
* Creates an html representation of pCategory suitable for display in the user guide.
* Creates an html representation of pCategory suitable for display in the
* user guide.
*/
private static String toHtml(String pCategory)
{
StringJoiner html = new StringJoiner("\n");
html.add(TEMPLATE_CATEGORY_BUTTON.replace(PLACEHOLDER,
html.add(TEMPLATE_CATEGORY_BUTTON.replace(PLACEHOLDER,
pCategory.substring(0, 1).toUpperCase() + pCategory.substring(1).toLowerCase()));
html.add(TEMPLATE_DIV_OPEN);
html.add("$" + pCategory + "$");
html.add(TEMPLATE_DIV_CLOSE);

return html.toString();
}

/*
* Creates an html representation of pTip suitable for display in the user guide.
* Creates an html representation of pTip suitable for display in the user
* guide.
*/
private static String toHtml(Tip pTip)
{
Expand All @@ -230,12 +248,13 @@ private static String toHtml(Tip pTip)
html.add(toHtml(element));
}
html.add(TEMPLATE_DIV_CLOSE);

return html.toString();
}

/*
* Creates an html representation of pTipElement suitable for display in the user guide.
* Creates an html representation of pTipElement suitable for display in the
* user guide.
*/
private static String toHtml(TipElement pTipElement)
{
Expand Down

0 comments on commit fae4801

Please sign in to comment.