source: main/trunk/gli/src/org/greenstone/gatherer/gui/FormatConversionDialog.java@ 29034

Last change on this file since 29034 was 29034, checked in by ak19, 10 years ago

Minor changes and corrections.

File size: 34.3 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Copyright (C) 1999 New Zealand Digital Library Project
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *########################################################################
24 */
25
26package org.greenstone.gatherer.gui;
27
28import org.greenstone.gatherer.Configuration;
29import org.greenstone.gatherer.Dictionary;
30import org.greenstone.gatherer.Gatherer;
31import org.greenstone.gatherer.cdm.CollectionConfigXMLReadWrite;
32import org.greenstone.gatherer.util.Codec;
33import org.greenstone.gatherer.util.InputStreamGobbler;
34import org.greenstone.gatherer.util.OutputStreamGobbler;
35import org.greenstone.gatherer.util.StaticStrings;
36import org.greenstone.gatherer.util.XMLTools;
37
38import org.w3c.dom.*;
39
40import java.io.File;
41import java.io.IOException;
42
43import java.awt.*;
44import java.awt.event.*;
45import java.util.*;
46import javax.swing.*;
47import javax.swing.border.*;
48import javax.swing.event.*;
49
50
51// TO DO:
52// + StreamGobblers: http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html
53// Need to test on Windows: the Ctrl-D/Ctrl-Z sent to formatconverter.exe. May not need to do anything special for Windows now that this code is using StreamGobblers to process the in/out streams of the process
54// + Need to use dictionary for labels
55// X convertFormat() for remote GS. Remote does not display Format Conversion wizard
56// + HTML tidy (put all "<br/>" back to "<br>" in luce/etc/collectionConfig.xml and run again)
57// + Help tooltips on buttons
58// + Undo, Redo buttons
59// X Split class into dialog/widgets and data processing?
60// http://www.java2s.com/Tutorial/Java/0240__Swing/SettingJOptionPanebuttonlabelstoFrench.htm
61
62public class FormatConversionDialog extends ModalDialog
63{
64 public static final String GSF_FORMAT_GS2_TAG = "gsf:format-gs2";
65 private static final String GSF_GS3_ROOT_TAG = "gsf:gs3-root";
66
67 // The cmdline programs launched by this dialog
68 private static final int XMLTIDY = 0;
69 private static final int FORMATCONVERTER = 1;
70 // Online HTML tidy for learning usage: http://infohound.net/tidy/
71 private static final String[] xmltidy_cmd_args = {"tidy", "-config", Configuration.getGS3BinPath() + "tidyconfig.cfg", "-utf8", "-wrap", "0", "-raw", "-q"}; // {"tidy", "-xml", "-utf8"};
72 private static final String[] formatconverter_cmd_args = {Configuration.getGS3BinPath() + "formatconverter", "--silent"};
73
74 private static final Dimension SIZE = new Dimension(640,480);
75
76 // the important member variables
77 private File collect_cfg_file = null;
78 private Document xml_file_doc = null;
79 private NodeList gsf_format_gs2_list = null;
80
81 private int current_index = -1;
82 private int dlgResult = OpenCollectionDialog.OK_OPTION;
83 private int process_exitValue = -1;
84
85 // widgets
86 final String WARNING_TITLE = Dictionary.get("General.Warning")
87 + ": " + Dictionary.get("FormatConversionDialog.Invalid_XML")
88 + " " + Dictionary.get("FormatConversionDialog.Invalid_XML_Warning_Title");
89
90 private static final Border EMPTYBORDER = new EmptyBorder(5, 5, 5, 5);
91 private JLabel section_label = null;
92 private GLIButton cancel_button = null;
93 private GLIButton next_button = null;
94 private GLIButton accept_all_button = null;
95 private GLIButton htmltidy_button = null;
96 private GLIButton xmltidy_button = null;
97 // the 2 NumberedJTextAreas. Each provide their own undo and redo buttons already hooked up to listeners
98 private NumberedJTextArea gs2_textarea = null;
99 private NumberedJTextArea gs3_textarea = null;
100 private JLabel count_label = null;
101 private JLabel statusbar = null;
102
103 public FormatConversionDialog (File collect_cfg_file, Document xml_file_doc, NodeList gsf_format_gs2_list) {
104 super(Gatherer.g_man, "", true);
105
106 this.collect_cfg_file = collect_cfg_file;
107 this.xml_file_doc = xml_file_doc;
108 this.gsf_format_gs2_list = gsf_format_gs2_list;
109
110 this.setComponentOrientation(Dictionary.getOrientation());
111 setSize(SIZE);
112 setTitle(Dictionary.get("FormatConversionDialog.Title"));
113 this.addWindowListener(new WindowClosingListener()); // the dialog's top right close button should behave as a Cancel press
114 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
115
116 // The NumberedJTextArea provides its own undo and redo buttons, already hooked up to listeners
117 // Just need to place these buttons in the dialog
118 gs2_textarea = new NumberedJTextArea("gs2", Dictionary.get("FormatConversionDialog.GS2_Text_Tooltip"));
119 gs3_textarea = new NumberedJTextArea("gs3", Dictionary.get("FormatConversionDialog.GS3_Text_Tooltip"));
120
121
122 JPanel midbutton_panel = new JPanel(); // FlowLayout by default in a JPanel
123 midbutton_panel.setComponentOrientation(Dictionary.getOrientation());
124
125 JButton reconvert_button = new GLIButton(Dictionary.get("FormatConversionDialog.Reconvert"), Dictionary.get("FormatConversionDialog.Reconvert_Tooltip"));
126
127 midbutton_panel.add(gs2_textarea.undoButton);
128 midbutton_panel.add(gs2_textarea.redoButton);
129 midbutton_panel.add(reconvert_button);
130 reconvert_button.addActionListener(new ReconvertListener());
131 reconvert_button.setAlignmentY(Component.CENTER_ALIGNMENT);
132
133 section_label = new JLabel("Section Label Goes Here"); // title will be replaced, don't need to translate
134 section_label.setBorder(EMPTYBORDER);
135 section_label.setComponentOrientation(Dictionary.getOrientation());
136
137 JPanel button1_panel = new JPanel();
138 button1_panel.setComponentOrientation(Dictionary.getOrientation());
139
140 xmltidy_button = new GLIButton(Dictionary.get("FormatConversionDialog.XHTML_Tidy"), Dictionary.get("FormatConversionDialog.XHTML_Tidy_Tooltip"));
141 xmltidy_button.addActionListener(new XMLTidyButtonListener());
142
143 button1_panel.add(gs3_textarea.undoButton);
144 button1_panel.add(gs3_textarea.redoButton);
145 button1_panel.add(xmltidy_button);
146
147 JPanel button_panel = new JPanel(new FlowLayout());
148 button_panel.setComponentOrientation(Dictionary.getOrientation());
149 count_label = new JLabel("<count>"); // placeholder text for numbers, no need to translate
150 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("FormatConversionDialog.Cancel_Tooltip"));
151 next_button = new GLIButton(Dictionary.get("FormatConversionDialog.Next"), Dictionary.get("FormatConversionDialog.Next_Tooltip"));
152 accept_all_button = new GLIButton(Dictionary.get("FormatConversionDialog.Accept_All"), Dictionary.get("FormatConversionDialog.Accept_All_Tooltip"));
153 cancel_button.addActionListener(new CancelButtonListener());
154 next_button.addActionListener(new NextButtonListener());
155 accept_all_button.addActionListener(new AcceptAllButtonListener());
156 button_panel.add(cancel_button);
157 button_panel.add(next_button);
158 button_panel.add(accept_all_button);
159 button_panel.add(count_label);
160
161
162 JPanel centre_panel = new JPanel();
163 centre_panel.setLayout(new BoxLayout(centre_panel, BoxLayout.Y_AXIS));
164 centre_panel.setComponentOrientation(Dictionary.getOrientation());
165 centre_panel.setBorder(EMPTYBORDER);
166 centre_panel.add(new JScrollPane(gs2_textarea));
167 centre_panel.add(midbutton_panel);
168 centre_panel.add(new JScrollPane(gs3_textarea));
169
170 JPanel bottom_panel = new JPanel();
171 bottom_panel.setLayout(new BoxLayout(bottom_panel, BoxLayout.Y_AXIS));
172 bottom_panel.setComponentOrientation(Dictionary.getOrientation());
173 bottom_panel.setBorder(EMPTYBORDER);
174
175 bottom_panel.add(button1_panel);
176 bottom_panel.add(button_panel);
177 statusbar = new JLabel("");
178 statusbar.setBorder(EMPTYBORDER);
179 // http://stackoverflow.com/questions/2560784/how-to-center-elements-in-the-boxlayout-using-center-of-the-element
180 statusbar.setAlignmentX(Component.CENTER_ALIGNMENT);
181 bottom_panel.add(statusbar);
182
183 // add all the widgets to the contentpane
184 JPanel content_pane = (JPanel) getContentPane();
185 content_pane.setComponentOrientation(Dictionary.getOrientation());
186 content_pane.setLayout(new BorderLayout());
187 //content_pane.setLayout(new GridLayout(7,1));
188 //content_pane.setLayout(new BoxLayout(content_pane, BoxLayout.Y_AXIS));
189 //content_pane.setBorder(EMPTYBORDER);
190
191 content_pane.add(section_label, BorderLayout.NORTH);
192 content_pane.add(centre_panel, BorderLayout.CENTER);
193 content_pane.add(bottom_panel, BorderLayout.SOUTH);
194
195
196 // Final dialog setup & positioning.
197 getRootPane().setDefaultButton(next_button);
198 Dimension screen_size = Configuration.screen_size;
199 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
200 screen_size = null;
201 //setVisible(true);
202 }
203
204
205 public int getDialogResult() {
206 return dlgResult; // OK_OPTION or CANCEL_OPTION
207 }
208
209 //*************************PROCESSING FUNCTIONS***************************//
210 public static int checkForGS2FormatStatements(File collect_cfg_file) {
211
212 if(Gatherer.GS3 && collect_cfg_file.getAbsolutePath().endsWith(".xml")) {
213
214 //System.err.println("*** Opening an xml config file");
215
216 Document xml_file_doc = XMLTools.parseXMLFile(collect_cfg_file);
217 Element root = xml_file_doc.getDocumentElement();
218
219 // check if there are any <gsf:format-gs2 /> elements. If there are, then may need to process them
220
221 //NodeList gsf_format_gs2_list = root.getElementsByTagNameNS("gsf", "format-gs2");
222 NodeList gsf_format_gs2_list = root.getElementsByTagName(FormatConversionDialog.GSF_FORMAT_GS2_TAG);
223 if(gsf_format_gs2_list != null && gsf_format_gs2_list.getLength() > 0) {
224
225 // Sample the first of the <gsf:gs2-format/> elements to
226 // check we don't have any CDataSections in the <gsf:gs2-format/> elements
227 // If the first <gsf:gs2-format/> has a CDataSection, it means we've already
228 // converted it to GS3 format statements during an earlier GLI session.
229
230 Node gs2format = gsf_format_gs2_list.item(0);
231 //gs2format.normalize();
232 NodeList children = gs2format.getChildNodes();
233
234 for(int i = 0; i < children.getLength(); i++) {
235 Node child = children.item(i);
236 if(child.getNodeType() == Node.CDATA_SECTION_NODE) {
237 // there are GS2 statements in col config, but they've already been converted to GS3
238 // can open the collection without going through the FormatConversionDialog
239 return OpenCollectionDialog.OK_OPTION;
240 }
241 }
242 System.err.println("*** Found GS2 format statements in config file to be converted to GS3.");
243
244
245 // if remote GS3, do we open the collection with the html-encoded GS2 format statements
246 // or do we not allow the remote user to open such a collection at all?
247 // For now we allow them to open it, but print a warning that conversions are not possible.
248
249 if(Gatherer.isGsdlRemote) { // remote GS3
250 System.err.println("*** Cannot convert GS2 collections from a remote GS3 server.");
251 return OpenCollectionDialog.OK_OPTION;
252 }
253
254 // If we get here, it means there were no CDataSections in the first (any) <gsf:gs2-format/>
255 // This means it's the first time the GS2 collection is being opened in GLI
256 // Open the FormatCconversionDialog and convert the gs2 statements to gs3:
257 FormatConversionDialog formatconversionDlg = new FormatConversionDialog(collect_cfg_file, xml_file_doc, gsf_format_gs2_list);
258 formatconversionDlg.convertGS2FormatStatements();
259 return formatconversionDlg.getDialogResult();
260
261 }
262 }
263 return OpenCollectionDialog.OK_OPTION; // no GS2 statements in col config, and can open the collection
264 }
265
266
267 /**
268 * runInteractiveProgram() runs a cmdline program that reads from stdinput
269 * until Ctrl-D is encountered. It outputs the result to stdout.
270 *
271 * The cmdline programs HTML Tidy and FormatConverter both behave the same way:
272 * When the formatconverter binary is run in silent mode, it expects input
273 * followed by a newline and then EOF (or if no newline, then 2 EOFs)
274 * which is Ctrl-D on Linux/Mac and Ctrl-Z on Windows.
275 * Then the cmdline program exits by printing the result of the conversion.
276 *
277 * Explicitly sending EOFs from java is no longer necessary, as the OutputStreamGobbler
278 * Thread takes care of closing the process stream.
279 *
280 * HTMLTidy returns 0 if no warnings or errors, 1 if just Warnings, 2 if Errors (failure)
281 * http://sourceforge.net/p/tidy/mailman/tidy-develop/thread/[email protected]/
282 *
283 * This code uses the StreamGobbler classes based on
284 * http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2
285 */
286 public String runInteractiveProgram(int program, String inputstr) {
287 String outputstr = "";
288 String[] command_args;
289 process_exitValue = -1;
290
291 if(program == XMLTIDY) {
292 command_args = xmltidy_cmd_args;
293 } else if(program == FORMATCONVERTER) {
294 command_args = formatconverter_cmd_args;
295 } else { // unknown command
296 return outputstr;
297 }
298
299 // Generate the formatconverter command
300 /*if (Gatherer.isGsdlRemote) {
301
302 }*/
303
304 try {
305
306 Runtime rt = Runtime.getRuntime();
307 Process prcs = null;
308
309 prcs = rt.exec(command_args);
310
311 // send inputstr to process
312 OutputStreamGobbler inputGobbler = new OutputStreamGobbler(prcs.getOutputStream(), inputstr);
313
314 // monitor for any error messages
315 InputStreamGobbler errorGobbler = new InputStreamGobbler(prcs.getErrorStream(), true);
316
317 // monitor for the expected output line(s)
318 InputStreamGobbler outputGobbler = new InputStreamGobbler(prcs.getInputStream());
319
320 // kick them off
321 inputGobbler.start();
322 errorGobbler.start();
323 outputGobbler.start();
324
325 // any error???
326 process_exitValue = prcs.waitFor();
327 //System.out.println("ExitValue: " + exitVal);
328
329 // From the comments of
330 // http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2
331 // To avoid running into nondeterministic failures to get the process output
332 // if there's no waiting for the threads, call join() on each Thread (StreamGobbler) object:
333 outputGobbler.join();
334 errorGobbler.join();
335 inputGobbler.join();
336
337 outputstr = outputGobbler.getOutput();
338 String errmsg = errorGobbler.getOutput();
339 if(!errmsg.equals("")) {
340 System.err.println("*** Process errorstream: \n" + errmsg + "\n****");
341 }
342
343 } catch(IOException ioe) {
344 System.err.println("IOexception " + ioe.getMessage());
345 //ioe.printStackTrace();
346 } catch(InterruptedException ie) {
347 System.err.println("Process InterruptedException " + ie.getMessage());
348 //ie.printStackTrace();
349 }
350
351 return outputstr;
352 }
353
354 public void convertGS2FormatStatements() {
355
356 // at this point, we know there are one or more <gsf:format-gs2 /> elements
357 // process each of them as follows: unescape, then call formatconverter, then call html tidy on it
358 //NodeList gsf_format_gs2_list = root.getElementsByTagNameNS("gsf", "format-gs2");
359
360 int len = gsf_format_gs2_list.getLength();
361
362 for(int i = 0; i < len; i++) {
363
364 Element gs2format = (Element)gsf_format_gs2_list.item(i);
365 String gs2formatstr = XMLTools.getElementTextValue(gs2format); // seems to already unescape the html entities
366 //gs2formatstr = Codec.transform(gs2formatstr, Codec.ESCAPEDHTML_TO_UNESCAPED);
367
368 processFormatStatement(i, gs2formatstr);
369 }
370
371 increment(); // modifies the textareas to initialise them
372 setVisible(true);
373 }
374
375
376 private String processFormatStatement(int i, String gs2formatstr) {
377 String errorMsg = "";
378
379 boolean startsWithTableCell = (gs2formatstr.toLowerCase().startsWith("<td")) ? true : false;
380
381 //System.err.println("*** Found: " + gs2formatstr);
382
383 String gs3formatstr = runInteractiveProgram(FORMATCONVERTER, gs2formatstr);
384 gs3formatstr = gs3formatstr.replace("> <", "><");
385
386 //System.err.println("*** Format is now: " + gs3formatstr);
387
388 String gs3formatstr_notags = gs3formatstr;
389 gs3formatstr = addSurroundingTags(gs3formatstr);
390
391
392 String validationMsg = XMLTools.parseDOM(gs3formatstr);
393 if(!validationMsg.startsWith(XMLTools.WELLFORMED)) {
394 // Run Html Tidy in XML mode
395 //System.err.println("*** Needing to run HTML Tidy on: ");
396 //System.err.println(gs3formatstr_notags);
397
398
399 // HTMLTidy returns 0 if no warnings or errors, 1 if just Warnings, 2 if Errors (failure)
400 // http://sourceforge.net/p/tidy/mailman/tidy-develop/thread/[email protected]/
401 String htmltidy_string = runInteractiveProgram(XMLTIDY, gs3formatstr_notags);//removeSurroundingTags(gs3formatstr));
402
403 if(process_exitValue >= 2) {
404 System.err.println("@@@ Process exit value: " + process_exitValue);
405 errorMsg = Dictionary.get("FormatConversionDialog.Tidy_Failed")
406 + " " + Dictionary.get("FormatConversionDialog.XML_Still_Invalid");
407 } else {
408 errorMsg = "";
409
410 gs3formatstr_notags = htmltidy_string;
411
412 gs3formatstr_notags = removeHTMLTags(i, gs3formatstr_notags, startsWithTableCell);
413 gs3formatstr = addSurroundingTags(gs3formatstr_notags);
414
415 // Having applied html-tidy, setGS3Format() will return true if it finally parsed
416 }
417 }
418
419 // For now, assume HTML Tidy has worked and that the gs3format has now been parsed successfully
420
421 boolean parsed = setGS3Format(i, gs3formatstr); // will parse the gs3formatstr into a DOM object
422 if(!parsed && errorMsg.equals("")) {
423 errorMsg = Dictionary.get("FormatConversionDialog.Tidy_Done")
424 + " " + Dictionary.get("FormatConversionDialog.XML_Still_Invalid");
425 }
426 return errorMsg;
427 //return gs3formatstr;
428 }
429
430 // HTML tidy adds entire HTML tags around a single format statement. This method removes it.
431 private String removeHTMLTags(int i, String gs3formatstr_notags, boolean startsWithTD) {
432
433 // if it's a VList classifier <gsf:template match="documentNode|classifierNode"/>
434 // and the gs2 format statement starts with a <td>,
435 // then remove up to and including the outermost <tr>,
436 // else remove up to and including the <body> tag
437
438 String removeOuterTag = "body>";
439 Element parent = (Element)getParentNode(i); // gets parent of GS2format: <gsf:template>
440 if(parent.hasAttribute("match") && (!parent.hasAttribute("mode") || !parent.getAttribute("mode").equals("horizontal"))) {
441 if(startsWithTD) {
442 removeOuterTag = "tr>"; // remove the outermost <tr> cell HTML tidy added around the tablecell
443 }
444 }
445
446 // <unwanted>
447 // <removeThisOuterTag>
448 // lines we want
449 // </removeThisOuterTag>
450 // <unwanted>
451
452 int end = gs3formatstr_notags.indexOf(removeOuterTag);
453 if(end != -1) {
454 gs3formatstr_notags = gs3formatstr_notags.substring(end+removeOuterTag.length());
455 }
456 int start = gs3formatstr_notags.lastIndexOf("</"+removeOuterTag); //closing tag
457 if(start != -1) {
458 gs3formatstr_notags = gs3formatstr_notags.substring(0, start);
459 }
460
461 //System.err.println("@@@@ " + startsWithTD + " - TAG: " + removeOuterTag + " - AFTER REMOVING TAGS:\n" + gs3formatstr_notags);
462
463 return gs3formatstr_notags;
464 }
465
466
467 //**************** ACCESS FUNCTIONS ***************//
468
469 // gs2 format text is the text string that goes into <gsf:format-gs2/>: <gsf:format-gs2>text</gsf:format-gs2>
470 private void setGS2Format(int i, String text) {
471 XMLTools.setElementTextValue(getGS2Format(i), text);
472 }
473
474 // gs3FormatStr represents DOM, and must be parsed and appended as sibling to the gs2format element
475 // as <gsf:gs3-root/>. If it fails to parse, nest it in a CDATA element of <gsf:gs3-root/>
476 private boolean setGS3Format(int i, String gs3formatstr) {
477
478 Document ownerDoc = getGS2Format(i).getOwnerDocument();
479 Node gs3format = null;
480 boolean parse_success = false;
481
482
483 String validationMsg = XMLTools.parseDOM(gs3formatstr);
484 if(!validationMsg.startsWith(XMLTools.WELLFORMED)) {
485 // silently add the gs3formatstr in a CDATA block into the root
486
487 gs3formatstr = removeSurroundingTags(gs3formatstr);
488 gs3format = ownerDoc.createElement(GSF_GS3_ROOT_TAG);
489 Node cdataSection = ownerDoc.createCDATASection(gs3formatstr);
490 gs3format.appendChild(cdataSection);
491 parse_success = false;
492 }
493 else {
494
495 // parse DOM into XML
496 Document doc = XMLTools.getDOM(gs3formatstr);
497 Element root = doc.getDocumentElement();
498 gs3format = ownerDoc.importNode(root, true); // an element
499 parse_success = true;
500
501 // System.err.println("@@@@ ROOT:\n" + XMLTools.xmlNodeToString(root));
502 }
503
504 // add gs3format element as sibling to gs2format element
505 Element oldgs3format = getGS3Format(i);
506 if(oldgs3format == null) {
507 getParentNode(i).appendChild(gs3format);
508 // System.err.println("@@@ APPEND");
509 } else {
510 // replace the existing
511 getParentNode(i).replaceChild(gs3format, oldgs3format);
512 // System.err.println("@@@ REPLACE");
513 }
514
515 //http://stackoverflow.com/questions/12524727/remove-empty-nodes-from-a-xml-recursively
516
517 return parse_success;
518 }
519
520 private Node getParentNode(int i) {
521 return gsf_format_gs2_list.item(i).getParentNode();
522 }
523
524 private Element getGS2Format(int i) {
525 return (Element)gsf_format_gs2_list.item(i);
526 }
527
528 private String getGS2FormatString(int i) {
529 return XMLTools.getElementTextValue(getGS2Format(i));
530 }
531
532 private Element getGS3Format(int i) {
533 Element parent = (Element)getParentNode(i);
534 NodeList nl = parent.getElementsByTagName(GSF_GS3_ROOT_TAG);
535 if(nl.getLength() > 0) {
536 return (Element)nl.item(0);
537 }
538 return null;
539 }
540
541 private String getGS3FormatString(int i) {
542 Element gs3format = getGS3Format(i);
543 if(gs3format == null) {
544 return "";
545 } else {
546
547 // if any child is a CData section, return its text as-is. There will be no indenting
548 NodeList children = gs3format.getChildNodes();
549 for(int j = 0 ; j < children.getLength(); j++) {
550 if(children.item(j).getNodeType() == Node.CDATA_SECTION_NODE) {
551 return children.item(j).getNodeValue(); // content of CDataSection
552 }
553 }
554
555 // else we have proper nodes, return indented string
556 StringBuffer sb = new StringBuffer();
557 XMLTools.xmlNodeToString(sb, gs3format, true, " ", 0);
558 return sb.toString();
559 }
560 }
561
562
563 private String getLabel(int i) {
564 String label = "";
565
566 // Given XML of the form:
567 // <browse|search>
568 // <format>
569 // <gsf:template match="documentNode|classifierNode" [mode=horizontal]>
570 // <gsf-format:gs2 />
571 // <gs3format/>
572 // </format>
573 // </browse|search>
574
575 // Want to return the label: "browse|search > documentNode|classifierNode [> horizontal]"
576
577 Element parent = (Element)getParentNode(i); // gets parent of GS2format: <gsf:template>
578 label = parent.getAttribute("match"); //e.g. documentNode, classifierNode
579
580 if(parent.hasAttribute("mode")) { // e.g. classifierNode mode=horizontal
581 label = label + " > " + parent.getAttribute("mode");
582 }
583
584 Element ancestor = (Element) parent.getParentNode().getParentNode(); // ancestor: <browse>|<search>
585 label = ancestor.getTagName() + " > " + label;
586
587 return label;
588 }
589
590 // gs2 format statements have spaces instead of newlines. For display, replace with newlines
591 private String makeLines(String gs2formatstr) {
592 return gs2formatstr.replaceAll(">\\s+<", ">\n<");
593 }
594
595 private String singleLine(String gs2formatstr) {
596 return gs2formatstr.replace(">\n<", "> <"); // put the spaces back
597 }
598
599 private String removeSurroundingTags(String xml)
600 {
601 //return xml.replace("<"+GSF_GS3_ROOT_TAG+" xmlns:gsf=\"http://www.greenstone.org/greenstone3/schema/ConfigFormat\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n", "").replace("\n</"+GSF_GS3_ROOT_TAG+">", "");//.trim();
602 return xml.replaceAll("<"+GSF_GS3_ROOT_TAG+" xmlns:gsf=(\"|\')http://www.greenstone.org/greenstone3/schema/ConfigFormat(\"|\') xmlns:xsl=(\"|\')http://www.w3.org/1999/XSL/Transform(\"|\')>\n?", "").replaceAll("\n?</"+GSF_GS3_ROOT_TAG+">", "");//.trim();
603 }
604
605 private String addSurroundingTags(String gs3formatstr) {
606 return "<"+GSF_GS3_ROOT_TAG+" xmlns:gsf='http://www.greenstone.org/greenstone3/schema/ConfigFormat' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>"+gs3formatstr+"</"+GSF_GS3_ROOT_TAG+">";
607 }
608
609 //*************************FUNCTIONS THAT INTERACT WITH WIDGETS************************//
610 // increment() loads the next values into the dialog
611 private boolean increment() {
612 current_index++;
613 section_label.setText ( getLabel(current_index) );
614 gs2_textarea.setText( makeLines(getGS2FormatString(current_index)) );
615 gs3_textarea.setText( removeSurroundingTags(getGS3FormatString(current_index)) );
616 statusbar.setText("");
617
618 // as we're on a new screen of dialog, need to clear all undo/redo history
619 gs2_textarea.discardAllEdits(); // will disable redo/undo buttons
620 gs3_textarea.discardAllEdits();
621
622 int len = gsf_format_gs2_list.getLength();
623 count_label.setText((current_index+1) + " / " + len);
624 if((current_index+1) == len) {
625 return false;
626 } else {
627 return true;
628 }
629 }
630
631
632 private void setStatus(String msg) {
633 statusbar.setText(msg);
634 }
635 private void setErrorStatus(String msg) {
636 statusbar.setBackground(Color.red);
637 statusbar.setText(msg);
638 }
639
640
641 public void dispose() {
642 //System.err.println("@@@@ DIALOG CLOSING!");
643 if(dlgResult != OpenCollectionDialog.CANCEL_OPTION) {
644 // Need to remove the <gsf:gs3-root/> siblings of all <gsf:format-gs2/>
645 // Then, get the children of each <gsf:gs3-root/> and add these as siblings of <gsf:format-gs2/>
646
647
648 int len = gsf_format_gs2_list.getLength();
649 for(int k=len-1; k >= 0; k--) {
650 Element parent = (Element)getParentNode(k);
651
652 //parent.normalize();
653
654 NodeList children = parent.getChildNodes();
655
656 // now have to loop/remove nodes in reverse order, since the following loop
657 // modifies the very nodelist we're looping over by removing nodes from it
658
659 int numChildren = children.getLength()-1;
660
661 for(int i=numChildren; i >= 0; i--) {
662 //for(int i = 0; i < children.getLength(); i++) {
663 Node child = children.item(i);
664
665 if(child.getNodeName().equals(GSF_FORMAT_GS2_TAG)) {
666 // if we're dealing with gs2-format-stmts, put their textnode contents in CData sections
667 // http://www.w3schools.com/xml/xml_cdata.asp
668 // This avoids having to look at html-encoded gs2-format tags in the Format pane
669
670 Element gs2format = (Element)child;
671 String gs2formatstr = XMLTools.getElementTextValue(gs2format);
672 gs2formatstr = Codec.transform(gs2formatstr, Codec.ESCAPEDHTML_TO_UNESCAPED);
673
674 Node textNode = XMLTools.getNodeTextNode(gs2format);
675 Node cdataSection = gs2format.getOwnerDocument().createCDATASection(gs2formatstr);
676 gs2format.replaceChild(cdataSection, textNode);
677 }
678 else if(child.getNodeName().equals(GSF_GS3_ROOT_TAG)) {
679
680 // remove GS3 node and append its children to the parent in its place
681 // the <gsf:gs3-root /> elements wouldn't be in the xml_file_doc DOM tree
682 // unless they were valid XML, so don't need to check for validity here
683
684 Node gs3root = child;
685 NodeList gs3_format_lines = gs3root.getChildNodes();
686
687 for(int j = 0; j < gs3_format_lines.getLength(); j++) {
688 Node duplicate = gs3_format_lines.item(j).cloneNode(true);
689 parent.appendChild(duplicate);
690 }
691 gs3root = parent.removeChild(gs3root);
692 gs3root = null; // finished processing
693
694 } // else - skip all nodes other than <gsf:format-gs2/> and <gsf:gs3-root/>
695 }
696 }
697
698 Element root = xml_file_doc.getDocumentElement();
699 //System.err.println("### XML file to write out:\n" + XMLTools.xmlNodeToString(root));
700
701 // Finally, write out the collection xml file
702 String[] nonEscapingTagNames = { StaticStrings.FORMAT_STR, StaticStrings.DISPLAYITEM_STR };
703 XMLTools.writeXMLFile(collect_cfg_file, xml_file_doc, nonEscapingTagNames);
704
705 }
706 super.dispose();
707
708 }
709
710 //******************INNER CLASSES including LISTENERS and STREAMGOBBLERS****************//
711
712 // windowClosing() is called when the user presses the top-right close button the dialog
713 // this means the user wanted to cancel out of the entire Format Conversion Wizard.
714 private class WindowClosingListener extends WindowAdapter {
715 public void windowClosing(WindowEvent e) {
716 dlgResult = OpenCollectionDialog.CANCEL_OPTION;
717 }
718 }
719
720 private class ReconvertListener implements ActionListener {
721 public void actionPerformed(ActionEvent e) {
722 String gs2formatstr = singleLine(gs2_textarea.getText());
723 String errorMsg = processFormatStatement(current_index, gs2formatstr);
724 gs3_textarea.setText( removeSurroundingTags(getGS3FormatString(current_index)) );
725 if(!errorMsg.equals("")) {
726 setErrorStatus(errorMsg);
727 } else {
728 statusbar.setText("");
729 }
730 }
731 }
732
733
734 private class NextButtonListener implements ActionListener {
735 public void actionPerformed(ActionEvent e) {
736 //statusbar.setText("");
737
738 // check if the GS3 format statement is valid XML before storing. If not, let user to decide
739 // whether they want to re-edit it or if they want to keep it as-is, in which case it needs
740 // to be stored as CDATA, which will make it an inactive format statement.
741 // See http://www.w3schools.com/xml/xml_cdata.asp
742 // setGS3Format() already stores invalidXML as CDATA.
743
744 // Check if the GS3 format statement is valid XML before storing. If not, let the
745 // user to decide whether they want to re-edit it or store it as-is and continue
746
747 // user okay-ed the lines currently displayed, store them
748 setGS2Format( current_index, singleLine(gs2_textarea.getText()) );
749 boolean parse_success = setGS3Format( current_index, addSurroundingTags(gs3_textarea.getText()) );
750
751 if(!parse_success) { // invalid XML, warn the user
752 String message = Dictionary.get("FormatConversionDialog.Invalid_XML") + " " + Dictionary.get("FormatConversionDialog.Cancel_Or_Continue_Next");
753 int user_choice = JOptionPane.showConfirmDialog(FormatConversionDialog.this, message, WARNING_TITLE, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
754
755 if(user_choice == JOptionPane.CANCEL_OPTION) {
756 return; // do nothing on this NextButton press. Don't increment. Let user re-adjust invalid XML for GS3 statement.
757 }
758 }
759
760 if(increment()) {
761 repaint();
762 } else {
763 next_button.setEnabled(false);
764 getRootPane().setDefaultButton(accept_all_button);
765
766 }
767
768 }
769 }
770
771 private class CancelButtonListener implements ActionListener {
772 public void actionPerformed(ActionEvent e) {
773 dlgResult = OpenCollectionDialog.CANCEL_OPTION;
774 FormatConversionDialog.this.dispose(); // close dialog
775 }
776 }
777
778
779 private class AcceptAllButtonListener implements ActionListener {
780 public void actionPerformed(ActionEvent e) {
781 //statusbar.setText("");
782
783 // user okay-ed the lines, store them
784 setGS2Format(current_index, gs2_textarea.getText());
785 String gs3formatstr = gs3_textarea.getText();
786 boolean parse_success = setGS3Format(current_index, addSurroundingTags(gs3formatstr));
787 String message = "";
788
789 if(!parse_success) { // invalid XML for current format statement, warn the user
790 setErrorStatus(Dictionary.get("FormatConversionDialog.Invalid_XML"));
791
792 message = Dictionary.get("FormatConversionDialog.Invalid_XML") + " " + Dictionary.get("FormatConversionDialog.Cancel_Or_Continue_Next");
793 }
794
795 // Even if the current GS3 format statement is valid XML, the user could have pressed
796 // Accept All at the very start of the FormatConversion dialog too. Check all the
797 // subsequent format statements, and if any have invalid XML, warn user.
798 for(int i = current_index+1; parse_success && i < gsf_format_gs2_list.getLength(); i++) {
799 gs3formatstr = getGS3FormatString(i);
800 String validationMsg = XMLTools.parseDOM(gs3formatstr);
801 if(!validationMsg.startsWith(XMLTools.WELLFORMED)) {
802 parse_success = false;
803 message = Dictionary.get("FormatConversionDialog.Cancel_Or_Accept_All");
804 }
805 }
806
807 if(!parse_success) {
808 int user_choice = JOptionPane.showConfirmDialog(FormatConversionDialog.this, message, WARNING_TITLE, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
809 if(user_choice == JOptionPane.CANCEL_OPTION) {
810 return; // Don't close the dialog. Let the user continue looking at this or subsequent GS3 format statements.
811 }
812 }
813
814 // If we're here, then either the format statements parsed, or the user accepted them anyway
815 FormatConversionDialog.this.dispose(); // close dialog
816 }
817 }
818
819 private class XMLTidyButtonListener implements ActionListener {
820 // run HTML tidy taking input from stdin
821 // http://www.w3.org/People/Raggett/tidy/
822 public void actionPerformed(ActionEvent e) {
823 String gs3formatstr_notags = gs3_textarea.getText();
824
825 // Flag to determine which tags that HTML tidy adds need to be removed
826 boolean startsWithTableCell = (gs3formatstr_notags.trim().toLowerCase().startsWith("<td")) ? true : false;
827 // run HTML tidy on the GS3 format statement
828 String htmltidy_string = runInteractiveProgram(XMLTIDY, gs3formatstr_notags);
829
830 if(process_exitValue >= 2) {
831 System.err.println("@@@ Process exit value: " + process_exitValue);
832 setErrorStatus(Dictionary.get("FormatConversionDialog.Tidy_Failed"));
833 } else {
834 gs3formatstr_notags = htmltidy_string;
835
836 // HTML tidy adds extra HTML markup around the formatstring, so will need to remove it:
837 gs3formatstr_notags = removeHTMLTags(current_index, gs3formatstr_notags, startsWithTableCell);
838
839 // put the XML tags around the gs3 format string again so we can convert it to a DOM object
840 String gs3formatstr = addSurroundingTags(gs3formatstr_notags);
841
842 boolean parse_success = setGS3Format(current_index, gs3formatstr); // converts to DOM object
843
844 // Get indented GS3 format string from DOM object and display it in the text area
845 gs3_textarea.setText( removeSurroundingTags(getGS3FormatString(current_index)) );
846
847 if(parse_success) {
848 statusbar.setText(Dictionary.get("FormatConversionDialog.Tidy_Done"));
849 } else {
850 setErrorStatus(Dictionary.get("FormatConversionDialog.Tidy_Done")
851 + " " + Dictionary.get("FormatConversionDialog.Error_GS3_Format")
852 + " " + Dictionary.get("FormatConversionDialog.Invalid_XML"));
853 }
854 }
855 }
856 }
857
858}
Note: See TracBrowser for help on using the repository browser.