001: /*
002: * $RCSfile: SceneGraphStreamWriter.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.5 $
041: * $Date: 2007/03/21 17:40:50 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.utils.scenegraph.io;
046:
047: import java.io.File;
048: import java.io.IOException;
049: import java.io.DataOutputStream;
050: import java.util.HashMap;
051:
052: import javax.media.j3d.BranchGroup;
053: import javax.media.j3d.SceneGraphObject;
054: import javax.media.j3d.CapabilityNotSetException;
055: import javax.media.j3d.DanglingReferenceException;
056: import com.sun.j3d.utils.scenegraph.io.retained.StreamControl;
057: import com.sun.j3d.utils.universe.SimpleUniverse;
058:
059: /**
060: * Writes a Java3D SceneGraph to a Java OutputStream.<p>
061: * Using this class to write to a FileOutputStream is not recommended. Use
062: * SceneGraphFileWriter instead to achieve maximum performance and flexibility.
063: */
064: public class SceneGraphStreamWriter extends java.lang.Object {
065:
066: private StreamControl control;
067: private DataOutputStream out;
068:
069: /** Creates new SceneGraphStreamWriter that will write to the supplied stream */
070: public SceneGraphStreamWriter(java.io.OutputStream outputStream)
071: throws IOException {
072: this .out = new java.io.DataOutputStream(outputStream);
073: control = new StreamControl(out);
074: control.writeStreamHeader();
075: }
076:
077: /**
078: * Write <code>universe</code> to the Stream.<p>
079: *
080: * If <code>writeContent</code> is true then all BranchGraphs attached to the
081: * universe will be saved. If it is false then only the universe
082: * data structures will be output (PlatformGeometry, ViewerAvatar, Locales,
083: * and the MultiTransformGroup between the ViewingPlatform and the View).<p>
084: *
085: * If <code>writeContent</code> is true then all the BranchGraphs
086: * attached to the Locales of the universe must have the
087: * ALLOW_DETACH capability set. If they do not, a <code>CapabilityNotSetException</code>
088: * will be thrown<p>
089: *
090: * @param universe The universe to write
091: * @param writeContent Flag enabling the BranchGraphs to be written
092: *
093: * @exception IOException
094: * @exception UnsupportedUniverseException Thrown if the universe class is not
095: * supported by this implementation
096: */
097: public void writeUniverse(SimpleUniverse universe,
098: boolean writeContent) throws IOException,
099: UnsupportedUniverseException {
100: control.writeUniverse(out, universe, writeContent);
101: }
102:
103: /**
104: * Write the entire graph to the stream.<p>
105: *
106: * The API will correctly handle NodeComponents that are shared
107: * between seperate graphs. However Nodes cannot be referenced
108: * in other Graphs.<p>
109: *
110: * If a reference to a Node in another graph is encountered a
111: * DanglingReferenceException will be thrown.
112: *
113: * <code>namedObjects</code> can contain a mapping between a key and a SceneGraphObject
114: * in the graph. During the read process this can be used to locate nodes
115: * in the graph.
116: */
117: public void writeBranchGraph(BranchGroup graph, HashMap namedObjects)
118: throws IOException, DanglingReferenceException,
119: NamedObjectException {
120: // TODO Add namedObjects to SymbolTable
121: control.addNamedObjects(namedObjects);
122: control.writeBranchGraph(graph, null);
123: }
124:
125: /**
126: * Close the SceneGraphStreamWriter and the associated stream
127: */
128: public void close() throws IOException {
129: control.close();
130: out.close();
131: }
132: }
|