diff --git a/src/info/dawelbeit/graphviz/dot/GraphViz.java b/src/info/dawelbeit/graphviz/dot/GraphViz.java index 04f7eee..783585c 100644 --- a/src/info/dawelbeit/graphviz/dot/GraphViz.java +++ b/src/info/dawelbeit/graphviz/dot/GraphViz.java @@ -31,10 +31,15 @@ import java.io.FileOutputStream; import java.io.FileWriter; import java.io.InputStreamReader; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** *
*
Purpose: GraphViz Java API @@ -63,7 +68,7 @@ * * @version v0.4, 2011/02/05 (February) -- Patch of Keheliya Gallaba is added. Now you * can specify the type of the output file: gif, dot, fig, pdf, ps, svg, png, etc. - * @version v0.3, 2010/11/29 (November) -- Windows support + ability + * @version v0.3, 2010/11/29 (November) -- Windows support + ability * to read the graph from a text file * @version v0.2, 2010/07/22 (July) -- bug fix * @version v0.1, 2003/12/04 (December) -- first release @@ -71,7 +76,7 @@ */ public class GraphViz { - + private static final Logger log = Logger.getLogger(GraphViz.class.getName()); /** * The dir. where temporary files will be created. @@ -84,10 +89,10 @@ public class GraphViz */ //private static String DOT = "/usr/local/bin/dot"; // MAC private static String DOT = "/usr/bin/dot"; // Linux - // private static String DOT = "c:/Program Files/Graphviz2.26.3/bin/dot.exe"; // Windows - + //private static String DOT = "C:/Program Files/Graphviz/bin/dot.exe"; // Windows + public static final String GRAPH_START = "digraph G {"; - + public static final String GRAPH_END = "}"; /** @@ -147,7 +152,7 @@ public byte[] getGraph(String dot_source, String type) if (dot != null) { img_stream = get_img_stream(dot, type); - if (dot.delete() == false) + if (dot.delete() == false) log.warn("Warning: " + dot.getAbsolutePath() + " could not be deleted!"); return img_stream; } @@ -211,7 +216,7 @@ private byte[] get_img_stream(File dot, String type) // Close it if we need to if( in != null ) in.close(); - if (img.delete() == false) + if (img.delete() == false) log.warn("Warning: " + img.getAbsolutePath() + " could not be deleted!"); } catch (java.io.IOException ioe) { @@ -266,7 +271,7 @@ public String end_graph() { /** * Read a DOT graph from a text file. - * + * * @param input Input text file containing the DOT graph * source. */ @@ -284,7 +289,7 @@ public void readSource(String input) sb.append(line); } dis.close(); - } + } catch (Exception e) { log.error("Error: ", e); } @@ -299,14 +304,32 @@ public void readSource(String input) public void readString(String dot) { this.graph = new StringBuilder(dot); } - + /** * is a valid dot source * @param dot * @return */ public static boolean isValidDotText(String dot) { - return StringUtils.isNotBlank(dot) && (dot.indexOf(GRAPH_START) > -1); + // return StringUtils.isNotBlank(dot) && (dot.indexOf(GRAPH_START) > -1); + return StringUtils.isNotBlank(dot) && (checkGraphStart(dot)); + } + + private static boolean checkGraphStart(String dot) { + boolean ret_val; + + // "digraph" 다음에 오는 단어를 찾는 정규 표현식 패턴 + String patternString = "digraph\\s+(\\w+)\\s*\\{"; + + Pattern pattern = Pattern.compile(patternString); + Matcher matcher = pattern.matcher(dot); + + if (matcher.find()) { + ret_val = true; + } else { + ret_val = false; + } + return ret_val; } } // end of class GraphViz diff --git a/src/info/dawelbeit/graphviz/dot/HttpDotGraphMessageHandler.java b/src/info/dawelbeit/graphviz/dot/HttpDotGraphMessageHandler.java index 15f8e9b..d4a1522 100644 --- a/src/info/dawelbeit/graphviz/dot/HttpDotGraphMessageHandler.java +++ b/src/info/dawelbeit/graphviz/dot/HttpDotGraphMessageHandler.java @@ -27,22 +27,23 @@ * */ public class HttpDotGraphMessageHandler implements HttpRequestHandler { - + private static final String TEMP_PATH = "/tmp/graph."; - + // 3 supported types private static final String GRAPH_TYPE_PNG = "png"; private static final String GRAPH_TYPE_PDF = "pdf"; private static final String GRAPH_TYPE_SVG = "svg"; - + private static final String CONTENT_TYPE_PNG = "image/png"; private static final String CONTENT_TYPE_PDF = "application/pdf"; - + private static final String CONTENT_TYPE_SVG = "application/svg+xml"; + private static final Logger log = Logger.getLogger(HttpDotGraphMessageHandler.class.getName()); /** - * + * * @param url * @param proxyUser * @param proxyPass @@ -60,17 +61,17 @@ public void handle( final HttpContext context) throws HttpException, IOException { String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH); - + if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) { throw new MethodNotSupportedException(method + " method not supported"); } - + log.info(request.toString()); - + String target = request.getRequestLine().getUri(); response.setStatusCode(HttpStatus.SC_OK); - + if (request instanceof HttpEntityEnclosingRequest) { HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity(); byte[] entityContent = EntityUtils.toByteArray(entity); @@ -81,7 +82,7 @@ public void handle( // only respond if we have a valid dot if(StringUtils.isNotBlank(dot) && GraphViz.isValidDotText(dot)) { - target = (StringUtils.isNotBlank(target) ? + target = (StringUtils.isNotBlank(target) ? StringUtils.remove((URLDecoder.decode(target, "UTF-8")).trim().toLowerCase(), '/') : null); log.info("requesting graph type: " + target); @@ -94,32 +95,33 @@ public void handle( } log.info("Responded with Success"); - + } - + /** - * + * * @param dot * @return */ private HttpEntity generateGraph(String dot, String target) { - + GraphViz gv = new GraphViz(); gv.readString(dot); log.info(gv.getDotSource()); - + ContentType contentType; String graphType; - + if(GRAPH_TYPE_SVG.equals(target)) { - + graphType = GRAPH_TYPE_SVG; - contentType = ContentType.APPLICATION_SVG_XML; - + // contentType = ContentType.APPLICATION_SVG_XML; + contentType = ContentType.create(CONTENT_TYPE_SVG, (Charset) null); + } else if(GRAPH_TYPE_PDF.equals(target)) { graphType = GRAPH_TYPE_PDF; contentType = ContentType.create(CONTENT_TYPE_PDF, (Charset) null); - + } else { // default png graphType = GRAPH_TYPE_PNG; @@ -136,11 +138,12 @@ private HttpEntity generateGraph(String dot, String target) { File out = new File(TEMP_PATH + graphType); // Linux gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), graphType ), out ); - + FileEntity body = new FileEntity(out, contentType); - + //FileEntity body = new FileEntity(out, "application/svg+xml; charset=utf-8"); + return body; - + } } \ No newline at end of file diff --git a/src/info/dawelbeit/graphviz/dot/test/RegExample01.java b/src/info/dawelbeit/graphviz/dot/test/RegExample01.java new file mode 100644 index 0000000..42ad4e1 --- /dev/null +++ b/src/info/dawelbeit/graphviz/dot/test/RegExample01.java @@ -0,0 +1,25 @@ +package info.dawelbeit.graphviz.dot.test; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class RegExample01 { + + public static void main(String[] args) { + String data = "digraph travle_schedule { ..."; + + // "digraph"과 "{" 사이에 있는 모든 문자를 찾는 정규 표현식 패턴 + String patternString = "digraph(.*?)\\{"; + + Pattern pattern = Pattern.compile(patternString, Pattern.DOTALL); + Matcher matcher = pattern.matcher(data); + + if (matcher.find()) { + System.out.println("Found: " + matcher.group(1).trim()); + } else { + System.out.println("Not Found"); + } + + } + +} diff --git a/src/info/dawelbeit/graphviz/dot/test/RegExample02.java b/src/info/dawelbeit/graphviz/dot/test/RegExample02.java new file mode 100644 index 0000000..4d92462 --- /dev/null +++ b/src/info/dawelbeit/graphviz/dot/test/RegExample02.java @@ -0,0 +1,22 @@ +package info.dawelbeit.graphviz.dot.test; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class RegExample02 { + public static void main(String[] args) { + String data = "digraph travle_schedule { ..."; + + // "digraph" 다음에 오는 단어를 찾는 정규 표현식 패턴 + String patternString = "digraph\\s+(\\w+)\\s*\\{"; + + Pattern pattern = Pattern.compile(patternString); + Matcher matcher = pattern.matcher(data); + + if (matcher.find()) { + System.out.println("Found: " + matcher.group(1)); + } else { + System.out.println("Not Found"); + } + } +}