aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/afp/modca/AbstractTripletStructuredObject.java
blob: a14af29678cd7d5a4d96b9338f4d1baef3c2f4e8 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * 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.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.fop.afp.modca.Registry.ObjectType;
import org.apache.fop.afp.modca.triplets.AbstractTriplet;
import org.apache.fop.afp.modca.triplets.CommentTriplet;
import org.apache.fop.afp.modca.triplets.FullyQualifiedNameTriplet;
import org.apache.fop.afp.modca.triplets.ObjectClassificationTriplet;

/**
 * A MODCA structured object base class providing support for Triplets
 */
public class AbstractTripletStructuredObject extends AbstractStructuredObject {

    /** list of object triplets */
    protected List/*<AbstractTriplet>*/ triplets = new java.util.ArrayList/*<AbstractTriplet>*/();

    /**
     * Returns the triplet data length
     *
     * @return the triplet data length
     */
    protected int getTripletDataLength() {
        int dataLength = 0;
        if (hasTriplets()) {
            Iterator it = triplets.iterator();
            while (it.hasNext()) {
                AbstractTriplet triplet = (AbstractTriplet)it.next();
                dataLength += triplet.getDataLength();
            }
        }
        return dataLength;
    }

    /**
     * Returns true when this structured field contains triplets
     *
     * @return true when this structured field contains triplets
     */
    public boolean hasTriplets() {
        return triplets.size() > 0;
    }

    /**
     * Writes any triplet data
     *
     * @param os The stream to write to
     * @throws IOException The stream to write to
     */
    protected void writeTriplets(OutputStream os) throws IOException {
        if (hasTriplets()) {
            writeObjects(triplets, os);
            triplets = null; // gc
        }
    }

    /**
     * Returns the first matching triplet found in the structured field triplet list
     *
     * @param tripletId the triplet identifier
     */
    private AbstractTriplet getTriplet(byte tripletId) {
        Iterator it = getTriplets().iterator();
        while (it.hasNext()) {
            AbstractTriplet triplet = (AbstractTriplet)it.next();
            if (triplet.getId() == tripletId) {
                return triplet;
            }
        }
        return null;
    }

    /**
     * Returns true of this structured field has the given triplet
     *
     * @param tripletId the triplet identifier
     * @return true if the structured field has the given triplet
     */
    public boolean hasTriplet(byte tripletId) {
        return getTriplet(tripletId) != null;
    }

    /**
     * Adds a triplet to this structured object
     *
     * @param triplet the triplet to add
     */
    protected void addTriplet(AbstractTriplet triplet) {
        triplets.add(triplet);
    }

    /**
     * Adds a list of triplets to the triplets contained within this structured field
     *
     * @param tripletCollection a collection of triplets
     */
    public void addTriplets(Collection/*<Triplet>*/ tripletCollection) {
        if (tripletCollection != null) {
            triplets.addAll(tripletCollection);
        }
    }

    /** @return the triplet list pertaining to this resource */
    protected List/*<Triplet>*/ getTriplets() {
        return triplets;
    }

    /**
     * Sets the fully qualified name of this resource
     *
     * @param fqnType the fully qualified name type of this resource
     * @param fqnFormat the fully qualified name format of this resource
     * @param fqName the fully qualified name of this resource
     */
    public void setFullyQualifiedName(byte fqnType, byte fqnFormat, String fqName) {
        addTriplet(new FullyQualifiedNameTriplet(fqnType, fqnFormat, fqName));
    }

    /** @return the fully qualified name of this triplet or null if it does not exist */
    public String getFullyQualifiedName() {
        FullyQualifiedNameTriplet fqNameTriplet
            = (FullyQualifiedNameTriplet)getTriplet(AbstractTriplet.FULLY_QUALIFIED_NAME);
        if (fqNameTriplet != null) {
            return fqNameTriplet.getFullyQualifiedName();
        }
        log.warn(this + " has no fully qualified name");
        return null;
    }

    /**
     * Sets the objects classification
     *
     * @param objectClass the classification of the object
     * @param objectType the MOD:CA registry object type entry for the given
     *        object/component type of the object
     * @param dataInContainer whether the data resides in the container
     * @param containerHasOEG whether the container has an object environment group
     * @param dataInOCD whether the data resides in a object container data structured field
     */
    public void setObjectClassification(
            byte objectClass, ObjectType objectType,
            boolean dataInContainer, boolean containerHasOEG, boolean dataInOCD) {
        addTriplet(
                new ObjectClassificationTriplet(
                        objectClass, objectType, dataInContainer, containerHasOEG, dataInOCD));
    }

    /**
     * Sets a comment on this resource
     *
     * @param commentString a comment string
     */
    public void setComment(String commentString) {
        addTriplet(new CommentTriplet(AbstractTriplet.COMMENT, commentString));
    }

}