aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/afp/modca/MapPageSegment.java
blob: 8bb7afe46fc058764ac90a758bd88565609b2640 (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
/*
 * 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.afp.modca;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Set;

import org.apache.fop.afp.AFPConstants;
import org.apache.fop.afp.util.BinaryUtils;

/**
 * The Map Page Segment structured field identifies page segments that are required to present
 * a page on a physical medium.
 */
public class MapPageSegment extends AbstractAFPObject {

    private static final int MAX_SIZE = 127;

    /**
     * The collection of page segments (maximum of 127 stored as String)
     */
    private Set pageSegments = null;

    /**
     * Constructor for the Map Page Overlay
     */
    public MapPageSegment() {
    }

    private Set getPageSegments() {
        if (pageSegments == null) {
            this.pageSegments = new java.util.HashSet();
        }
        return this.pageSegments;
    }

    /**
     * Add a page segment to to the map page segment object.
     * @param name the name of the page segment.
     * @throws MaximumSizeExceededException if the maximum size is reached
     */
    public void addPageSegment(String name) throws MaximumSizeExceededException {
        if (getPageSegments().size() > MAX_SIZE) {
            throw new MaximumSizeExceededException();
        }
        if (name.length() > 8) {
            throw new IllegalArgumentException("The name of page segment " + name
                + " must not be longer than 8 characters");
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("addPageSegment():: adding page segment " + name);
        }
        getPageSegments().add(name);
    }

    /**
     * Indicates whether this object already contains the maximum number of
     * page segments.
     * @return true if the object is full
     */
    public boolean isFull() {
        return this.pageSegments.size() >= MAX_SIZE;
    }

    /** {@inheritDoc} */
    public void writeToStream(OutputStream os) throws IOException {
        int count = getPageSegments().size();
        byte groupLength = 0x0C;
        int groupsLength = count * groupLength;

        byte[] data = new byte[groupsLength + 12 + 1];

        data[0] = 0x5A;

        // Set the total record length
        byte[] rl1 = BinaryUtils.convert(data.length - 1, 2); //Ignore the
        // first byte in
        // the length
        data[1] = rl1[0];
        data[2] = rl1[1];

        // Structured field ID for a MPS
        data[3] = (byte) 0xD3;
        data[4] = Type.MIGRATION;
        data[5] = Category.PAGE_SEGMENT;

        data[6] = 0x00; // Flags
        data[7] = 0x00; // Reserved
        data[8] = 0x00; // Reserved

        data[9] = groupLength;
        data[10] = 0x00; // Reserved
        data[11] = 0x00; // Reserved
        data[12] = 0x00; // Reserved

        int pos = 13;

        Iterator iter = this.pageSegments.iterator();
        while (iter.hasNext()) {
            pos += 4;

            String name = (String)iter.next();
            try {
                byte[] nameBytes = name.getBytes(AFPConstants.EBCIDIC_ENCODING);
                System.arraycopy(nameBytes, 0, data, pos, nameBytes.length);
            } catch (UnsupportedEncodingException usee) {
                LOG.error("UnsupportedEncodingException translating the name "
                    + name);
            }
            pos += 8;
        }
        os.write(data);
    }
}