123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- /* $Id$ */
-
- package org.apache.fop.pdf;
-
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.Writer;
- import java.util.Collection;
- import java.util.List;
-
- import org.apache.commons.io.output.CountingOutputStream;
-
- /**
- * Class representing an array object.
- */
- public class PDFArray extends PDFObject {
- /**
- * List holding the values of this array
- */
- protected List values = new java.util.ArrayList();
-
- /**
- * Create a new, empty array object
- * @param parent the array's parent if any
- */
- public PDFArray(PDFObject parent) {
- /* generic creation of PDF object */
- super(parent);
- }
-
- /**
- * Create an array object.
- * @param parent the array's parent if any
- * @param values the actual array wrapped by this object
- */
- public PDFArray(PDFObject parent, int[] values) {
- /* generic creation of PDF object */
- super(parent);
-
- for (int i = 0, c = values.length; i < c; i++) {
- this.values.add(new Integer(values[i]));
- }
- }
-
- /**
- * Create an array object.
- * @param parent the array's parent if any
- * @param values the actual values wrapped by this object
- */
- public PDFArray(PDFObject parent, Collection values) {
- /* generic creation of PDF object */
- super(parent);
-
- this.values.addAll(values);
- }
-
- /**
- * Create the array object
- * @param parent the array's parent if any
- * @param values the actual array wrapped by this object
- */
- public PDFArray(PDFObject parent, Object[] values) {
- /* generic creation of PDF object */
- super(parent);
-
- for (int i = 0, c = values.length; i < c; i++) {
- this.values.add(values[i]);
- }
- }
-
- /**
- * Returns the length of the array
- * @return the length of the array
- */
- public int length() {
- return this.values.size();
- }
-
- /**
- * Sets an entry at a given location.
- * @param index the index of the value to set
- * @param obj the new value
- */
- public void set(int index, Object obj) {
- this.values.set(index, obj);
- }
-
- /**
- * Sets an entry at a given location.
- * @param index the index of the value to set
- * @param value the new value
- */
- public void set(int index, double value) {
- this.values.set(index, new Double(value));
- }
-
- /**
- * Gets an entry at a given location.
- * @param index the index of the value to set
- * @return the requested value
- */
- public Object get(int index) {
- return this.values.get(index);
- }
-
- /**
- * Adds a new value to the array.
- * @param obj the value
- */
- public void add(PDFObject obj) {
- if (obj != null) {
- obj.setParent(this);
- }
- this.values.add(obj);
- }
-
- /**
- * Adds a new value to the array.
- * @param obj the value
- */
- public void add(Object obj) {
- this.values.add(obj);
- }
-
- /**
- * Adds a new value to the array.
- * @param value the value
- */
- public void add(double value) {
- this.values.add(new Double(value));
- }
-
- /** {@inheritDoc} */
- protected int output(OutputStream stream) throws IOException {
- CountingOutputStream cout = new CountingOutputStream(stream);
- Writer writer = PDFDocument.getWriterFor(cout);
- if (hasObjectNumber()) {
- writer.write(getObjectID());
- }
-
- writer.write('[');
- for (int i = 0; i < values.size(); i++) {
- if (i > 0) {
- writer.write(' ');
- }
- Object obj = this.values.get(i);
- formatObject(obj, cout, writer);
- }
- writer.write(']');
-
- if (hasObjectNumber()) {
- writer.write("\nendobj\n");
- }
-
- writer.flush();
- return cout.getCount();
- }
-
- }
|