KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > fdf > FDFJavaScript


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.pdmodel.fdf;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.pdfbox.cos.COSArray;
37 import org.pdfbox.cos.COSBase;
38 import org.pdfbox.cos.COSDictionary;
39 import org.pdfbox.cos.COSName;
40
41 import org.pdfbox.pdmodel.common.COSObjectable;
42 import org.pdfbox.pdmodel.common.COSArrayList;
43 import org.pdfbox.pdmodel.common.PDTextStream;
44 import org.pdfbox.pdmodel.common.PDNamedTextStream;
45
46 /**
47  * This represents an FDF JavaScript dictionary that is part of the FDF document.
48  *
49  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
50  * @version $Revision: 1.4 $
51  */

52 public class FDFJavaScript implements COSObjectable
53 {
54     private COSDictionary js;
55
56     /**
57      * Default constructor.
58      */

59     public FDFJavaScript()
60     {
61         js = new COSDictionary();
62     }
63
64     /**
65      * Constructor.
66      *
67      * @param javaScript The FDF java script.
68      */

69     public FDFJavaScript( COSDictionary javaScript )
70     {
71         js = javaScript;
72     }
73
74     /**
75      * Convert this standard java object to a COS object.
76      *
77      * @return The cos object that matches this Java object.
78      */

79     public COSBase getCOSObject()
80     {
81         return js;
82     }
83
84     /**
85      * Convert this standard java object to a COS object.
86      *
87      * @return The cos object that matches this Java object.
88      */

89     public COSDictionary getCOSDictionary()
90     {
91         return js;
92     }
93
94     /**
95      * This will get the javascript that is executed before the import.
96      *
97      * @return Some javascript code.
98      */

99     public PDTextStream getBefore()
100     {
101         return PDTextStream.createTextStream( js.getDictionaryObject( "Before" ) );
102     }
103
104     /**
105      * This will set the javascript code the will get execute before the import.
106      *
107      * @param before A reference to some javascript code.
108      */

109     public void setBefore( PDTextStream before )
110     {
111         js.setItem( "Before", before );
112     }
113
114     /**
115      * This will get the javascript that is executed after the import.
116      *
117      * @return Some javascript code.
118      */

119     public PDTextStream getAfter()
120     {
121         return PDTextStream.createTextStream( js.getDictionaryObject( "After" ) );
122     }
123
124     /**
125      * This will set the javascript code the will get execute after the import.
126      *
127      * @param after A reference to some javascript code.
128      */

129     public void setAfter( PDTextStream after )
130     {
131         js.setItem( "After", after );
132     }
133
134     /**
135      * This will return a list of PDNamedTextStream objects. This is the "Doc"
136      * entry of the pdf document. These will be added to the PDF documents
137      * javascript name tree. This will not return null.
138      *
139      * @return A list of all named javascript entries.
140      */

141     public List JavaDoc getNamedJavaScripts()
142     {
143         COSArray array = (COSArray)js.getDictionaryObject( "Doc" );
144         List JavaDoc namedStreams = new ArrayList JavaDoc();
145         if( array == null )
146         {
147             array = new COSArray();
148             js.setItem( "Doc", array );
149         }
150         for( int i=0; i<array.size(); i++ )
151         {
152             COSName name = (COSName)array.get( i );
153             i++;
154             COSBase stream = array.get( i );
155             PDNamedTextStream namedStream = new PDNamedTextStream( name, stream );
156             namedStreams.add( namedStream );
157         }
158         return new COSArrayList( namedStreams, array );
159     }
160
161     /**
162      * This should be a list of PDNamedTextStream objects.
163      *
164      * @param namedStreams The named streams.
165      */

166     public void setNamedJavaScripts( List JavaDoc namedStreams )
167     {
168         COSArray array = COSArrayList.converterToCOSArray( namedStreams );
169         js.setItem( "Doc", array );
170     }
171 }
Popular Tags