001: /*
002: * $RCSfile: VrmlLoader.java,v $
003: *
004: * @(#)VrmlLoader.java 1.15 99/03/08 11:02:02
005: *
006: * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
007: *
008: * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
009: * modify and redistribute this software in source and binary code form,
010: * provided that i) this copyright notice and license appear on all copies of
011: * the software; and ii) Licensee does not utilize the software in a manner
012: * which is disparaging to Sun.
013: *
014: * This software is provided "AS IS," without a warranty of any kind. ALL
015: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
016: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
017: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
018: * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
019: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
020: * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
021: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
022: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
023: * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGES.
025: *
026: * This software is not designed or intended for use in on-line control of
027: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
028: * the design, construction, operation or maintenance of any nuclear
029: * facility. Licensee represents and warrants that it will not use or
030: * redistribute the Software for such purposes.
031: *
032: *
033: * $Revision: 1.4 $
034: * $Date: 2006/03/30 08:19:29 $
035: * $State: Exp $
036: */
037: package org.jdesktop.j3d.loaders.vrml97;
038:
039: import com.sun.j3d.loaders.IncorrectFormatException;
040: import com.sun.j3d.loaders.ParsingErrorException;
041: import java.io.FileNotFoundException;
042: import java.io.Reader;
043: import java.io.IOException;
044: import java.lang.String;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047:
048: /**
049: * This is an implementation of the Java3D utility loader interface.
050: *
051: * A VrmlScene can be loaded via URL, filesystem pathname or a
052: * Reader.
053: *
054: * The "base" URL or pathname is the pathname used for relative paths in the
055: * WRL. If the base is not specified it will be derived from the main URL or
056: * pathname.
057: *
058: *@see VrmlScene
059: *@see com.sun.j3d.loaders.Loader
060: *@see com.sun.j3d.loaders.Scene
061: *@author Rick Goldberg
062: *@author Doug Gehringer
063: *@author Nikolai V. Chr.
064: */
065: public class VrmlLoader extends com.sun.j3d.loaders.LoaderBase {
066:
067: org.jdesktop.j3d.loaders.vrml97.impl.Loader loader;
068:
069: /**
070: * Constructor for the VrmlLoader object
071: *
072: * Will be setup to load static scene.
073: *
074: *
075: *@param flags These are ignored for the time being.
076: */
077: public VrmlLoader(int flags) {
078: super (flags);
079: loader = new org.jdesktop.j3d.loaders.vrml97.impl.Loader(null,
080: org.jdesktop.j3d.loaders.vrml97.impl.Loader.LOAD_STATIC);
081: }
082:
083: /**
084: * Constructor for the VrmlLoader object
085: *
086: * Will be setup to load static scene.
087: */
088: public VrmlLoader() {
089: this (0);
090: }
091:
092: /**
093: * Description of the Method
094: *
095: *@param reader Description of the Parameter
096: *@return Description of the Return Value
097: *@exception FileNotFoundException If the file could not be found or opened.
098: *@exception IncorrectFormatException If a file of an incorrect type was passed.
099: *@exception ParsingErrorException If a problem was encountered parsing the file.
100: */
101: public com.sun.j3d.loaders.Scene load(Reader reader)
102: throws FileNotFoundException, IncorrectFormatException,
103: ParsingErrorException {
104: org.jdesktop.j3d.loaders.vrml97.impl.Scene scene;
105: try {
106: loader.setWorldURL(baseUrl, null);
107: } catch (MalformedURLException e) {
108: // TODO: make a better handler
109: FileNotFoundException f = new FileNotFoundException(
110: "BaseUrl: " + baseUrl);
111: f.initCause(e);
112: throw f;
113: }
114: try {
115: scene = loader.load(reader);
116: } catch (vrml.InvalidVRMLSyntaxException e) {
117: ParsingErrorException p = new ParsingErrorException();
118: p.initCause(e);
119: throw p;
120: }
121: return new VrmlScene(scene);
122: }
123:
124: /**
125: * Description of the Method
126: *
127: *@param pathname Description of the Parameter
128: *@return Description of the Return Value
129: *@exception FileNotFoundException If the file could not be found or opened.
130: *@exception IncorrectFormatException If a file of an incorrect type was passed.
131: *@exception ParsingErrorException If a problem was encountered parsing the file.
132: */
133: public com.sun.j3d.loaders.Scene load(String pathname)
134: throws FileNotFoundException, IncorrectFormatException,
135: ParsingErrorException {
136: URL url = null;
137: try {
138: url = pathToURL(pathname);
139: } catch (MalformedURLException e) {
140: FileNotFoundException f = new FileNotFoundException(
141: "Loading: " + pathname);
142: f.initCause(e);
143: throw f;
144: }
145: try {
146: loader.setWorldURL(pathToURL(basePath), url);
147: } catch (MalformedURLException e) {
148: FileNotFoundException f = new FileNotFoundException(
149: "BasePath: " + basePath);
150: f.initCause(e);
151: throw f;
152: }
153: return doLoad(url);
154: }
155:
156: /**
157: * Description of the Method
158: *
159: *@param url Description of the Parameter
160: *@return Description of the Return Value
161: *@exception FileNotFoundException If the file could not be found or opened.
162: *@exception IncorrectFormatException If a file of an incorrect type was passed.
163: *@exception ParsingErrorException If a problem was encountered parsing the file.
164: */
165: public com.sun.j3d.loaders.Scene load(URL url)
166: throws FileNotFoundException, IncorrectFormatException,
167: ParsingErrorException {
168: try {
169: loader.setWorldURL(baseUrl, url);
170: } catch (MalformedURLException e) {
171: FileNotFoundException f = new FileNotFoundException(
172: "BaseUrl: " + baseUrl);
173: f.initCause(e);
174: throw f;
175: }
176: return doLoad(url);
177: }
178:
179: /**
180: * Description of the Method
181: *
182: *@param url Description of the Parameter
183: *@return The loaded scene
184: *@exception ParsingErrorException If a problem was encountered parsing the file.
185: *@exception FileNotFoundException If the file could not be found or opened.
186: */
187: private com.sun.j3d.loaders.Scene doLoad(URL url)
188: throws ParsingErrorException, FileNotFoundException {
189: org.jdesktop.j3d.loaders.vrml97.impl.Scene scene;
190: try {
191: scene = loader.load(url);
192: } catch (IOException e) {
193: FileNotFoundException f = new FileNotFoundException(url
194: .toString());
195: f.initCause(e);
196: throw f;
197: } catch (vrml.InvalidVRMLSyntaxException e) {
198: ParsingErrorException p = new ParsingErrorException(url
199: .toString());
200: p.initCause(e);
201: throw p;
202: }
203: return new VrmlScene(scene);
204: }
205:
206: // this method should only be used by methods that expect to only operate
207: // on filesystem resident files, and those methods should not be used.
208: /**
209: * Description of the Method
210: *
211: *@param path Description of the Parameter
212: *@return Description of the Return Value
213: *@exception MalformedURLException If an URL could not be made from the path.
214: */
215: private URL pathToURL(String path) throws MalformedURLException {
216: URL retval = null;
217: if (path == null) {
218: return null;
219: }
220:
221: // hack: for win32 we check drive specifier
222: // for solaris we check startsWith /
223:
224: // this really should all take a clean look at URL(context,spec)
225:
226: if (!path.startsWith(java.io.File.separator)
227: && (path.indexOf(':') != 1)) {
228: path = System.getProperties().getProperty("user.dir") + '/'
229: + path;
230: }
231:
232: // switch from file separator to URL separator
233: path = path.replace(java.io.File.separatorChar, '/');
234:
235: retval = new URL("file:" + path);
236: return retval;
237: }
238:
239: }
|