diff --git a/Document.php b/Document.php index 7803df6..e19e3c3 100644 --- a/Document.php +++ b/Document.php @@ -1,49 +1,28 @@ 5); $this->user = $user; $this->name = $name; } public function getTitle() { - $db = Database::getInstance(); - $row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'); - return $row[3]; // third column in a row + return $this->getColumn(3); // third column in a row } public function getContent() { - $db = Database::getInstance(); - $row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'); - return $row[6]; // sixth column in a row + return $this->getColumn(6); // sixth column in a row } - public static function getAllDocuments() { - // to be implemented later - } - -} - -class User { - - public function makeNewDocument($name) { - $doc = new Document(); - $doc->init($name, $this); - return $doc; - } - - public function getMyDocuments() { - $list = array(); - foreach (Document::getAllDocuments() as $doc) { - if ($doc->user == $this) - $list[] = $doc; - } - return $list; + private function getColumn($column) { + $db = Database::getInstance(); + $row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'); + return $row[$column]; // sixth column in a row } } diff --git a/FileWithContent.java b/FileWithContent.java new file mode 100644 index 0000000..9a192bd --- /dev/null +++ b/FileWithContent.java @@ -0,0 +1,40 @@ +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +/** + * This class is thread safe. + */ +public class FileWithContent { + private File file; + public FileWithContent(File f) { + this.file = f; + } + public String getContent() throws IOException { + return getContent(true); + } + public String getContentWithoutUnicode() throws IOException { + return getContent(false); + } + public synchronized void saveContent(String content) { + try (FileOutputStream o = new FileOutputStream(file)) { + for (int i = 0; i < content.length(); i += 1) { + o.write(content.charAt(i)); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + private synchronized String getContent(boolean withUnicode) throws IOException { + try (FileInputStream i = new FileInputStream(file)) { + String output = ""; + int data; + while ((data = i.read()) > 0) { + if(withUnicode || data >= 0x80) { + output += (char) data; + }) + } + return output; + } + } +} diff --git a/Parser.java b/Parser.java deleted file mode 100644 index c13a9fe..0000000 --- a/Parser.java +++ /dev/null @@ -1,46 +0,0 @@ -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -/** - * This class is thread safe. - */ -public class Parser { - private File file; - public synchronized void setFile(File f) { - file = f; - } - public synchronized File getFile() { - return file; - } - public String getContent() throws IOException { - FileInputStream i = new FileInputStream(file); - String output = ""; - int data; - while ((data = i.read()) > 0) { - output += (char) data; - } - return output; - } - public String getContentWithoutUnicode() throws IOException { - FileInputStream i = new FileInputStream(file); - String output = ""; - int data; - while ((data = i.read()) > 0) { - if (data < 0x80) { - output += (char) data; - } - } - return output; - } - public void saveContent(String content) { - FileOutputStream o = new FileOutputStream(file); - try { - for (int i = 0; i < content.length(); i += 1) { - o.write(content.charAt(i)); - } - } catch (IOException e) { - e.printStackTrace(); - } - } -} diff --git a/User.php b/User.php new file mode 100644 index 0000000..7e7d2d6 --- /dev/null +++ b/User.php @@ -0,0 +1,23 @@ +id = $id; + } + + public function createDocument($name) { + return new Document($name, $this); + } + + public function getMyDocuments() { + $db = Database::getInstance(); + $list = array(); + foreach ($db->query('SELECT name FROM document WHERE user_id = ' . $this->id) as $name) { + $list[] = $this->createDocument($name); + } + return $list; + } + +}