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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.List;
  23. import org.apache.fop.afp.modca.Registry.ObjectType;
  24. import org.apache.fop.afp.modca.triplets.AbstractTriplet;
  25. import org.apache.fop.afp.modca.triplets.CommentTriplet;
  26. import org.apache.fop.afp.modca.triplets.FullyQualifiedNameTriplet;
  27. import org.apache.fop.afp.modca.triplets.ObjectClassificationTriplet;
  28. import org.apache.fop.afp.modca.triplets.Triplet;
  29. /**
  30. * A MODCA structured object base class providing support for Triplets
  31. */
  32. public abstract 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. for (Triplet triplet : triplets) {
  43. dataLength += triplet.getDataLength();
  44. }
  45. return dataLength;
  46. }
  47. /**
  48. * Returns true when this structured field contains triplets
  49. *
  50. * @return true when this structured field contains triplets
  51. */
  52. public boolean hasTriplets() {
  53. return triplets.size() > 0;
  54. }
  55. /**
  56. * Writes any triplet data
  57. *
  58. * @param os The stream to write to
  59. * @throws IOException The stream to write to
  60. */
  61. protected void writeTriplets(OutputStream os) throws IOException {
  62. if (hasTriplets()) {
  63. writeObjects(triplets, os);
  64. triplets = null; // gc
  65. }
  66. }
  67. /**
  68. * Returns the first matching triplet found in the structured field triplet list
  69. *
  70. * @param tripletId the triplet identifier
  71. */
  72. private AbstractTriplet getTriplet(byte tripletId) {
  73. for (AbstractTriplet trip : triplets) {
  74. if (trip.getId() == tripletId) {
  75. return trip;
  76. }
  77. }
  78. return null;
  79. }
  80. /**
  81. * Returns true of this structured field has the given triplet
  82. *
  83. * @param tripletId the triplet identifier
  84. * @return true if the structured field has the given triplet
  85. */
  86. public boolean hasTriplet(byte tripletId) {
  87. return getTriplet(tripletId) != null;
  88. }
  89. /**
  90. * Adds a triplet to this structured object
  91. *
  92. * @param triplet the triplet to add
  93. */
  94. protected void addTriplet(AbstractTriplet triplet) {
  95. triplets.add(triplet);
  96. }
  97. /**
  98. * Adds a list of triplets to the triplets contained within this structured field
  99. *
  100. * @param tripletCollection a collection of triplets
  101. */
  102. public void addTriplets(Collection<AbstractTriplet> tripletCollection) {
  103. if (tripletCollection != null) {
  104. triplets.addAll(tripletCollection);
  105. }
  106. }
  107. /** @return the triplet list pertaining to this resource */
  108. protected List<AbstractTriplet> getTriplets() {
  109. return triplets;
  110. }
  111. /**
  112. * Sets the fully qualified name of this structured field
  113. *
  114. * @param fqnType the fully qualified name type of this resource
  115. * @param fqnFormat the fully qualified name format of this resource
  116. * @param fqName the fully qualified name of this resource
  117. */
  118. public void setFullyQualifiedName(byte fqnType, byte fqnFormat, String fqName) {
  119. addTriplet(new FullyQualifiedNameTriplet(fqnType, fqnFormat, fqName));
  120. }
  121. /** @return the fully qualified name of this triplet or null if it does not exist */
  122. public String getFullyQualifiedName() {
  123. FullyQualifiedNameTriplet fqNameTriplet
  124. = (FullyQualifiedNameTriplet)getTriplet(AbstractTriplet.FULLY_QUALIFIED_NAME);
  125. if (fqNameTriplet != null) {
  126. return fqNameTriplet.getFullyQualifiedName();
  127. }
  128. LOG.warn(this + " has no fully qualified name");
  129. return null;
  130. }
  131. /**
  132. * Sets the objects classification
  133. *
  134. * @param objectClass the classification of the object
  135. * @param objectType the MOD:CA registry object type entry for the given
  136. * object/component type of the object
  137. * @param dataInContainer whether the data resides in the container
  138. * @param containerHasOEG whether the container has an object environment group
  139. * @param dataInOCD whether the data resides in a object container data structured field
  140. */
  141. public void setObjectClassification(
  142. byte objectClass, ObjectType objectType,
  143. boolean dataInContainer, boolean containerHasOEG, boolean dataInOCD) {
  144. addTriplet(
  145. new ObjectClassificationTriplet(
  146. objectClass, objectType, dataInContainer, containerHasOEG, dataInOCD));
  147. }
  148. /**
  149. * Sets a comment on this resource
  150. *
  151. * @param commentString a comment string
  152. */
  153. public void setComment(String commentString) {
  154. addTriplet(new CommentTriplet(AbstractTriplet.COMMENT, commentString));
  155. }
  156. }