<%@ page import = "com.cyscape.browserhawk.*" %>
<%@ page import = "java.io.*" %>
<%@ page import = "java.util.*" %>

<!--
   This file is provided under the terms of the BrowserHawk4J license
   agreement.  Copyright (c) 2004-2006 cyScape, Inc. All rights reserved.
-->

<%
  // First we make sure the system executing this example has an
  // Enterprise Edition license as required for this test.
  // Note: This is NOT needed in your pages, only for this generic sample.
  if (BrowserHawk.getVersion().indexOf("Ent") == -1) {
    out.println("Enterprise License Required.  ");
    out.println("You are using " + BrowserHawk.getVersion() + ".  ");
    out.println("See <a href='http://www.cyscape.com/order/'>http://www.cyscape.com/order/</a> for upgrade information.");
    return;
  }

  String fonts = "Arial,Courier,Helvetica,Palatino Linotype,Times,Trebuchet MS";
  String userFonts = request.getParameter("fonts");
  if (userFonts != null && !userFonts.equals("")) {
    fonts = fonts + "," + escape(userFonts);
  }

  ExtendedOptions options = new ExtendedOptions();
  options.addProperty("FontsInstalled");

  boolean checkingAll = false;
  if (request.getParameter("checkAll") != null) {
     fonts = "all";
     checkingAll = true;
  }

  options.setFontsToCheck(fonts);
  options.setPageMessage("Now checking fonts...");

  BrowserInfo info = BrowserHawk.getBrowserInfo(request);
  ExtendedBrowserInfo einfo = null;
  try {
    einfo = BrowserHawk.getExtendedBrowserInfo(request, response, options);
  }
  catch (Exception e) {
    PrintWriter pw = new PrintWriter(out, true);
    pw.println("<pre>");
    e.printStackTrace(pw);
    pw.println("</pre>");
    return;
  }

  // A null return value indicates JavaScript has been sent and 
  // we should immediately return
  if (einfo == null) return;
%>

<html>
<head><title>Font Test Page</title></head>

<body bgcolor=white>
<font face="helvetica">

<h2>Font Test Page</h2>

<% if (einfo == ExtendedBrowserInfo.UNSUPPORTED) { %>
  <big>No extended information is available.  Only Netscape,
  Mozilla, Firefox, IE, Opera, Safari, and AOL Explorer browsers are
  supported.</big>
<% } else if (einfo.getJavaScriptEnabled() == false) { %>
  <big>No extended information is available.  This client has
  JavaScript disabled.</big>
<% } else if (!info.getBrowser().equals("IE") || !(info.getVersion() > 5)) { %>
  <big>No font detection is available.  Your browser has to be IE on Windows or
  Mac version 5 or later.</big>
<% } else { %>


<%
  if (checkingAll == false) {
%>
    This example tests which fonts your browser has installed.<br>
    First we check a few standard fonts.  Then you can try a few of your own.<p>
<%
} else {
%>
    The following is a list of all fonts installed on your computer.<p>
<%
  }
%>


<table border=1 cellspacing=2 cellpadding=5>
<tr><th align="left">Font Name</th><th>Installed</th><th>Sample output</th></tr>

<%
  if (checkingAll) {
     // Since we are checking for all fonts, set our fonts var to list of fonts
     // that BrowserHawk detected as installed.  This way it will enumerate
     // through the list of installed fonts.  If we're not checking for ALL
     // fonts then the fonts variable will remain set equal to the list of
     // default fonts that we tested for so those will be enumerated.
     fonts = einfo.getFontsInstalled();
  }

  List tested = getComponents(fonts); 
  Iterator itr = tested.iterator();
  while (itr.hasNext()) {
    String font = (String) itr.next();
%>
    <tr><td width="150"><%= font %></font></td>
        <td><%= einfo.getFontInstalled(font) %></td>
        <td><font face="<%= font %>">Detect fonts with BrowserHawk!</font></td>
    </tr>
<%
  }
%>

</table>

<br>
Want to check for your own fonts?  Enter a comma-separated list below.
<br>
Or <a href="fontcheck_adv.jsp?checkAll=1">click here</a> to obtain a list of
<b>all installed fonts.</b>
<form>
Fonts: <input type="text" name="fonts" size="50"
         value="<%=userFonts == null ? "" : userFonts%>">
<input type="submit" value="Test">
</form>

<% } %>

<P>
<b><a href="fontcheck_adv.jsp">Retry this test</a></b><P>
<b><a href="index.html">Return to list of other JSP samples</a></b><P>
<b><a href="http://www.cyscape.com/order/">BrowserHawk Pricing and Ordering</a></b><P>
<b><a href="http://www.cyscape.com/">cyScape home page</a></b><P>
</font>
</body>
</html>

<%!
  // Convert comma separated to an array
  static List getComponents(String s) {
    List list = new ArrayList();
    StringTokenizer tokenizer = new StringTokenizer(s, ",");
    while (tokenizer.hasMoreTokens()) {
      list.add(tokenizer.nextToken().trim());
    }
    return list;
  }

  // A method to do simple escaping away of special characters.
  // It's wise to escape any user-entered data before display.
  // A longer form of this method would convert < to &lt; and so on,
  // but it's not worth it here since fonts don't have these special chars.
  // We assume the problem chars are just errors and convert them to spaces.
  static String escape(String s) {
    s = s.replace('<', ' ');
    s = s.replace('>', ' ');
    s = s.replace('\'', ' ');
    s = s.replace('\"', ' ');
    return s;
  }
%>
