001/* 002 * Copyright (c) 2003-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2014-06-16 18:22:01 +0000 (Mon, 16 Jun 2014) $' 007 * '$Revision: 32771 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.moml; 031 032import org.apache.commons.logging.Log; 033import org.apache.commons.logging.LogFactory; 034import org.kepler.objectmanager.ActorMetadata; 035import org.kepler.objectmanager.cache.ActorCacheObject; 036import org.kepler.objectmanager.cache.CacheManager; 037import org.kepler.objectmanager.library.LibItem; 038import org.kepler.objectmanager.library.LibraryManager; 039 040import ptolemy.kernel.ComponentEntity; 041import ptolemy.kernel.util.NamedObj; 042import ptolemy.util.MessageHandler; 043 044/** 045 * This class returns moml given an LSID. It is instantiated via reflection when 046 * the moml of a class is needed. 047 * 048 *@author Dan Higgins 049 *@created July 13, 2007 050 */ 051public class GetMomlFromLSID { 052 053 private static final Log log = LogFactory.getLog(GetMomlFromLSID.class 054 .getName()); 055 private static final boolean isDebugging = log.isDebugEnabled(); 056 057 /** Constructor */ 058 public GetMomlFromLSID() { 059 } 060 061 /** 062 * Get the MoMl for an actor represented by the NameObj 'dropObj' 063 */ 064 public String getMoml(NamedObj dropObj) { 065 if (isDebugging) log.debug("getMoml("+dropObj.getName()+")"); 066 067 /** 068 * This boolean will tell us if this is just a plain old NamedObj as 069 * used in the Outline tab rather than if it is a special 070 * CompositeEntity placeholder object as is used in the Component 071 * Library. 072 */ 073 boolean isPlainOldNamedObj = false; 074 075 String actorString = ""; 076 if (dropObj instanceof ComponentEntity) { 077 try { 078 int liid = LibraryManager.getLiidFor((ComponentEntity<?>)dropObj); 079 if (isDebugging) log.debug(liid); 080 if (liid >= 0) { 081 LibItem li = LibraryManager.getInstance().getTreeItemIndexInformation(liid); 082 083 CacheManager cacheMan = CacheManager.getInstance(); 084 085 String name = li.getName(); 086 ActorCacheObject aco = (ActorCacheObject) cacheMan 087 .getObject(li.getLsid()); 088 if (aco == null) { 089 throw new Exception("Object not found: " + li.getLsid()); 090 } 091 092 ActorMetadata am = aco.getMetadata(); 093 if(am == null) { 094 MessageHandler.error("Error getting metadata for component."); 095 } else { 096 NamedObj no = am.getActorAsNamedObj(null); 097 actorString = no.exportMoML(name); 098 if (isDebugging) log.debug("***************actorString: " + actorString); 099 // this string has an xml header and a DOCTYPE declaration 100 // following removes those elements 101 int pos1 = actorString.indexOf("<!DOCTYPE"); 102 int pos2 = actorString.indexOf(">", pos1); 103 int pos3 = actorString.indexOf("<", pos2); 104 if (pos1 > -1) { 105 actorString = actorString.substring(pos3, actorString.length()); 106 } 107 } 108 } else { 109 isPlainOldNamedObj = true; 110 //throw new Exception("No Library Index ID was found"); 111 } 112 113 } catch (Exception w) { 114 MessageHandler.error("Error getting MoML for dropped object.", w); 115 } 116 } else { 117 isPlainOldNamedObj = true; 118 //log.error("Only ComponentEntities are allowed in the Library"); 119 } 120 121 if (isPlainOldNamedObj) { 122 // just return the NamedObj as it is 123 actorString = dropObj.exportMoML(); 124 } 125 126 // System.out.println("actorString: " + actorString); 127 return actorString; 128 } 129 130}