Changeset 26045 for main/trunk/greenstone3/src/java/org/greenstone
- Timestamp:
- 2012-07-31T15:06:27+12:00 (12 years ago)
- Location:
- main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/AbstractBasicDocument.java
r26041 r26045 19 19 package org.greenstone.gsdl3.util; 20 20 21 import java.util.ArrayList; 22 21 23 import org.apache.log4j.*; 22 24 import org.w3c.dom.Document; … … 24 26 25 27 public abstract class AbstractBasicDocument { 28 29 /** info types */ 30 protected static final String INFO_NUM_SIBS = "numSiblings"; 31 protected static final String INFO_NUM_CHILDREN = "numChildren"; 32 protected static final String INFO_SIB_POS = "siblingPosition"; 33 protected static final String INFO_DOC_TYPE = "documentType"; 26 34 27 35 /** XML element for describe requests - the container doc */ … … 59 67 } 60 68 69 /** 70 * adds all the children of doc_id to the doc element, and if 71 * recursive=true, adds all their children as well 72 */ 73 abstract public void addDescendants(Element doc, String doc_id, boolean recursive); 74 75 76 /** 77 * adds all the siblings of current_id to the parent element. returns the 78 * new current element 79 */ 80 abstract public Element addSiblings(Element parent_node, String parent_id, String current_id); 61 81 62 82 /** returns the node type of the specified node. … … 97 117 abstract public boolean hasChildren(String node_id); 98 118 119 /** 120 * returns the structural information asked for. info_type may be one of 121 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS, INFO_DOC_TYPE 122 */ 123 abstract public String getStructureInfo(String doc_id, String info_type); 124 125 abstract public int getNumChildren(String node_id) ; 126 /** returns a list of the child ids in order, null if no children 127 */ 128 abstract public ArrayList<String> getChildrenIds(String node_id); 99 129 /** returns true if the node has a parent 100 130 */ 101 131 abstract public boolean hasParent(String node_id); 132 133 /** 134 * returns the node id of the parent node, null if no parent 135 */ 136 abstract public String getParentId(String node_id); 137 138 /** 139 * returns the node id of the root node of the document containing node_id 140 */ 141 abstract public String getRootId(String node_id); 142 143 /** returns the list of sibling ids, including the specified node_id */ 144 abstract public ArrayList<String> getSiblingIds(String node_id); 102 145 } -
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/BasicDocument.java
r26041 r26045 18 18 */ 19 19 package org.greenstone.gsdl3.util; 20 21 import java.util.ArrayList; 20 22 21 23 import org.apache.log4j.*; … … 39 41 } 40 42 43 /** 44 * returns the structural information asked for. info_type may be one of 45 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS, INFO_DOC_TYPE 46 */ 47 public String getStructureInfo(String doc_id, String info_type) { 48 if (info_type.equals(INFO_NUM_SIBS)) { 49 return "0"; 50 } 51 if (info_type.equals(INFO_NUM_CHILDREN)) { 52 return "0"; 53 } 54 if (info_type.equals(INFO_SIB_POS)) { 55 return "-1"; 56 } 57 if (info_type.equals(INFO_DOC_TYPE)) { 58 return getDocType(doc_id); 59 } 60 return null; 61 } 41 62 /** returns the document type of the doc that the specified node 42 63 belongs to. should be one of … … 62 83 return false; 63 84 } 64 85 public int getNumChildren(String node_id) { 86 return 0; 87 } 88 /** returns a list of the child ids in order, null if no children 89 * default implementation: return null 90 */ 91 public ArrayList<String> getChildrenIds(String node_id) { 92 return null; 93 } 65 94 /** returns true if the node has a parent 66 95 * default implementation returns false, over ride if documents can be … … 69 98 return false; 70 99 } 100 101 /** 102 * returns the node id of the parent node, null if no parent 103 * default implementation return null 104 */ 105 public String getParentId(String node_id) { 106 return null; 107 } 108 109 /** 110 * returns the node id of the root node of the document containing node_id 111 * default implementation: return node_id 112 */ 113 public String getRootId(String node_id) { 114 return node_id; 115 } 116 /** 117 * adds all the children of doc_id to the doc element, and if 118 * recursive=true, adds all their children as well 119 * default implementation: do nothing 120 */ 121 public void addDescendants(Element doc, String doc_id, boolean recursive) { 122 return; 123 } 124 125 /** 126 * adds all the siblings of current_id to the parent element. returns the 127 * new current element 128 */ 129 public Element addSiblings(Element parent_node, String parent_id, String current_id) 130 { 131 return null; 132 } 133 134 /** returns the list of sibling ids, including the specified node_id */ 135 public ArrayList<String> getSiblingIds(String node_id) { 136 return null; 137 } 138 139 71 140 } -
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/BasicDocumentDatabase.java
r26041 r26045 36 36 import java.util.Map; 37 37 import java.util.Set; 38 import java.util.StringTokenizer; 38 39 import java.util.Iterator; 39 40 import java.io.File; 40 41 42 import org.apache.commons.lang3.StringUtils; 41 43 import org.apache.log4j.*; 42 44 … … 136 138 } 137 139 140 /** 141 * returns the structural information asked for. info_type may be one of 142 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS, INFO_DOC_TYPE 143 */ 144 public String getStructureInfo(String doc_id, String info_type){ 145 String value = ""; 146 if (info_type.equals(INFO_NUM_SIBS)) 147 { 148 String parent_id = OID.getParent(doc_id); 149 if (parent_id.equals(doc_id)) 150 { 151 value = "0"; 152 } 153 else 154 { 155 value = String.valueOf(getNumChildren(parent_id)); 156 } 157 return value; 158 } 159 160 if (info_type.equals(INFO_NUM_CHILDREN)) 161 { 162 return String.valueOf(getNumChildren(doc_id)); 163 } 164 165 if (info_type.equals(INFO_SIB_POS)) 166 { 167 String parent_id = OID.getParent(doc_id); 168 if (parent_id.equals(doc_id)) 169 { 170 return "-1"; 171 } 172 173 DBInfo info = this.coll_db.getInfo(parent_id); 174 if (info == null) 175 { 176 return "-1"; 177 } 178 179 String contains = info.getInfo("contains"); 180 contains = StringUtils.replace(contains, "\"", parent_id); 181 String[] children = contains.split(";"); 182 for (int i = 0; i < children.length; i++) 183 { 184 String child_id = children[i]; 185 if (child_id.equals(doc_id)) 186 { 187 return String.valueOf(i + 1); // make it from 1 to length 188 189 } 190 } 191 192 return "-1"; 193 } 194 if (info_type.equals(INFO_DOC_TYPE)) 195 196 { 197 return getDocType(doc_id); 198 } 199 return null; 200 201 } 202 138 203 /** returns true if the node has child nodes */ 139 204 public boolean hasChildren(String node_id){ … … 149 214 } 150 215 216 public int getNumChildren(String node_id) { 217 DBInfo info = this.coll_db.getInfo(node_id); 218 if (info == null) 219 { 220 return 0; 221 } 222 String contains = info.getInfo("contains"); 223 if (contains.equals("")) 224 { 225 return 0; 226 } 227 String[] children = contains.split(";"); 228 return children.length; 229 230 231 } 232 /** returns a list of the child ids in order, null if no children 233 */ 234 public ArrayList<String> getChildrenIds(String node_id) { 235 DBInfo info = this.coll_db.getInfo(node_id); 236 if (info == null) 237 { 238 return null; 239 } 240 241 String contains = info.getInfo("contains"); 242 if (contains.equals("")) 243 { 244 return null; 245 } 246 ArrayList<String> children = new ArrayList<String>(); 247 StringTokenizer st = new StringTokenizer(contains, ";"); 248 while (st.hasMoreTokens()) 249 { 250 String child_id = StringUtils.replace(st.nextToken(), "\"", node_id); 251 children.add(child_id); 252 } 253 return children; 254 255 256 } 257 151 258 /** returns true if the node has a parent */ 152 259 public boolean hasParent(String node_id){ … … 157 264 return true; 158 265 } 266 /** 267 * returns the node id of the parent node, null if no parent 268 */ 269 public String getParentId(String node_id) { 270 String parent = OID.getParent(node_id); 271 if (parent.equals(node_id)) 272 { 273 return null; 274 } 275 return parent; 276 277 } 159 278 279 /** 280 * returns the node id of the root node of the document containing node_id 281 */ 282 public String getRootId(String node_id) { 283 return OID.getTop(node_id); 284 } 285 160 286 /** convert indexer internal id to Greenstone oid */ 161 287 public String internalNum2OID(long docnum) … … 169 295 } 170 296 297 298 /** 299 * adds all the children of doc_id to the doc element, and if 300 * recursive=true, adds all their children as well 301 */ 302 public void addDescendants(Element doc, String doc_id, boolean recursive) 303 { 304 ArrayList<String> child_ids = getChildrenIds(doc_id); 305 if (child_ids == null) 306 return; 307 for (int i = 0; i < child_ids.size(); i++) 308 { 309 String child_id = child_ids.get(i); 310 Element child_elem = createDocNode(child_id); 311 doc.appendChild(child_elem); 312 if (recursive && (!child_elem.getAttribute(GSXML.NODE_TYPE_ATT).equals(GSXML.NODE_TYPE_LEAF) || child_elem.getAttribute(GSXML.DOC_TYPE_ATT).equals(GSXML.DOC_TYPE_PAGED))) 313 { 314 addDescendants(child_elem, child_id, recursive); 315 } 316 } 317 } 318 319 /** 320 * adds all the siblings of current_id to the parent element. returns the 321 * new current element 322 */ 323 public Element addSiblings(Element parent_node, String parent_id, String current_id) 324 { 325 Element current_node = GSXML.getFirstElementChild(parent_node);//(Element)parent_node.getFirstChild(); 326 if (current_node == null) 327 { 328 // create a sensible error message 329 logger.error(" there should be a first child."); 330 return null; 331 } 332 // remove the current child,- will add it in later in its correct place 333 parent_node.removeChild(current_node); 334 335 // add in all the siblings, 336 addDescendants(parent_node, parent_id, false); 337 338 // find the node that is now the current node 339 // this assumes that the new node that was created is the same as 340 // the old one that was removed - we may want to replace the new one 341 // with the old one. 342 Element new_current = GSXML.getNamedElement(parent_node, current_node.getNodeName(), GSXML.NODE_ID_ATT, current_id); 343 return new_current; 344 } 345 346 /** returns the list of sibling ids, including the specified node_id */ 347 public ArrayList<String> getSiblingIds(String node_id){ 348 String parent_id = getParentId(node_id); 349 if (parent_id == null) 350 { 351 return null; 352 } 353 return getChildrenIds(parent_id); 354 355 } 356 171 357 } 172 358
Note:
See TracChangeset
for help on using the changeset viewer.