001: /*
002: * $Id: FlashOutput.java,v 1.3 2002/05/16 05:08:41 skavish Exp $
003: *
004: * ===========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.util;
052:
053: import java.io.*;
054: import java.util.*;
055: import org.openlaszlo.iv.flash.api.*;
056: import org.openlaszlo.iv.flash.api.text.*;
057:
058: /**
059: * This class is basically a {@link FlashBuffer} which
060: * tracks flash definitions written into the buffer and
061: * provides unique IDs for them.
062: * <P>
063: * The class also supports hierarchy of flash buffers
064: *
065: * @author Dmitry Skavish
066: * @see FlashBuffer
067: */
068: public class FlashOutput extends FlashBuffer {
069:
070: // definitions caches
071: private FlashItem jpegTables;
072: private Hashtable hashTable = new Hashtable();
073: private FlashOutput main;
074: private int currentID = 1;
075: private Object userData;
076: private FlashFile file;
077:
078: public FlashOutput(int size) {
079: super (size);
080: }
081:
082: public FlashOutput(FlashFile file, int size) {
083: super (size);
084: this .file = file;
085: }
086:
087: public FlashOutput(FlashOutput main, int size) {
088: super (size);
089: this .main = main;
090: }
091:
092: public FlashFile getFlashFile() {
093: if (main != null)
094: return main.getFlashFile();
095: return file;
096: }
097:
098: /**
099: * Gets ID of flash definition.
100: *
101: * @param def flash definition ID of which is requisted
102: * @return unique ID
103: */
104: public int getDefID(FlashDef def) {
105: return getObjID(def);
106: }
107:
108: private synchronized int getObjID(Object key) {
109: if (main != null)
110: return main.getObjID(key);
111: Integer id = (Integer) hashTable.get(key);
112: if (id != null)
113: return id.intValue();
114: hashTable.put(key, new Integer(currentID));
115: /*if( key instanceof FlashDef ) {
116: FlashDef def = (FlashDef) key;
117: System.out.println( "FlashOutput.getDefID: oldID="+def.getID()+", newID="+currentID );
118: }*/
119: return currentID++;
120: }
121:
122: public synchronized boolean defined(Object key) {
123: if (main != null)
124: return main.defined(key);
125: return (hashTable.get(key) != null);
126: }
127:
128: /**
129: * Writes ID of flash definiton into the buffer.
130: *
131: * @param def flash definiton, ID of which will be written
132: */
133: public void writeDefID(FlashDef def) {
134: writeWord(getObjID(def));
135: }
136:
137: /**
138: * Writes ID of font into the buffer.
139: *
140: * @param font font, ID of which will be written
141: */
142: public void writeFontID(Font font) {
143: writeWord(getObjID(font));
144: }
145:
146: public void writeJPegTables(FlashItem jt) {
147: if (main != null) {
148: main.writeJPegTables(jt);
149: } else {
150: if (jpegTables != null)
151: return;
152: jpegTables = jt;
153: jt.write(this );
154: }
155: }
156:
157: public void setUserData(Object data) {
158: userData = data;
159: }
160:
161: public Object getUserData() {
162: return userData;
163: }
164: }
|