aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/intermediate/extensions/Bookmark.java
blob: 446da6ef9a42856d635d5289c4a64feca57debbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.render.intermediate.extensions;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import org.apache.xmlgraphics.util.XMLizable;

import org.apache.fop.util.XMLUtil;

/**
 * This class is a bookmark element for use in the intermediate format.
 */
public class Bookmark implements XMLizable, DocumentNavigationExtensionConstants {

    private String title;
    private boolean show;
    private List childBookmarks;
    private AbstractAction action;

    /**
     * Creates a new bookmark.
     * @param title the bookmark's title
     * @param show true if the bookmark shall be shown, false for hidden
     * @param action the action performed when the bookmark is clicked
     */
    public Bookmark(String title, boolean show, AbstractAction action) {
        this.title = title;
        this.show = show;
        this.action = action;
    }

    /**
     * Returns the bookmark's title.
     * @return the title
     */
    public String getTitle() {
        return this.title;
    }

    /**
     * Indicates whether the bookmark shall be shown initially.
     * @return true if it shall be shown
     */
    public boolean isShown() {
        return this.show;
    }

    /**
     * Returns the action performed when the bookmark is clicked.
     * @return the action
     */
    public AbstractAction getAction() {
        return this.action;
    }

    /**
     * Sets the action performed when the bookmark is clicked.
     * @param action the action
     */
    public void setAction(AbstractAction action) {
        this.action = action;
    }

    /**
     * Adds a child bookmark.
     * @param bookmark the child bookmark
     */
    public void addChildBookmark(Bookmark bookmark) {
        if (this.childBookmarks == null) {
            this.childBookmarks = new java.util.ArrayList();
        }
        this.childBookmarks.add(bookmark);
    }

    /**
     * Returns a list of child bookmarks.
     * @return the child bookmarks
     */
    public List getChildBookmarks() {
        if (this.childBookmarks == null) {
            return Collections.EMPTY_LIST;
        } else {
            return Collections.unmodifiableList(this.childBookmarks);
        }
    }

    /** {@inheritDoc} */
    public void toSAX(ContentHandler handler) throws SAXException {
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute(null, "title", "title", XMLUtil.CDATA, getTitle());
        atts.addAttribute(null, "starting-state", "starting-state",
                XMLUtil.CDATA, isShown() ? "show" : "hide");
        handler.startElement(BOOKMARK.getNamespaceURI(),
                BOOKMARK.getLocalName(), BOOKMARK.getQName(), atts);
        if (getAction() != null) {
            getAction().toSAX(handler);
        }
        if (this.childBookmarks != null) {
            Iterator iter = this.childBookmarks.iterator();
            while (iter.hasNext()) {
                Bookmark b = (Bookmark)iter.next();
                b.toSAX(handler);
            }
        }
        handler.endElement(BOOKMARK.getNamespaceURI(),
                BOOKMARK.getLocalName(), BOOKMARK.getQName());
    }

}