source: branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/service/GS3MGRetrieve.java@ 9815

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

some methods from documentretrieve classes now throw GSExceptions. am trying to make it so that no Exceptions get to the user interface. returning a lot more error elements too, in the hope that they may be useful for other people

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