[3649] | 1 | /*
|
---|
| 2 | * GS2MGRetrieve.java
|
---|
[8959] | 3 | * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
|
---|
[3649] | 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.service;
|
---|
| 20 |
|
---|
[3799] | 21 | // Greenstone classes
|
---|
[15326] | 22 | import org.greenstone.mg.MGRetrieveWrapper;
|
---|
[9874] | 23 | import org.greenstone.gsdl3.core.GSException;
|
---|
[8959] | 24 | import org.greenstone.gsdl3.util.GSFile;
|
---|
| 25 | import org.greenstone.gsdl3.util.GSXML;
|
---|
[3649] | 26 |
|
---|
[3799] | 27 | // XML classes
|
---|
[28966] | 28 | import org.w3c.dom.Document;
|
---|
[3799] | 29 | import org.w3c.dom.Element;
|
---|
[8959] | 30 | import org.w3c.dom.Text;
|
---|
[3754] | 31 |
|
---|
[3799] | 32 | // General Java classes
|
---|
[3754] | 33 | import java.io.File;
|
---|
| 34 |
|
---|
[13124] | 35 | import org.apache.log4j.*;
|
---|
| 36 |
|
---|
[23792] | 37 | // Apache Commons
|
---|
| 38 | import org.apache.commons.lang3.*;
|
---|
| 39 |
|
---|
[3649] | 40 | public class GS2MGRetrieve
|
---|
[14431] | 41 | extends AbstractGS2DocumentRetrieve {
|
---|
| 42 | static Logger logger = Logger.getLogger (org.greenstone.gsdl3.service.GS2MGRetrieve.class.getName ());
|
---|
| 43 |
|
---|
[3799] | 44 | // Elements used in the config file that are specific to this class
|
---|
[3754] | 45 | private static final String DEFAULT_INDEX_ELEM = "defaultIndex";
|
---|
[13567] | 46 | private static final String INDEX_LIST_ELEM = "indexList";
|
---|
| 47 | private static final String INDEX_ELEM = "index";
|
---|
[14000] | 48 | private static final String DEFAULT_INDEX_SUBCOLLECTION_ELEM = "defaultIndexSubcollection";
|
---|
| 49 | private static final String DEFAULT_INDEX_LANGUAGE_ELEM = "defaultIndexLanguage";
|
---|
[14431] | 50 |
|
---|
[14632] | 51 | private static MGRetrieveWrapper mg_src = null;
|
---|
[8959] | 52 | private String mg_basedir = null;
|
---|
| 53 | private String mg_textdir = null;
|
---|
[5098] | 54 | private String default_index = null;
|
---|
[13567] | 55 | private boolean has_default_index = false;
|
---|
[14431] | 56 |
|
---|
| 57 | public GS2MGRetrieve () {
|
---|
[14632] | 58 | if (this.mg_src == null){
|
---|
| 59 | this.mg_src = new MGRetrieveWrapper ();
|
---|
| 60 | }
|
---|
[3649] | 61 | }
|
---|
[14431] | 62 |
|
---|
| 63 | public void cleanUp () {
|
---|
| 64 | super.cleanUp ();
|
---|
| 65 | this.mg_src.unloadIndexData ();
|
---|
[9874] | 66 | }
|
---|
[14431] | 67 |
|
---|
[3649] | 68 | /** configure this service */
|
---|
[14431] | 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;
|
---|
[3799] | 112 | }
|
---|
[8959] | 113 |
|
---|
| 114 | /** returns the content of a node
|
---|
| 115 | * should return a nodeContent element:
|
---|
| 116 | * <nodeContent>text content or other elements</nodeContent>
|
---|
| 117 | */
|
---|
[28966] | 118 | protected Element getNodeContent (Document doc, String doc_id, String lang) throws GSException {
|
---|
[15326] | 119 | long doc_num = this.coll_db.OID2DocnumLong (doc_id);
|
---|
[14431] | 120 | if (doc_num == -1) {
|
---|
| 121 | logger.error ("OID "+doc_id +" couldn't be converted to mg num");
|
---|
| 122 | return null;
|
---|
| 123 | }
|
---|
[28966] | 124 | Element content_node = doc.createElement (GSXML.NODE_CONTENT_ELEM);
|
---|
[14632] | 125 |
|
---|
[14431] | 126 | String doc_content = null;
|
---|
| 127 |
|
---|
| 128 | //means that this.mg_src is up and running
|
---|
| 129 | if (has_default_index ){
|
---|
[14632] | 130 | synchronized(this.mg_src){
|
---|
| 131 | String indexpath = GSFile.collectionIndexPath(this.index_stem, this.default_index);
|
---|
[14936] | 132 | this.mg_src.setIndex(indexpath);
|
---|
[14632] | 133 | doc_content = this.mg_src.getDocument (this.mg_basedir,
|
---|
| 134 | this.mg_textdir, doc_num);
|
---|
| 135 | }
|
---|
[14431] | 136 | }
|
---|
| 137 |
|
---|
| 138 | if (doc_content!=null) {
|
---|
| 139 | // remove any ctrl-c or ctrl-b
|
---|
[23792] | 140 | doc_content = StringUtils.replace(doc_content, "\u0002|\u0003", "");
|
---|
[14431] | 141 | // replace _httpimg_ with the correct address
|
---|
| 142 | doc_content = resolveTextMacros (doc_content, doc_id, lang);
|
---|
[28966] | 143 | //GSXML.addDocText(doc, doc_content);
|
---|
[14431] | 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 | }
|
---|
[28966] | 148 | Text t = doc.createTextNode (doc_content);
|
---|
[14431] | 149 | content_node.appendChild (t);
|
---|
| 150 | return content_node;
|
---|
| 151 |
|
---|
[14632] | 152 | }
|
---|
[3990] | 153 | }
|
---|