Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

AbstractResourceGroupContainer.java 5.4KB

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