KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > documents > contents > ContentStreams


1 /*
2   Copyright © 2006 Stefano Chizzolini. http://clown.stefanochizzolini.it
3
4   Contributors:
5     * Stefano Chizzolini (original code developer, info@stefanochizzolini.it):
6       contributed code is Copyright © 2006 by Stefano Chizzolini.
7
8   This file should be part of the source code distribution of "PDF Clown library"
9   (the Program): see the accompanying README files for more info.
10
11   This Program is free software; you can redistribute it and/or modify it under
12   the terms of the GNU General Public License as published by the Free Software
13   Foundation; either version 2 of the License, or (at your option) any later version.
14
15   This Program is distributed in the hope that it will be useful, but WITHOUT ANY
16   WARRANTY, either expressed or implied; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
18
19   You should have received a copy of the GNU General Public License along with this
20   Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
21
22   Redistribution and use, with or without modification, are permitted provided that such
23   redistributions retain the above copyright notice, license and disclaimer, along with
24   this list of conditions.
25 */

26
27 package it.stefanochizzolini.clown.documents.contents;
28
29 import it.stefanochizzolini.clown.documents.Document;
30 import it.stefanochizzolini.clown.documents.Page;
31 import it.stefanochizzolini.clown.objects.PdfArray;
32 import it.stefanochizzolini.clown.objects.PdfDataObject;
33 import it.stefanochizzolini.clown.objects.PdfDirectObject;
34 import it.stefanochizzolini.clown.objects.PdfName;
35 import it.stefanochizzolini.clown.objects.PdfObjectWrapper;
36 import it.stefanochizzolini.clown.objects.PdfReference;
37 import it.stefanochizzolini.clown.objects.PdfStream;
38 import it.stefanochizzolini.clown.util.NotImplementedException;
39
40 import java.util.Collection JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.ListIterator JavaDoc;
44
45 /**
46   Content stream collection.
47 */

48 public class ContentStreams
49   extends PdfObjectWrapper<PdfDataObject>
50   implements List JavaDoc<ContentStream>
51 {
52   // <class>
53
// <dynamic>
54
// <fields>
55
private Page parentPage;
56   // </fields>
57

58   // <constructors>
59
/**
60     <h3>Remarks</h3>
61     <p>For internal use only.</p>
62   */

63   public ContentStreams(
64     PdfDirectObject baseObject,
65     Page parentPage
66     )
67   {
68     super(
69       baseObject,
70       ((PdfReference)parentPage.getBaseObject()).getIndirectObject()
71       );
72     this.parentPage = parentPage;
73   }
74   // </constructors>
75

76   // <interface>
77
// <public>
78
public Object JavaDoc clone(
79     Document context
80     )
81   {throw new NotImplementedException();}
82
83   public Page getParentPage(
84     )
85   {return parentPage;}
86
87   // <List>
88
public void add(
89     int index,
90     ContentStream content
91     )
92   {
93     // Add the content inside the ensured array!
94
ensureArray().add(index,content.getBaseObject());
95     // Update context!
96
content.setStreamContext(parentPage);
97   }
98
99   public boolean addAll(
100     int index,
101     Collection JavaDoc<? extends ContentStream> contents
102     )
103   {throw new NotImplementedException();}
104
105   public ContentStream get(
106     int index
107     )
108   {
109     PdfDirectObject content;
110     PdfDataObject contents = getBaseDataObject();
111     if(contents instanceof PdfStream)
112     {
113       if(index == 0)
114         content = getBaseObject();
115       else
116         throw new IndexOutOfBoundsException JavaDoc();
117     }
118     else
119     {
120       content = ((PdfArray)contents).get(index);
121     }
122
123     return new ContentStream(
124       content,
125       parentPage
126       );
127   }
128
129   public int indexOf(
130     Object JavaDoc content
131     )
132   {
133     /*
134       NOTE: Due to the contract, we must NOT modify the state of this object,
135       as this is a getter operation (particularly, we don't use ensureArray()).
136     */

137     PdfDataObject baseDataObject = getBaseDataObject();
138     // Are contents just a single stream object?
139
if(baseDataObject instanceof PdfStream) // Stream.
140
{
141       if(baseDataObject.equals(((ContentStream)content).getBaseDataObject()))
142         return 0;
143       else
144         return -1;
145     }
146     else // Array.
147
{
148       return ((PdfArray)baseDataObject).indexOf(((ContentStream)content).getBaseObject());
149     }
150   }
151
152   public int lastIndexOf(
153     Object JavaDoc content
154     )
155   {
156     /*
157       NOTE: Each content stream should NOT appear more than once.
158     */

159     return indexOf(content);
160   }
161
162   public ListIterator JavaDoc<ContentStream> listIterator(
163     )
164   {throw new NotImplementedException();}
165
166   public ListIterator JavaDoc<ContentStream> listIterator(
167     int index
168     )
169   {throw new NotImplementedException();}
170
171   public ContentStream remove(
172     int index
173     )
174   {
175     return new ContentStream(
176       ensureArray().remove(index),
177       parentPage
178       );
179   }
180
181   public ContentStream set(
182     int index,
183     ContentStream content
184     )
185   {throw new NotImplementedException();}
186
187   public List JavaDoc<ContentStream> subList(
188     int fromIndex,
189     int toIndex
190     )
191   {throw new NotImplementedException();}
192
193   // <Collection>
194
public boolean add(
195     ContentStream content
196     )
197   {
198     ensureArray().add(content.getBaseObject());
199
200     // Update context!
201
content.setStreamContext(parentPage);
202
203     return true;
204   }
205
206   public boolean addAll(
207     Collection JavaDoc<? extends ContentStream> contents
208     )
209   {throw new NotImplementedException();}
210
211   public void clear(
212     )
213   {
214     ensureArray().clear();
215   }
216
217   public boolean contains(
218     Object JavaDoc content
219     )
220   {
221     /*
222       NOTE: Due to the contract, we must NOT modify the state of this object,
223       as this is a getter operation (particularly, we don't use ensureArray()).
224     */

225     PdfDataObject baseDataObject = getBaseDataObject();
226     // Are contents just a single stream object?
227
if(baseDataObject instanceof PdfStream) // Stream.
228
{
229       return baseDataObject.equals(((ContentStream)content).getBaseDataObject());
230     }
231     else // Array.
232
{
233       return ((PdfArray)baseDataObject).contains(((ContentStream)content).getBaseObject());
234     }
235   }
236
237   public boolean containsAll(
238     Collection JavaDoc<?> contents
239     )
240   {throw new NotImplementedException();}
241
242   public boolean equals(
243     Object JavaDoc object
244     )
245   {throw new NotImplementedException();}
246
247   public int hashCode(
248     )
249   {throw new NotImplementedException();}
250
251   public boolean isEmpty(
252     )
253   {return (size() == 0);}
254
255   public boolean remove(
256     Object JavaDoc content
257     )
258   {return ensureArray().remove(((ContentStream)content).getBaseObject());}
259
260   public boolean removeAll(
261     Collection JavaDoc<?> contents
262     )
263   {throw new NotImplementedException();}
264
265   public boolean retainAll(
266     Collection JavaDoc<?> contents
267     )
268   {throw new NotImplementedException();}
269
270   public int size(
271     )
272   {
273     /*
274       NOTE: Due to the contract, we must NOT modify the state of this object,
275       as this is a getter operation (particularly, we don't use ensureArray()).
276     */

277     PdfDataObject baseDataObject = getBaseDataObject();
278     if(baseDataObject instanceof PdfStream) // Stream.
279
return 1;
280     else // Array.
281
return ((PdfArray)baseDataObject).size();
282   }
283
284   public ContentStream[] toArray(
285     )
286   {throw new NotImplementedException();}
287
288   public <ContentStream> ContentStream[] toArray(
289     ContentStream[] contents
290     )
291   {throw new NotImplementedException();}
292
293   // <Iterable>
294
public Iterator JavaDoc<ContentStream> iterator(
295     )
296   {throw new NotImplementedException();}
297   // </Iterable>
298
// </Collection>
299
// </List>
300
// </public>
301

302   // <protected>
303
/**
304     Gets the content stream collection array forcing its existence.
305   */

306   protected PdfArray ensureArray(
307     )
308   {
309     PdfDataObject baseDataObject = getBaseDataObject();
310     // Are contents just a single stream object?
311
if(baseDataObject instanceof PdfStream)
312     {
313       /*
314         NOTE: We have to convert our parent page Contents entry
315         from a single stream reference to an array.
316       */

317       baseDataObject = new PdfArray(new PdfDirectObject[]{getBaseObject()});
318       setBaseDataObject(baseDataObject); setBaseObject((PdfDirectObject)baseDataObject);
319       // Convert the page contents!
320
parentPage.getBaseDataObject().put(PdfName.Contents,getBaseObject());
321       // Update context!
322
setContainer(((PdfReference)parentPage.getBaseObject()).getIndirectObject());
323     }
324
325     return (PdfArray)baseDataObject;
326   }
327   // </protected>
328
// </interface>
329
// </dynamic>
330
// </class>
331
}
Popular Tags