From 7d7bdca9309eb2010d30ddc32f4298fe34efb97c Mon Sep 17 00:00:00 2001 From: Maxime Sinclair Date: Thu, 22 Aug 2013 14:31:05 +0200 Subject: [PATCH] [FEATURE] Map usage integrated in the interactive servlet JSTL (without EL) added --- pom.xml | 5 +++ .../plantuml/servlet/PlantUmlServlet.java | 6 +++ .../servlet/utility/SourceInformation.java | 40 +++++++++++++++++++ .../plantuml/servlet/utility/package.html | 1 + src/main/webapp/index.jsp | 18 +++++++-- .../plantuml/servlet/TestForm.java | 20 ++++++++++ 6 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/sourceforge/plantuml/servlet/utility/SourceInformation.java diff --git a/pom.xml b/pom.xml index 7aee33a..c5b2510 100644 --- a/pom.xml +++ b/pom.xml @@ -75,6 +75,11 @@ plantuml LATEST + + javax.servlet + jstl + 1.2 + HTTPClient HTTPClient diff --git a/src/main/java/net/sourceforge/plantuml/servlet/PlantUmlServlet.java b/src/main/java/net/sourceforge/plantuml/servlet/PlantUmlServlet.java index 0539838..88da8b8 100644 --- a/src/main/java/net/sourceforge/plantuml/servlet/PlantUmlServlet.java +++ b/src/main/java/net/sourceforge/plantuml/servlet/PlantUmlServlet.java @@ -42,6 +42,7 @@ import net.sourceforge.plantuml.SourceStringReader; import net.sourceforge.plantuml.StringUtils; import net.sourceforge.plantuml.code.Transcoder; import net.sourceforge.plantuml.code.TranscoderUtil; +import net.sourceforge.plantuml.servlet.utility.SourceInformation; import HTTPClient.CookieModule; import HTTPClient.HTTPConnection; import HTTPClient.HTTPResponse; @@ -141,6 +142,11 @@ public class PlantUmlServlet extends HttpServlet { request.setAttribute("net.sourceforge.plantuml.servlet.decoded", text); request.setAttribute("net.sourceforge.plantuml.servlet.encoded", encoded); + + // check if an image map is necessary + if (text != null && SourceInformation.containsLink(text)) { + request.setAttribute("net.sourceforge.plantuml.servlet.mapneeded", Boolean.TRUE); + } // forward to index.jsp RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp"); diff --git a/src/main/java/net/sourceforge/plantuml/servlet/utility/SourceInformation.java b/src/main/java/net/sourceforge/plantuml/servlet/utility/SourceInformation.java new file mode 100644 index 0000000..7565fb6 --- /dev/null +++ b/src/main/java/net/sourceforge/plantuml/servlet/utility/SourceInformation.java @@ -0,0 +1,40 @@ +/* ======================================================================== + * PlantUML : a free UML diagram generator + * ======================================================================== + * + * Project Info: http://plantuml.sourceforge.net + * + * This file is part of PlantUML. + * + * PlantUML 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. + * + * PlantUML 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 Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + */ +package net.sourceforge.plantuml.servlet.utility; + +/** + * Utility class giving information about a diagram based on its source description. + */ +public class SourceInformation { + /* + * Check if there is a link ([[url]]) in the source of the diagram. + * @param source the full textual representation of a diagram + * @return true if there is at least one link + */ + public static boolean containsLink(String source) { + // TODO build a better implementation + return source.contains("[["); + } + +} diff --git a/src/main/java/net/sourceforge/plantuml/servlet/utility/package.html b/src/main/java/net/sourceforge/plantuml/servlet/utility/package.html index 570e7b4..0ce2b66 100644 --- a/src/main/java/net/sourceforge/plantuml/servlet/utility/package.html +++ b/src/main/java/net/sourceforge/plantuml/servlet/utility/package.html @@ -4,6 +4,7 @@ \ No newline at end of file diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 1b3c0fe..f1bc3a3 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -1,5 +1,5 @@ <%@ page info="index" contentType="text/html; charset=utf-8" pageEncoding="utf-8" session="false" %> - +<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %> <% String contextRoot = request.getContextPath(); String host = "http://" + request.getServerName() + ":" + request.getServerPort(); @@ -8,6 +8,8 @@ String umltext = ""; String imgurl = ""; String svgurl = ""; String txturl = ""; +String mapurl = ""; +Object mapNeeded = request.getAttribute("net.sourceforge.plantuml.servlet.mapneeded"); Object encodedAttribute = request.getAttribute("net.sourceforge.plantuml.servlet.encoded"); if (encodedAttribute != null) { encoded = encodedAttribute.toString(); @@ -15,6 +17,9 @@ if (encodedAttribute != null) { imgurl = host + contextRoot + "/img/" + encoded; svgurl = host + contextRoot + "/svg/" + encoded; txturl = host + contextRoot + "/txt/" + encoded; + if (mapNeeded != null) { + mapurl = host + contextRoot + "/map/" + encoded; + } } } Object decodedAttribute = request.getAttribute("net.sourceforge.plantuml.servlet.decoded"); @@ -59,12 +64,19 @@ if (decodedAttribute != null) {

- <% if ( !imgurl.isEmpty()) { %> + <% if (!imgurl.isEmpty()) { %>
View as SVG  View as ASCII Art

- PlantUML diagram + <% if (mapNeeded != null) { %> + PlantUML diagram + + + + <% } else { %> + PlantUML diagram + <% } %>

<% } //endif %> diff --git a/src/test/java/net/sourceforge/plantuml/servlet/TestForm.java b/src/test/java/net/sourceforge/plantuml/servlet/TestForm.java index 2cb32ed..f8a2d50 100644 --- a/src/test/java/net/sourceforge/plantuml/servlet/TestForm.java +++ b/src/test/java/net/sourceforge/plantuml/servlet/TestForm.java @@ -1,6 +1,7 @@ package net.sourceforge.plantuml.servlet; import com.meterware.httpunit.GetMethodWebRequest; +import com.meterware.httpunit.HTMLElement; import com.meterware.httpunit.WebConversation; import com.meterware.httpunit.WebForm; import com.meterware.httpunit.WebRequest; @@ -113,4 +114,23 @@ public class TestForm extends WebappTestCase { assertEquals(1, response.getImages().length); } + /** + * Verifies that an image map is produced if the diagram contains a link + */ + public void testImageMap() throws Exception { + WebConversation conversation = new WebConversation(); + // Fill the form and submit it + WebRequest request = new GetMethodWebRequest(getServerUrl()); + WebResponse response = conversation.getResponse(request); + WebForm formText = response.getForms()[0]; + formText.setParameter("text", "@startuml \nBob -> Alice : [[http://yahoo.com]] Hello \n@enduml"); + response = formText.submit(); + // Analyze response + // Ensure the generated image is present + assertEquals(1, response.getImages().length); + // Ensure the image map is present + HTMLElement maps[] = response.getElementsByTagName("map"); + assertEquals(1, maps.length); + } + }