/* * GS3MGRetrieve.java * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ package org.greenstone.gsdl3.service; // Greenstone classes import org.greenstone.mg.*; import org.greenstone.gsdl3.util.GSFile; import org.greenstone.gsdl3.util.GSXML; import org.greenstone.gsdl3.util.GS3OID; // XML classes import org.w3c.dom.Element; import org.w3c.dom.Text; // General Java classes import java.io.File; public class GS3MGRetrieve extends AbstractGS3DocumentRetrieve { // Elements used in the config file that are specific to this class private static final String DEFAULT_INDEX_ELEM = "defaultIndex"; private MGWrapper mg_src = null; private String mg_basedir = null; private String mg_textdir = null; private String default_index = null; public GS3MGRetrieve() { this.mg_src = new MGWrapper(); } /** configure this service */ public boolean configure(Element info, Element extra_info) { // Do generic configuration if (!super.configure(info, extra_info)) { return false; } // Do specific configuration System.out.println("Configuring GS3MGRetrieve..."); // System.out.println("info:\n" + converter_.getString(info)); // System.out.println("extra_info:\n" + converter_.getString(extra_info)); // Get the default index out of (buildConfig.xml) Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_ELEM); if (def != null) { this.default_index = def.getAttribute(GSXML.NAME_ATT); } if (this.default_index == null || this.default_index.equals("")) { System.err.println("Error: default index not specified!"); return false; } // System.out.println("Default index: " + this.default_index); // The location of the MG index and text files mg_basedir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar; // Needed by MG mg_textdir = GSFile.collectionTextPath(this.index_stem); // index is only needed to start up MG, not used so just use the default index String indexpath = GSFile.collectionIndexPath(this.index_stem, this.default_index); this.mg_src.setIndex(indexpath); return true; } /** returns the content of a node. * node_id should already have been translated if necessary * should return a nodeContent element: * text content or other elements */ protected Element getNodeContent(String doc_id) { if (GS3OID.isDocTop(doc_id) && database.isHierarchicalDocument(doc_id)) { // if we have a whole doc id, and the document is hierarchical, // we want to change the id to be the top id of the section // hierarchy doc_id = GS3OID.createOID(doc_id, "1"); } String doc_num = this.database.OID2MGNum(doc_id); // doc nums have the index prefixed doc_num = doc_num.substring(doc_num.indexOf(".")+1); int doc_int = Integer.parseInt(doc_num); String doc_content = ""; try { doc_content = this.mg_src.getDocument(this.mg_basedir, this.mg_textdir, doc_int); // remove any ctrl-c or ctrl-b doc_content = doc_content.replaceAll("\u0002|\u0003", ""); doc_content = resolveRelativeLinks(doc_content, doc_id); } catch (Exception e) { System.out.println("exception happended with mg_src.getDocument()"); doc_content = "this is the content for section hash id "+ doc_id+", mg doc num "+doc_int+"\n"; } Element content_node = this.doc.createElement(GSXML.NODE_CONTENT_ELEM); Text t = this.doc.createTextNode(doc_content); content_node.appendChild(t); return content_node; } }