Author: fred
Date: 2007-08-16 15:12:02 +0000 (Thu, 16 Aug 2007)
New Revision: 14722
Added:
trunk/plugins/Echo/src/plugins/echo/Project.java
trunk/plugins/Echo/src/plugins/echo/ProjectManager.java
trunk/plugins/Echo/src/plugins/echo/Util.java
Modified:
trunk/plugins/Echo/src/plugins/echo/Echo.java
trunk/plugins/Echo/src/plugins/echo/NodesManager.java
trunk/plugins/Echo/src/plugins/echo/i18n/echo.i18n.en.properties
trunk/plugins/Echo/src/xml/edit.xsl
Log:
Enable the user to have more than one project
Modified: trunk/plugins/Echo/src/plugins/echo/Echo.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/Echo.java 2007-08-16 13:58:18 UTC
(rev 14721)
+++ trunk/plugins/Echo/src/plugins/echo/Echo.java 2007-08-16 15:12:02 UTC
(rev 14722)
@@ -35,6 +35,8 @@
import java.io.FileNotFoundException;
import java.io.IOException;
+import freenet.keys.*;
+
// TODO
// * Exceptions !
@@ -42,14 +44,13 @@
public class Echo implements FredPlugin, FredPluginHTTP,
FredPluginHTTPAdvanced, FredPluginThreadless {
public static final File BASE_DIR = new File("plugins/Echo/");
- public static final File NODES_DIR = new File(BASE_DIR.getPath() +
File.separator + "nodes");
- public static final File BLOCKS_DIR = new File(BASE_DIR.getPath() +
File.separator + "blocks");
+ public static final int PROJECT_ID_LENGTH = 3;
public static final int NODE_ID_LENGTH = 4;
public static final int CATEGORY_ID_LENGTH = 3;
private static final int MAX_TITLE_LENGTH = 200;
private static final int MAX_BODY_LENGTH = 100000;
- private static final int MAX_ID_LENGTH = Math.max(NODE_ID_LENGTH,
CATEGORY_ID_LENGTH);
+ private static final int MAX_ID_LENGTH = Math.max(PROJECT_ID_LENGTH,
Math.max(NODE_ID_LENGTH, CATEGORY_ID_LENGTH));
private static final int MAX_OBJECT_LENGTH = 8;
private static final int MAX_CATEGORY_NAME_LENGTH = 100;
@@ -58,16 +59,19 @@
private XSLTransform transform;
private I18n i18n;
private Page page;
+ private ProjectManager projectManager;
+ private Project project;
private NodesManager nodesManager;
private BlockManager blockManager;
private Node node;
+// private InsertTest test;
+
public Echo() throws PluginHTTPException
{
try {
i18n = new I18n("en");
- // suxx
- NODES_DIR.mkdirs();
+
parser = new Builder();
Document styleSheet =
parser.build(getClass().getResourceAsStream("/xml/edit.xsl"));
@@ -75,11 +79,10 @@
i18n.translateXML(styleSheet);
transform = new XSLTransform(styleSheet);
- transform.setParameter("baseDir",
BASE_DIR.getAbsolutePath() + "/");
- nodesManager = new NodesManager(BASE_DIR);
- blockManager = new BlockManager(BLOCKS_DIR);
-
+ projectManager = new ProjectManager(BASE_DIR);
+ setProject(projectManager.loadProject("001"));
+
} catch (Exception e) {
e.printStackTrace(System.err);
}
@@ -95,6 +98,10 @@
public void runPlugin(PluginRespirator p) {
this.respirator = p;
+ /*
+ test = new InsertTest(respirator.getNode(), new
File("/home/fred/test.jpg"));
+ System.err.println("Test.isNull ?? : " + (test == null));*/
+
}
public void terminate() {
@@ -102,6 +109,14 @@
// Bleh
}
+ private void setProject(Project p) {
+
+ this.project = p;
+ this.nodesManager = project.getNodesManager();
+ transform.setParameter("baseDir",
project.getProjectDir().getAbsolutePath() + "/");
+
+ }
+
private String transform(Page page) {
try {
@@ -227,18 +242,35 @@
Element pub = new Element("publish");
page = new Page(pub);
- page.setTitle(i18n.getString("echo.action.publish"));
- try {
- SiteGenerator test = new SiteGenerator();
- test.generate();
-
- pub.addAttribute(new Attribute("outputdir",(new
File(BASE_DIR.getPath() + File.separator + "out")).getAbsolutePath() + "/"));
-
- } catch (Exception e) {
- page.appendError(e.getMessage());
+// page.setTitle(i18n.getString("echo.action.publish"));
+
+// try {
+// SiteGenerator test = new SiteGenerator();
+// test.generate();
+//
+// pub.addAttribute(new Attribute("outputdir",(new
File(BASE_DIR.getPath() + File.separator + "out")).getAbsolutePath() + "/"));
+//
+// } catch (Exception e) {
+// page.appendError(e.getMessage());
+// }
+ } else if ("configure".equals(fileName)) {
+
+ Element configure = new Element("configure");
+ Element projects = new Element("projects");
+ configure.appendChild(projects);
+
+ for(String id : projectManager.getProjectsIds()) {
+
+ Element project = new Element("project");
+ project.addAttribute(new Attribute("id", id));
+ projects.appendChild(project);
+
}
+ page = new Page(configure);
+ page.setTitle(i18n.getString("echo.action.configure"));
+
} else if ("nodes".equals(fileName)) {
setNodesPage();
Modified: trunk/plugins/Echo/src/plugins/echo/NodesManager.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/NodesManager.java 2007-08-16
13:58:18 UTC (rev 14721)
+++ trunk/plugins/Echo/src/plugins/echo/NodesManager.java 2007-08-16
15:12:02 UTC (rev 14722)
@@ -22,17 +22,15 @@
public class NodesManager {
private Builder parser;
- private File baseDir;
private File nodesDir;
private File categoriesFile;
private HashMap<String, File> nodes;
private HashMap<String, String> categories;
- public NodesManager(File baseDirectory) throws IOException,
ParsingException{
+ public NodesManager(File nodesDirectory) throws IOException,
ParsingException{
- baseDir = baseDirectory;
- nodesDir = new File(baseDir.getPath() + File.separator +
"nodes");
- categoriesFile = new File(baseDir.getPath() + File.separator +
"categories.xml");
+ nodesDir = nodesDirectory;
+ categoriesFile = new File(nodesDir.getPath() + File.separator +
"categories.xml");
nodes = new HashMap<String, File> ();
parser = new Builder();
Added: trunk/plugins/Echo/src/plugins/echo/Project.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/Project.java
(rev 0)
+++ trunk/plugins/Echo/src/plugins/echo/Project.java 2007-08-16 15:12:02 UTC
(rev 14722)
@@ -0,0 +1,57 @@
+package plugins.echo;
+
+import plugins.echo.block.BlockManager;
+
+import java.util.Properties;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import nu.xom.ParsingException;
+
+public class Project {
+
+ private File projectDir;
+ private Properties projectConfig;
+ private NodesManager nodesManager;
+ private BlockManager blockManager;
+
+
+ public Project(File projectDir) throws FileNotFoundException,
ParsingException, IOException {
+
+ this.projectDir = projectDir;
+ projectConfig = new Properties();
+ projectConfig.loadFromXML(new
FileInputStream(projectDir.getPath() + File.separator + "conf.xml"));
+
+ nodesManager = new NodesManager(new File(projectDir.getPath() +
File.separator + "nodes"));
+ blockManager = new BlockManager(new File(projectDir.getPath() +
File.separator + "blocks"));
+
+ }
+
+ public File getProjectDir() {
+
+ return projectDir;
+
+ }
+
+ public String getName() {
+
+ return projectConfig.getProperty("name");
+
+ }
+
+ public NodesManager getNodesManager() {
+
+ return nodesManager;
+
+ }
+
+ public BlockManager getBlockManger() {
+
+ return blockManager;
+
+ }
+
+
+}
\ No newline at end of file
Added: trunk/plugins/Echo/src/plugins/echo/ProjectManager.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/ProjectManager.java
(rev 0)
+++ trunk/plugins/Echo/src/plugins/echo/ProjectManager.java 2007-08-16
15:12:02 UTC (rev 14722)
@@ -0,0 +1,88 @@
+package plugins.echo;
+
+import java.util.HashMap;
+import java.util.Properties;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.FileNotFoundException;
+
+import nu.xom.ParsingException;
+
+public class ProjectManager {
+
+ private File baseDir;
+ private HashMap<String,File> projects;
+
+ public ProjectManager(File baseDir) {
+
+ this.baseDir= baseDir;
+ projects = new HashMap<String,File>();
+
+ File[] files = baseDir.listFiles();
+ for(File f : files) {
+ if(f.isDirectory() && f.getName().matches("[0-9]{" +
Echo.PROJECT_ID_LENGTH + "}"))
+ projects.put(f.getName(), f);
+ }
+
+ }
+
+ public Project loadProject(String projectId) throws
FileNotFoundException, ParsingException, IOException {
+
+ if(projects.containsKey(projectId)) {
+
+ return new Project(projects.get(projectId));
+
+ }
+
+ return null;
+ }
+
+ public Project newProject(String name) throws IOException,
ParsingException {
+
+ String id = "";
+ for(int i=1; i < Math.pow(10, Echo.PROJECT_ID_LENGTH); i++) {
+ id = String.valueOf(i);
+ while(id.length() < Echo.PROJECT_ID_LENGTH)
+ id = "0" + id;
+
+ if(! projects.containsKey(id))
+ break;
+ }
+
+ File projectDir = new File(baseDir.getPath() + File.separator +
id);
+ if(projectDir.mkdirs()) {
+
+ (new File(projectDir.getPath() + File.separator +
"nodes")).mkdirs();
+ (new File(projectDir.getPath() + File.separator +
"blocks")).mkdirs();
+
+ FileOutputStream configFile = new
FileOutputStream(projectDir.getPath() + File.separator + "conf.xml");
+ Properties conf = new Properties();
+ conf.setProperty("name", name);
+ conf.storeToXML(configFile, null);
+ configFile.close();
+
+ projects.put(id, projectDir);
+ return loadProject(id);
+
+ } else
+ throw new IOException("Unable to make the project
directory");
+
+ }
+
+ public void removeProject(String projectId) {
+
+ if(projects.containsKey(projectId)) {
+ if(Util.deleteDirectory(projects.get(projectId)))
+ projects.remove(projectId);
+
+ }
+ }
+
+ public String[] getProjectsIds() {
+
+ return projects.keySet().toArray(new String[]{});
+
+
+ }
+}
\ No newline at end of file
Added: trunk/plugins/Echo/src/plugins/echo/Util.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/Util.java
(rev 0)
+++ trunk/plugins/Echo/src/plugins/echo/Util.java 2007-08-16 15:12:02 UTC
(rev 14722)
@@ -0,0 +1,29 @@
+package plugins.echo;
+
+import java.io.File;
+
+public class Util {
+
+
+ /**
+ * Recursively delete a directory
+ * @param path The directory to delete
+ * @return true if and only if the directory is successfully
deleted; false otherwise
+ */
+ public static boolean deleteDirectory (File path) {
+
+ boolean success = true;
+ File[] files = path.listFiles();
+ for(File f : files) {
+ if(f.isDirectory())
+ success &= deleteDirectory(f);
+ else
+ success &= f.delete();
+ }
+
+ return success & path.delete();
+
+ }
+
+
+}
\ No newline at end of file
Modified: trunk/plugins/Echo/src/plugins/echo/i18n/echo.i18n.en.properties
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/i18n/echo.i18n.en.properties
2007-08-16 13:58:18 UTC (rev 14721)
+++ trunk/plugins/Echo/src/plugins/echo/i18n/echo.i18n.en.properties
2007-08-16 15:12:02 UTC (rev 14722)
@@ -2,6 +2,7 @@
echo.action.write=Write
echo.action.manage=Manage
echo.action.publish=Publish
+echo.action.configure=Configure
echo.common.blogPost=Blog post
echo.common.staticPage=Static page
Modified: trunk/plugins/Echo/src/xml/edit.xsl
===================================================================
--- trunk/plugins/Echo/src/xml/edit.xsl 2007-08-16 13:58:18 UTC (rev 14721)
+++ trunk/plugins/Echo/src/xml/edit.xsl 2007-08-16 15:12:02 UTC (rev 14722)
@@ -16,10 +16,10 @@
<xsl:param name="baseDir" />
- <xsl:variable name="categoriesFile"><xsl:value-of select="$baseDir"
/>categories.xml</xsl:variable>
<xsl:variable name="nodesDir"><xsl:value-of select="$baseDir"
/>nodes/</xsl:variable>
<xsl:variable name="blocksDir"><xsl:value-of select="$baseDir"
/>blocks/</xsl:variable>
-
+ <xsl:variable name="categoriesFile"><xsl:value-of select="$nodesDir"
/>categories.xml</xsl:variable>
+
<!-- <xsl:import href="block.xsl" /> -->
<xsl:template match="/page">
@@ -39,6 +39,7 @@
<li><a
href="write"><i18n key="echo.action.write" /></a></li>
<li><a
href="manage"><i18n key="echo.action.manage" /></a></li>
<li><a
href="publish"><i18n key="echo.action.publish" /></a></li>
+ <li><a
href="configure"><i18n key="echo.action.configure" /></a></li>
</ul>
</div>
@@ -81,7 +82,23 @@
</dl>
</xsl:when>
-
+
+ <xsl:when test="publish">
+ <a href="file://{publish/@outputdir}">Output
dir</a>
+ </xsl:when>
+
+ <xsl:when test="configure">
+ <form name="" method="POST" action="">
+ <select name="project">
+ <xsl:for-each
select="configure/projects/project">
+ <option
value="@id"><xsl:value-of select="@id" /></option>
+ </xsl:for-each>
+ </select>
+ <input type="hidden"
name="formPassword" value="{formpassword/@value}" class="inline" />
+ <input type="submit" value="Load"
class="inline" />
+ </form>
+ </xsl:when>
+
<xsl:when test="categories">
<xsl:if
test="count(document($categoriesFile)//category) > 0">
<table>
@@ -251,10 +268,6 @@
</form>
</xsl:when>
- <xsl:when test="publish">
- <a href="file://{publish/@outputdir}">Output
dir</a>
- </xsl:when>
-
<xsl:otherwise>
Welcome
</xsl:otherwise>