From c22121819c4fd90d579bcfaf3f3afad16c32932f Mon Sep 17 00:00:00 2001 From: sro00002 Date: Mon, 16 Apr 2012 10:06:54 +0000 Subject: [PATCH] Added classes to obtain files information from a directory tree of documents/shared area of a selected course/group git-svn-id: https://forja.rediris.es/svn/cusl6-swadroid/trunk@284 5bc14d19-1e4b-4ba2-aa50-860af135f48c --- .../modules/downloads/DirectoryItem.java | 109 +++++++ .../modules/downloads/DirectoryNavigator.java | 290 ++++++++++++++++++ 2 files changed, 399 insertions(+) create mode 100644 SWADroid/src/es/ugr/swad/swadroid/modules/downloads/DirectoryItem.java create mode 100644 SWADroid/src/es/ugr/swad/swadroid/modules/downloads/DirectoryNavigator.java diff --git a/SWADroid/src/es/ugr/swad/swadroid/modules/downloads/DirectoryItem.java b/SWADroid/src/es/ugr/swad/swadroid/modules/downloads/DirectoryItem.java new file mode 100644 index 00000000..fb5cd8c3 --- /dev/null +++ b/SWADroid/src/es/ugr/swad/swadroid/modules/downloads/DirectoryItem.java @@ -0,0 +1,109 @@ +package es.ugr.swad.swadroid.modules.downloads; + +/** + * Item description. Represent the elements that will be shown in the menu. + * @author Sergio Ropero Oliver. + * @version 1.0 + */ +public class DirectoryItem +{ + String name; + String type; + String url; + int size; //In bytes + int date; + + /** + * Constructor of a directory type. + * @param name + */ + DirectoryItem(String name) + { + this.name = name; + this.type = "dir"; + } + + /** + * Constructor of a File item. + * @param name + * @param type + * @param url + * @param size + * @param date + */ + DirectoryItem(String name, String type, String url, int size, int date) + { + this.name = name; + this.type = type; + this.url = url; + this.size = size; + this.date = date; + } + + /** + * Check if the item is a folder. + * @return Return true if the item is a folder. False if not. + */ + public boolean isFolder() + { + if(type == "dir") + { + return true; + } + else + { + return false; + } + } + + public void setName(String name) + { + this.name = name; + } + + public void setType(String type) + { + this.type = type; + } + + public void setUrl(String url) + { + this.url = url; + } + + public void setSize(int size) + { + this.size = size; + } + + public void setDate(int date) + { + this.date = date; + } + + + public String getName() + { + return name; + } + + public String getType() + { + return type; + } + + public String getUrl() + { + return url; + } + + public Integer getSize() + { + return size; + } + + public Integer getDate() + { + return date; + } +} diff --git a/SWADroid/src/es/ugr/swad/swadroid/modules/downloads/DirectoryNavigator.java b/SWADroid/src/es/ugr/swad/swadroid/modules/downloads/DirectoryNavigator.java new file mode 100644 index 00000000..75dade3e --- /dev/null +++ b/SWADroid/src/es/ugr/swad/swadroid/modules/downloads/DirectoryNavigator.java @@ -0,0 +1,290 @@ +package es.ugr.swad.swadroid.modules.downloads; + +import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +/** + * Class used to navigate arround the XML file. That XML file contains the + information of all the directory. + * @author Sergio Ropero Oliver. + * @version 1.0 + */ +public class DirectoryNavigator +{ + private String XMLinfo; + ArrayList path; + + /** + * Constructor. + * @param fileXML File where we obtain all the information. + */ + public DirectoryNavigator(String fileXML) + { + this.XMLinfo = new String(fileXML); + this.path = new ArrayList(); + } + + /** + * Travel inside a subdirectory. + * @param subDirectory The subdirectory where we will travel. + * @return Return a list of items that are inside the subdirectory. + * @throws InvalidPath When the directory don't exist. + */ + public List subDirectory(String subDirectory) throws InvalidPath + { + //We increase the path. + path.add(subDirectory); + + Node node = goToDirectory(); + + List itemsToShow; + itemsToShow = new ArrayList(getItems(node)); + + return itemsToShow; + } + + /** + * Travel to the parent directory. + * @return Return a list of items that are inside the parent directory. + * @throws InvalidPath When the directory don't exist. + */ + public List parentDirectory() throws InvalidPath + { + //We decrease the path. + path.remove(path.size()-1); + + Node node = goToDirectory(); + + List itemsToShow; + itemsToShow = new ArrayList(getItems(node)); + + return itemsToShow; + } + + /** + * Refresh the XML file and refresh the directory data. We throw an exception if the directory was erased. + * @return Return a list of items in the current directory. + * @throws InvalidPath When the directory don't exist. + */ + public List refresh(String fileXML) throws InvalidPath + { + this.XMLinfo = fileXML; + + Node node = goToDirectory(); + + List itemsToShow; + itemsToShow = new ArrayList(getItems(node)); + + return itemsToShow; + } + + /** + * Go to the root directory. + * @return The items of the root directory. + * @throws InvalidPath When the directory don't exist. + */ + public List goToRoot() throws InvalidPath + { + path.clear(); + + Node node = goToDirectory(); + + List itemsToShow; + itemsToShow = new ArrayList(getItems(node)); + + return itemsToShow; + } + + /** + * Obtain all the items of the specific directory. + * @param node Node that represents the current directory. + * @return Return a list of items of the directory passed as parameter. + */ + private List getItems(Node node) + { + List items = new ArrayList(); + + NodeList childs = node.getChildNodes(); + + DirectoryItem item; + System.out.println(childs.getLength()); + for(int i=0; i