Revision 337 (by dpavlin, 2004/06/10 19:22:40) new trunk for webpac v2
<%@page import="org.openisis.*,java.io.IOException,java.util.Date,java.util.Iterator,java.net.URLEncoder" contentType="text/html;charset=UTF-8"%><%!
	static final String EMPTY = "";
	static Db db;
	static String dberr = EMPTY;
	static {
		try {
			String[] args = new String[] { "-v", "9", "-encoding", "UTF-8",
				"-format", "aligned" };
			db = Db.open( "/var/db/come2uni/come2uni", args );
			if ( db instanceof NativeDb )
				System.err.println( "db.db = "+((NativeDb)db).db );
		} catch (IOException e) {
			dberr = e.toString();
		}
	};
	static String NV ( String x ) { return null==x ? EMPTY : x; }
	static int ROWS = 30; // number of rows to show at once
	static String vif ( String pre, Rec r, int t, String post ) {
		StringBuffer ret = null;
		int l = r.getLen();
		for ( int i=0; i<l; i++ ) {
			Field f = r.getField(i);
			if ( t != f.tag ) continue;
			if ( null == ret )
				ret = new StringBuffer( pre );
			else
				ret.append( ", " );
			ret.append( f.html() );
		}
		return null == ret ? "" : ret.append(post).toString();
	}
	static String v ( Rec r, int t ) {
		return vif( "", r, t, "" );
	}
%><%
	String search = NV( request.getParameter( "search" ) );
	// brute force ...
	search = new String(search.getBytes("ISO-8859-1"), "UTF-8");
	int off = 0;
	int rows = ROWS;
	try {
		off = Integer.parseInt( request.getParameter( "off" ) );
	} catch ( Exception e ) {}
	try {
		rows = Integer.parseInt( request.getParameter( "rows" ) );
	} catch ( Exception e ) {}
	int[] set = (int[])session.getAttribute( "set" );
%><html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>OpenIsis unicode demo</title>
</head>
<body bgcolor="#FFFFFF">
	<%= dberr %>
	<form action="unicode.jsp" accept-charset="UTF-8">
	<%-- method="POST" enctype="multipart/form-data"
		buys us nothing -- the ;charset=UTF-8 is parsed by newer tomcat,
		but not transmitted by Mozilla, so we have to do conversion manually anyway
	--%>
		<input type="text" name="search" value="<%=search%>"/>
		<input type="checkbox" name="prefix" value="1" checked /> as prefix
		<input type="submit" name="action" value="search"/>
		<input type="text" name="rows" size="2" value="<%=rows%>"/> rows at once
		<br>
		<input type="checkbox" name="terms" value="1"/> show index terms, not records
	</form>
	<b>
	text excerpts are from
	<a href="http://www.unicode.org/iuc/iuc10/x-utf8.html">
	the invitation to the Tenth International Unicode Conference.</a>
	</b>
<p>
<%
	if ( EMPTY != search ) {
		if ( null != request.getParameter( "terms" ) ) {
			Iterator t = db.terms( search );
			while ( t.hasNext() ) {
				String s = (String)t.next();
				if ( 0 == s.length() ) continue;
				String u = URLEncoder.encode(
					new String(s.getBytes("UTF-8"), "ISO-8859-1") );
%>
<a href="unicode.jsp?prefix=1&search=<%=u%>"><%=s%></a><br>
<%
			}
%>
</body></html><%
			return;
		} // list terms

		long then = (new Date()).getTime();
		int mode = null==request.getParameter( "prefix" ) ? 0 : Db.QRY_KEYPF;
		int[] nset = db.search( search, mode );
		long now = (new Date()).getTime();
		if ( null == nset ) {
%>
	sorry, no results for <%=search%>
<%
			session.removeAttribute( "set" );
			return;
		} else {
%>
	got <%=nset.length%> rows for <%=search%>
	in <%= 0==mode ? "word" : "prefix" %> mode
	in <%=(now - then)/1000.%> seconds.<br>
<%
			set = nset;
			session.setAttribute( "set", set );
		}
	} // EMPTY != search
	if ( null == set )
		return;
	if ( 0 < off ) {
		int boff = off-rows;
		if ( boff < 0 ) boff = 0;
%>
		<a href="unicode.jsp?off=<%=boff%>&rows=<%=rows%>">&lt;&lt;rows <%=boff+1%> to <%=boff+rows%></a>
<%
	}
	int end = off + rows;
	if ( end >= set.length )
		end = set.length;
	else {
%>
		<a href="unicode.jsp?off=<%=end%>&rows=<%=rows%>">rows <%=end+1%> to <%=end+rows%>&gt;&gt;</a>
<%
	}
%>
	<h4> rows <%=off+1%> to <%=end%> </h4>
<%-- start output --%>
	<table cols="1" width="100%">
<%
	for ( int i=off; i<end; i++ ) {
		Rec r = db.readRow( set[i] );
		if ( null == r )
			continue;
		int id = r.getRowId();
		String name = v(r,1);
		String lang = v(r,2);
		String dir  = v(r,3);
		String head = v(r,4);
		String body = v(r,5);
		if ( ! "".equals(dir) ) dir = " dir="+dir;
%>
	<tr> <td bgcolor="#CC99FF" align=center> <%=name%> </td> </tr>
	<tr> <td align=center<%=dir%> lang=<%=lang%>><b><font size="+2"><%=head%></font></b></td> </tr>
	<tr> <td <%=dir%> lang=<%=lang%>> <%=body%> </td> </tr>
<%
	} // for rows
%>
	</table>
</body></html>