001/* A representative of a doc file.
002
003 Copyright (c) 2006-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026 */
027package ptolemy.vergil.actor;
028
029import java.net.URL;
030
031import ptolemy.actor.gui.Effigy;
032import ptolemy.actor.gui.EffigyFactory;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036import ptolemy.vergil.basic.DocAttribute;
037
038///////////////////////////////////////////////////////////////////
039//// DocEffigy
040
041/**
042 An effigy for a doc file.
043
044 @author Edward A. Lee
045 @version $Id$
046 @since Ptolemy II 5.2
047 @Pt.ProposedRating Red (neuendor)
048 @Pt.AcceptedRating Red (neuendor)
049 */
050public class DocEffigy extends Effigy {
051
052    /** Create a new effigy in the given directory with the given name.
053     *  @param container The directory that contains this effigy.
054     *  @param name The name of this effigy.
055     *  @exception IllegalActionException If thrown by the superclass.
056     *  @exception NameDuplicationException If thrown by the superclass.
057     */
058    public DocEffigy(CompositeEntity container, String name)
059            throws IllegalActionException, NameDuplicationException {
060        super(container, name);
061    }
062
063    ///////////////////////////////////////////////////////////////////
064    ////                         public methods                    ////
065
066    /** Get the DocAttribute represented by this effigy, if any.
067     *  @return The DocAttribute represented by this effigy.
068     *  @see #setDocAttribute(DocAttribute)
069     */
070    public DocAttribute getDocAttribute() {
071        return _docAttribute;
072    }
073
074    /** Set the DocAttribute represented by this effigy, if any.
075     *  @param docAttribute The DocAttribute represented by this effigy.
076     *  @see #getDocAttribute()
077     */
078    public void setDocAttribute(DocAttribute docAttribute) {
079        _docAttribute = docAttribute;
080    }
081
082    ///////////////////////////////////////////////////////////////////
083    ////                         private variables                 ////
084
085    /** The doc attribute represented by this effigy, if any. */
086    private DocAttribute _docAttribute;
087
088    ///////////////////////////////////////////////////////////////////
089    ////                         inner classes                     ////
090
091    /** A factory for creating new effigies.
092     */
093    public static class Factory extends EffigyFactory {
094        /** Create a factory with the given name and container.
095         *  @param container The container.
096         *  @param name The name.
097         *  @exception IllegalActionException If the container is incompatible
098         *   with this entity.
099         *  @exception NameDuplicationException If the name coincides with
100         *   an entity already in the container.
101         */
102        public Factory(CompositeEntity container, String name)
103                throws IllegalActionException, NameDuplicationException {
104            super(container, name);
105        }
106
107        ///////////////////////////////////////////////////////////////
108        ////                     public methods                    ////
109
110        /** Return false, indicating that this effigy factory is not
111         *  capable of creating an effigy without a URL being specified.
112         *  @return False.
113         */
114        @Override
115        public boolean canCreateBlankEffigy() {
116            return false;
117        }
118
119        /** Create a new effigy in the given container by reading the
120         *  specified URL. If the specified URL is null, or
121         *  if the URL does not end with extension ".xml", or
122         *  if it does end with ".xml" but the file does not contain
123         *  a line that starts with the string
124         *  "<!DOCTYPE doc PUBLIC "-//UC Berkeley//DTD DocML"
125         *  within the first five lines, then return null.
126         *  @param container The container for the effigy.
127         *  @param base The base for relative file references, or null if
128         *   there are no relative file references.  This is ignored in this
129         *   class.
130         *  @param input The input URL.
131         *  @return A new instance of DocEffigy, or null if the URL
132         *   does not have a doc file.
133         *  @exception Exception If the URL cannot be read.
134         */
135        @Override
136        public Effigy createEffigy(CompositeEntity container, URL base,
137                URL input) throws Exception {
138            if (input != null) {
139                String extension = getExtension(input);
140                if (extension.equals("xml")) {
141                    // Check for DTD designation.
142                    if (checkForDTD(input,
143                            "<!DOCTYPE doc PUBLIC \"-//UC Berkeley//DTD DocML",
144                            null)) {
145                        // This is a doc file.
146                        DocEffigy effigy = new DocEffigy(container,
147                                container.uniqueName("effigy"));
148                        effigy.uri.setURL(input);
149                        return effigy;
150                    }
151                }
152            }
153            return null;
154        }
155    }
156}