fixed parsing for science direct html with more than one <a href=> per one <tr>
[webpac] / openisis / unicode.jsp
1 <%@page import="org.openisis.*,java.io.IOException,java.util.Date,java.util.Iterator,java.net.URLEncoder" contentType="text/html;charset=UTF-8"%><%!
2         static final String EMPTY = "";
3         static Db db;
4         static String dberr = EMPTY;
5         static {
6                 try {
7                         String[] args = new String[] { "-v", "9", "-encoding", "UTF-8",
8                                 "-format", "aligned" };
9                         db = Db.open( "/var/db/come2uni/come2uni", args );
10                         if ( db instanceof NativeDb )
11                                 System.err.println( "db.db = "+((NativeDb)db).db );
12                 } catch (IOException e) {
13                         dberr = e.toString();
14                 }
15         };
16         static String NV ( String x ) { return null==x ? EMPTY : x; }
17         static int ROWS = 30; // number of rows to show at once
18         static String vif ( String pre, Rec r, int t, String post ) {
19                 StringBuffer ret = null;
20                 int l = r.getLen();
21                 for ( int i=0; i<l; i++ ) {
22                         Field f = r.getField(i);
23                         if ( t != f.tag ) continue;
24                         if ( null == ret )
25                                 ret = new StringBuffer( pre );
26                         else
27                                 ret.append( ", " );
28                         ret.append( f.html() );
29                 }
30                 return null == ret ? "" : ret.append(post).toString();
31         }
32         static String v ( Rec r, int t ) {
33                 return vif( "", r, t, "" );
34         }
35 %><%
36         String search = NV( request.getParameter( "search" ) );
37         // brute force ...
38         search = new String(search.getBytes("ISO-8859-1"), "UTF-8");
39         int off = 0;
40         int rows = ROWS;
41         try {
42                 off = Integer.parseInt( request.getParameter( "off" ) );
43         } catch ( Exception e ) {}
44         try {
45                 rows = Integer.parseInt( request.getParameter( "rows" ) );
46         } catch ( Exception e ) {}
47         int[] set = (int[])session.getAttribute( "set" );
48 %><html>
49 <head>
50         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
51   <title>OpenIsis unicode demo</title>
52 </head>
53 <body bgcolor="#FFFFFF">
54         <%= dberr %>
55         <form action="unicode.jsp" accept-charset="UTF-8">
56         <%-- method="POST" enctype="multipart/form-data"
57                 buys us nothing -- the ;charset=UTF-8 is parsed by newer tomcat,
58                 but not transmitted by Mozilla, so we have to do conversion manually anyway
59         --%>
60                 <input type="text" name="search" value="<%=search%>"/>
61                 <input type="checkbox" name="prefix" value="1" checked /> as prefix
62                 <input type="submit" name="action" value="search"/>
63                 <input type="text" name="rows" size="2" value="<%=rows%>"/> rows at once
64                 <br>
65                 <input type="checkbox" name="terms" value="1"/> show index terms, not records
66         </form>
67         <b>
68         text excerpts are from
69         <a href="http://www.unicode.org/iuc/iuc10/x-utf8.html">
70         the invitation to the Tenth International Unicode Conference.</a>
71         </b>
72 <p>
73 <%
74         if ( EMPTY != search ) {
75                 if ( null != request.getParameter( "terms" ) ) {
76                         Iterator t = db.terms( search );
77                         while ( t.hasNext() ) {
78                                 String s = (String)t.next();
79                                 if ( 0 == s.length() ) continue;
80                                 String u = URLEncoder.encode(
81                                         new String(s.getBytes("UTF-8"), "ISO-8859-1") );
82 %>
83 <a href="unicode.jsp?prefix=1&search=<%=u%>"><%=s%></a><br>
84 <%
85                         }
86 %>
87 </body></html><%
88                         return;
89                 } // list terms
90
91                 long then = (new Date()).getTime();
92                 int mode = null==request.getParameter( "prefix" ) ? 0 : Db.QRY_KEYPF;
93                 int[] nset = db.search( search, mode );
94                 long now = (new Date()).getTime();
95                 if ( null == nset ) {
96 %>
97         sorry, no results for <%=search%>
98 <%
99                         session.removeAttribute( "set" );
100                         return;
101                 } else {
102 %>
103         got <%=nset.length%> rows for <%=search%>
104         in <%= 0==mode ? "word" : "prefix" %> mode
105         in <%=(now - then)/1000.%> seconds.<br>
106 <%
107                         set = nset;
108                         session.setAttribute( "set", set );
109                 }
110         } // EMPTY != search
111         if ( null == set )
112                 return;
113         if ( 0 < off ) {
114                 int boff = off-rows;
115                 if ( boff < 0 ) boff = 0;
116 %>
117                 <a href="unicode.jsp?off=<%=boff%>&rows=<%=rows%>">&lt;&lt;rows <%=boff+1%> to <%=boff+rows%></a>
118 <%
119         }
120         int end = off + rows;
121         if ( end >= set.length )
122                 end = set.length;
123         else {
124 %>
125                 <a href="unicode.jsp?off=<%=end%>&rows=<%=rows%>">rows <%=end+1%> to <%=end+rows%>&gt;&gt;</a>
126 <%
127         }
128 %>
129         <h4> rows <%=off+1%> to <%=end%> </h4>
130 <%-- start output --%>
131         <table cols="1" width="100%">
132 <%
133         for ( int i=off; i<end; i++ ) {
134                 Rec r = db.readRow( set[i] );
135                 if ( null == r )
136                         continue;
137                 int id = r.getRowId();
138                 String name = v(r,1);
139                 String lang = v(r,2);
140                 String dir  = v(r,3);
141                 String head = v(r,4);
142                 String body = v(r,5);
143                 if ( ! "".equals(dir) ) dir = " dir="+dir;
144 %>
145         <tr> <td bgcolor="#CC99FF" align=center> <%=name%> </td> </tr>
146         <tr> <td align=center<%=dir%> lang=<%=lang%>><b><font size="+2"><%=head%></font></b></td> </tr>
147         <tr> <td <%=dir%> lang=<%=lang%>> <%=body%> </td> </tr>
148 <%
149         } // for rows
150 %>
151         </table>
152 </body></html>