package org.greenstone.gatherer.util; /** *######################################################################### * * A component of the Gatherer application, part of the Greenstone digital * library suite from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * Author: John Thompson, Greenstone Digital Library, University of Waikato * * Copyright (C) 1999 New Zealand Digital Library Project * * 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. *######################################################################## */ import java.awt.Point; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.ArrayList; import java.util.Collections; import java.util.Hashtable; import java.util.Vector; import javax.swing.tree.TreePath; //import net.n3.nanoxml.IXMLElement; //import net.n3.nanoxml.IXMLParser; //import net.n3.nanoxml.IXMLReader; //import net.n3.nanoxml.StdXMLReader; //import net.n3.nanoxml.XMLParserFactory; import org.greenstone.gatherer.Gatherer; import org.greenstone.gatherer.gui.SaveProgressDialog; import org.greenstone.gatherer.msm.ElementWrapper; import org.greenstone.gatherer.msm.Metadata; import org.greenstone.gatherer.msm.MetadataSetManager; import org.greenstone.gatherer.util.Utility; import org.greenstone.gatherer.valuetree.GValueModel; import org.greenstone.gatherer.valuetree.GValueNode; import org.greenstone.gatherer.valuetree.GValueTree; import org.w3c.dom.Element; public class MetadataXML { //private boolean can_wait = true; private Gatherer gatherer = null; private Hashtable known_indexes = null; //private int spare_processes = 25; //private MetadataXML mummy = null; private SaveProgressDialog spd = null; //private Vector complete = null; //private Vector processes = null; static public String METADATA_FILE = "metadata.xml"; // not actually used anywhere /* static public IXMLElement read(String filename) { IXMLElement xml = null; IXMLParser parser = null; IXMLReader reader = null; try { parser = XMLParserFactory.createDefaultXMLParser(); File in_file = new File(filename); FileReader in_reader = new FileReader(in_file); BufferedReader in = new BufferedReader(in_reader, Utility.BUFFER_SIZE); String content = ""; String next_line = null; while((next_line = in.readLine()) != null) { // Throw away the document type, as I don't want to deal // with dtd's. If someone has tampered with these files to // the point that this parser can't read them then too bad. if(!next_line.startsWith(" " + index); known_indexes.put(value, index); } return index; } static private String safe(String unsafe) { String safe_str = ""; for(int i = 0; i < unsafe.length(); i++) { char c = unsafe.charAt(i); if(c != ' ') { safe_str = safe_str + c; } } return safe_str; } static private void write(Writer w, String text) throws Exception { text = text + "\r\n"; char buffer[] = text.toCharArray(); w.write(buffer, 0, buffer.length); } static public void write(GValueModel model, MetadataSetManager msm, String etc_dir) { try { File out_file = new File(etc_dir + model.toString() + ".txt"); FileOutputStream fos = new FileOutputStream(out_file); OutputStreamWriter osw = new OutputStreamWriter(fos); BufferedWriter bw = new BufferedWriter(osw, Utility.BUFFER_SIZE); Vector all_values = model.traverseTree(); for(int i = 0; i < all_values.size(); i++) { GValueNode node = (GValueNode)all_values.get(i); TreePath path = new TreePath(node.getPath()); String value = GValueTree.formatPath(null, path, true); String index = model.getHIndex(value); String alias = node.getAlias(index); if(value.indexOf("\\") != -1) { value = value.substring(value.lastIndexOf("\\") + 1); } write(bw, index + "\t\"" + alias + "\"\t\"" + Utility.stripNL(value) + "\""); } // Very important we do this, or else buffer may not have // flushed. bw.flush(); bw.close(); } catch(Exception error) { error.printStackTrace(); } } static final private long showTime(String message, long time) { if(time == -1) { ///ystem.err.println(message + System.currentTimeMillis()); } else { ///ystem.err.println(message + (System.currentTimeMillis() - time)); } return System.currentTimeMillis(); } }