KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdfwriter > ContentStreamWriter


1 /**
2  * Copyright (c) 2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdfwriter;
32
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38
39 import org.pdfbox.cos.COSArray;
40 import org.pdfbox.cos.COSBase;
41 import org.pdfbox.cos.COSBoolean;
42 import org.pdfbox.cos.COSDictionary;
43 import org.pdfbox.cos.COSFloat;
44 import org.pdfbox.cos.COSInteger;
45 import org.pdfbox.cos.COSName;
46 import org.pdfbox.cos.COSString;
47
48 import org.pdfbox.util.ImageParameters;
49 import org.pdfbox.util.PDFOperator;
50
51 /**
52  * A class that will take a list of tokens and write out a stream with them.
53  *
54  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
55  * @version $Revision: 1.8 $
56  */

57 public class ContentStreamWriter
58 {
59     private OutputStream JavaDoc output;
60     /**
61      * space character.
62      */

63     public static final byte[] SPACE = new byte[] { 32 };
64     
65     /**
66      * standard line separator on this platform.
67      */

68     public static final byte[] EOL = System.getProperty("line.separator").getBytes();
69     
70     /**
71      * This will create a new content stream writer.
72      *
73      * @param out The stream to write the data to.
74      */

75     public ContentStreamWriter( OutputStream JavaDoc out )
76     {
77         output = out;
78     }
79     
80     /**
81      * This will write out the list of tokens to the stream.
82      *
83      * @param tokens The tokens to write to the stream.
84      * @param start The start index into the list of tokens.
85      * @param end The end index into the list of tokens.
86      * @throws IOException If there is an error writing to the stream.
87      */

88     public void writeTokens( List JavaDoc tokens, int start, int end ) throws IOException JavaDoc
89     {
90         for( int i=start; i<end; i++ )
91         {
92             Object JavaDoc o = tokens.get( i );
93             writeObject( o );
94             //write a space between each object.
95
output.write( 32 );
96         }
97         output.flush();
98     }
99     
100     private void writeObject( Object JavaDoc o ) throws IOException JavaDoc
101     {
102         if( o instanceof COSString )
103         {
104             ((COSString)o).writePDF( output );
105         }
106         else if( o instanceof COSFloat )
107         {
108             ((COSFloat)o).writePDF( output );
109         }
110         else if( o instanceof COSInteger )
111         {
112             ((COSInteger)o).writePDF( output );
113         }
114         else if( o instanceof COSBoolean )
115         {
116             ((COSBoolean)o).writePDF( output );
117         }
118         else if( o instanceof COSName )
119         {
120             ((COSName)o).writePDF( output );
121         }
122         else if( o instanceof COSArray )
123         {
124             COSArray array = (COSArray)o;
125             output.write(COSWriter.ARRAY_OPEN);
126             for( int i=0; i<array.size(); i++ )
127             {
128                 writeObject( array.get( i ) );
129                 output.write( SPACE );
130             }
131             
132             output.write( COSWriter.ARRAY_CLOSE );
133         }
134         else if( o instanceof COSDictionary )
135         {
136             COSDictionary obj = (COSDictionary)o;
137             output.write( COSWriter.DICT_OPEN );
138             for (Iterator JavaDoc i = obj.keyList().iterator(); i.hasNext();)
139             {
140                 COSName name = (COSName) i.next();
141                 COSBase value = obj.getItem(name);
142                 if (value != null)
143                 {
144                     writeObject( name );
145                     output.write( SPACE );
146
147                     writeObject( value );
148
149                     output.write( SPACE );
150
151                 }
152             }
153             output.write( COSWriter.DICT_CLOSE );
154             output.write( SPACE );
155         }
156         else if( o instanceof PDFOperator )
157         {
158             PDFOperator op = (PDFOperator)o;
159             if( op.getOperation().equals( "BI" ) )
160             {
161                 output.write( "BI".getBytes() );
162                 ImageParameters params = op.getImageParameters();
163                 COSDictionary dic = params.getDictionary();
164                 Iterator JavaDoc iter = dic.keyList().iterator();
165                 while( iter.hasNext() )
166                 {
167                     COSName key = (COSName)iter.next();
168                     Object JavaDoc value = dic.getDictionaryObject( key );
169                     key.writePDF( output );
170                     output.write( SPACE );
171                     writeObject( value );
172                     output.write( EOL );
173                 }
174                 output.write( "ID".getBytes() );
175                 output.write( EOL );
176                 output.write( op.getImageData() );
177             }
178             else
179             {
180                 output.write( op.getOperation().getBytes() );
181                 output.write( EOL );
182             }
183         }
184         else
185         {
186             throw new IOException JavaDoc( "Error:Unknown type in content stream:" + o );
187         }
188     }
189     
190     /**
191      * This will write out the list of tokens to the stream.
192      *
193      * @param tokens The tokens to write to the stream.
194      * @throws IOException If there is an error writing to the stream.
195      */

196     public void writeTokens( List JavaDoc tokens ) throws IOException JavaDoc
197     {
198         writeTokens( tokens, 0, tokens.size() );
199     }
200 }
201
Popular Tags