[24395] | 1 | /*
|
---|
[26041] | 2 | * AbstractBasicDocument.java
|
---|
[24395] | 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 |
|
---|
[26045] | 21 | import java.util.ArrayList;
|
---|
| 22 |
|
---|
[24395] | 23 | import org.apache.log4j.*;
|
---|
| 24 | import org.w3c.dom.Document;
|
---|
| 25 | import org.w3c.dom.Element;
|
---|
| 26 |
|
---|
[26041] | 27 | public abstract class AbstractBasicDocument {
|
---|
[24395] | 28 |
|
---|
[26045] | 29 | /** info types */
|
---|
[26269] | 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";
|
---|
[26045] | 34 |
|
---|
[24395] | 35 | /** XML element for describe requests - the container doc */
|
---|
[28965] | 36 | //protected Document doc = null; // typically a shared reference to the one in ServiceRack
|
---|
[24395] | 37 |
|
---|
[26041] | 38 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.AbstractBasicDocument.class.getName());
|
---|
[24395] | 39 |
|
---|
[28965] | 40 | public AbstractBasicDocument()
|
---|
[24395] | 41 | {
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[26039] | 44 | /** create an element to go into a results list. A node element
|
---|
[24395] | 45 | * has the form
|
---|
[26039] | 46 | * <docNode nodeId='xxx' nodeType='leaf' docType='hierarchy'/>
|
---|
[24395] | 47 | */
|
---|
[28965] | 48 | public Element createDocNode(Document doc, String node_id) {
|
---|
| 49 | return createDocNode(doc, node_id, null);
|
---|
[26039] | 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 | */
|
---|
[28965] | 55 | public Element createDocNode(Document doc, String node_id, String rank) {
|
---|
| 56 | Element node = doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
[24395] | 57 | node.setAttribute(GSXML.NODE_ID_ATT, node_id);
|
---|
[26039] | 58 | if (rank != null) {
|
---|
| 59 | node.setAttribute(GSXML.NODE_RANK_ATT, rank);
|
---|
| 60 | }
|
---|
[24395] | 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 |
|
---|
[36976] | 68 | abstract public boolean isValidOID(String doc_id);
|
---|
[26045] | 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 |
|
---|
[24395] | 75 |
|
---|
[26045] | 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);
|
---|
| 81 |
|
---|
[24395] | 82 | /** returns the node type of the specified node.
|
---|
| 83 | should be one of
|
---|
| 84 | GSXML.NODE_TYPE_LEAF,
|
---|
| 85 | GSXML.NODE_TYPE_INTERNAL,
|
---|
| 86 | GSXML.NODE_TYPE_ROOT
|
---|
| 87 | */
|
---|
| 88 | public String getNodeType(String node_id, String doc_type) {
|
---|
| 89 | if (doc_type.equals(GSXML.DOC_TYPE_SIMPLE)) {
|
---|
| 90 | return GSXML.NODE_TYPE_LEAF;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | if (!hasParent(node_id)) {
|
---|
| 94 | return GSXML.NODE_TYPE_ROOT;
|
---|
| 95 | }
|
---|
| 96 | if (doc_type.equals(GSXML.DOC_TYPE_PAGED)) {
|
---|
| 97 | return GSXML.NODE_TYPE_LEAF;
|
---|
| 98 | }
|
---|
| 99 | if (!hasChildren(node_id)) {
|
---|
| 100 | return GSXML.NODE_TYPE_LEAF;
|
---|
| 101 | }
|
---|
| 102 | return GSXML.NODE_TYPE_INTERNAL;
|
---|
| 103 |
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /** returns the document type of the doc that the specified node
|
---|
| 107 | belongs to. should be one of
|
---|
| 108 | GSXML.DOC_TYPE_SIMPLE,
|
---|
| 109 | GSXML.DOC_TYPE_PAGED,
|
---|
[26039] | 110 | GSXML.DOC_TYPE_PAGEDHIERARCHY,
|
---|
[24395] | 111 | GSXML.DOC_TYPE_HIERARCHY
|
---|
| 112 | */
|
---|
| 113 | abstract public String getDocType(String node_id);
|
---|
| 114 |
|
---|
| 115 | /** returns true if the node has child nodes
|
---|
| 116 | */
|
---|
| 117 | abstract public boolean hasChildren(String node_id);
|
---|
| 118 |
|
---|
[26045] | 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);
|
---|
[24395] | 129 | /** returns true if the node has a parent
|
---|
| 130 | */
|
---|
| 131 | abstract public boolean hasParent(String node_id);
|
---|
[26045] | 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);
|
---|
[29089] | 145 |
|
---|
| 146 | /** get a piece of metadata about the document node */
|
---|
| 147 | abstract public String getMetadata(String node_id, String meta_name);
|
---|
[36976] | 148 | }
|
---|