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