Webiyo

This page was generated using Webiyo. See its source code and unit tests.

org/webiyo/examples/sourceforge/Project.java

package org.webiyo.examples.sourceforge;

import org.webiyo.examples.viewjava.SourceDir;
import org.webiyo.examples.viewjava.SourceIndex;
import org.webiyo.util.Files;
import org.webiyo.web.Image;
import org.webiyo.web.Link;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

class Project {

    public static final Link SOURCEFORGE_NET = new Link("http://sourceforge.net");

    public static final int WEBIYO_GROUP_ID = 134357;

    public static final String[] WEBIYO_SOURCE_DIRS = new String[]{"src/deploy", "src/testutils", "src/examples", "test/src"};

    private final String name;
    private final String unixName;
    private final int groupId;
    private final File projectRoot;
    private final List<SourceDir> sourceDirs;

    private SourceIndex sourceIndexCache;

    Project(String name, String unixName, int groupId, File projectRoot, String... sourcePaths) {
        this.name = name;
        this.unixName = unixName;
        this.groupId = groupId;
        this.projectRoot = projectRoot;
        this.sourceDirs = new ArrayList<SourceDir>();
        for (String path : sourcePaths) {
            sourceDirs.add(new SourceDir(projectRoot, path));
        }
    }

    public String getName() {
        return name;
    }

    public Image getSourceForgeImage() {
        Link link = new Link("http://sourceforge.net/sflogo.php?group_id=" + groupId + "&type=1");
        return new Image(link, 88, 31, "SourceForge");
    }

    public Link getSourceforgeProjectLink() {
        return new Link("http://sourceforge.net/projects/" + unixName + "/");
    }

    public Link getNewsFeedLink() {
        return new Link("http://sourceforge.net/export/rss2_projnews.php?group_id=" + groupId + "&rss_fulltext=1");
    }

    public News loadNews() throws IOException {
        return new News(getNewsFeedLink());
    }

    public SourceIndex getSourceIndex() {
        if (sourceIndexCache == null) {
            sourceIndexCache = new SourceIndex(sourceDirs.toArray(new SourceDir[]{}));
        }
        return sourceIndexCache;
    }

    public ChangeLog loadChangeLog() throws IOException {
        return loadChangeLog(projectRoot);
    }
    
    // end of public methods

    static ChangeLog loadChangeLog(File projectDir) throws IOException {
        ProcessBuilder builder = new ProcessBuilder();
        builder.directory(projectDir);
        builder.command("cvs", "log");
        Process process = builder.start();

        ChangeLog changeLog = readChangeLog(process.getInputStream());
        String stderr = Files.readString(process.getErrorStream());

        try {
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new IOException("cvs log command failed, stderr:\n" + stderr);
            }
        } catch (InterruptedException e) {
            IOException ioe = new IOException("interrupted while waiting for cvs log to finish");
            ioe.initCause(e);
            throw ioe;
        }

        return changeLog;
    }

    private static ChangeLog readChangeLog(InputStream input) throws IOException {
        ChangeLogReader reader = new ChangeLogReader(input);
        try {
            ChangeLog log = new ChangeLog();
            for (LogEntry entry : reader.readAll()) {
                log.add(entry);
            }
            return log;
        } finally {
            reader.close();
        }
    }

}

SourceForge