You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractTripletStructuredObject.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.afp.modca;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.util.Collection;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import org.apache.fop.afp.modca.Registry.ObjectType;
  25. import org.apache.fop.afp.modca.triplets.AbstractTriplet;
  26. import org.apache.fop.afp.modca.triplets.CommentTriplet;
  27. import org.apache.fop.afp.modca.triplets.FullyQualifiedNameTriplet;
  28. import org.apache.fop.afp.modca.triplets.ObjectClassificationTriplet;
  29. /**
  30. * A MODCA structured object base class providing support for Triplets
  31. */
  32. public class AbstractTripletStructuredObject extends AbstractStructuredObject {
  33. /** list of object triplets */
  34. protected List/*<AbstractTriplet>*/ triplets = new java.util.ArrayList/*<AbstractTriplet>*/();
  35. /**
  36. * Returns the triplet data length
  37. *
  38. * @return the triplet data length
  39. */
  40. protected int getTripletDataLength() {
  41. int dataLength = 0;
  42. if (hasTriplets()) {
  43. Iterator it = triplets.iterator();
  44. while (it.hasNext()) {
  45. AbstractTriplet triplet = (AbstractTriplet)it.next();
  46. dataLength += triplet.getDataLength();
  47. }
  48. }
  49. return dataLength;
  50. }
  51. /**
  52. * Returns true when this structured field contains triplets
  53. *
  54. * @return true when this structured field contains triplets
  55. */
  56. public boolean hasTriplets() {
  57. return triplets.size() > 0;
  58. }
  59. /**
  60. * Writes any triplet data
  61. *
  62. * @param os The stream to write to
  63. * @throws IOException The stream to write to
  64. */
  65. protected void writeTriplets(OutputStream os) throws IOException {
  66. if (hasTriplets()) {
  67. writeObjects(triplets, os);
  68. triplets = null; // gc
  69. }
  70. }
  71. /**
  72. * Returns the first matching triplet found in the structured field triplet list
  73. *
  74. * @param tripletId the triplet identifier
  75. */
  76. private AbstractTriplet getTriplet(byte tripletId) {
  77. Iterator it = getTriplets().iterator();
  78. while (it.hasNext()) {
  79. AbstractTriplet triplet = (AbstractTriplet)it.next();
  80. if (triplet.getId() == tripletId) {
  81. return triplet;
  82. }
  83. }
  84. return null;
  85. }
  86. /**
  87. * Returns true of this structured field has the given triplet
  88. *
  89. * @param tripletId the triplet identifier
  90. * @return true if the structured field has the given triplet
  91. */
  92. public boolean hasTriplet(byte tripletId) {
  93. return getTriplet(tripletId) != null;
  94. }
  95. /**
  96. * Adds a triplet to this structured object
  97. *
  98. * @param triplet the triplet to add
  99. */
  100. protected void addTriplet(AbstractTriplet triplet) {
  101. triplets.add(triplet);
  102. }
  103. /**
  104. * Adds a list of triplets to the triplets contained within this structured field
  105. *
  106. * @param tripletCollection a collection of triplets
  107. */
  108. public void addTriplets(Collection/*<Triplet>*/ tripletCollection) {
  109. if (tripletCollection != null) {
  110. triplets.addAll(tripletCollection);
  111. }
  112. }
  113. /** @return the triplet list pertaining to this resource */
  114. protected List/*<Triplet>*/ getTriplets() {
  115. return triplets;
  116. }
  117. /**
  118. * Sets the fully qualified name of this resource
  119. *
  120. * @param fqnType the fully qualified name type of this resource
  121. * @param fqnFormat the fully qualified name format of this resource
  122. * @param fqName the fully qualified name of this resource
  123. */
  124. public void setFullyQualifiedName(byte fqnType, byte fqnFormat, String fqName) {
  125. addTriplet(new FullyQualifiedNameTriplet(fqnType, fqnFormat, fqName));
  126. }
  127. /** @return the fully qualified name of this triplet or null if it does not exist */
  128. public String getFullyQualifiedName() {
  129. FullyQualifiedNameTriplet fqNameTriplet
  130. = (FullyQualifiedNameTriplet)getTriplet(AbstractTriplet.FULLY_QUALIFIED_NAME);
  131. if (fqNameTriplet != null) {
  132. return fqNameTriplet.getFullyQualifiedName();
  133. }
  134. log.warn(this + " has no fully qualified name");
  135. return null;
  136. }
  137. /**
  138. * Sets the objects classification
  139. *
  140. * @param objectClass the classification of the object
  141. * @param objectType the MOD:CA registry object type entry for the given
  142. * object/component type of the object
  143. * @param dataInContainer whether the data resides in the container
  144. * @param containerHasOEG whether the container has an object environment group
  145. * @param dataInOCD whether the data resides in a object container data structured field
  146. */
  147. public void setObjectClassification(
  148. byte objectClass, ObjectType objectType,
  149. boolean dataInContainer, boolean containerHasOEG, boolean dataInOCD) {
  150. addTriplet(
  151. new ObjectClassificationTriplet(
  152. objectClass, objectType, dataInContainer, containerHasOEG, dataInOCD));
  153. }
  154. /**
  155. * Sets a comment on this resource
  156. *
  157. * @param commentString a comment string
  158. */
  159. public void setComment(String commentString) {
  160. addTriplet(new CommentTriplet(AbstractTriplet.COMMENT, commentString));
  161. }
  162. }