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.greenstone.gatherer.valuetree.GValueTree;
|
---|
56 | import org.w3c.dom.Element;
|
---|
57 |
|
---|
58 | public 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 | public static void write(Gatherer gatherer, String etc_dir) {
|
---|
101 | if(gatherer != null && gatherer.c_man != null && gatherer.c_man.getCollection() != null && gatherer.c_man.getCollection().msm != null) {
|
---|
102 | Vector elements = gatherer.c_man.getCollection().msm.getElements(true);
|
---|
103 | for(int i = 0; i < elements.size(); i++) {
|
---|
104 | ElementWrapper element = (ElementWrapper)elements.get(i);
|
---|
105 | ///ystem.err.print("Checking " + element + " for HFile: ");
|
---|
106 | GValueModel model = gatherer.c_man.getCollection().msm.getValueTree(element);
|
---|
107 | if(model != null && (element.getNamespace().equals(MetadataSetManager.HIDDEN) || model.isHierarchy())) {
|
---|
108 | ///ystem.err.println("Found. Writing file.");
|
---|
109 | write(model, gatherer.c_man.getCollection().msm, etc_dir);
|
---|
110 | }
|
---|
111 | else {
|
---|
112 | ///ystem.err.println("No file found.");
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | private String getHIndex(Gatherer gatherer, GValueModel model, String value) {
|
---|
119 | String index = null;
|
---|
120 | index = (String) known_indexes.get(value);
|
---|
121 | if(index == null) {
|
---|
122 | index = model.getHIndex(value);
|
---|
123 | ///ystem.err.println("Adding to known indexes: " + value + " -> " + index);
|
---|
124 | known_indexes.put(value, index);
|
---|
125 | }
|
---|
126 | return index;
|
---|
127 | }
|
---|
128 |
|
---|
129 | static private String safe(String unsafe) {
|
---|
130 | String safe_str = "";
|
---|
131 | for(int i = 0; i < unsafe.length(); i++) {
|
---|
132 | char c = unsafe.charAt(i);
|
---|
133 | if(c != ' ') {
|
---|
134 | safe_str = safe_str + c;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | return safe_str;
|
---|
138 | }
|
---|
139 |
|
---|
140 | static private void write(Writer w, String text)
|
---|
141 | throws Exception {
|
---|
142 | text = text + "\r\n";
|
---|
143 | char buffer[] = text.toCharArray();
|
---|
144 | w.write(buffer, 0, buffer.length);
|
---|
145 | }
|
---|
146 |
|
---|
147 | static public void write(GValueModel model, MetadataSetManager msm, String etc_dir) {
|
---|
148 | try {
|
---|
149 | File out_file = new File(etc_dir + model.toString() + ".txt");
|
---|
150 | FileOutputStream fos = new FileOutputStream(out_file);
|
---|
151 | OutputStreamWriter osw = new OutputStreamWriter(fos);
|
---|
152 | BufferedWriter bw = new BufferedWriter(osw, Utility.BUFFER_SIZE);
|
---|
153 | Vector all_values = model.traverseTree();
|
---|
154 | for(int i = 0; i < all_values.size(); i++) {
|
---|
155 | GValueNode node = (GValueNode)all_values.get(i);
|
---|
156 | TreePath path = new TreePath(node.getPath());
|
---|
157 | String value = GValueTree.formatPath(null, path, true);
|
---|
158 | String index = model.getHIndex(value);
|
---|
159 | String alias = node.getAlias(index);
|
---|
160 | if(value.indexOf("\\") != -1) {
|
---|
161 | value = value.substring(value.lastIndexOf("\\") + 1);
|
---|
162 | }
|
---|
163 | write(bw, index + "\t\"" + alias + "\"\t\"" + Utility.stripNL(value) + "\"");
|
---|
164 | }
|
---|
165 | // Very important we do this, or else buffer may not have
|
---|
166 | // flushed.
|
---|
167 | bw.flush();
|
---|
168 | bw.close();
|
---|
169 | }
|
---|
170 | catch(Exception error) {
|
---|
171 | error.printStackTrace();
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | static final private long showTime(String message, long time) {
|
---|
176 | if(time == -1) {
|
---|
177 | ///ystem.err.println(message + System.currentTimeMillis());
|
---|
178 | }
|
---|
179 | else {
|
---|
180 | ///ystem.err.println(message + (System.currentTimeMillis() - time));
|
---|
181 | }
|
---|
182 | return System.currentTimeMillis();
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|