source: trunk/gli/src/org/greenstone/gatherer/util/MetadataXML.java@ 5523

Last change on this file since 5523 was 5523, checked in by mdewsnip, 21 years ago

Removed some LONG dead code.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1package org.greenstone.gatherer.util;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * Author: John Thompson, Greenstone Digital Library, University of Waikato
10 *
11 * Copyright (C) 1999 New Zealand Digital Library Project
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *########################################################################
27 */
28import java.awt.Point;
29import java.io.BufferedReader;
30import java.io.BufferedWriter;
31import java.io.File;
32import java.io.FileInputStream;
33import java.io.FileOutputStream;
34import java.io.FileReader;
35import java.io.OutputStreamWriter;
36import java.io.Writer;
37import java.util.ArrayList;
38import java.util.Collections;
39import java.util.Hashtable;
40import java.util.Vector;
41import javax.swing.tree.TreePath;
42import org.greenstone.gatherer.Gatherer;
43import org.greenstone.gatherer.gui.SaveProgressDialog;
44import org.greenstone.gatherer.msm.ElementWrapper;
45import org.greenstone.gatherer.msm.Metadata;
46import org.greenstone.gatherer.msm.MetadataSetManager;
47import org.greenstone.gatherer.util.Utility;
48import org.greenstone.gatherer.valuetree.GValueModel;
49import org.greenstone.gatherer.valuetree.GValueNode;
50import org.w3c.dom.Element;
51
52public class MetadataXML {
53 //private boolean can_wait = true;
54 private Gatherer gatherer = null;
55 private Hashtable known_indexes = null;
56 //private int spare_processes = 25;
57 //private MetadataXML mummy = null;
58 private SaveProgressDialog spd = null;
59 //private Vector complete = null;
60 //private Vector processes = null;
61
62 static public String METADATA_FILE = "metadata.xml";
63
64 /**
65 public static void write(Gatherer gatherer, String etc_dir) {
66 if(gatherer != null && gatherer.c_man != null && gatherer.c_man.getCollection() != null && gatherer.c_man.getCollection().msm != null) {
67 Vector elements = gatherer.c_man.getCollection().msm.getElements(true);
68 for(int i = 0; i < elements.size(); i++) {
69 ElementWrapper element = (ElementWrapper)elements.get(i);
70 ///ystem.err.print("Checking " + element + " for HFile: ");
71 GValueModel model = gatherer.c_man.getCollection().msm.getValueTree(element);
72 if(model != null && (element.getNamespace().equals(MetadataSetManager.HIDDEN) || model.isHierarchy())) {
73 ///ystem.err.println("Found. Writing file.");
74 write(model, gatherer.c_man.getCollection().msm, etc_dir);
75 }
76 else {
77 ///ystem.err.println("No file found.");
78 }
79 }
80 }
81 }
82 */
83
84 private String getHIndex(Gatherer gatherer, GValueModel model, String value) {
85 String index = null;
86 index = (String) known_indexes.get(value);
87 if(index == null) {
88 index = model.getHIndex(value);
89 ///ystem.err.println("Adding to known indexes: " + value + " -> " + index);
90 known_indexes.put(value, index);
91 }
92 return index;
93 }
94
95 static private String safe(String unsafe) {
96 String safe_str = "";
97 for(int i = 0; i < unsafe.length(); i++) {
98 char c = unsafe.charAt(i);
99 if(c != ' ') {
100 safe_str = safe_str + c;
101 }
102 }
103 return safe_str;
104 }
105
106 static private void write(Writer w, String text)
107 throws Exception {
108 text = text + "\r\n";
109 char buffer[] = text.toCharArray();
110 w.write(buffer, 0, buffer.length);
111 }
112
113 static public void write(ElementWrapper element, GValueModel model, MetadataSetManager msm, String etc_dir) {
114 try {
115 File out_file = new File(etc_dir + element.getName() + ".txt");
116 FileOutputStream fos = new FileOutputStream(out_file);
117 OutputStreamWriter osw = new OutputStreamWriter(fos);
118 BufferedWriter bw = new BufferedWriter(osw, Utility.BUFFER_SIZE);
119 Vector all_values = model.traverseTree();
120 for(int i = 0; i < all_values.size(); i++) {
121 GValueNode node = (GValueNode)all_values.get(i);
122 TreePath path = new TreePath(node.getPath());
123 String value = node.getFullPath(false);
124 String index = model.getHIndex(value);
125 String alias = node.getAlias(index);
126 if(value.indexOf(GValueModel.PATH_SEP) != -1) {
127 value = value.substring(value.lastIndexOf(GValueModel.PATH_SEP) + GValueModel.PATH_SEP.length());
128 }
129 write(bw, "\"" + Utility.stripNL(value) + "\"\t" + index + "\t\"" + Utility.stripNL(value) + "\"");
130 }
131 // Very important we do this, or else buffer may not have
132 // flushed.
133 bw.flush();
134 bw.close();
135 }
136 catch(Exception error) {
137 error.printStackTrace();
138 }
139 }
140
141 /** Returns the given tree path as path formatted string (ie subject\subject\subject).
142 * @param tree The <strong>JTree</strong> the TreePath came from. Used to determine if the root node should be encoded as well.
143 * @param path A <strong>TreePath</strong> that you wish to encode to String.
144 * @param full <i>true</i> if this is the full path (ie a leaf node), <i>false</i> otherwise.
145 * @return A <strong>String</strong> encoding the information from the TreePath.
146 * @see javax.swing.JTree
147 * @see javax.swing.tree.TreePath
148 */
149// static public String formatPath(JTree tree, TreePath path, boolean full) {
150// String text = "";
151// int i = 0;
152// if(tree == null || (!tree.isRootVisible() && !full)) {
153// i = 1;
154// }
155// for( ; i < path.getPathCount(); i++) {
156// GValueNode node = (GValueNode)path.getPathComponent(i);
157// text = text + node.toString();
158// if(node.getChildCount() > 0) {
159// text = text + "\\";
160// }
161// }
162// if(full && text.endsWith("\\")) {
163// return text.substring(0, text.length() - 1);
164// }
165// return text;
166// }
167
168 static final private long showTime(String message, long time) {
169 if(time == -1) {
170 ///ystem.err.println(message + System.currentTimeMillis());
171 }
172 else {
173 ///ystem.err.println(message + (System.currentTimeMillis() - time));
174 }
175 return System.currentTimeMillis();
176 }
177}
Note: See TracBrowser for help on using the repository browser.