source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/AbstractBasicDocument.java@ 28965

Last change on this file since 28965 was 28965, checked in by kjdon, 10 years ago

removing stored Document field - now get passed it as needed or work it out from Element owner documents

File size: 4.8 KB
Line 
1/*
2 * AbstractBasicDocument.java
3 * Copyright (C) 2011 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21import java.util.ArrayList;
22
23import org.apache.log4j.*;
24import org.w3c.dom.Document;
25import org.w3c.dom.Element;
26
27public abstract class AbstractBasicDocument {
28
29 /** info types */
30 public static final String INFO_NUM_SIBS = "numSiblings";
31 public static final String INFO_NUM_CHILDREN = "numChildren";
32 public static final String INFO_SIB_POS = "siblingPosition";
33 public static final String INFO_DOC_TYPE = "documentType";
34
35 /** XML element for describe requests - the container doc */
36 //protected Document doc = null; // typically a shared reference to the one in ServiceRack
37
38 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.AbstractBasicDocument.class.getName());
39
40 public AbstractBasicDocument()
41 {
42 }
43
44 /** create an element to go into a results list. A node element
45 * has the form
46 * <docNode nodeId='xxx' nodeType='leaf' docType='hierarchy'/>
47 */
48 public Element createDocNode(Document doc, String node_id) {
49 return createDocNode(doc, node_id, null);
50 }
51 /** create an element to go into a results list. A node element
52 * has the form
53 * <docNode nodeId='xxx' nodeType='leaf' docType='hierarchy' [rank='0.23']/>
54 */
55 public Element createDocNode(Document doc, String node_id, String rank) {
56 Element node = doc.createElement(GSXML.DOC_NODE_ELEM);
57 node.setAttribute(GSXML.NODE_ID_ATT, node_id);
58 if (rank != null) {
59 node.setAttribute(GSXML.NODE_RANK_ATT, rank);
60 }
61 String doc_type = getDocType(node_id);
62 node.setAttribute(GSXML.DOC_TYPE_ATT, doc_type);
63 String node_type = getNodeType(node_id, doc_type);
64 node.setAttribute(GSXML.NODE_TYPE_ATT, node_type);
65 return node;
66 }
67
68 /**
69 * adds all the children of doc_id to the doc element, and if
70 * recursive=true, adds all their children as well
71 */
72 abstract public void addDescendants(Element doc, String doc_id, boolean recursive);
73
74
75 /**
76 * adds all the siblings of current_id to the parent element. returns the
77 * new current element
78 */
79 abstract public Element addSiblings(Element parent_node, String parent_id, String current_id);
80
81 /** returns the node type of the specified node.
82 should be one of
83 GSXML.NODE_TYPE_LEAF,
84 GSXML.NODE_TYPE_INTERNAL,
85 GSXML.NODE_TYPE_ROOT
86 */
87 public String getNodeType(String node_id, String doc_type) {
88 if (doc_type.equals(GSXML.DOC_TYPE_SIMPLE)) {
89 return GSXML.NODE_TYPE_LEAF;
90 }
91
92 if (!hasParent(node_id)) {
93 return GSXML.NODE_TYPE_ROOT;
94 }
95 if (doc_type.equals(GSXML.DOC_TYPE_PAGED)) {
96 return GSXML.NODE_TYPE_LEAF;
97 }
98 if (!hasChildren(node_id)) {
99 return GSXML.NODE_TYPE_LEAF;
100 }
101 return GSXML.NODE_TYPE_INTERNAL;
102
103 }
104
105 /** returns the document type of the doc that the specified node
106 belongs to. should be one of
107 GSXML.DOC_TYPE_SIMPLE,
108 GSXML.DOC_TYPE_PAGED,
109 GSXML.DOC_TYPE_PAGEDHIERARCHY,
110 GSXML.DOC_TYPE_HIERARCHY
111 */
112 abstract public String getDocType(String node_id);
113
114 /** returns true if the node has child nodes
115 */
116 abstract public boolean hasChildren(String node_id);
117
118 /**
119 * returns the structural information asked for. info_type may be one of
120 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS, INFO_DOC_TYPE
121 */
122 abstract public String getStructureInfo(String doc_id, String info_type);
123
124 abstract public int getNumChildren(String node_id) ;
125 /** returns a list of the child ids in order, null if no children
126 */
127 abstract public ArrayList<String> getChildrenIds(String node_id);
128 /** returns true if the node has a parent
129 */
130 abstract public boolean hasParent(String node_id);
131
132 /**
133 * returns the node id of the parent node, null if no parent
134 */
135 abstract public String getParentId(String node_id);
136
137 /**
138 * returns the node id of the root node of the document containing node_id
139 */
140 abstract public String getRootId(String node_id);
141
142 /** returns the list of sibling ids, including the specified node_id */
143 abstract public ArrayList<String> getSiblingIds(String node_id);
144}
Note: See TracBrowser for help on using the repository browser.