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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.render.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.render.afp.Streamable;
  24. /**
  25. * An abstract container of resource objects
  26. */
  27. public abstract class AbstractResourceGroupContainer extends AbstractPageObject
  28. implements Streamable {
  29. /** The container started state */
  30. protected boolean started = false;
  31. /** the resource group object */
  32. protected ResourceGroup resourceGroup = null;
  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. protected 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. public void writeToStream(OutputStream os) throws IOException {
  112. if (!started) {
  113. writeStart(os);
  114. started = true;
  115. }
  116. writeContent(os);
  117. if (complete) {
  118. writeEnd(os);
  119. }
  120. }
  121. /** {@inheritDoc} */
  122. protected void writeObjects(Collection/*<AbstractAFPObject>*/ objects, OutputStream os)
  123. throws IOException {
  124. if (objects != null && objects.size() > 0) {
  125. Iterator it = objects.iterator();
  126. while (it.hasNext()) {
  127. AbstractAFPObject ao = (AbstractAFPObject)it.next();
  128. if (canWrite(ao)) {
  129. ao.writeToStream(os);
  130. it.remove();
  131. } else {
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Returns true if this object can be written
  139. *
  140. * @param obj an AFP object
  141. * @return true if this object can be written
  142. */
  143. protected boolean canWrite(AbstractAFPObject obj) {
  144. return obj instanceof AbstractPageObject && ((AbstractPageObject)obj).isComplete();
  145. }
  146. }