source: trunk/greenstone3-extensions/gs3build/src/org/greenstone/gsdl3/service/GS3MGRetrieve.java@ 13242

Last change on this file since 13242 was 12188, checked in by kjdon, 18 years ago

Initial revision

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