001: /**
002: * Copyright (c) 2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdfwriter;
031:
032: import java.io.IOException;
033: import java.io.OutputStream;
034:
035: import java.util.Iterator;
036: import java.util.List;
037:
038: import org.pdfbox.cos.COSArray;
039: import org.pdfbox.cos.COSBase;
040: import org.pdfbox.cos.COSBoolean;
041: import org.pdfbox.cos.COSDictionary;
042: import org.pdfbox.cos.COSFloat;
043: import org.pdfbox.cos.COSInteger;
044: import org.pdfbox.cos.COSName;
045: import org.pdfbox.cos.COSString;
046:
047: import org.pdfbox.util.ImageParameters;
048: import org.pdfbox.util.PDFOperator;
049:
050: /**
051: * A class that will take a list of tokens and write out a stream with them.
052: *
053: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
054: * @version $Revision: 1.8 $
055: */
056: public class ContentStreamWriter {
057: private OutputStream output;
058: /**
059: * space character.
060: */
061: public static final byte[] SPACE = new byte[] { 32 };
062:
063: /**
064: * standard line separator on this platform.
065: */
066: public static final byte[] EOL = System.getProperty(
067: "line.separator").getBytes();
068:
069: /**
070: * This will create a new content stream writer.
071: *
072: * @param out The stream to write the data to.
073: */
074: public ContentStreamWriter(OutputStream out) {
075: output = out;
076: }
077:
078: /**
079: * This will write out the list of tokens to the stream.
080: *
081: * @param tokens The tokens to write to the stream.
082: * @param start The start index into the list of tokens.
083: * @param end The end index into the list of tokens.
084: * @throws IOException If there is an error writing to the stream.
085: */
086: public void writeTokens(List tokens, int start, int end)
087: throws IOException {
088: for (int i = start; i < end; i++) {
089: Object o = tokens.get(i);
090: writeObject(o);
091: //write a space between each object.
092: output.write(32);
093: }
094: output.flush();
095: }
096:
097: private void writeObject(Object o) throws IOException {
098: if (o instanceof COSString) {
099: ((COSString) o).writePDF(output);
100: } else if (o instanceof COSFloat) {
101: ((COSFloat) o).writePDF(output);
102: } else if (o instanceof COSInteger) {
103: ((COSInteger) o).writePDF(output);
104: } else if (o instanceof COSBoolean) {
105: ((COSBoolean) o).writePDF(output);
106: } else if (o instanceof COSName) {
107: ((COSName) o).writePDF(output);
108: } else if (o instanceof COSArray) {
109: COSArray array = (COSArray) o;
110: output.write(COSWriter.ARRAY_OPEN);
111: for (int i = 0; i < array.size(); i++) {
112: writeObject(array.get(i));
113: output.write(SPACE);
114: }
115:
116: output.write(COSWriter.ARRAY_CLOSE);
117: } else if (o instanceof COSDictionary) {
118: COSDictionary obj = (COSDictionary) o;
119: output.write(COSWriter.DICT_OPEN);
120: for (Iterator i = obj.keyList().iterator(); i.hasNext();) {
121: COSName name = (COSName) i.next();
122: COSBase value = obj.getItem(name);
123: if (value != null) {
124: writeObject(name);
125: output.write(SPACE);
126:
127: writeObject(value);
128:
129: output.write(SPACE);
130:
131: }
132: }
133: output.write(COSWriter.DICT_CLOSE);
134: output.write(SPACE);
135: } else if (o instanceof PDFOperator) {
136: PDFOperator op = (PDFOperator) o;
137: if (op.getOperation().equals("BI")) {
138: output.write("BI".getBytes());
139: ImageParameters params = op.getImageParameters();
140: COSDictionary dic = params.getDictionary();
141: Iterator iter = dic.keyList().iterator();
142: while (iter.hasNext()) {
143: COSName key = (COSName) iter.next();
144: Object value = dic.getDictionaryObject(key);
145: key.writePDF(output);
146: output.write(SPACE);
147: writeObject(value);
148: output.write(EOL);
149: }
150: output.write("ID".getBytes());
151: output.write(EOL);
152: output.write(op.getImageData());
153: } else {
154: output.write(op.getOperation().getBytes());
155: output.write(EOL);
156: }
157: } else {
158: throw new IOException(
159: "Error:Unknown type in content stream:" + o);
160: }
161: }
162:
163: /**
164: * This will write out the list of tokens to the stream.
165: *
166: * @param tokens The tokens to write to the stream.
167: * @throws IOException If there is an error writing to the stream.
168: */
169: public void writeTokens(List tokens) throws IOException {
170: writeTokens(tokens, 0, tokens.size());
171: }
172: }
|