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.

InstreamForeignObject.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.fo.flow;
  19. import java.awt.geom.Point2D;
  20. import org.xml.sax.Locator;
  21. import org.apache.xmlgraphics.util.QName;
  22. import org.apache.fop.ResourceEventProducer;
  23. import org.apache.fop.apps.FOPException;
  24. import org.apache.fop.datatypes.Length;
  25. import org.apache.fop.fo.FONode;
  26. import org.apache.fop.fo.ValidationException;
  27. import org.apache.fop.fo.XMLObj;
  28. /**
  29. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_instream-foreign-object">
  30. * <code>fo:instream-foreign-object</code></a> object.
  31. * This is an atomic inline object that contains XML data.
  32. */
  33. public class InstreamForeignObject extends AbstractGraphics {
  34. // The value of properties relevant for fo:instream-foreign-object.
  35. // All property values contained in AbstractGraphics
  36. // End of property values
  37. //Additional value
  38. private Point2D intrinsicDimensions;
  39. private boolean instrisicSizeDetermined;
  40. private Length intrinsicAlignmentAdjust;
  41. /**
  42. * Constructs an instream-foreign-object object
  43. * (called by {@link org.apache.fop.fo.ElementMapping.Maker}).
  44. *
  45. * @param parent the parent {@link FONode}
  46. */
  47. public InstreamForeignObject(FONode parent) {
  48. super(parent);
  49. }
  50. @Override
  51. protected void startOfNode() throws FOPException {
  52. super.startOfNode();
  53. getFOEventHandler().startInstreamForeignObject(this);
  54. }
  55. /**
  56. * Make sure content model satisfied, if so then tell the
  57. * {@link org.apache.fop.fo.FOEventHandler} that we are at
  58. * the end of the instream-foreign-object.
  59. * {@inheritDoc}
  60. */
  61. protected void endOfNode() throws FOPException {
  62. if (firstChild == null) {
  63. missingChildElementError("one (1) non-XSL namespace child");
  64. }
  65. getFOEventHandler().endInstreamForeignObject(this);
  66. }
  67. /**
  68. * {@inheritDoc}
  69. * <br>XSL Content Model: one (1) non-XSL namespace child
  70. */
  71. protected void validateChildNode(Locator loc, String nsURI, String localName)
  72. throws ValidationException {
  73. if (FO_URI.equals(nsURI)) {
  74. invalidChildError(loc, nsURI, localName);
  75. } else if (firstChild != null) {
  76. tooManyNodesError(loc, new QName(nsURI, null, localName));
  77. }
  78. }
  79. /** {@inheritDoc} */
  80. public String getLocalName() {
  81. return "instream-foreign-object";
  82. }
  83. /**
  84. * {@inheritDoc}
  85. * @return {@link org.apache.fop.fo.Constants#FO_INSTREAM_FOREIGN_OBJECT}
  86. */
  87. public int getNameId() {
  88. return FO_INSTREAM_FOREIGN_OBJECT;
  89. }
  90. /** Preloads the image so the intrinsic size is available. */
  91. private void prepareIntrinsicSize() {
  92. if (!this.instrisicSizeDetermined) {
  93. XMLObj child = (XMLObj) firstChild;
  94. Point2D csize = new Point2D.Float(-1, -1);
  95. intrinsicDimensions = child.getDimension(csize);
  96. if (intrinsicDimensions == null) {
  97. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  98. getUserAgent().getEventBroadcaster());
  99. eventProducer.ifoNoIntrinsicSize(this, getLocator());
  100. }
  101. intrinsicAlignmentAdjust = child.getIntrinsicAlignmentAdjust();
  102. this.instrisicSizeDetermined = true;
  103. }
  104. }
  105. /** {@inheritDoc} */
  106. public int getIntrinsicWidth() {
  107. prepareIntrinsicSize();
  108. if (intrinsicDimensions != null) {
  109. return (int)(intrinsicDimensions.getX() * 1000);
  110. } else {
  111. return 0;
  112. }
  113. }
  114. /** {@inheritDoc} */
  115. public int getIntrinsicHeight() {
  116. prepareIntrinsicSize();
  117. if (intrinsicDimensions != null) {
  118. return (int)(intrinsicDimensions.getY() * 1000);
  119. } else {
  120. return 0;
  121. }
  122. }
  123. /** {@inheritDoc} */
  124. public Length getIntrinsicAlignmentAdjust() {
  125. prepareIntrinsicSize();
  126. return intrinsicAlignmentAdjust;
  127. }
  128. /** {@inheritDoc} */
  129. protected void addChildNode(FONode child) throws FOPException {
  130. super.addChildNode(child);
  131. }
  132. /** @return the {@link XMLObj} child node of the instream-foreign-object. */
  133. public XMLObj getChildXMLObj() {
  134. return (XMLObj) firstChild;
  135. }
  136. }