001: /*
002: * $RCSfile: EnvelopeHandler.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.4 $
041: * $Date: 2007/02/09 17:20:06 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.loaders.lw3d;
046:
047: import java.io.StreamTokenizer;
048: import java.io.IOException;
049: import java.lang.reflect.Constructor;
050: import com.sun.j3d.loaders.ParsingErrorException;
051: import java.lang.ClassNotFoundException;
052: import java.lang.InstantiationException;
053: import java.lang.IllegalAccessException;
054: import java.lang.reflect.InvocationTargetException;
055:
056: /**
057: * This class is used in implementing Envelope objects (of which there
058: * is currently only one - LightIntensity).
059: * The class is called whenever the parser has encountered
060: * a token which could have an envelope description. If the
061: * token simply has a numeric value, this value is stored.
062: * If, instead, there is an envelope, then the class creates
063: * the envelope class and parses that information.
064: */
065:
066: class EnvelopeHandler extends TextfileParser {
067:
068: float theValue = 0;
069: boolean hasValue = false;
070: boolean hasEnvelope = true;
071: LwsEnvelope theEnvelope = null;
072:
073: /**
074: * Constructor: This constructor is used if there is no existing
075: * implementation for this type of envelope. The real constructor
076: * is called with the generic LwsEnvelope class name, which will
077: * allow s to parse and ignore the envelope data
078: */
079: EnvelopeHandler(StreamTokenizer st, int totalFrames, float totalTime) {
080: this (st, totalFrames, totalTime,
081: "com.sun.j3d.utils.loaders.lw3d.LwsEnvelope");
082: }
083:
084: /**
085: * Constructor: This constructor is called with the name of a class
086: * that can handle the encountered envelope. This is done so that this
087: * EnvelopeHandler class can generically call the given class to handle
088: * the envelope, whether it results in parsing/ignoring the data or
089: * in actually using the data
090: */
091: EnvelopeHandler(StreamTokenizer st, int totalFrames,
092: float totalTime, String envClassName)
093: throws ParsingErrorException {
094: try {
095: theValue = (float) getNumber(st);
096: hasValue = true;
097: } catch (NumberFormatException e) {
098: if (st.ttype == '(') {
099: st.pushBack();
100: // This code creates a new instance for the given class name
101: try {
102: Class envClass = Class.forName(envClassName);
103: Constructor constructors[] = envClass
104: .getConstructors();
105: Constructor con = constructors[0];
106: Object args[] = new Object[3];
107: args[0] = (Object) st;
108: args[1] = (Object) (new Integer(totalFrames));
109: args[2] = (Object) (new Float(totalTime));
110: try {
111: theEnvelope = (LwsEnvelope) con
112: .newInstance(args);
113: hasEnvelope = true;
114: } catch (InstantiationException e3) {
115: // Ignore
116: } catch (IllegalAccessException e3) {
117: // Ignore
118: } catch (InvocationTargetException e3) {
119: // Ignore
120: }
121: } catch (ClassNotFoundException e2) {
122: // Ignore
123: }
124: }
125: }
126: }
127: }
|