1 /*
2 * Copyright (c) 2008, 2017, Oracle and/or its affiliates.
3 * All rights reserved. Use is subject to license terms.
4 *
5 * This file is available and licensed under the following license:
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the distribution.
16 * - Neither the name of Oracle Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
111 private static AudioClip getNoteClip(String name) {
112 // First look for the clips in a directory next to our jar file
113 try {
114 // Get a URI to this class file
115 URI baseURI = AudioClipApp.class.getResource("AudioClipApp.class").toURI();
116
117 // If we have a jar URL, get the embedded http or file URL
118 // and trim off the internal jar path, this will leave us
119 // with a URL to the jar file
120 if (baseURI.getScheme().equals("jar")) {
121 String basePath = baseURI.getSchemeSpecificPart();
122 if (basePath.contains("!/")) {
123 basePath = basePath.substring(0, basePath.indexOf("!/"));
124 }
125 baseURI = new URI(basePath);
126 }
127
128 URL noteURL = baseURI.resolve("resources/"+name).toURL();
129
130 // check if the resource exists, then try to load it
131 if (noteURL.getProtocol().equals("http")) {
132 HttpURLConnection urlCon = (HttpURLConnection)noteURL.openConnection();
133 urlCon.setRequestMethod("HEAD");
134 urlCon.connect();
135 if (urlCon.getResponseCode() != HttpURLConnection.HTTP_OK) {
136 noteURL = null;
137 }
138 urlCon.disconnect();
139 } else if (noteURL.getProtocol().equals("file")) {
140 File f = new File(noteURL.getPath());
141 if (!f.exists() || !f.isFile()) {
142 noteURL = null;
143 }
144 } else {
145 // unsupported protocol
146 noteURL = null;
147 }
148 if (noteURL != null) {
149 return new AudioClip(noteURL.toExternalForm());
150 }
151 } catch (Exception e) {} // fail gracefully
|
1 /*
2 * Copyright (c) 2008, 2019, Oracle and/or its affiliates.
3 * All rights reserved. Use is subject to license terms.
4 *
5 * This file is available and licensed under the following license:
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the distribution.
16 * - Neither the name of Oracle Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
111 private static AudioClip getNoteClip(String name) {
112 // First look for the clips in a directory next to our jar file
113 try {
114 // Get a URI to this class file
115 URI baseURI = AudioClipApp.class.getResource("AudioClipApp.class").toURI();
116
117 // If we have a jar URL, get the embedded http or file URL
118 // and trim off the internal jar path, this will leave us
119 // with a URL to the jar file
120 if (baseURI.getScheme().equals("jar")) {
121 String basePath = baseURI.getSchemeSpecificPart();
122 if (basePath.contains("!/")) {
123 basePath = basePath.substring(0, basePath.indexOf("!/"));
124 }
125 baseURI = new URI(basePath);
126 }
127
128 URL noteURL = baseURI.resolve("resources/"+name).toURL();
129
130 // check if the resource exists, then try to load it
131 if (noteURL.getProtocol().equals("http") || noteURL.getProtocol().equals("https")) {
132 HttpURLConnection urlCon = (HttpURLConnection)noteURL.openConnection();
133 urlCon.setRequestMethod("HEAD");
134 urlCon.connect();
135 if (urlCon.getResponseCode() != HttpURLConnection.HTTP_OK) {
136 noteURL = null;
137 }
138 urlCon.disconnect();
139 } else if (noteURL.getProtocol().equals("file")) {
140 File f = new File(noteURL.getPath());
141 if (!f.exists() || !f.isFile()) {
142 noteURL = null;
143 }
144 } else {
145 // unsupported protocol
146 noteURL = null;
147 }
148 if (noteURL != null) {
149 return new AudioClip(noteURL.toExternalForm());
150 }
151 } catch (Exception e) {} // fail gracefully
|