%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="uk.org.mafoo.wordbubbles.*" %>
<%@ page errorPage="error.jsp" %>
<%
// String dict = request.getParameter("dictionary");
// if(dict != "twl06") throw new Exception("failed to get dictionary");
String dict = "/usr/share/dict/twl06";
uk.org.mafoo.wordbubbles.Lexicon words = uk.org.mafoo.wordbubbles.Lexicon.loadLexiconFromFile(dict);
ArrayList> grid = new ArrayList>();
HashSet desired = new HashSet();
boolean desired_active = false;
if(request.getParameterValues("desired") != null) {
	desired_active = true;
	for( String p : request.getParameterValues("desired") )  {
		desired.add(new Integer(p));
	}
}
if (request.getParameter("grid").length() > 2048) { throw new Exception("Input too large"); }
for ( String line : request.getParameter("grid").split("\r\n")) {
  ArrayList row = new ArrayList();
  for ( char c : line.toLowerCase().toCharArray() ) {
    if( (c >= 'a' && c <= 'z') || c == ' ' || c == '.' || c == '_') {
	    if(c == '.' || c == '_') c = ' ';
	    row.add(new Character(c));
	} else {
		throw new Exception("invalid char");
	}
  }
  grid.add(row);
}
uk.org.mafoo.wordbubbles.Prison prison = new uk.org.mafoo.wordbubbles.Prison(grid);
ArrayList> found = prison.search(words);
HashSet foundwords = new HashSet();
for ( LinkedHashSet w : found ) {
	String str = "";
	for ( Cell letter : w ) {
		str += letter;
	} 
	if(desired_active) {
		if(desired.contains(new Integer(str.length()))) { foundwords.add(str); }
	} else {
		foundwords.add(str);
	}
}
%>
Wordbubble Solver
Input
<% for (ArrayList row : grid) { %>
  
<%    for(Character c : row) { %>
    | <%= c %> | 
<%      }
   }
%>
  
<% if(desired_active) { %> Looking for words of length(s): <%= desired %> <% } %>
Results
<% 
   List output = new ArrayList(foundwords);
   Collections.sort(output);
   for (String word : output) { %>
- <%= word %>
 
<% } %>
 
 |