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.

AbstractResourceGroupContainer.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 org.apache.fop.afp.Completable;
  24. import org.apache.fop.afp.Factory;
  25. import org.apache.fop.afp.Streamable;
  26. /**
  27. * An abstract container of resource objects
  28. */
  29. public abstract class AbstractResourceGroupContainer extends AbstractPageObject
  30. implements Streamable {
  31. /** The container started state */
  32. protected boolean started;
  33. /** the resource group object */
  34. protected ResourceGroup resourceGroup;
  35. /**
  36. * Default constructor
  37. *
  38. * @param factory the object factory
  39. */
  40. public AbstractResourceGroupContainer(Factory factory) {
  41. super(factory);
  42. }
  43. /**
  44. * Named constructor
  45. *
  46. * @param factory the object factory
  47. * @param name the name of this resource container
  48. */
  49. public AbstractResourceGroupContainer(Factory factory, String name) {
  50. super(factory, name);
  51. }
  52. /**
  53. * Construct a new page object for the specified name argument, the page
  54. * name should be an 8 character identifier.
  55. *
  56. * @param factory
  57. * the object factory
  58. * @param name
  59. * the name of the page.
  60. * @param width
  61. * the width of the page.
  62. * @param height
  63. * the height of the page.
  64. * @param rotation
  65. * the rotation of the page.
  66. * @param widthRes
  67. * the width resolution of the page.
  68. * @param heightRes
  69. * the height resolution of the page.
  70. */
  71. public AbstractResourceGroupContainer(Factory factory,
  72. String name, int width, int height, int rotation, int widthRes, int heightRes) {
  73. super(factory, name, width, height, rotation, widthRes, heightRes);
  74. }
  75. /**
  76. * Return the number of resources in this container
  77. *
  78. * @return the number of resources in this container
  79. */
  80. protected int getResourceCount() {
  81. if (resourceGroup != null) {
  82. return resourceGroup.getResourceCount();
  83. }
  84. return 0;
  85. }
  86. /**
  87. * Returns true if this resource group container contains resources
  88. *
  89. * @return true if this resource group container contains resources
  90. */
  91. protected boolean hasResources() {
  92. return resourceGroup != null && resourceGroup.getResourceCount() > 0;
  93. }
  94. /**
  95. * Returns the resource group in this resource group container
  96. *
  97. * @return the resource group in this resource group container
  98. */
  99. public ResourceGroup getResourceGroup() {
  100. if (resourceGroup == null) {
  101. resourceGroup = factory.createResourceGroup();
  102. }
  103. return resourceGroup;
  104. }
  105. // /** {@inheritDoc} */
  106. // protected void writeContent(OutputStream os) throws IOException {
  107. // if (resourceGroup != null) {
  108. // resourceGroup.writeToStream(os);
  109. // }
  110. // super.writeContent(os);
  111. // }
  112. /** {@inheritDoc} */
  113. @Override
  114. public void writeToStream(OutputStream os) throws IOException {
  115. if (!started) {
  116. writeStart(os);
  117. started = true;
  118. }
  119. writeContent(os);
  120. if (complete) {
  121. writeEnd(os);
  122. }
  123. }
  124. /** {@inheritDoc} */
  125. @Override
  126. protected void writeObjects(Collection/*<AbstractAFPObject>*/ objects, OutputStream os)
  127. throws IOException {
  128. writeObjects(objects, os, false);
  129. }
  130. /**
  131. * Writes a collection of {@link AbstractAFPObject}s to the AFP Datastream.
  132. *
  133. * @param objects a list of AFPObjects
  134. * @param os The stream to write to
  135. * @param forceWrite true if writing should happen in any case
  136. * @throws java.io.IOException an I/O exception of some sort has occurred.
  137. */
  138. protected void writeObjects(Collection/*<AbstractAFPObject>*/ objects, OutputStream os,
  139. boolean forceWrite) throws IOException {
  140. if (objects != null && objects.size() > 0) {
  141. Iterator it = objects.iterator();
  142. while (it.hasNext()) {
  143. AbstractAFPObject ao = (AbstractAFPObject)it.next();
  144. if (forceWrite || canWrite(ao)) {
  145. ao.writeToStream(os);
  146. it.remove();
  147. } else {
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. /**
  154. * Returns true if this object can be written
  155. *
  156. * @param obj an AFP object
  157. * @return true if this object can be written
  158. */
  159. protected boolean canWrite(AbstractAFPObject obj) {
  160. if (obj instanceof Completable) {
  161. return ((Completable)obj).isComplete();
  162. } else {
  163. return this.isComplete();
  164. }
  165. }
  166. }