source: main/trunk/gli/src/org/greenstone/gatherer/cdm/MGIndex.java@ 36272

Last change on this file since 36272 was 36138, checked in by kjdon, 2 years ago

split Index into 2 classes - Index: for mgpp/lucene/solr indexes, and MGIndex, which inherits from Index, for MG indexes.

File size: 6.3 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Copyright (C) 2022 New Zealand Digital Library Project
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *########################################################################
24 */
25package org.greenstone.gatherer.cdm;
26
27import java.util.*;
28import org.greenstone.gatherer.DebugStream;
29import org.greenstone.gatherer.Gatherer;
30import org.greenstone.gatherer.metadata.MetadataElement;
31import org.greenstone.gatherer.metadata.MetadataTools;
32import org.greenstone.gatherer.util.StaticStrings;
33import org.greenstone.gatherer.util.XMLTools;
34import org.w3c.dom.*;
35
36/** This class encapsulates a single indexing pair, representing an MG Index
37 * it adds level information into the standard index
38 * @author John Thompson, Greenstone Digital Library, University of Waikato
39 * @version 2.3d
40 */
41public class MGIndex extends Index {
42
43 /** An values of items in the index level enumeration. */
44 static protected final String LEVEL[] = {StaticStrings.DOCUMENT_STR, StaticStrings.SECTION_STR, StaticStrings.PARAGRAPH_STR};
45
46 /** The level of this index (if old sckool MG). */
47 private int level = -1;
48
49 /** Default constructor, which should only be used during DOMProxyListModel creation. */
50 public MGIndex() {
51 }
52
53 /** Constructor. */
54 public MGIndex(Element element) {
55 this.element = element;
56 }
57
58 /** Constructor for a newly assigned index with specified level. */
59 public MGIndex(int level, ArrayList sources) {
60 super(sources);
61 this.level = level;
62 element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, LEVEL[level]);
63 }
64
65 public MGIndex(String level, ArrayList sources) {
66 super(sources);
67 for(int i = 0; i < LEVEL.length; i++) {
68 if(LEVEL[i].equalsIgnoreCase(level)) {
69 this.level = i;
70 }
71 }
72 element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, LEVEL[this.level]);
73
74 }
75
76 public DOMProxyListEntry create(Element element) {
77 return new MGIndex(element);
78 }
79
80 /** Method to get the value of level.
81 * @return the level as a int
82 */
83 public int getLevel() {
84 if(level == -1 && element != null) {
85 String level_str = element.getAttribute(StaticStrings.LEVEL_ATTRIBUTE);
86 for(int i = 0; level == -1 && i < LEVEL.length; i++) {
87 if(level_str.equals(LEVEL[i])) {
88 level = i;
89 }
90 }
91 level_str = null;
92 }
93 return level;
94 }
95
96 public String getID() {
97 if(element == null) {
98 id="";
99 }
100 else if(id == null) {
101 StringBuffer id_buffer = new StringBuffer();
102 // Write level information, if any.
103 int level = getLevel();
104 if(0 <= level && level < 3) {
105 id_buffer.append(LEVEL[level]);
106 id_buffer.append(StaticStrings.COLON_CHARACTER);
107 }
108 // Write data information. Retrieve each of the content sources and add them in a comma separated list.
109 ArrayList sources = getSources();
110 int sources_size = sources.size();
111 for(int i = 0; i < sources_size; i++) {
112 Object source_object = sources.get(i);
113 if (source_object instanceof MetadataElement) {
114 String full_element_name = ((MetadataElement)source_object).getFullName();
115 if(full_element_name.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && full_element_name.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
116 id_buffer.append(full_element_name.substring(StaticStrings.EXTRACTED_NAMESPACE.length()));
117 }
118 else {
119 id_buffer.append(full_element_name);
120 }
121 }
122 else {
123 id_buffer.append(source_object.toString());
124 }
125 id_buffer.append(StaticStrings.COMMA_CHARACTER);
126 }
127 sources = null;
128 if (id_buffer.length()==0) {
129 id = "";
130 } else {
131 id = id_buffer.substring(0, id_buffer.length() - 1);
132 }
133 }
134 return id;
135 }
136
137
138 public void setElement(Element element) {
139 this.level = -1;
140 super.setElement(element);
141 }
142
143 /** Method to set the level of this index which can only be used for the default index.
144 * @param new_level the new level as an int
145 */
146 public void setLevel(int new_level) {
147 // System.err.println("SetLevel(" + new_level + ")");
148 if(element != null && element.getNodeName().equals(StaticStrings.INDEX_DEFAULT_ELEMENT)) {
149 if (0 <= new_level && new_level < 3) {
150 element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, LEVEL[new_level]);
151 } else {
152 element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, "");
153 }
154 this.id = null; // Regenerate ID.
155 this.level = new_level;
156 }
157 else {
158 DebugStream.println("Error! Called setLevel() of index other than the default.");
159 }
160 }
161
162
163 /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
164 * @return A <strong>String</strong> containing the information of this class.
165 */
166 public String toString() {
167 StringBuffer text_buffer = new StringBuffer("");
168 // Generate language dependant id (include extracted metadata namespace)
169 // Write level information, if any.
170 int level = getLevel();
171 if(0 <= level && level < 3) {
172 text_buffer.append(LEVEL[level]);
173 text_buffer.append(StaticStrings.COLON_CHARACTER);
174 }
175 // Write data information. Retrieve each of the content sources and add them in a comma separated list.
176 ArrayList sources = getSources();
177 int sources_size = sources.size();
178 for(int i = 0; i < sources_size; i++) {
179 String source_name = (sources.get(i)).toString();
180 text_buffer.append(source_name);
181 if(i < sources_size - 1) {
182 text_buffer.append(StaticStrings.COMMA_CHARACTER);
183 }
184 }
185 sources = null;
186 return text_buffer.toString();
187 }
188}
Note: See TracBrowser for help on using the repository browser.