package org.greenstone.gsdl3.gs3build.collection; import java.io.File; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import org.greenstone.gsdl3.gs3build.doctypes.DocumentLoader; import org.greenstone.gsdl3.gs3build.util.XMLTools; import org.greenstone.gsdl3.gs3build.util.GS2TextFileHandler; import org.greenstone.gsdl3.gs3build.classifier.ClassifierInterface; import org.greenstone.gsdl3.gs3build.classifier.ClassifierManager; import org.greenstone.gsdl3.gs3build.indexers.*; import org.greenstone.gsdl3.gs3build.BuildManager; public class GS2CollectionCfg { BuildManager buildManager; IndexerInterface indexer; class GS2CollectCfgHandler extends GS2TextFileHandler { GS2CollectCfgHandler(String content, File collectFile, GS2CollectionCfg configurator) { super(content); while (this.hasMoreLines()) { String lineType; this.getLine(); lineType = this.getEntry(true); if (lineType == null || lineType.length() == 0) { continue; } if (lineType.equals("classify")) { String classType = this.getEntry(true); if (classType == null || classType.length() == 0) { continue; } classType = "GS2" + classType + "Classifier"; List paramList = new ArrayList(); // read in the parameters - to allow for better abstraction of other parts, // the handling of this is a bit 'odd', specifically modifying any file // references here before passing them on... String parameter = null; String lastParam = null; while (this.hasMore()) { String param = this.getEntry(true); if (param != null && param.length() > 0) { // expand any hfile references... if (lastParam != null && lastParam.equals("-hfile")) { param = collectFile.getParent() + File.separator + param; } paramList.add(param); } lastParam = param; } ClassifierInterface classifier = ClassifierManager.loadClassifier(classType, paramList); if (classifier != null) { configurator.addClassifier(classifier); } else { } } else if (lineType.equals("buildtype")) { if (this.hasMore()) { String type = this.getEntry(true); if (type != null) { configurator.setBuildType(type); } } } else if (lineType.equals("indexes")) { while (this.hasMore()) { String index = this.getEntry(true); if (index != null) { System.out.println("Adding index " + index); configurator.addIndex(index); } } } } } } public GS2CollectionCfg(BuildManager manager, File file) { String documentText = DocumentLoader.getAsString(file); if (documentText == null) { return; } this.buildManager = manager; GS2CollectCfgHandler handler = new GS2CollectCfgHandler(documentText, file, this); } void addClassifier(ClassifierInterface classifier) { ClassifierManager manager = this.buildManager.getClassifierManager(); if (manager != null) { manager.addClassifier(classifier); } else { System.out.println("Unable to install due to missing manager"); } } void addIndex(String index) { if (this.indexer == null) { this.setBuildType("mg"); } this.indexer.configure(IndexerInterface.GS2_INDEX_LABEL, index); } void setBuildType(String type) { if (type.equals("mgpp")) { this.indexer = new MGPPIndexer(); } else { this.indexer = new MGIndexer(); } this.buildManager.addIndexer(this.indexer); } public static GS2CollectionCfg getGS2CollectionCfg(BuildManager manager, File cfgFile) { GS2CollectionCfg configurator = new GS2CollectionCfg(manager, cfgFile); return configurator; } public static GS2CollectionCfg getGS2CollectionCfg(BuildManager manager, String etcDir) { File file = new File(etcDir, "collect.cfg"); if (!file.exists()) { return null; } return getGS2CollectionCfg(manager, file); } }