2 * This file is part of the Vamsas Client version 0.2.
\r
3 * Copyright 2010 by Jim Procter, Iain Milne, Pierre Marguerite,
\r
4 * Andrew Waterhouse and Dominik Lindner.
\r
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\r
9 * The Vamsas Client is free software: you can redistribute it and/or modify
\r
10 * it under the terms of the GNU Lesser General Public License as published by
\r
11 * the Free Software Foundation, either version 3 of the License, or
\r
12 * (at your option) any later version.
\r
14 * The Vamsas Client is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
17 * GNU Lesser General Public License for more details.
\r
19 * You should have received a copy of the GNU Lesser General Public License
\r
20 * along with the Vamsas Client. If not, see <http://www.gnu.org/licenses/>.
\r
22 package uk.ac.vamsas.test.document;
\r
27 Copyright (C) 2001 Brett McLaughlin.
\r
28 All rights reserved.
\r
30 Redistribution and use in source and binary forms, with or without
\r
31 modification, are permitted provided that the following conditions
\r
34 1. Redistributions of source code must retain the above copyright
\r
35 notice, this list of conditions, and the following disclaimer.
\r
37 2. Redistributions in binary form must reproduce the above copyright
\r
38 notice, this list of conditions, and the disclaimer that follows
\r
39 these conditions in the documentation and/or other materials
\r
40 provided with the distribution.
\r
42 3. The name "Java and XML" must not be used to endorse or promote products
\r
43 derived from this software without prior written permission. For
\r
44 written permission, please contact brett@newInstance.com.
\r
46 In addition, we request (but do not require) that you include in the
\r
47 end-user documentation provided with the redistribution and/or in the
\r
48 software itself an acknowledgement equivalent to the following:
\r
49 "This product includes software developed for the
\r
50 'Java and XML' book, by Brett McLaughlin (O'Reilly & Associates)."
\r
52 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
\r
53 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
54 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
55 DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
\r
56 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
57 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
58 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
\r
59 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
\r
60 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
\r
61 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
\r
62 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
\r
66 import java.io.File;
\r
67 import java.io.IOException;
\r
68 import java.util.HashMap;
\r
69 import java.util.Iterator;
\r
70 import java.util.Map;
\r
71 import org.xml.sax.Attributes;
\r
72 import org.xml.sax.ContentHandler;
\r
73 import org.xml.sax.ErrorHandler;
\r
74 import org.xml.sax.InputSource;
\r
75 import org.xml.sax.Locator;
\r
76 import org.xml.sax.SAXException;
\r
77 import org.xml.sax.SAXParseException;
\r
78 import org.xml.sax.XMLReader;
\r
79 import org.xml.sax.helpers.XMLReaderFactory;
\r
81 import uk.ac.vamsas.client.simpleclient.FileWatcher;
\r
82 import uk.ac.vamsas.client.simpleclient.Lock;
\r
83 import uk.ac.vamsas.client.simpleclient.SimpleDocument;
\r
84 import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
\r
86 // This is an XML book - no need for explicit Swing imports
\r
88 import javax.swing.*;
\r
89 import javax.swing.tree.*;
\r
92 * <b><code>SAXTreeViewer</code></b> uses Swing to graphically
\r
93 * display an XML document.
\r
95 public class SAXTreeViewer extends JFrame {
\r
96 /** Default parser to use */
\r
97 private String vendorParserClass =
\r
98 "org.apache.xerces.parsers.SAXParser";
\r
100 /** The base tree to render */
\r
101 private JTree jTree;
\r
103 /** Tree model to use */
\r
104 DefaultTreeModel defaultTreeModel;
\r
107 * <p> This initializes the needed Swing settings. </p>
\r
109 public SAXTreeViewer() {
\r
110 // Handle Swing setup
\r
111 super("SAX Tree Viewer");
\r
116 * <p> This will construct the tree using Swing. </p>
\r
118 * @param filename <code>String</code> path to XML document.
\r
120 public void init(String xmlURI) throws IOException, SAXException {
\r
121 init(xmlURI, null);
\r
124 * <p> This will construct the tree using Swing. </p>
\r
126 * @param filename <code>String</code> apparent path to XML document.
\r
127 * @param inputSource <code>InputSource</code> content of XML document
\r
129 public void init(String xmlURI, InputSource inputSource) throws IOException, SAXException {
\r
131 DefaultMutableTreeNode base =
\r
132 new DefaultMutableTreeNode("XML Document: " +
\r
135 // Build the tree model
\r
136 defaultTreeModel = new DefaultTreeModel(base);
\r
137 jTree = new JTree(defaultTreeModel);
\r
139 // Construct the tree hierarchy
\r
140 if (inputSource==null) {
\r
141 buildTree(defaultTreeModel, base, xmlURI);
\r
143 buildTree(defaultTreeModel, base, xmlURI,inputSource);
\r
145 // Display the results
\r
146 getContentPane().add(new JScrollPane(jTree),
\r
147 BorderLayout.CENTER);
\r
151 * <p>This handles building the Swing UI tree.</p>
\r
153 * @param treeModel Swing component to build upon.
\r
154 * @param base tree node to build on.
\r
155 * @param xmlURI URI to build XML document from.
\r
156 * @throws <code>IOException</code> - when reading the XML URI fails.
\r
157 * @throws <code>SAXException</code> - when errors in parsing occur.
\r
159 public void buildTree(DefaultTreeModel treeModel,
\r
160 DefaultMutableTreeNode base, String xmlURI)
\r
161 throws IOException, SAXException {
\r
163 InputSource inputSource =
\r
164 new InputSource(xmlURI);
\r
165 buildTree(treeModel,base,xmlURI,inputSource);
\r
168 * <p>This handles building the Swing UI tree.</p>
\r
170 * @param treeModel Swing component to build upon.
\r
171 * @param base tree node to build on.
\r
172 * @param xmlURI apparent URI to build XML document from.
\r
173 * @param inputSource the xml datasource to get the content from
\r
174 * @throws SAXException
\r
175 * @throws IOException
\r
176 * @throws <code>IOException</code> - when reading the XML URI fails.
\r
177 * @throws <code>SAXException</code> - when errors in parsing occur.
\r
179 public void buildTree(DefaultTreeModel treeModel,
\r
180 DefaultMutableTreeNode base, String xmlURI, InputSource inputSource) throws IOException, SAXException {
\r
182 // Create instances needed for parsing
\r
183 XMLReader reader =
\r
184 XMLReaderFactory.createXMLReader(vendorParserClass);
\r
185 ContentHandler jTreeContentHandler =
\r
186 new JTreeContentHandler(treeModel, base);
\r
187 ErrorHandler jTreeErrorHandler = new JTreeErrorHandler();
\r
189 // Register content handler
\r
190 reader.setContentHandler(jTreeContentHandler);
\r
192 // Register error handler
\r
193 reader.setErrorHandler(jTreeErrorHandler);
\r
195 reader.parse(inputSource);
\r
197 private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(SAXTreeViewer.class);
\r
200 * <p> Static entry point for running the viewer. </p>
\r
202 public static void main(String[] args) {
\r
204 File archive = new File(args[0]);
\r
205 // start watching a vamsas document archive
\r
207 log.info("Endlessly Watching file " + archive);
\r
209 * if (!archive.exists()) archive.createNewFile();
\r
210 */// watch the new file... - taken straight from ClientsFileTest
\r
211 FileWatcher w = new FileWatcher(archive);
\r
212 SAXTreeViewer currentview = null;
\r
213 boolean first=true;
\r
215 // get watcher's lock to ensure state change is fixed for
\r
217 Lock chlock = w.getChangedState();
\r
218 if (first || chlock != null) {
\r
219 log.info("Got lock on "
\r
221 + (archive.exists() ? " exists l=" + archive.length()
\r
222 : "(non existant)"));
\r
224 if (archive.length() > 0) {
\r
225 VamsasArchiveReader vreader = new VamsasArchiveReader(archive);
\r
226 SimpleDocument sdoc = new SimpleDocument(
\r
227 "testing vamsas watcher");
\r
229 // pass the archive XML content to the xml viewer.
\r
230 SAXTreeViewer newview = new SAXTreeViewer();
\r
231 newview.init(archive.toURI().toString(), new org.xml.sax.InputSource(vreader.getVamsasDocumentStream()));
\r
232 if (currentview != null)
\r
234 newview.setBounds(currentview.getBounds());
\r
235 // somehow copy over expanded state for existing objects and scroll state.
\r
236 // could also highlight new / modified nodes.
\r
237 newview.setVisible(true);
\r
238 currentview.setVisible(false);
\r
239 currentview.dispose();
\r
241 newview.setVisible(true);
\r
243 currentview = newview;
\r
245 /* VamsasDocument d = sdoc.getVamsasDocument(vreader);
\r
247 ArchiveReports.reportDocument(d, vreader, false,
\r
251 .println("Update at "
\r
252 + System.currentTimeMillis()
\r
253 + "\n\n********************************************************\n");
\r
254 } catch (Exception e) {
\r
255 log.error("Unmarshalling failed.", e);
\r
261 Thread.sleep(2000);
\r
263 } catch (Exception e) {
\r
264 log.info("Going away now.",e);
\r
270 * <b><code>JTreeContentHandler</code></b> implements the SAX
\r
271 * <code>ContentHandler</code> interface and defines callback
\r
272 * behavior for the SAX callbacks associated with an XML
\r
273 * document's content, bulding up JTree nodes.
\r
275 class JTreeContentHandler implements ContentHandler {
\r
277 /** Hold onto the locator for location information */
\r
278 private Locator locator;
\r
280 /** Store URI to prefix mappings */
\r
281 private Map namespaceMappings;
\r
283 /** Tree Model to add nodes to */
\r
284 private DefaultTreeModel treeModel;
\r
286 /** Current node to add sub-nodes to */
\r
287 private DefaultMutableTreeNode current;
\r
290 * <p> Set up for working with the JTree. </p>
\r
292 * @param treeModel tree to add nodes to.
\r
293 * @param base node to start adding sub-nodes to.
\r
295 public JTreeContentHandler(DefaultTreeModel treeModel,
\r
296 DefaultMutableTreeNode base) {
\r
297 this.treeModel = treeModel;
\r
298 this.current = base;
\r
299 this.namespaceMappings = new HashMap();
\r
304 * Provide reference to <code>Locator</code> which provides
\r
305 * information about where in a document callbacks occur.
\r
308 * @param locator <code>Locator</code> object tied to callback
\r
311 public void setDocumentLocator(Locator locator) {
\r
312 // Save this for later use
\r
313 this.locator = locator;
\r
318 * This indicates the start of a Document parse-this precedes
\r
319 * all callbacks in all SAX Handlers with the sole exception
\r
320 * of <code>{@link #setDocumentLocator}</code>.
\r
323 * @throws <code>SAXException</code> when things go wrong
\r
325 public void startDocument() throws SAXException {
\r
326 // No visual events occur here
\r
331 * This indicates the end of a Document parse-this occurs after
\r
332 * all callbacks in all SAX Handlers.</code>.
\r
335 * @throws <code>SAXException</code> when things go wrong
\r
337 public void endDocument() throws SAXException {
\r
338 // No visual events occur here
\r
343 * This indicates that a processing instruction (other than
\r
344 * the XML declaration) has been encountered.
\r
347 * @param target <code>String</code> target of PI
\r
348 * @param data <code>String</code containing all data sent to the PI.
\r
349 * This typically looks like one or more attribute value
\r
351 * @throws <code>SAXException</code> when things go wrong
\r
353 public void processingInstruction(String target, String data)
\r
354 throws SAXException {
\r
356 DefaultMutableTreeNode pi =
\r
357 new DefaultMutableTreeNode("PI (target = '" + target +
\r
358 "', data = '" + data + "')");
\r
364 * This indicates the beginning of an XML Namespace prefix
\r
365 * mapping. Although this typically occurs within the root element
\r
366 * of an XML document, it can occur at any point within the
\r
367 * document. Note that a prefix mapping on an element triggers
\r
368 * this callback <i>before</i> the callback for the actual element
\r
369 * itself (<code>{@link #startElement}</code>) occurs.
\r
372 * @param prefix <code>String</code> prefix used for the namespace
\r
374 * @param uri <code>String</code> URI for the namespace
\r
376 * @throws <code>SAXException</code> when things go wrong
\r
378 public void startPrefixMapping(String prefix, String uri) {
\r
379 // No visual events occur here.
\r
380 namespaceMappings.put(uri, prefix);
\r
385 * This indicates the end of a prefix mapping, when the namespace
\r
386 * reported in a <code>{@link #startPrefixMapping}</code> callback
\r
387 * is no longer available.
\r
390 * @param prefix <code>String</code> of namespace being reported
\r
391 * @throws <code>SAXException</code> when things go wrong
\r
393 public void endPrefixMapping(String prefix) {
\r
394 // No visual events occur here.
\r
395 for (Iterator i = namespaceMappings.keySet().iterator();
\r
398 String uri = (String)i.next();
\r
399 String thisPrefix = (String)namespaceMappings.get(uri);
\r
400 if (prefix.equals(thisPrefix)) {
\r
401 namespaceMappings.remove(uri);
\r
409 * This reports the occurrence of an actual element. It includes
\r
410 * the element's attributes, with the exception of XML vocabulary
\r
411 * specific attributes, such as
\r
412 * <code>xmlns:[namespace prefix]</code> and
\r
413 * <code>xsi:schemaLocation</code>.
\r
416 * @param namespaceURI <code>String</code> namespace URI this element
\r
417 * is associated with, or an empty <code>String</code>
\r
418 * @param localName <code>String</code> name of element (with no
\r
419 * namespace prefix, if one is present)
\r
420 * @param qName <code>String</code> XML 1.0 version of element name:
\r
421 * [namespace prefix]:[localName]
\r
422 * @param atts <code>Attributes</code> list for this element
\r
423 * @throws <code>SAXException</code> when things go wrong
\r
425 public void startElement(String namespaceURI, String localName,
\r
426 String qName, Attributes atts)
\r
427 throws SAXException {
\r
429 DefaultMutableTreeNode element =
\r
430 new DefaultMutableTreeNode("Element: " + localName);
\r
431 current.add(element);
\r
434 // Determine namespace
\r
435 if (namespaceURI.length() > 0) {
\r
437 (String)namespaceMappings.get(namespaceURI);
\r
438 if (prefix.equals("")) {
\r
441 DefaultMutableTreeNode namespace =
\r
442 new DefaultMutableTreeNode("Namespace: prefix = '" +
\r
443 prefix + "', URI = '" + namespaceURI + "'");
\r
444 current.add(namespace);
\r
447 // Process attributes
\r
448 for (int i=0; i<atts.getLength(); i++) {
\r
449 DefaultMutableTreeNode attribute =
\r
450 new DefaultMutableTreeNode("Attribute (name = '" +
\r
451 atts.getLocalName(i) +
\r
453 atts.getValue(i) + "')");
\r
454 String attURI = atts.getURI(i);
\r
455 if (attURI.length() > 0) {
\r
456 String attPrefix =
\r
457 (String)namespaceMappings.get(namespaceURI);
\r
458 if (attPrefix.equals("")) {
\r
459 attPrefix = "[None]";
\r
461 DefaultMutableTreeNode attNamespace =
\r
462 new DefaultMutableTreeNode("Namespace: prefix = '" +
\r
463 attPrefix + "', URI = '" + attURI + "'");
\r
464 attribute.add(attNamespace);
\r
466 current.add(attribute);
\r
472 * Indicates the end of an element
\r
473 * (<code></[element name]></code>) is reached. Note that
\r
474 * the parser does not distinguish between empty
\r
475 * elements and non-empty elements, so this occurs uniformly.
\r
478 * @param namespaceURI <code>String</code> URI of namespace this
\r
479 * element is associated with
\r
480 * @param localName <code>String</code> name of element without prefix
\r
481 * @param qName <code>String</code> name of element in XML 1.0 form
\r
482 * @throws <code>SAXException</code> when things go wrong
\r
484 public void endElement(String namespaceURI, String localName,
\r
486 throws SAXException {
\r
488 // Walk back up the tree
\r
489 current = (DefaultMutableTreeNode)current.getParent();
\r
494 * This reports character data (within an element).
\r
497 * @param ch <code>char[]</code> character array with character data
\r
498 * @param start <code>int</code> index in array where data starts.
\r
499 * @param length <code>int</code> index in array where data ends.
\r
500 * @throws <code>SAXException</code> when things go wrong
\r
502 public void characters(char[] ch, int start, int length)
\r
503 throws SAXException {
\r
505 String s = new String(ch, start, length);
\r
506 DefaultMutableTreeNode data =
\r
507 new DefaultMutableTreeNode("Character Data: '" + s + "'");
\r
513 * This reports whitespace that can be ignored in the
\r
514 * originating document. This is typically invoked only when
\r
515 * validation is ocurring in the parsing process.
\r
518 * @param ch <code>char[]</code> character array with character data
\r
519 * @param start <code>int</code> index in array where data starts.
\r
520 * @param end <code>int</code> index in array where data ends.
\r
521 * @throws <code>SAXException</code> when things go wrong
\r
523 public void ignorableWhitespace(char[] ch, int start, int length)
\r
524 throws SAXException {
\r
526 // This is ignorable, so don't display it
\r
531 * This reports an entity that is skipped by the parser. This
\r
532 * should only occur for non-validating parsers, and then is still
\r
533 * implementation-dependent behavior.
\r
536 * @param name <code>String</code> name of entity being skipped
\r
537 * @throws <code>SAXException</code> when things go wrong
\r
539 public void skippedEntity(String name) throws SAXException {
\r
540 DefaultMutableTreeNode skipped =
\r
541 new DefaultMutableTreeNode("Skipped Entity: '" + name + "'");
\r
542 current.add(skipped);
\r
547 * <b><code>JTreeErrorHandler</code></b> implements the SAX
\r
548 * <code>ErrorHandler</code> interface and defines callback
\r
549 * behavior for the SAX callbacks associated with an XML
\r
550 * document's warnings and errors.
\r
552 class JTreeErrorHandler implements ErrorHandler {
\r
556 * This will report a warning that has occurred; this indicates
\r
557 * that while no XML rules were "broken", something appears
\r
558 * to be incorrect or missing.
\r
561 * @param exception <code>SAXParseException</code> that occurred.
\r
562 * @throws <code>SAXException</code> when things go wrong
\r
564 public void warning(SAXParseException exception)
\r
565 throws SAXException {
\r
567 System.out.println("**Parsing Warning**\n" +
\r
569 exception.getLineNumber() + "\n" +
\r
571 exception.getSystemId() + "\n" +
\r
573 exception.getMessage());
\r
574 throw new SAXException("Warning encountered");
\r
579 * This will report an error that has occurred; this indicates
\r
580 * that a rule was broken, typically in validation, but that
\r
581 * parsing can reasonably continue.
\r
584 * @param exception <code>SAXParseException</code> that occurred.
\r
585 * @throws <code>SAXException</code> when things go wrong
\r
587 public void error(SAXParseException exception)
\r
588 throws SAXException {
\r
590 System.out.println("**Parsing Error**\n" +
\r
592 exception.getLineNumber() + "\n" +
\r
594 exception.getSystemId() + "\n" +
\r
596 exception.getMessage());
\r
597 throw new SAXException("Error encountered");
\r
602 * This will report a fatal error that has occurred; this indicates
\r
603 * that a rule has been broken that makes continued parsing either
\r
604 * impossible or an almost certain waste of time.
\r
607 * @param exception <code>SAXParseException</code> that occurred.
\r
608 * @throws <code>SAXException</code> when things go wrong
\r
610 public void fatalError(SAXParseException exception)
\r
611 throws SAXException {
\r
613 System.out.println("**Parsing Fatal Error**\n" +
\r
615 exception.getLineNumber() + "\n" +
\r
617 exception.getSystemId() + "\n" +
\r
619 exception.getMessage());
\r
620 throw new SAXException("Fatal Error encountered");
\r