001/* An actor that displays XML tokens in a nice format. 002 003 Copyright (c) 2012 The Regents of the University of California. 004 All rights reserved. 005 006 Permission is hereby granted, without written agreement and without 007 license or royalty fees, to use, copy, modify, and distribute this 008 software and its documentation for any purpose, provided that the 009 above copyright notice and the following two paragraphs appear in all 010 copies of this software. 011 012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016 SUCH DAMAGE. 017 018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023 ENHANCEMENTS, OR MODIFICATIONS. 024 025 */ 026package org.kepler.actor.gui; 027 028import org.jdom.input.DOMBuilder; 029import org.jdom.output.Format; 030import org.jdom.output.XMLOutputter; 031import org.w3c.dom.Document; 032 033import ptolemy.actor.lib.gui.Display; 034import ptolemy.data.XMLToken; 035import ptolemy.data.type.BaseType; 036import ptolemy.kernel.CompositeEntity; 037import ptolemy.kernel.util.IllegalActionException; 038import ptolemy.kernel.util.NameDuplicationException; 039import ptolemy.kernel.util.Workspace; 040 041/** An actor that displays XML tokens in a nice format. 042 * 043 * @author Daniel Crawl 044 * @version $Id: XMLViewer.java 33547 2015-07-15 00:38:07Z crawl $ 045 */ 046public class XMLViewer extends Display { 047 048 /** Construct a new XMLViewer in a container with a specific name. 049 * @param container The container. 050 * @param name The name of this actor. 051 * @exception IllegalActionException If the entity cannot be contained 052 * by the proposed container. 053 * @exception NameDuplicationException If the container already has an 054 * actor with this name. 055 */ 056 public XMLViewer(CompositeEntity container, String name) 057 throws IllegalActionException, NameDuplicationException { 058 super(container, name); 059 060 input.setTypeEquals(BaseType.XMLTOKEN); 061 062 _builder = new DOMBuilder(); 063 _outputter = new XMLOutputter(); 064 _outputter.setFormat(Format.getPrettyFormat()); 065 } 066 067 /** Clone the actor into the specified workspace. */ 068 @Override 069 public Object clone(Workspace workspace) throws CloneNotSupportedException { 070 XMLViewer newObject = (XMLViewer) super.clone(workspace); 071 newObject._builder = new DOMBuilder(); 072 newObject._outputter = new XMLOutputter(); 073 newObject._outputter.setFormat(Format.getPrettyFormat()); 074 return newObject; 075 } 076 077 /////////////////////////////////////////////////////////////////// 078 //// protected methods //// 079 080 /** Return a nicely formatted string describing XML input on channel i. 081 * @param i The channel 082 * @return A string representation of the input, or null 083 * if there is nothing to display. 084 * @throws IllegalActionException If reading the input fails. 085 */ 086 @Override 087 protected String _getInputString(int i) throws IllegalActionException { 088 if (input.hasToken(i)) { 089 final XMLToken token = (XMLToken) input.get(i); 090 final Document document = token.getDomTree(); 091 synchronized(document) { 092 final org.jdom.Document jdomDoc = _builder.build(document); 093 return _outputter.outputString(jdomDoc); 094 } 095 } 096 return null; 097 } 098 099 /** An object to convert org.w3c.dom.Document objects to org.jdom.Document objects. */ 100 private DOMBuilder _builder; 101 102 /** An object to serialize org.jdom.Documents into nicely formatted strings. */ 103 private XMLOutputter _outputter; 104 105}