001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/loaderwrappers/J3sLoader.java,v 1.1 2007/02/07 01:13:31 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2007.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.utils.loaderwrappers;
019:
020: import com.sun.j3d.loaders.Scene;
021: import com.sun.j3d.loaders.SceneBase;
022: import com.sun.j3d.utils.scenegraph.io.SceneGraphStreamReader;
023: import java.io.BufferedInputStream;
024: import java.io.FileInputStream;
025: import java.util.HashMap;
026:
027: import javax.media.j3d.BranchGroup;
028:
029: /**
030: * Provides a Loader which can load Java3D scene graphs saved using the
031: * SceneGraphStreamWriter.
032: *
033: * The Java3D LoaderInterface implemented by this class does not provide access
034: * to all the features that can be stored in a .j3s file.
035: * Most importantly the file may contain a universe which will be ignored
036: * by this loader.
037: *
038: * @author Paul Byrne
039: */
040: public class J3sLoader extends com.sun.j3d.loaders.LoaderBase {
041:
042: private SceneGraphStreamReader reader = null;
043: private BranchGroup[] graphs = null;
044: private static ClassLoader classLoader = null;
045:
046: /**
047: * Set the ClassLoader which will be used by the SceneGraphIO system
048: * when loading the scene graph.
049: *
050: * The default null in which case the system will use system class loader
051: */
052: public static void setClassLoader(ClassLoader cLoader) {
053: classLoader = cLoader;
054: }
055:
056: /**
057: * Get the ClassLoader set by setClassLoader
058: *
059: * If setClassLoader has not been called the default null will be returned.
060: * This indicates that systemClassLoader will be used.
061: */
062: public static ClassLoader getClassLoader() {
063: return classLoader;
064: }
065:
066: /**
067: * Load all the BranchGraphs from the file called filename and return
068: * a Scene containing all the BranchGraphs
069: */
070: public Scene load(String filename)
071: throws java.io.FileNotFoundException,
072: com.sun.j3d.loaders.IncorrectFormatException,
073: com.sun.j3d.loaders.ParsingErrorException {
074:
075: SceneBase scenebase = null;
076: Scene scene = null;
077: try {
078: System.out.println("Loading Stream " + filename);
079: BufferedInputStream stream = new BufferedInputStream(
080: new FileInputStream(filename));
081: reader = new SceneGraphStreamReader(stream);
082: if (classLoader != null)
083: reader.setClassLoader(classLoader);
084: scenebase = new SceneBase();
085:
086: BranchGroup bg = new BranchGroup();
087: bg = reader.readBranchGraph(new HashMap());
088:
089: scenebase.setSceneGroup(bg);
090:
091: // String[] objectNames = reader.getNames();
092: // for(int i=0; i<objectNames.length; i++)
093: // try {
094: // scenebase.addNamedObject( objectNames[i], reader.getNamedObject( objectNames[i] ) );
095: // } catch( com.sun.j3d.utils.scenegraph.io.ObjectNotLoadedException e ) {}
096: // catch( com.sun.j3d.utils.scenegraph.io.NamedObjectException e ) {}
097:
098: stream.close();
099:
100: } catch (java.io.InvalidClassException ex) {
101: throw new com.sun.j3d.loaders.IncorrectFormatException(ex
102: .getMessage());
103: } catch (java.io.FileNotFoundException e) {
104: throw new java.io.FileNotFoundException(filename);
105: } catch (java.io.IOException exc) {
106: exc.printStackTrace();
107: // if (exc.getMessage().startsWith("Use Java 3D Fly"))
108: // return oldLoad( filename );
109: // else
110: throw new java.io.FileNotFoundException(exc.getMessage());
111: }
112:
113: if (scenebase == null)
114: return scene;
115: else
116: return scenebase;
117: }
118:
119: /** Not Implemented
120: */
121: public Scene load(java.net.URL url)
122: throws java.io.FileNotFoundException,
123: com.sun.j3d.loaders.IncorrectFormatException,
124: com.sun.j3d.loaders.ParsingErrorException {
125: throw new RuntimeException("NOT IMPLEMENTED");
126: }
127:
128: /** Not Implemented
129: */
130: public Scene load(java.io.Reader reader)
131: throws java.io.FileNotFoundException,
132: com.sun.j3d.loaders.IncorrectFormatException,
133: com.sun.j3d.loaders.ParsingErrorException {
134:
135: throw new RuntimeException("NOT IMPLEMENTED");
136: }
137:
138: }
|