source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS3MGRetrieve.java@ 9001

Last change on this file since 9001 was 9001, checked in by kjdon, 19 years ago

use index_stem instead of assuming 'index' when creating paths for mg

  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1/*
2 * GS3MGRetrieve.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.*;
23import org.greenstone.gsdl3.util.GSFile;
24import org.greenstone.gsdl3.util.GSXML;
25import org.greenstone.gsdl3.util.GS3OID;
26
27// XML classes
28import org.w3c.dom.Element;
29import org.w3c.dom.Text;
30
31// General Java classes
32import java.io.File;
33
34public class GS3MGRetrieve
35 extends AbstractGS3DocumentRetrieve
36{
37 // Elements used in the config file that are specific to this class
38 private static final String DEFAULT_INDEX_ELEM = "defaultIndex";
39
40 private MGWrapper mg_src = null;
41 private String mg_basedir = null;
42 private String mg_textdir = null;
43 private String default_index = null;
44 public GS3MGRetrieve() {
45 this.mg_src = new MGWrapper();
46 }
47
48 /** configure this service */
49 public boolean configure(Element info, Element extra_info)
50 {
51 // Do generic configuration
52 if (!super.configure(info, extra_info)) {
53 return false;
54 }
55
56 // Do specific configuration
57 System.out.println("Configuring GS3MGRetrieve...");
58 // System.out.println("info:\n" + converter_.getString(info));
59 // System.out.println("extra_info:\n" + converter_.getString(extra_info));
60
61 // Get the default index out of <defaultIndex> (buildConfig.xml)
62 Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_ELEM);
63 if (def != null) {
64 this.default_index = def.getAttribute(GSXML.NAME_ATT);
65 }
66 if (this.default_index == null || this.default_index.equals("")) {
67 System.err.println("Error: default index not specified!");
68 return false;
69 }
70
71 // System.out.println("Default index: " + this.default_index);
72
73
74 // The location of the MG index and text files
75 mg_basedir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar; // Needed by MG
76 mg_textdir = GSFile.collectionTextPath(this.index_stem);
77 // index is only needed to start up MG, not used so just use the default index
78 String indexpath = GSFile.collectionIndexPath(this.index_stem, this.default_index);
79 this.mg_src.setIndex(indexpath);
80 return true;
81 }
82
83 /** returns the content of a node.
84 * node_id should already have been translated if necessary
85 * should return a nodeContent element:
86 * <nodeContent>text content or other elements</nodeContent>
87 */
88 protected Element getNodeContent(String doc_id) {
89
90 if (GS3OID.isDocTop(doc_id) && database.isHierarchicalDocument(doc_id)) {
91 // if we have a whole doc id, and the document is hierarchical,
92 // we want to change the id to be the top id of the section
93 // hierarchy
94 doc_id = GS3OID.createOID(doc_id, "1");
95 }
96
97 String doc_num = this.database.OID2MGNum(doc_id);
98 // doc nums have the index prefixed
99 doc_num = doc_num.substring(doc_num.indexOf(".")+1);
100 int doc_int = Integer.parseInt(doc_num);
101
102 String doc_content = "";
103 try {
104 doc_content = this.mg_src.getDocument(this.mg_basedir,
105 this.mg_textdir,
106 doc_int);
107 // remove any ctrl-c or ctrl-b
108 doc_content = doc_content.replaceAll("\u0002|\u0003", "");
109 doc_content = resolveRelativeLinks(doc_content, doc_id);
110
111 } catch (Exception e) {
112 System.out.println("exception happended with mg_src.getDocument()");
113 doc_content = "this is the content for section hash id "+ doc_id+", mg doc num "+doc_int+"\n";
114 }
115
116 Element content_node = this.doc.createElement(GSXML.NODE_CONTENT_ELEM);
117
118 Text t = this.doc.createTextNode(doc_content);
119 content_node.appendChild(t);
120 return content_node;
121
122 }
123
124
125}
Note: See TracBrowser for help on using the repository browser.