aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/fo/pagination/LayoutMasterSet.java
blob: 1c5b85118eaff096f229f624c81f5bff451bf737 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

package org.apache.fop.fo.pagination;

// FOP
import org.apache.fop.fo.*;
import org.apache.fop.fo.properties.*;
import org.apache.fop.apps.FOPException;
import org.apache.fop.layout.PageMaster;

// Java
import java.util.*;

public class LayoutMasterSet extends FObj {

    public static class Maker extends FObj.Maker {
        public FObj make(FObj parent,
                         PropertyList propertyList) throws FOPException {
            return new LayoutMasterSet(parent, propertyList);
        }

    }

    public static FObj.Maker maker() {
        return new LayoutMasterSet.Maker();
    }

    private Hashtable simplePageMasters;
    private Hashtable pageSequenceMasters;
    private Hashtable allRegions;

    private Root root;

    protected LayoutMasterSet(FObj parent,
                              PropertyList propertyList) throws FOPException {
        super(parent, propertyList);
        this.name = "fo:layout-master-set";

        this.simplePageMasters = new Hashtable();
        this.pageSequenceMasters = new Hashtable();

        if (parent.getName().equals("fo:root")) {
            this.root = (Root)parent;
            root.setLayoutMasterSet(this);
        } else {
            throw new FOPException("fo:layout-master-set must be child of fo:root, not "
                                   + parent.getName());
        }
        allRegions = new Hashtable();

    }

    protected void addSimplePageMaster(SimplePageMaster simplePageMaster)
            throws FOPException {
        // check against duplication of master-name
        if (existsName(simplePageMaster.getMasterName()))
            throw new FOPException("'master-name' ("
                                   + simplePageMaster.getMasterName()
                                   + ") must be unique "
                                   + "across page-masters and page-sequence-masters");
        this.simplePageMasters.put(simplePageMaster.getMasterName(),
                                   simplePageMaster);
    }

    protected SimplePageMaster getSimplePageMaster(String masterName) {
        return (SimplePageMaster)this.simplePageMasters.get(masterName);
    }

    protected void addPageSequenceMaster(String masterName, PageSequenceMaster pageSequenceMaster)
            throws FOPException {
        // check against duplication of master-name
        if (existsName(masterName))
            throw new FOPException("'master-name' (" + masterName
                                   + ") must be unique "
                                   + "across page-masters and page-sequence-masters");
        this.pageSequenceMasters.put(masterName, pageSequenceMaster);
    }

    protected PageSequenceMaster getPageSequenceMaster(String masterName) {
        return (PageSequenceMaster)this.pageSequenceMasters.get(masterName);
    }

    private boolean existsName(String masterName) {
        if (simplePageMasters.containsKey(masterName)
                || pageSequenceMasters.containsKey(masterName))
            return true;
        else
            return false;
    }

    protected void resetPageMasters() {
        for (Enumeration e = pageSequenceMasters.elements();
                e.hasMoreElements(); ) {
            ((PageSequenceMaster)e.nextElement()).reset();
        }

    }

    protected void checkRegionNames() throws FOPException {
        // Section 7.33.15 check to see that if a region-name is a
        // duplicate, that it maps to the same region-class.
        for (Enumeration spm = simplePageMasters.elements();
                spm.hasMoreElements(); ) {
            SimplePageMaster simplePageMaster =
                (SimplePageMaster)spm.nextElement();
            Hashtable spmRegions = simplePageMaster.getRegions();
            for (Enumeration e = spmRegions.elements();
                    e.hasMoreElements(); ) {
                Region region = (Region)e.nextElement();
                if (allRegions.containsKey(region.getRegionName())) {
                    String localClass =
                        (String)allRegions.get(region.getRegionName());
                    if (!localClass.equals(region.getRegionClass())) {
                        throw new FOPException("Duplicate region-names ("
                                               + region.getRegionName()
                                               + ") must map "
                                               + "to the same region-class ("
                                               + localClass + "!="
                                               + region.getRegionClass()
                                               + ")");
                    }
                }
                allRegions.put(region.getRegionName(),
                               region.getRegionClass());
            }
        }
    }

    /**
     * Checks whether or not a region name exists in this master set
     * @returns true when the region name specified has a region in this LayoutMasterSet
     */
    protected boolean regionNameExists(String regionName) {
        boolean result = false;
        for (Enumeration e = simplePageMasters.elements();
                e.hasMoreElements(); ) {
            result =
                ((SimplePageMaster)e.nextElement()).regionNameExists(regionName);
            if (result) {
                return result;
            }
        }
        return result;
    }


}