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