public IoBuffer generateIndex()

in fileservice/src/main/java/org/apache/asyncweb/fileservice/index/DefaultDirectoryIndexGenerator.java [39:101]


    public IoBuffer generateIndex(File directory) {
        if (bundle == null) {
            
            bundle = ResourceBundle.getBundle(DefaultDirectoryIndexGenerator.class.getPackage()
                    .getName()+".strings");
        }
            
        File[] files = directory.listFiles();

        StringBuilder html = new StringBuilder(1024);
        html.append("<html><head><title>");
        html.append("AsyncWeb Server - ");
        html.append(directory.getName());
        html.append("</title><style><!--");
        CSS.appendTo(html).append("--></style>");
        html.append("</head></body>\n");
        html.append("<H1>Index of : ").append(directory.getName()).append(
                "</H1>\n");
        html.append("<table cellpadding=\"5\"><tr><th>");
        html.append(bundle.getString("nameHeader")).append("</th><th>");
        html.append(bundle.getString("typeHeader")).append("</th><th>");
        html.append(bundle.getString("sizeHeader")).append("</th><th>");
        html.append(bundle.getString("lastModifiedHeader"))
                .append("</th></tr>\n");

        // back to parent directory
        html.append("<tr><td><a href=\"..\">").append(
                bundle.getString("parentDirectory"));
        html.append("</a></td><td>DIR</td><td></td><td></td></tr>\n");

        for (File file : files) {
            html.append("<tr><td><a href=\"").append(file.getName());
            if (file.isDirectory()) {
                html.append("/");
            }
            html.append("\">");
            html.append(file.getName());
            if (file.isDirectory()) {
                html.append("/");
            }
            html.append("</a></td><td>");
            html.append(getType(file));
            html.append("</td><td>");
            html.append(file.length());
            html.append("</td><td>");
            html.append(new Date(file.lastModified()));
            html.append("</td></tr>\n");
        }
        html.append("</table>\n");
        html.append("<HR size=\"1\" noshade=\"noshade\">");
        html.append("<H2>AsyncWeb Server</H2></body></html>");
        IoBuffer out = IoBuffer.allocate(html.length());

        // TODO: Need to sort this out when we start dealing with character encodings
        try {
            byte[] bytes = html.toString().getBytes("US-ASCII");
            out.put(bytes);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

        return out;
    }