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

Last change on this file since 5150 was 5150, checked in by jmt12, 21 years ago

Fix 203B006

  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 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;
42//import net.n3.nanoxml.IXMLElement;
43//import net.n3.nanoxml.IXMLParser;
44//import net.n3.nanoxml.IXMLReader;
45//import net.n3.nanoxml.StdXMLReader;
46//import net.n3.nanoxml.XMLParserFactory;
47import org.greenstone.gatherer.Gatherer;
48import org.greenstone.gatherer.gui.SaveProgressDialog;
49import org.greenstone.gatherer.msm.ElementWrapper;
50import org.greenstone.gatherer.msm.Metadata;
51import org.greenstone.gatherer.msm.MetadataSetManager;
52import org.greenstone.gatherer.util.Utility;
53import org.greenstone.gatherer.valuetree.GValueModel;
54import org.greenstone.gatherer.valuetree.GValueNode;
55import org.greenstone.gatherer.valuetree.GValueTree;
56import org.w3c.dom.Element;
57
58public class MetadataXML {
59 //private boolean can_wait = true;
60 private Gatherer gatherer = null;
61 private Hashtable known_indexes = null;
62 //private int spare_processes = 25;
63 //private MetadataXML mummy = null;
64 private SaveProgressDialog spd = null;
65 //private Vector complete = null;
66 //private Vector processes = null;
67
68 static public String METADATA_FILE = "metadata.xml";
69
70 // not actually used anywhere
71 /* static public IXMLElement read(String filename) {
72 IXMLElement xml = null;
73 IXMLParser parser = null;
74 IXMLReader reader = null;
75 try {
76 parser = XMLParserFactory.createDefaultXMLParser();
77 File in_file = new File(filename);
78 FileReader in_reader = new FileReader(in_file);
79 BufferedReader in = new BufferedReader(in_reader, Utility.BUFFER_SIZE);
80 String content = "";
81 String next_line = null;
82 while((next_line = in.readLine()) != null) {
83 // Throw away the document type, as I don't want to deal
84 // with dtd's. If someone has tampered with these files to
85 // the point that this parser can't read them then too bad.
86 if(!next_line.startsWith("<!DOCTYPE")) {
87 content = content + next_line;
88 }
89 }
90 in.close();
91 reader = StdXMLReader.stringReader(content);
92 parser.setReader(reader);
93 xml = (IXMLElement) parser.parse();
94 }
95 catch (Exception e) {
96 }
97 return xml;
98 }
99 */
100
101 /**
102 public static void write(Gatherer gatherer, String etc_dir) {
103 if(gatherer != null && gatherer.c_man != null && gatherer.c_man.getCollection() != null && gatherer.c_man.getCollection().msm != null) {
104 Vector elements = gatherer.c_man.getCollection().msm.getElements(true);
105 for(int i = 0; i < elements.size(); i++) {
106 ElementWrapper element = (ElementWrapper)elements.get(i);
107 ///ystem.err.print("Checking " + element + " for HFile: ");
108 GValueModel model = gatherer.c_man.getCollection().msm.getValueTree(element);
109 if(model != null && (element.getNamespace().equals(MetadataSetManager.HIDDEN) || model.isHierarchy())) {
110 ///ystem.err.println("Found. Writing file.");
111 write(model, gatherer.c_man.getCollection().msm, etc_dir);
112 }
113 else {
114 ///ystem.err.println("No file found.");
115 }
116 }
117 }
118 }
119 */
120
121 private String getHIndex(Gatherer gatherer, GValueModel model, String value) {
122 String index = null;
123 index = (String) known_indexes.get(value);
124 if(index == null) {
125 index = model.getHIndex(value);
126 ///ystem.err.println("Adding to known indexes: " + value + " -> " + index);
127 known_indexes.put(value, index);
128 }
129 return index;
130 }
131
132 static private String safe(String unsafe) {
133 String safe_str = "";
134 for(int i = 0; i < unsafe.length(); i++) {
135 char c = unsafe.charAt(i);
136 if(c != ' ') {
137 safe_str = safe_str + c;
138 }
139 }
140 return safe_str;
141 }
142
143 static private void write(Writer w, String text)
144 throws Exception {
145 text = text + "\r\n";
146 char buffer[] = text.toCharArray();
147 w.write(buffer, 0, buffer.length);
148 }
149
150 static public void write(ElementWrapper element, GValueModel model, MetadataSetManager msm, String etc_dir) {
151 try {
152 File out_file = new File(etc_dir + element.getName() + ".txt");
153 FileOutputStream fos = new FileOutputStream(out_file);
154 OutputStreamWriter osw = new OutputStreamWriter(fos);
155 BufferedWriter bw = new BufferedWriter(osw, Utility.BUFFER_SIZE);
156 Vector all_values = model.traverseTree();
157 for(int i = 0; i < all_values.size(); i++) {
158 GValueNode node = (GValueNode)all_values.get(i);
159 TreePath path = new TreePath(node.getPath());
160 String value = node.getFullPath(); // formatPath(null, path, true);
161 String index = model.getHIndex(value);
162 String alias = node.getAlias(index);
163 if(value.indexOf("\\") != -1) {
164 value = value.substring(value.lastIndexOf("\\") + 1);
165 }
166 write(bw, index + "\t\"" + alias + "\"\t\"" + Utility.stripNL(value) + "\"");
167 }
168 // Very important we do this, or else buffer may not have
169 // flushed.
170 bw.flush();
171 bw.close();
172 }
173 catch(Exception error) {
174 error.printStackTrace();
175 }
176 }
177
178 /** Returns the given tree path as path formatted string (ie subject\subject\subject).
179 * @param tree The <strong>JTree</strong> the TreePath came from. Used to determine if the root node should be encoded as well.
180 * @param path A <strong>TreePath</strong> that you wish to encode to String.
181 * @param full <i>true</i> if this is the full path (ie a leaf node), <i>false</i> otherwise.
182 * @return A <strong>String</strong> encoding the information from the TreePath.
183 * @see javax.swing.JTree
184 * @see javax.swing.tree.TreePath
185 */
186// static public String formatPath(JTree tree, TreePath path, boolean full) {
187// String text = "";
188// int i = 0;
189// if(tree == null || (!tree.isRootVisible() && !full)) {
190// i = 1;
191// }
192// for( ; i < path.getPathCount(); i++) {
193// GValueNode node = (GValueNode)path.getPathComponent(i);
194// text = text + node.toString();
195// if(node.getChildCount() > 0) {
196// text = text + "\\";
197// }
198// }
199// if(full && text.endsWith("\\")) {
200// return text.substring(0, text.length() - 1);
201// }
202// return text;
203// }
204
205 static final private long showTime(String message, long time) {
206 if(time == -1) {
207 ///ystem.err.println(message + System.currentTimeMillis());
208 }
209 else {
210 ///ystem.err.println(message + (System.currentTimeMillis() - time));
211 }
212 return System.currentTimeMillis();
213 }
214}
Note: See TracBrowser for help on using the repository browser.