source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2MGRetrieve.java@ 28966

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

Lots of changes. Mainly to do with removing this.doc from everywhere. Document is not thread safe. Now we tend to create a new Document everytime we are starting a new page/message etc. in service this.desc_doc is available as teh document to create service info stuff. But it should only be used for this and not for other messages. newDOM is now static for XMLConverter. method param changes for some GSXML methods.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1/*
2 * GS2MGRetrieve.java
3 * Copyright (C) 2005 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.service;
20
21// Greenstone classes
22import org.greenstone.mg.MGRetrieveWrapper;
23import org.greenstone.gsdl3.core.GSException;
24import org.greenstone.gsdl3.util.GSFile;
25import org.greenstone.gsdl3.util.GSXML;
26
27// XML classes
28import org.w3c.dom.Document;
29import org.w3c.dom.Element;
30import org.w3c.dom.Text;
31
32// General Java classes
33import java.io.File;
34
35import org.apache.log4j.*;
36
37// Apache Commons
38import org.apache.commons.lang3.*;
39
40public class GS2MGRetrieve
41extends AbstractGS2DocumentRetrieve {
42 static Logger logger = Logger.getLogger (org.greenstone.gsdl3.service.GS2MGRetrieve.class.getName ());
43
44 // Elements used in the config file that are specific to this class
45 private static final String DEFAULT_INDEX_ELEM = "defaultIndex";
46 private static final String INDEX_LIST_ELEM = "indexList";
47 private static final String INDEX_ELEM = "index";
48 private static final String DEFAULT_INDEX_SUBCOLLECTION_ELEM = "defaultIndexSubcollection";
49 private static final String DEFAULT_INDEX_LANGUAGE_ELEM = "defaultIndexLanguage";
50
51 private static MGRetrieveWrapper mg_src = null;
52 private String mg_basedir = null;
53 private String mg_textdir = null;
54 private String default_index = null;
55 private boolean has_default_index = false;
56
57 public GS2MGRetrieve () {
58 if (this.mg_src == null){
59 this.mg_src = new MGRetrieveWrapper ();
60 }
61 }
62
63 public void cleanUp () {
64 super.cleanUp ();
65 this.mg_src.unloadIndexData ();
66 }
67
68 /** configure this service */
69 public boolean configure (Element info, Element extra_info) {
70 if (!super.configure (info, extra_info)){
71 return false;
72 }
73
74 // Do specific configuration
75 logger.info ("Configuring GS2MGRetrieve...");
76
77 // Get the default index out of <defaultIndex> (buildConfig.xml)
78 Element def = (Element) GSXML.getChildByTagName (info, DEFAULT_INDEX_ELEM);
79 if (def != null) {
80 this.default_index = def.getAttribute (GSXML.SHORTNAME_ATT);
81 }
82 Element defSub = (Element) GSXML.getChildByTagName (info, DEFAULT_INDEX_SUBCOLLECTION_ELEM);
83 if (defSub != null) {
84 this.default_index += defSub.getAttribute (GSXML.SHORTNAME_ATT);
85 logger.info ("default indexSubcollection is "+defSub.getAttribute (GSXML.SHORTNAME_ATT));
86 } //concate defaultIndex + defaultIndexSubcollection
87
88 //get the default indexLanguage out of <defaultIndexLanguage> (buildConfig.xml)
89 Element defLang = (Element) GSXML.getChildByTagName (info, DEFAULT_INDEX_LANGUAGE_ELEM);
90 if (defLang != null) {
91 this.default_index += defLang.getAttribute (GSXML.SHORTNAME_ATT);
92 logger.info ("default indexLanguage is "+defLang.getAttribute (GSXML.SHORTNAME_ATT));
93 } //concate defaultIndex + defaultIndexSubcollection + defaultIndexLanguage
94
95
96 if (this.default_index == null || this.default_index.equals ("")) {
97 logger.error ("default index is not specified, the content of a document will not be retrieved");
98 has_default_index = false;
99 return true;
100 //}
101 }
102 logger.debug ("Default index: " + this.default_index);
103
104 // The location of the MG index and text files
105 mg_basedir = GSFile.collectionBaseDir (this.site_home, this.cluster_name) + File.separatorChar; // Needed by MG
106 mg_textdir = GSFile.collectionTextPath (this.index_stem);
107 // index is only needed to start up MG, not used so just use the default index
108 String indexpath = GSFile.collectionIndexPath (this.index_stem, this.default_index);
109 this.mg_src.setIndex (indexpath);
110 has_default_index = true;
111 return true;
112 }
113
114 /** returns the content of a node
115 * should return a nodeContent element:
116 * <nodeContent>text content or other elements</nodeContent>
117 */
118 protected Element getNodeContent (Document doc, String doc_id, String lang) throws GSException {
119 long doc_num = this.coll_db.OID2DocnumLong (doc_id);
120 if (doc_num == -1) {
121 logger.error ("OID "+doc_id +" couldn't be converted to mg num");
122 return null;
123 }
124 Element content_node = doc.createElement (GSXML.NODE_CONTENT_ELEM);
125
126 String doc_content = null;
127
128 //means that this.mg_src is up and running
129 if (has_default_index ){
130 synchronized(this.mg_src){
131 String indexpath = GSFile.collectionIndexPath(this.index_stem, this.default_index);
132 this.mg_src.setIndex(indexpath);
133 doc_content = this.mg_src.getDocument (this.mg_basedir,
134 this.mg_textdir, doc_num);
135 }
136 }
137
138 if (doc_content!=null) {
139 // remove any ctrl-c or ctrl-b
140 doc_content = StringUtils.replace(doc_content, "\u0002|\u0003", "");
141 // replace _httpimg_ with the correct address
142 doc_content = resolveTextMacros (doc_content, doc_id, lang);
143 //GSXML.addDocText(doc, doc_content);
144 } else {
145 logger.error ("the doc content was null, not getting that section\n");
146 doc_content = "couldn't retrieve content for this section, please check the log file for more detail\n";
147 }
148 Text t = doc.createTextNode (doc_content);
149 content_node.appendChild (t);
150 return content_node;
151
152 }
153}
Note: See TracBrowser for help on using the repository browser.