001/* An attribute that identifies the URI from which the container was read. 002 003 Copyright (c) 2000-2018 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.kernel.attributes; 028 029import java.net.MalformedURLException; 030import java.net.URI; 031import java.net.URISyntaxException; 032import java.net.URL; 033 034import ptolemy.kernel.util.IllegalActionException; 035import ptolemy.kernel.util.InternalErrorException; 036import ptolemy.kernel.util.NameDuplicationException; 037import ptolemy.kernel.util.NamedObj; 038import ptolemy.kernel.util.SingletonAttribute; 039import ptolemy.kernel.util.Workspace; 040 041/////////////////////////////////////////////////////////////////// 042//// URIAttribute 043 044/** 045 An attribute that identifies the URI from which the container was read. 046 This attribute is not persistent by default. That is, it exports no 047 MoML description. This makes sense because it should be set by the 048 code that reads the container's specification. It is also a singleton, 049 meaning that it will replace any previous attribute that has the same 050 name and is an instance of the base class, SingletonAttribute. 051 <p> 052 In most cases, this URI will specify a URL. The difference between 053 a URL and a URI is that a URI is unevaluated. That is, it is a string 054 representation of a resource, without any assurance or indication of a 055 file, stream, or other associated network resource. To access a URI, 056 it is common to create a URL from its specification. 057 058 <p>Unfortunately, URLs are not necessarily valid URIs. For example, a 059 URL that has a space in it is not a valid URI, the space must be 060 quoted (converted) to <code>%20</code>. 061 062 @author Edward A. Lee 063 @version $Id$ 064 @since Ptolemy II 2.1 065 @Pt.ProposedRating Green (eal) 066 @Pt.AcceptedRating Yellow (cxh) 067 */ 068public class URIAttribute extends SingletonAttribute { 069 /** Construct an attribute with the given name contained by the specified 070 * container. The container argument must not be null, or a 071 * NullPointerException will be thrown. This attribute will use the 072 * workspace of the container for synchronization and version counts. 073 * If the name argument is null, then the name is set to the empty 074 * string. The object is added to the directory of the workspace 075 * if the container is null. 076 * Increment the version of the workspace. 077 * @param container The container. 078 * @param name The name of this attribute. 079 * @exception IllegalActionException If the attribute is not of an 080 * acceptable class for the container, or if the name contains a period. 081 * @exception NameDuplicationException If the name coincides with 082 * an attribute already in the container. 083 */ 084 public URIAttribute(NamedObj container, String name) 085 throws IllegalActionException, NameDuplicationException { 086 super(container, name); 087 setPersistent(false); 088 } 089 090 /////////////////////////////////////////////////////////////////// 091 //// public methods //// 092 093 /** Clone the attribute into the specified workspace. The 094 * resulting object has a null value for the value of the URI. 095 096 * @param workspace The workspace for the cloned object. 097 * @return A new attribute. 098 * @exception CloneNotSupportedException If a derived class contains 099 * an attribute that cannot be cloned. 100 */ 101 @Override 102 public Object clone(Workspace workspace) throws CloneNotSupportedException { 103 URIAttribute newObject = (URIAttribute) super.clone(workspace); 104 // This line is not strictly necessary because the way 105 // this class is constructed means that _value will properly 106 // be set in the master and the clone. However, for correctness, 107 // we set the new value to null. 108 // In theory, we could do something like: 109 // newObject._value = URI.create(newObject.getURI().toString()); 110 // but this means that the _value field of the master and the 111 // clone are equal(), which means that our test in Configuration 112 // indicates a problem. 113 newObject._value = null; 114 return newObject; 115 } 116 117 /** Return the URI from which the specified model was read, 118 * or null if there is no such URI. 119 * This is obtained by finding a URIAttribute in the first 120 * container above the specified container in the hierarchy that has 121 * such an attribute. Note that this URI may represent a 122 * file on the local filesystem, in which case it will use 123 * the "file" scheme. 124 * @param container The container to start searching. 125 * @return A URI, or null if none can be found. 126 */ 127 public static URI getModelURI(NamedObj container) { 128 // Search up the tree for this attribute. 129 URIAttribute modelURI = null; 130 131 while (container != null && modelURI == null) { 132 try { 133 modelURI = (URIAttribute) container.getAttribute("_uri", 134 URIAttribute.class); 135 } catch (IllegalActionException ex) { 136 // An attribute was found with name "_uri", but it is not 137 // an instance of URIAttribute. Continue the search. 138 } 139 140 container = container.getContainer(); 141 } 142 143 if (modelURI != null) { 144 return modelURI.getURI(); 145 } else { 146 return null; 147 } 148 } 149 150 /** Get the URI that has been set by setURI(), 151 * or null if there is none. 152 * @return The URI. 153 * @see #setURI(URI) 154 */ 155 public URI getURI() { 156 return _value; 157 } 158 159 /** Get a URL representation of the URI that has been set by setURI(), 160 * or null if there is none. For this to succeed, it is necessary 161 * that the URI be absolute or an IllegalArgumentException will be 162 * thrown. 163 * @return A new URL. 164 * @exception MalformedURLException If the URI cannot be converted to 165 * a URL. 166 * @exception IllegalArgumentException If the URI is not absolute. 167 * @see #setURL(URL) 168 */ 169 public URL getURL() throws MalformedURLException { 170 // Warning, this method used to call java.net.URI.toURL(), which has 171 // a bug with jar urls. If we have a jar url, (for example 172 // jar:file:/C:/foo.jar!/intro.htm) then the java.net.URI toURL() 173 // method will return a URL like jar:, which is missing the file: part 174 // This causes problems with Web Start. 175 // 176 // BTW - Here is how to replicate the problem with 177 // java.net.URI.toURL() in ptjacl: 178 // % set uri [java::new java.net.URI "jar:file:/C:/foo.jar!/intro.htm"] 179 // java0x1 180 // % set url [$uri toURL] 181 // java0x2 182 // % $url toString 183 // jar: 184 // The above should be jar:file:/C:/foo.jar!/intro.htm 185 if (_value == null) { 186 return null; 187 } 188 189 return new URL(_value.toString()); 190 } 191 192 /** Set the value of the URI, and call the attributeChanged() method 193 * of the container. 194 * @param uri The new URI. 195 * @exception IllegalActionException If the change is not acceptable 196 * to the container. 197 * @see #getURI() 198 */ 199 public void setURI(URI uri) throws IllegalActionException { 200 _value = uri; 201 202 NamedObj container = getContainer(); 203 204 if (container != null) { 205 container.attributeChanged(this); 206 } 207 } 208 209 /** Set the value of the URI by specifying a URL, 210 * and call the attributeChanged() method of the container. 211 * @param url The URL. 212 * @exception IllegalActionException If the change is not acceptable 213 * to the container. 214 * @see #getURL() 215 */ 216 public void setURL(URL url) throws IllegalActionException { 217 try { 218 _value = new URI(url.toExternalForm()); 219 } catch (URISyntaxException ex) { 220 // Unfortunately, URLs are not necessarily valid URIs. 221 // For example, a URL that has a space in it is not 222 // a valid URI, the space must be quoted (converted) to %20 223 try { 224 _value = new URI(url.getProtocol(), url.getFile(), 225 url.getRef()); 226 } catch (URISyntaxException ex2) { 227 // Should not occur because a URL is a valid URI. 228 throw new InternalErrorException(this, ex2, 229 "Error constructing URI from " + url); 230 } 231 } 232 233 NamedObj container = getContainer(); 234 235 if (container != null) { 236 container.attributeChanged(this); 237 } 238 } 239 240 /////////////////////////////////////////////////////////////////// 241 //// private variables //// 242 // The value. 243 private URI _value; 244}