KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > objects > PdfObjectWrapper


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.objects;
28
29 import it.stefanochizzolini.clown.documents.Document;
30 import it.stefanochizzolini.clown.files.File;
31
32 /**
33   This is the base high-level representation of a PDF object; all specialized objects
34   (e.g. Document, Page, Pages, ContentStream...) inherit from it.
35   <h3>Remarks</h3>
36   <p>Somebody may wonder why I didn't directly make specialized objects inherit from
37   their low-level counterparts (e.g. Page extends PdfDictionary, ContentStream
38   extends PdfStream and so on): that could have been much smoother anyway, you argue.
39   Yeah, I could agree if there was a plain one-to-one mapping between primitive PDF
40   types and specialized instances, but (you know!) reality is not so polished as
41   theory: the 'Content' entry of Page dictionaries may be a simple reference to a
42   PdfStream or a PdfArray of references to PdfStream-s, Pages collections may be
43   spread across a B-tree instead of a flat PdfArray etc. So: <i>in order to hide all
44   these annoying inner workings, I chose to adopt a composition pattern instead of
45   the apparently-reasonable (but actually awkward!) inheritance pattern</i>.
46   Nonetheless, users are always enabled to navigate through the low-level structure
47   accessing the <see cref="BaseObject"/> underlying property.</p>
48 */

49 public abstract class PdfObjectWrapper<TDataObject extends PdfDataObject>
50 {
51   // <class>
52
// <dynamic>
53
// <fields>
54
private PdfDirectObject baseObject;
55   private TDataObject baseDataObject;
56   private PdfIndirectObject container;
57   // </fields>
58

59   // <constructors>
60
protected PdfObjectWrapper(
61     File context,
62     TDataObject baseDataObject
63     )
64   {
65     this(
66       context.getIndirectObjects().add(baseDataObject).getReference(),
67       null
68       );
69   }
70
71   /**
72     @param baseObject Base PDF object. MUST be a <see cref="PdfReference"/>
73     everytime available.
74     @param container Indirect object containing the base object.
75   */

76   protected PdfObjectWrapper(
77     PdfDirectObject baseObject,
78     PdfIndirectObject container
79     )
80   {
81     this.baseObject = baseObject;
82     this.baseDataObject = (TDataObject)File.resolve(baseObject);
83
84     setContainer(container);
85   }
86   // </constructors>
87

88   // <interface>
89
// <public>
90
/**
91     Gets the underlying data object.
92   */

93   public TDataObject getBaseDataObject(
94     )
95   {return baseDataObject;}
96
97   /**
98     Gets the underlying reference object; if it's not available
99     (non-indirect object), gets the underlying data object.
100   */

101   public PdfDirectObject getBaseObject(
102     )
103   {return baseObject;}
104
105   /**
106     Gets the clone of the object, registered inside the given document context.
107     @param context Which document the clone has to be registered in.
108   */

109   public abstract Object JavaDoc clone(
110     Document context
111     );
112
113   /**
114     Removes the object from its document context.
115     <h3>Remarks</h3>
116     <p>The object is no more usable after this method returns.</p>
117     @return Whether the object was actually decontextualized (only indirect objects can be
118     decontextualize).
119   */

120   public boolean delete(
121     )
122   {
123     // Is the object indirect?
124
if(baseObject instanceof PdfReference) // Indirect object.
125
{
126       ((PdfReference)baseObject).delete();
127       return true;
128     }
129     else // Direct object.
130
{return false;}
131   }
132
133   /**
134     Gets the indirect object containing the base object.
135     <h3>Remarks</h3>
136     <p>It's used for update purposes.</p>
137   */

138   public PdfIndirectObject getContainer(
139     )
140   {return container;}
141
142   /**
143     Gets the document context.
144   */

145   public Document getDocument(
146     )
147   {return container.getFile().getDocument();}
148
149   /**
150     Gets the file context.
151   */

152   public File getFile(
153     )
154   {return container.getFile();}
155
156   /**
157     Manually update the underlying indirect object.
158   */

159   public void update(
160     )
161   {container.update();}
162   // </public>
163

164   // <protected>
165
protected void setBaseDataObject(
166     TDataObject value
167     )
168   {baseDataObject = value;}
169
170   protected void setBaseObject(
171     PdfDirectObject value
172     )
173   {baseObject = value;}
174   // </protected>
175

176   // <internal>
177
/**
178     <h3>Remarks</h3>
179     <p>For internal use only.</p>
180   */

181   public void setContainer(
182     PdfIndirectObject value
183     )
184   {
185     // Is base object indirect (self-contained)?
186
if(baseObject instanceof PdfReference) // Base object is indirect (self-contained).
187
{container = ((PdfReference)baseObject).getIndirectObject();}
188     else // Base object is direct (contained).
189
{container = value;}
190   }
191   // </internal>
192
// </interface>
193
// </dynamic>
194
// </class>
195
}
Popular Tags