1 | /*
|
---|
2 | * AbstractSimpleDocument.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 | */
|
---|
19 | package org.greenstone.gsdl3.util;
|
---|
20 |
|
---|
21 | import org.apache.log4j.*;
|
---|
22 | import org.w3c.dom.Document;
|
---|
23 | import org.w3c.dom.Element;
|
---|
24 |
|
---|
25 | public abstract class AbstractSimpleDocument {
|
---|
26 |
|
---|
27 | /** XML element for describe requests - the container doc */
|
---|
28 | protected Document doc = null; // typically a shared reference to the one in ServiceRack
|
---|
29 |
|
---|
30 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.AbstractSimpleDocument.class.getName());
|
---|
31 |
|
---|
32 | public AbstractSimpleDocument(Document doc)
|
---|
33 | {
|
---|
34 | this.doc = doc;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /** create an element to go into the search results list. A node element
|
---|
38 | * has the form
|
---|
39 | * <docNode nodeId='xxx' nodeType='leaf' docType='hierarchy' rank='0.23'/>
|
---|
40 | */
|
---|
41 | public Element createDocNode(String node_id, String rank) {
|
---|
42 | Element node = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
43 | node.setAttribute(GSXML.NODE_ID_ATT, node_id);
|
---|
44 | node.setAttribute(GSXML.NODE_RANK_ATT, rank);
|
---|
45 | String doc_type = getDocType(node_id);
|
---|
46 | node.setAttribute(GSXML.DOC_TYPE_ATT, doc_type);
|
---|
47 | String node_type = getNodeType(node_id, doc_type);
|
---|
48 | node.setAttribute(GSXML.NODE_TYPE_ATT, node_type);
|
---|
49 | return node;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /** returns the node type of the specified node.
|
---|
54 | should be one of
|
---|
55 | GSXML.NODE_TYPE_LEAF,
|
---|
56 | GSXML.NODE_TYPE_INTERNAL,
|
---|
57 | GSXML.NODE_TYPE_ROOT
|
---|
58 | */
|
---|
59 | public String getNodeType(String node_id, String doc_type) {
|
---|
60 | if (doc_type.equals(GSXML.DOC_TYPE_SIMPLE)) {
|
---|
61 | return GSXML.NODE_TYPE_LEAF;
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (!hasParent(node_id)) {
|
---|
65 | return GSXML.NODE_TYPE_ROOT;
|
---|
66 | }
|
---|
67 | if (doc_type.equals(GSXML.DOC_TYPE_PAGED)) {
|
---|
68 | return GSXML.NODE_TYPE_LEAF;
|
---|
69 | }
|
---|
70 | if (!hasChildren(node_id)) {
|
---|
71 | return GSXML.NODE_TYPE_LEAF;
|
---|
72 | }
|
---|
73 | return GSXML.NODE_TYPE_INTERNAL;
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | /** returns the document type of the doc that the specified node
|
---|
78 | belongs to. should be one of
|
---|
79 | GSXML.DOC_TYPE_SIMPLE,
|
---|
80 | GSXML.DOC_TYPE_PAGED,
|
---|
81 | GSXML.DOC_TYPE_HIERARCHY
|
---|
82 | */
|
---|
83 | abstract public String getDocType(String node_id);
|
---|
84 |
|
---|
85 | /** returns true if the node has child nodes
|
---|
86 | */
|
---|
87 | abstract public boolean hasChildren(String node_id);
|
---|
88 |
|
---|
89 | /** returns true if the node has a parent
|
---|
90 | */
|
---|
91 | abstract public boolean hasParent(String node_id);
|
---|
92 | } |
---|