~ubuntu-branches/ubuntu/saucy/fop/saucy-proposed

« back to all changes in this revision

Viewing changes to src/java/org/apache/fop/pdf/PDFStructElem.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-05-21 12:21:26 UTC
  • mfrom: (15.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130521122126-3c9r5fo6ountjg6r
Tags: 1:1.1.dfsg-2ubuntu1
* Merge from Debian unstable.  Remaining changes:
  -  Transition libservlet2.5-java -> libservlet3.0-java.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 * limitations under the License.
16
16
 */
17
17
 
18
 
/* $Id: PDFStructElem.java 830293 2009-10-27 19:07:52Z vhennebert $ */
 
18
/* $Id: PDFStructElem.java 1341992 2012-05-23 19:22:59Z vhennebert $ */
19
19
 
20
20
package org.apache.fop.pdf;
21
21
 
 
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;
23
27
 
24
 
import org.apache.fop.util.XMLUtil;
 
28
import org.apache.fop.accessibility.StructureTreeElement;
 
29
import org.apache.fop.util.LanguageTags;
25
30
 
26
31
/**
27
32
 * Class representing a PDF Structure Element.
28
33
 */
29
 
public class PDFStructElem extends PDFDictionary {
 
34
public class PDFStructElem extends PDFDictionary implements StructureTreeElement, CompressedObject {
30
35
 
31
36
    private PDFStructElem parentElement;
32
37
 
33
38
    /**
 
39
     * Elements to be added to the kids array.
 
40
     */
 
41
    protected List<PDFObject> kids;
 
42
 
 
43
    /**
34
44
     * Creates a new structure element.
35
45
     *
36
46
     * @param parent parent of this element
40
50
        if (parent instanceof PDFStructElem) {
41
51
            parentElement = (PDFStructElem) parent;
42
52
        }
43
 
        put("Type", new PDFName("StructElem"));
44
53
        put("S", structureType);
45
54
        setParent(parent);
46
55
    }
57
66
 
58
67
    /** {@inheritDoc} */
59
68
    public void setParent(PDFObject parent) {
60
 
        if (parent != null) {
 
69
        if (parent != null && parent.hasObjectNumber()) {
61
70
           put("P", new PDFReference(parent));
62
71
        }
63
72
    }
64
73
 
65
74
    /**
66
 
     * Returns the kids of this structure element.
67
 
     *
68
 
     * @return the value of the K entry
69
 
     */
70
 
    private PDFArray getKids() {
71
 
        return (PDFArray) get("K");
72
 
    }
73
 
 
74
 
    /**
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
80
80
     */
81
81
    public void addKid(PDFObject kid) {
82
 
        PDFArray kids = getKids();
83
82
        if (kids == null) {
84
 
            kids = new PDFArray();
85
 
            put("K", kids);
 
83
            kids = new ArrayList<PDFObject>();
86
84
        }
87
85
        kids.add(kid);
88
 
        joinHierarchy();
89
 
    }
90
 
 
91
 
    private boolean containsKid(PDFObject kid) {
92
 
        PDFArray kids = getKids();
93
 
        return kids != null && kids.contains(kid);
94
 
    }
95
 
 
96
 
    private void joinHierarchy() {
97
 
        if (parentElement != null && !parentElement.containsKid(this)) {
98
 
            parentElement.addKid(this);
99
 
        }
100
86
    }
101
87
 
102
88
    /**
109
95
     */
110
96
    public void setMCIDKid(int mcid) {
111
97
        put("K", mcid);
112
 
        joinHierarchy();
113
98
    }
114
99
 
115
100
    /**
127
112
     * @return the value of the S entry
128
113
     */
129
114
    public PDFName getStructureType() {
130
 
        return (PDFName)get("S");
 
115
        return (PDFName) get("S");
131
116
    }
132
117
 
133
118
    /**
145
130
     * @param language a value for the Lang entry
146
131
     */
147
132
    public void setLanguage(Locale language) {
148
 
        setLanguage(XMLUtil.toRFC3066(language));
 
133
        setLanguage(LanguageTags.toLanguageTag(language));
149
134
    }
150
135
 
151
136
    /**
154
139
     * @return the value of the Lang entry (<code>null</code> if no language was specified)
155
140
     */
156
141
    public String getLanguage() {
157
 
        return (String)get("Lang");
158
 
    }
 
142
        return (String) get("Lang");
 
143
    }
 
144
 
 
145
    @Override
 
146
    protected void writeDictionary(OutputStream out, StringBuilder textBuffer) throws IOException {
 
147
        attachKids();
 
148
        super.writeDictionary(out, textBuffer);
 
149
    }
 
150
 
 
151
    /**
 
152
     * Attaches all valid kids to the kids array.
 
153
     *
 
154
     * @return true iff 1+ kids were added to the kids array
 
155
     */
 
156
    protected boolean attachKids() {
 
157
        List<PDFObject> validKids = new ArrayList<PDFObject>();
 
158
        if (kids != null) {
 
159
            for (PDFObject kid : kids) {
 
160
                if (kid instanceof Placeholder)  {
 
161
                    if (((Placeholder) kid).attachKids()) {
 
162
                        validKids.add(kid);
 
163
                    }
 
164
                } else {
 
165
                    validKids.add(kid);
 
166
                }
 
167
            }
 
168
        }
 
169
        boolean kidsAttached = !validKids.isEmpty();
 
170
        if (kidsAttached) {
 
171
            PDFArray array = new PDFArray();
 
172
            for (PDFObject ob : validKids) {
 
173
                array.add(ob);
 
174
            }
 
175
            put("K", array);
 
176
        }
 
177
        return kidsAttached;
 
178
    }
 
179
 
 
180
    /**
 
181
     * Class representing a placeholder for a PDF Structure Element.
 
182
     */
 
183
    public static class Placeholder extends PDFStructElem {
 
184
 
 
185
        @Override
 
186
        public void outputInline(OutputStream out, StringBuilder textBuffer) throws IOException {
 
187
            if (kids != null) {
 
188
                assert kids.size() > 0;
 
189
                for (int i = 0; i < kids.size(); i++) {
 
190
                    if (i > 0) {
 
191
                        textBuffer.append(' ');
 
192
                    }
 
193
                    Object obj = kids.get(i);
 
194
                    formatObject(obj, out, textBuffer);
 
195
                }
 
196
            }
 
197
        }
 
198
 
 
199
        /**
 
200
         * Constructor
 
201
         * @param parent -
 
202
         * @param name -
 
203
         */
 
204
        public Placeholder(PDFObject parent, String name) {
 
205
            super(parent, new PDFName(name));
 
206
        }
 
207
    }
 
208
 
159
209
}