15
15
* limitations under the License.
18
/* $Id: PDFStructElem.java 830293 2009-10-27 19:07:52Z vhennebert $ */
18
/* $Id: PDFStructElem.java 1341992 2012-05-23 19:22:59Z vhennebert $ */
20
20
package org.apache.fop.pdf;
22
import java.io.IOException;
23
import java.io.OutputStream;
24
import java.util.ArrayList;
25
import java.util.List;
22
26
import java.util.Locale;
24
import org.apache.fop.util.XMLUtil;
28
import org.apache.fop.accessibility.StructureTreeElement;
29
import org.apache.fop.util.LanguageTags;
27
32
* Class representing a PDF Structure Element.
29
public class PDFStructElem extends PDFDictionary {
34
public class PDFStructElem extends PDFDictionary implements StructureTreeElement, CompressedObject {
31
36
private PDFStructElem parentElement;
39
* Elements to be added to the kids array.
41
protected List<PDFObject> kids;
34
44
* Creates a new structure element.
36
46
* @param parent parent of this element
58
67
/** {@inheritDoc} */
59
68
public void setParent(PDFObject parent) {
69
if (parent != null && parent.hasObjectNumber()) {
61
70
put("P", new PDFReference(parent));
66
* Returns the kids of this structure element.
68
* @return the value of the K entry
70
private PDFArray getKids() {
71
return (PDFArray) get("K");
75
75
* Add a kid to this structure element. This element will then add itself to
76
76
* its parent structure element if it has not already, and so will the
77
77
* parent, and so on.
79
79
* @param kid element to be added
81
81
public void addKid(PDFObject kid) {
82
PDFArray kids = getKids();
83
82
if (kids == null) {
84
kids = new PDFArray();
83
kids = new ArrayList<PDFObject>();
91
private boolean containsKid(PDFObject kid) {
92
PDFArray kids = getKids();
93
return kids != null && kids.contains(kid);
96
private void joinHierarchy() {
97
if (parentElement != null && !parentElement.containsKid(this)) {
98
parentElement.addKid(this);
154
139
* @return the value of the Lang entry (<code>null</code> if no language was specified)
156
141
public String getLanguage() {
157
return (String)get("Lang");
142
return (String) get("Lang");
146
protected void writeDictionary(OutputStream out, StringBuilder textBuffer) throws IOException {
148
super.writeDictionary(out, textBuffer);
152
* Attaches all valid kids to the kids array.
154
* @return true iff 1+ kids were added to the kids array
156
protected boolean attachKids() {
157
List<PDFObject> validKids = new ArrayList<PDFObject>();
159
for (PDFObject kid : kids) {
160
if (kid instanceof Placeholder) {
161
if (((Placeholder) kid).attachKids()) {
169
boolean kidsAttached = !validKids.isEmpty();
171
PDFArray array = new PDFArray();
172
for (PDFObject ob : validKids) {
181
* Class representing a placeholder for a PDF Structure Element.
183
public static class Placeholder extends PDFStructElem {
186
public void outputInline(OutputStream out, StringBuilder textBuffer) throws IOException {
188
assert kids.size() > 0;
189
for (int i = 0; i < kids.size(); i++) {
191
textBuffer.append(' ');
193
Object obj = kids.get(i);
194
formatObject(obj, out, textBuffer);
204
public Placeholder(PDFObject parent, String name) {
205
super(parent, new PDFName(name));