001/* Test class for moml.IconLoader
002
003 Copyright (c) 2007-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
027
028 */
029package ptolemy.moml.test;
030
031import java.io.InputStream;
032import java.net.URL;
033
034import ptolemy.kernel.util.NamedObj;
035import ptolemy.moml.IconLoader;
036import ptolemy.moml.MoMLParser;
037
038///////////////////////////////////////////////////////////////////
039//// TestIconLoader
040
041/**
042 Test class for ptolemy.moml.IconLoader.
043 The IconLoader class has an abstract method, so we use this class
044 to define that method.
045 @author Christopher Brooks
046 @version $Id$
047 @since Ptolemy II 6.1
048 @Pt.ProposedRating Red (cxh)
049 @Pt.AcceptedRating Red (cxh)
050 */
051public class TestIconLoader implements IconLoader {
052    ///////////////////////////////////////////////////////////////////
053    ////                         public methods                    ////
054
055    /** Load an icon for a class in a particular context.
056     *  @param className The name of the class for which the icon is
057     *  to be loaded.
058     *  @param context The context in which the icon is loaded.
059     *  @return true if the icon was successfully loaded.
060     *  @exception Exception If there is a problem adding
061     *  the icon.
062     */
063    @Override
064    public boolean loadIconForClass(String className, NamedObj context)
065            throws Exception {
066        String fileName = className.replace('.', '/') + "Icon.xml";
067        URL xmlFile = getClass().getClassLoader().getResource(fileName);
068        if (xmlFile == null) {
069            return false;
070        }
071        InputStream input = xmlFile.openStream();
072        MoMLParser newParser = new MoMLParser();
073        newParser.setContext(context);
074        newParser.parse(null, fileName, input);
075        return true;
076    }
077
078}