dviner      2003/08/06 12:02:08

  Modified:    java/src/org/apache/xindice/server UglyBrowser.java
  Log:
  fixing the double slash in the path.
  now xml documents will be displayed correctly (that is, with the proper html 
escaping)
  also, added a small revision number to the output display.
  
  --dviner
  
  Revision  Changes    Path
  1.3       +67 -8     
xml-xindice/java/src/org/apache/xindice/server/UglyBrowser.java
  
  Index: UglyBrowser.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/server/UglyBrowser.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- UglyBrowser.java  1 Aug 2003 19:40:19 -0000       1.2
  +++ UglyBrowser.java  6 Aug 2003 19:02:08 -0000       1.3
  @@ -44,13 +44,15 @@
                   XPathPseudoParser parser = new XPathPseudoParser(path);
   
                   /* building the page once we have all the information */
  -                sb.append("<table border=\"1\" width=\"90%\">");
  -                sb.append("<tr><td rowspan=\"1\" colspan=\"2\">" + 
getPathNavigation(parser, contextPath) + "</td></tr>");
  +                sb.append("<table border=\"1\" width=\"90%\">\n");
  +                sb.append("<tr><td rowspan=\"1\" colspan=\"2\">" + 
getPathNavigation(parser, contextPath) + "</td></tr>\n");
                   sb.append("<tr><td valign=\"top\">" + getHierarchy(parser, 
contextPath, db) + "</td>");
  -                sb.append("<td valign=\"top\">" + getDetailView(parser, db) 
+ "</td></tr>");
  -                sb.append("</table>");
  +                sb.append("<td valign=\"top\">" + getDetailView(parser, db) 
+ "</td></tr>\n");
  +                sb.append("</table>\n");
               }
  -            sb.append("</center></body></html>");
  +            sb.append("</center>");
  +            sb.append("<p><font size=\"-1\">Ugly Browser version: $Revision$ 
</font>\n</p>");
  +            sb.append("</body></html>");
   
               response.setContentType("text/html; charset=utf-8");
               byte[] resultBytes = sb.toString().getBytes("utf-8");
  @@ -101,8 +103,17 @@
           }
   
           String[] cols = col.listCollections();
  +             String dbLoc = parser.getDatabase();
  +             String parserPath = parser.getPath();
  +             StringBuffer baseHref = new StringBuffer();
  +             baseHref.append("<a href=\"" + contextPath + "?" + dbLoc);      
      
  +             if(parserPath.startsWith("/") || dbLoc.endsWith("/"))
  +                     baseHref.append(parserPath);
  +             else
  +                     baseHref.append("/" + parserPath);
  +                     
           for (int i = 0; i < cols.length; i++) {
  -            result.append("<a href=\"" + contextPath + "?" + 
parser.getDatabase() + "/" + parser.getPath());
  +            result.append(baseHref);
               result.append("/");
               result.append(cols[i]);
               result.append("\">");
  @@ -113,7 +124,7 @@
           try {
               String[] docs = col.listDocuments();
               for (int i = 0; i < docs.length; i++) {
  -                result.append("<a href=\"" + contextPath + "?" + 
parser.getDatabase() + "/" + parser.getPath());
  +                result.append(baseHref);
                   result.append("&");
                   result.append(docs[i]);
                   result.append("\">");
  @@ -140,8 +151,56 @@
               return "Document '" + document + "' not found";
           }
   
  -        return "Document '" + document + "'<p>" + TextWriter.toString(doc);
  +        return "Document '" + document + "'<p>\n" 
  +             + escapeHtml(TextWriter.toString(doc));
       }
  +
  +     /* simple function to escape the xml stuff that'll 
  +      * not be displayed correctly in html.
  +      */
  +     static public String escapeHtml(String value) {
  +             StringBuffer buf = new StringBuffer();
  +             for (int i = 0; i < value.length(); i++) {
  +                     char c = value.charAt(i);
  +                     switch (c) {
  +                             case '<' :
  +                                     buf.append("&lt;");
  +                                     break;
  +                             case '>' :
  +                                     buf.append("&gt;");
  +                                     break;
  +                             case '\'' :
  +                                     buf.append("&apos;");
  +                                     break;
  +                             case '\"' :
  +                                     buf.append("&quot;");
  +                                     break;
  +                             case '&' :
  +                                     buf.append("&amp;");
  +                                     break;
  +                             default :
  +                                     if (isLegalCharacter(c)) {
  +                                             buf.append(c);
  +                                     } else {
  +                                             buf.append("&#");
  +                                             buf.append(String.valueOf(c));
  +                                             buf.append(";");
  +                                     }
  +                                     break;
  +                     }
  +             }
  +             return buf.toString();
  +     }
  +
  +     /**
  +      * Is the given character allowed inside an HTML document?
  +      */
  +     static public boolean isLegalCharacter(char c) {
  +             if ((c <= 0x07) || (c >= 0x80))
  +                     return false;
  +             else
  +                     return true;
  +     }
   
       public static class XPathPseudoParser {
   
  
  
  

Reply via email to