Author: fred
Date: 2007-07-27 15:27:25 +0000 (Fri, 27 Jul 2007)
New Revision: 14380

Added:
   trunk/plugins/Echo/src/plugins/echo/Nodes.java
Modified:
   trunk/plugins/Echo/src/plugins/echo/Node.java
   trunk/plugins/Echo/src/plugins/echo/NodesManager.java
   trunk/plugins/Echo/src/plugins/echo/Test.java
Log:
new class Nodes : A simple list of nodes

Modified: trunk/plugins/Echo/src/plugins/echo/Node.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/Node.java       2007-07-27 14:46:54 UTC 
(rev 14379)
+++ trunk/plugins/Echo/src/plugins/echo/Node.java       2007-07-27 15:27:25 UTC 
(rev 14380)
@@ -8,6 +8,7 @@

 import java.util.Date;
 import java.util.Calendar;
+import java.util.StringTokenizer;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.IOException;
@@ -17,6 +18,8 @@
        public enum NodeType { POST_NODE, STATIC_PAGE_NODE };

        private String id;
+       private NodeType type = null;
+       private Date creationDate = null;
        private Document doc = null;
        private Element nodeElement = null;
        private Element modifiedElement = null;
@@ -39,13 +42,15 @@
                }

                this.id = nodeId;
+               this.type = nodeType;

                nodeElement = new Element("node");
                nodeElement.addAttribute(new Attribute("id", id));
                nodeElement.addAttribute(new Attribute("type", type));

+               creationDate = new Date();
                Element createdElement = new Element("created");
-               createdElement.appendChild(dateToString(new Date()));
+               createdElement.appendChild(dateToString(creationDate));
                nodeElement.appendChild(createdElement);

                modifiedElement = new Element("modified");
@@ -82,6 +87,21 @@
                return str;
        }

+       public static Date stringToDate(String str) {
+       
+               StringTokenizer tokenizer = new StringTokenizer(str, "/");
+               if(tokenizer.countTokens() == 3) {
+                       Calendar calendar = Calendar.getInstance();
+                       calendar.set(Calendar.YEAR, 
Integer.parseInt(tokenizer.nextToken()));
+                       calendar.set(Calendar.MONTH, 
Integer.parseInt(tokenizer.nextToken()));
+                       calendar.set(Calendar.DAY_OF_MONTH, 
Integer.parseInt(tokenizer.nextToken()));
+                       
+                       return calendar.getTime();
+               }
+               
+               return null;
+       }
+       
        private Element getModifiedElement() {
                if(modifiedElement == null)
                        modifiedElement = (Element) 
doc.query("/node/modified").get(0);
@@ -93,6 +113,13 @@
                modifiedElement.appendChild(dateToString(date));
        }

+       public Date getCreationDate() {
+               if(creationDate == null) 
+                       creationDate = stringToDate(((Element) 
doc.query("/node/modified").get(0)).getValue());
+                       
+               return creationDate;
+       }
+       
        private Element getCategoriesElement() {
                if(categoriesElement == null)
                        categoriesElement = (Element) 
doc.query("/node/categories").get(0);
@@ -152,6 +179,18 @@
                return id;
        }

+       public NodeType getType() {
+               if(type == null) {
+                       String t = ((Attribute) 
doc.query("/node/@type").get(0)).getValue();
+                       if("post".equals(t))
+                               type = NodeType.POST_NODE;
+                       else
+                               type = NodeType.STATIC_PAGE_NODE;
+               }
+               
+               return type;
+       }
+       
        protected Document getDoc() {
                return doc;
        }

Added: trunk/plugins/Echo/src/plugins/echo/Nodes.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/Nodes.java                              
(rev 0)
+++ trunk/plugins/Echo/src/plugins/echo/Nodes.java      2007-07-27 15:27:25 UTC 
(rev 14380)
@@ -0,0 +1,62 @@
+package plugins.echo;
+
+import java.util.Vector;
+import java.util.Comparator;
+import java.util.Collections;
+
+/**
+*      A simple list of nodes
+*/
+public class Nodes {
+
+       private Vector<Node> nodes;
+       
+       public Nodes() {
+               
+               nodes = new Vector<Node>();
+               
+       }
+       
+       public class CreationDateComparator implements Comparator {
+       
+               public int compare(Object node1, Object node2) {
+                       if(! (node1 instanceof Node && node2 instanceof Node))
+                               throw new ClassCastException();
+                       
+                       return ((Node) 
node1).getCreationDate().compareTo(((Node) node2).getCreationDate());
+               }
+       }
+       
+       /**
+       *       Adds a node at the end of the list.
+       *       @param node the node to add to the list
+       */
+       public void append(Node node) {
+       
+               nodes.add(node);
+               
+       }
+
+       /**
+       *       Returns the index<sup>th</sup> node of the list.
+       *       @param index the index of the node to return
+       *       @return the node at the specified position 
+       */
+       public Node get(int index) {
+       
+               return nodes.get(index);
+               
+       }
+
+       /**
+       *       Sorts the list by creation date of the nodes using the 
CreationDateComparator.
+       */
+       public void sortByCreationDate() {
+       
+               Collections.sort(nodes, new CreationDateComparator());
+
+       }
+
+       
+
+}
\ No newline at end of file

Modified: trunk/plugins/Echo/src/plugins/echo/NodesManager.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/NodesManager.java       2007-07-27 
14:46:54 UTC (rev 14379)
+++ trunk/plugins/Echo/src/plugins/echo/NodesManager.java       2007-07-27 
15:27:25 UTC (rev 14380)
@@ -27,7 +27,6 @@
        private HashMap<String, File> nodes;
        private HashMap<String, String> categories;

-       
        public NodesManager(File baseDirectory) throws IOException, 
ParsingException{

                baseDir = baseDirectory;
@@ -51,7 +50,7 @@
                }

        }
-
+       
        public Node getNodeById(String nodeId) throws IOException, 
ParsingException {

                File file = nodes.get(nodeId);
@@ -62,6 +61,20 @@

        }

+       public Nodes getPosts() throws IOException, ParsingException {
+               
+               Nodes posts = new Nodes();
+               String[] ids = getIds();
+               for(String id : ids) {
+               
+                       Node node = getNodeById(id);
+                       if(node.getType() == Node.NodeType.POST_NODE)
+                               posts.append(node);
+               }
+               
+               return posts;
+       }
+       
        // TODO + use Echo.
        public String getFreeNodeId() {


Modified: trunk/plugins/Echo/src/plugins/echo/Test.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/Test.java       2007-07-27 14:46:54 UTC 
(rev 14379)
+++ trunk/plugins/Echo/src/plugins/echo/Test.java       2007-07-27 15:27:25 UTC 
(rev 14380)
@@ -37,14 +37,14 @@
                        Element content = (Element) 
root.query("./content").get(0);
                        Text contentText = (Text) content.getChild(0);

-                       Nodes result = render.render(contentText.toXML());
+                       nu.xom.Nodes result = 
render.render(contentText.toXML());

                        contentText.detach();
                        for (int i=0; i < result.size(); i++) {
                                content.appendChild(result.get(i));
                        }

-                       Nodes t = transform.transform(new Document(root));
+                       nu.xom.Nodes t = transform.transform(new 
Document(root));
                        serializer.setOutputStream(new 
FileOutputStream(outDir.getPath() + File.separator + id + ".html"));
                        serializer.write(new Document((Element) t.get(0)));



Reply via email to