1 | /*
|
---|
2 | * GS3MGPPRetrieve.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.mgpp.*;
|
---|
23 | import org.greenstone.gsdl3.core.GSException;
|
---|
24 | import org.greenstone.gsdl3.util.GSFile;
|
---|
25 | import org.greenstone.gsdl3.util.GSXML;
|
---|
26 | import org.greenstone.gsdl3.util.GS3OID;
|
---|
27 |
|
---|
28 | // XML classes
|
---|
29 | import org.w3c.dom.Element;
|
---|
30 | import org.w3c.dom.Text;
|
---|
31 |
|
---|
32 | // General Java classes
|
---|
33 | import java.io.File;
|
---|
34 |
|
---|
35 | public class GS3MGPPRetrieve
|
---|
36 | extends AbstractGS3DocumentRetrieve
|
---|
37 | {
|
---|
38 | // Elements used in the config file that are specific to this class
|
---|
39 | private static final String DEFAULT_LEVEL_ELEM = "defaultLevel";
|
---|
40 |
|
---|
41 | private MGPPWrapper mgpp_src = null;
|
---|
42 |
|
---|
43 | private String default_level = null;
|
---|
44 | private String mgpp_textdir = null;
|
---|
45 |
|
---|
46 | public GS3MGPPRetrieve() {
|
---|
47 | this.mgpp_src = new MGPPWrapper();
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void cleanUp() {
|
---|
51 | super.cleanUp();
|
---|
52 | this.mgpp_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 GS3MGPPRetrieve...");
|
---|
65 |
|
---|
66 | // Get the default level out of <defaultLevel> (buildConfig.xml)
|
---|
67 | Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_LEVEL_ELEM);
|
---|
68 | if (def != null) {
|
---|
69 | this.default_level = def.getAttribute(GSXML.NAME_ATT);
|
---|
70 | }
|
---|
71 | if (this.default_level == null || this.default_level.equals("")) {
|
---|
72 | System.err.println("Error: default level not specified!");
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 | // System.out.println("Default level: " + default_level_);
|
---|
76 |
|
---|
77 | // The location of the MGPP text files
|
---|
78 | mgpp_textdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) +
|
---|
79 | File.separatorChar + GSFile.collectionTextPath(this.index_stem);
|
---|
80 |
|
---|
81 | return true;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** returns the content of a node
|
---|
85 | * should return a nodeContent element:
|
---|
86 | * <nodeContent>text content or other elements</nodeContent>
|
---|
87 | */
|
---|
88 | protected Element getNodeContent(String doc_id) throws GSException {
|
---|
89 | if (GS3OID.isDocTop(doc_id) && database.isHierarchicalDocument(doc_id)) {
|
---|
90 | // if we have a whole doc id, and the document is hierarchical,
|
---|
91 | // we want to change the id to be the top id of the section
|
---|
92 | // hierarchy
|
---|
93 | doc_id = GS3OID.createOID(doc_id, "1");
|
---|
94 | }
|
---|
95 |
|
---|
96 | String doc_num = this.database.OID2MGNum(doc_id);
|
---|
97 | // doc nums have the index prefixed
|
---|
98 | doc_num = doc_num.substring(doc_num.indexOf(".")+1);
|
---|
99 | int doc_int = Integer.parseInt(doc_num);
|
---|
100 |
|
---|
101 | Element content_node = this.doc.createElement(GSXML.NODE_CONTENT_ELEM);
|
---|
102 |
|
---|
103 | String doc_content = "";
|
---|
104 | try {
|
---|
105 | doc_content = this.mgpp_src.getDocument(this.mgpp_textdir,
|
---|
106 | this.default_level,
|
---|
107 | doc_int);
|
---|
108 | doc_content = resolveRelativeLinks(doc_content, doc_id);
|
---|
109 | } catch (Exception e) {
|
---|
110 | System.out.println("exception happended with mgpp_src.getDocument()" + e);
|
---|
111 | doc_content = "this is the content for section hash id "+ doc_id+", mgpp doc num "+doc_int+"\n";
|
---|
112 | }
|
---|
113 |
|
---|
114 | Text t = this.doc.createTextNode(doc_content);
|
---|
115 | content_node.appendChild(t);
|
---|
116 | return content_node;
|
---|
117 |
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | }
|
---|