Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions src/info/dawelbeit/graphviz/dot/GraphViz.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <dl>
* <dt>Purpose: GraphViz Java API
Expand Down Expand Up @@ -63,15 +68,15 @@
*
* @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
* @author Laszlo Szathmary (<a href="jabba.laci@gmail.com">jabba.laci@gmail.com</a>)
*/
public class GraphViz
{

private static final Logger log = Logger.getLogger(GraphViz.class.getName());
/**
* The dir. where temporary files will be created.
Expand All @@ -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 = "}";

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
*/
Expand All @@ -284,7 +289,7 @@ public void readSource(String input)
sb.append(line);
}
dis.close();
}
}
catch (Exception e) {
log.error("Error: ", e);
}
Expand All @@ -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
Expand Down
49 changes: 26 additions & 23 deletions src/info/dawelbeit/graphviz/dot/HttpDotGraphMessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;

}

}
25 changes: 25 additions & 0 deletions src/info/dawelbeit/graphviz/dot/test/RegExample01.java
Original file line number Diff line number Diff line change
@@ -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");
}

}

}
22 changes: 22 additions & 0 deletions src/info/dawelbeit/graphviz/dot/test/RegExample02.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
}