001: /*
002: * $Id: PdfStructureTreeRoot.java 2366 2006-09-14 23:10:58Z xlv $
003: *
004: * Copyright 2005 by Paulo Soares.
005: *
006: * The contents of this file are subject to the Mozilla Public License Version 1.1
007: * (the "License"); you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the License.
013: *
014: * The Original Code is 'iText, a free JAVA-PDF library'.
015: *
016: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
017: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
018: * All Rights Reserved.
019: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
020: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
021: *
022: * Contributor(s): all the names of the contributors are added in the source code
023: * where applicable.
024: *
025: * Alternatively, the contents of this file may be used under the terms of the
026: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
027: * provisions of LGPL are applicable instead of those above. If you wish to
028: * allow use of your version of this file only under the terms of the LGPL
029: * License and not to allow others to use your version of this file under
030: * the MPL, indicate your decision by deleting the provisions above and
031: * replace them with the notice and other provisions required by the LGPL.
032: * If you do not delete the provisions above, a recipient may use your version
033: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
034: *
035: * This library is free software; you can redistribute it and/or modify it
036: * under the terms of the MPL as stated above or under the terms of the GNU
037: * Library General Public License as published by the Free Software Foundation;
038: * either version 2 of the License, or any later version.
039: *
040: * This library is distributed in the hope that it will be useful, but WITHOUT
041: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
042: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
043: * details.
044: *
045: * If you didn't download this code from the following link, you should check if
046: * you aren't using an obsolete version:
047: * http://www.lowagie.com/iText/
048: */
049: package com.lowagie.text.pdf;
050:
051: import java.io.IOException;
052: import java.util.ArrayList;
053: import java.util.HashMap;
054: import java.util.Iterator;
055:
056: /**
057: * The structure tree root corresponds to the highest hierarchy level in a tagged PDF.
058: * @author Paulo Soares (psoares@consiste.pt)
059: */
060: public class PdfStructureTreeRoot extends PdfDictionary {
061:
062: private HashMap parentTree = new HashMap();
063: private PdfIndirectReference reference;
064:
065: /**
066: * Holds value of property writer.
067: */
068: private PdfWriter writer;
069:
070: /** Creates a new instance of PdfStructureTreeRoot */
071: PdfStructureTreeRoot(PdfWriter writer) {
072: super (PdfName.STRUCTTREEROOT);
073: this .writer = writer;
074: reference = writer.getPdfIndirectReference();
075: }
076:
077: /**
078: * Maps the user tags to the standard tags. The mapping will allow a standard application to make some sense of the tagged
079: * document whatever the user tags may be.
080: * @param used the user tag
081: * @param standard the standard tag
082: */
083: public void mapRole(PdfName used, PdfName standard) {
084: PdfDictionary rm = (PdfDictionary) get(PdfName.ROLEMAP);
085: if (rm == null) {
086: rm = new PdfDictionary();
087: put(PdfName.ROLEMAP, rm);
088: }
089: rm.put(used, standard);
090: }
091:
092: /**
093: * Gets the writer.
094: * @return the writer
095: */
096: public PdfWriter getWriter() {
097: return this .writer;
098: }
099:
100: /**
101: * Gets the reference this object will be written to.
102: * @return the reference this object will be written to
103: */
104: public PdfIndirectReference getReference() {
105: return this .reference;
106: }
107:
108: void setPageMark(int page, PdfIndirectReference struc) {
109: Integer i = new Integer(page);
110: PdfArray ar = (PdfArray) parentTree.get(i);
111: if (ar == null) {
112: ar = new PdfArray();
113: parentTree.put(i, ar);
114: }
115: ar.add(struc);
116: }
117:
118: private void nodeProcess(PdfDictionary struc,
119: PdfIndirectReference reference) throws IOException {
120: PdfObject obj = struc.get(PdfName.K);
121: if (obj != null
122: && obj.isArray()
123: && !((PdfObject) ((PdfArray) obj).getArrayList().get(0))
124: .isNumber()) {
125: PdfArray ar = (PdfArray) obj;
126: ArrayList a = ar.getArrayList();
127: for (int k = 0; k < a.size(); ++k) {
128: PdfStructureElement e = (PdfStructureElement) a.get(k);
129: a.set(k, e.getReference());
130: nodeProcess(e, e.getReference());
131: }
132: }
133: if (reference != null)
134: writer.addToBody(struc, reference);
135: }
136:
137: void buildTree() throws IOException {
138: HashMap numTree = new HashMap();
139: for (Iterator it = parentTree.keySet().iterator(); it.hasNext();) {
140: Integer i = (Integer) it.next();
141: PdfArray ar = (PdfArray) parentTree.get(i);
142: numTree.put(i, writer.addToBody(ar).getIndirectReference());
143: }
144: PdfDictionary dicTree = PdfNumberTree
145: .writeTree(numTree, writer);
146: if (dicTree != null)
147: put(PdfName.PARENTTREE, writer.addToBody(dicTree)
148: .getIndirectReference());
149:
150: nodeProcess(this, reference);
151: }
152: }
|