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.pdmodel.fdf;
031:
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: import org.pdfbox.cos.COSArray;
036: import org.pdfbox.cos.COSBase;
037: import org.pdfbox.cos.COSDictionary;
038: import org.pdfbox.cos.COSName;
039:
040: import org.pdfbox.pdmodel.common.COSObjectable;
041: import org.pdfbox.pdmodel.common.COSArrayList;
042: import org.pdfbox.pdmodel.common.PDTextStream;
043: import org.pdfbox.pdmodel.common.PDNamedTextStream;
044:
045: /**
046: * This represents an FDF JavaScript dictionary that is part of the FDF document.
047: *
048: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
049: * @version $Revision: 1.4 $
050: */
051: public class FDFJavaScript implements COSObjectable {
052: private COSDictionary js;
053:
054: /**
055: * Default constructor.
056: */
057: public FDFJavaScript() {
058: js = new COSDictionary();
059: }
060:
061: /**
062: * Constructor.
063: *
064: * @param javaScript The FDF java script.
065: */
066: public FDFJavaScript(COSDictionary javaScript) {
067: js = javaScript;
068: }
069:
070: /**
071: * Convert this standard java object to a COS object.
072: *
073: * @return The cos object that matches this Java object.
074: */
075: public COSBase getCOSObject() {
076: return js;
077: }
078:
079: /**
080: * Convert this standard java object to a COS object.
081: *
082: * @return The cos object that matches this Java object.
083: */
084: public COSDictionary getCOSDictionary() {
085: return js;
086: }
087:
088: /**
089: * This will get the javascript that is executed before the import.
090: *
091: * @return Some javascript code.
092: */
093: public PDTextStream getBefore() {
094: return PDTextStream.createTextStream(js
095: .getDictionaryObject("Before"));
096: }
097:
098: /**
099: * This will set the javascript code the will get execute before the import.
100: *
101: * @param before A reference to some javascript code.
102: */
103: public void setBefore(PDTextStream before) {
104: js.setItem("Before", before);
105: }
106:
107: /**
108: * This will get the javascript that is executed after the import.
109: *
110: * @return Some javascript code.
111: */
112: public PDTextStream getAfter() {
113: return PDTextStream.createTextStream(js
114: .getDictionaryObject("After"));
115: }
116:
117: /**
118: * This will set the javascript code the will get execute after the import.
119: *
120: * @param after A reference to some javascript code.
121: */
122: public void setAfter(PDTextStream after) {
123: js.setItem("After", after);
124: }
125:
126: /**
127: * This will return a list of PDNamedTextStream objects. This is the "Doc"
128: * entry of the pdf document. These will be added to the PDF documents
129: * javascript name tree. This will not return null.
130: *
131: * @return A list of all named javascript entries.
132: */
133: public List getNamedJavaScripts() {
134: COSArray array = (COSArray) js.getDictionaryObject("Doc");
135: List namedStreams = new ArrayList();
136: if (array == null) {
137: array = new COSArray();
138: js.setItem("Doc", array);
139: }
140: for (int i = 0; i < array.size(); i++) {
141: COSName name = (COSName) array.get(i);
142: i++;
143: COSBase stream = array.get(i);
144: PDNamedTextStream namedStream = new PDNamedTextStream(name,
145: stream);
146: namedStreams.add(namedStream);
147: }
148: return new COSArrayList(namedStreams, array);
149: }
150:
151: /**
152: * This should be a list of PDNamedTextStream objects.
153: *
154: * @param namedStreams The named streams.
155: */
156: public void setNamedJavaScripts(List namedStreams) {
157: COSArray array = COSArrayList.converterToCOSArray(namedStreams);
158: js.setItem("Doc", array);
159: }
160: }
|