[5800] | 1 | package org.greenstone.gsdl3.gs3build.util;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.List;
|
---|
| 5 |
|
---|
| 6 | import org.greenstone.gsdl3.gs3build.doctypes.DocumentID;
|
---|
| 7 |
|
---|
| 8 | public class FileTrie
|
---|
| 9 | { String stem;
|
---|
| 10 | List childTries;
|
---|
| 11 | List childDocs;
|
---|
| 12 | FileTrie parent;
|
---|
| 13 |
|
---|
| 14 | public FileTrie(String stem)
|
---|
| 15 | { this.childTries = new ArrayList();
|
---|
| 16 | this.childDocs = new ArrayList();
|
---|
| 17 | this.stem = stem;
|
---|
| 18 | this.parent = null;
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | public void setParent(FileTrie trie)
|
---|
| 22 | { this.parent = trie;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | public FileTrie getParent()
|
---|
| 26 | { return this.parent;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public void addChild(FileTrie trie)
|
---|
| 30 | { trie.setParent(this);
|
---|
| 31 | this.childTries.add(trie);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | public void replaceChild(FileTrie oldChild, FileTrie newChild)
|
---|
| 35 | { for (int i = 0; i < this.childTries.size(); i ++)
|
---|
| 36 | { FileTrie thisChild = (FileTrie) this.childTries.get(i);
|
---|
| 37 | if (thisChild == oldChild)
|
---|
| 38 | { this.childTries.set(i, newChild);
|
---|
| 39 | return;
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public void addDocument(DocumentID documentID)
|
---|
| 45 | { this.childDocs.add(documentID);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public boolean postDocument(String filename, DocumentID documentID)
|
---|
| 49 | { int common = 0;
|
---|
| 50 | int i;
|
---|
| 51 |
|
---|
| 52 | while (common < filename.length() &&
|
---|
| 53 | common < this.stem.length() &&
|
---|
| 54 | filename.charAt(common) == stem.charAt(common))
|
---|
| 55 | { common ++;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | if (common == 0)
|
---|
| 59 | { return false;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | if (common == this.stem.length())
|
---|
| 63 | { // post it in this node
|
---|
| 64 | filename = filename.substring(this.stem.length());
|
---|
| 65 |
|
---|
| 66 | // select which sub-trie to attempt...
|
---|
| 67 | for (i = 0; i < this.childTries.size(); i ++)
|
---|
| 68 | { FileTrie child = (FileTrie) this.childTries.get(i);
|
---|
| 69 |
|
---|
| 70 | if (child.postDocument(filename, documentID))
|
---|
| 71 | { break;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | if (i == this.childTries.size())
|
---|
| 76 | { FileTrie newChild = new FileTrie(filename);
|
---|
| 77 | this.addChild(newChild);
|
---|
| 78 |
|
---|
| 79 | newChild.addDocument(documentID);
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | else
|
---|
| 83 | { // divide this node at the 'common' point...
|
---|
| 84 | FileTrie parentTrie = new FileTrie(this.stem.substring(0, common));
|
---|
| 85 | this.parent.replaceChild(this, parentTrie);
|
---|
| 86 |
|
---|
| 87 | // truncate our root
|
---|
| 88 | this.stem = this.stem.substring(common);
|
---|
| 89 |
|
---|
| 90 | // add ourselves to our new parent
|
---|
| 91 | parentTrie.addChild(this);
|
---|
| 92 |
|
---|
| 93 | // trim back the file name
|
---|
| 94 | filename = filename.substring(common);
|
---|
| 95 |
|
---|
| 96 | // recurse into posting the document
|
---|
| 97 | this.postDocument(filename, documentID);
|
---|
| 98 | }
|
---|
| 99 | return true;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public boolean postFile(String filename)
|
---|
| 103 | { return this.postDocument(filename, null);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public List getDocuments(String filename)
|
---|
| 107 | { FileTrie leaf = this.getNode(filename);
|
---|
| 108 | return leaf.childDocs;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | public FileTrie getNode(String filename)
|
---|
| 112 | { int common = 0;
|
---|
| 113 | int i;
|
---|
| 114 |
|
---|
| 115 | while (common < filename.length() &&
|
---|
| 116 | common < this.stem.length() &&
|
---|
| 117 | filename.charAt(common) == stem.charAt(common))
|
---|
| 118 | { common ++;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | if (common == 0)
|
---|
| 122 | { return null;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | if (common == this.stem.length())
|
---|
| 126 | { if (common == filename.length())
|
---|
| 127 | { return this;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | filename = filename.substring(this.stem.length());
|
---|
| 131 |
|
---|
| 132 | // select which sub-trie to attempt...
|
---|
| 133 | for (i = 0; i < this.childTries.size(); i ++)
|
---|
| 134 | { FileTrie child = (FileTrie) this.childTries.get(i);
|
---|
| 135 |
|
---|
| 136 | child = child.getNode(filename);
|
---|
| 137 |
|
---|
| 138 | if (child != null)
|
---|
| 139 | { return child;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | return null;
|
---|
| 144 | }
|
---|
| 145 | }
|
---|