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.

ExternalGraphic.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.io.FileNotFoundException;
  20. import java.io.IOException;
  21. import org.xml.sax.Locator;
  22. import org.apache.xmlgraphics.image.loader.ImageException;
  23. import org.apache.xmlgraphics.image.loader.ImageInfo;
  24. import org.apache.xmlgraphics.image.loader.ImageManager;
  25. import org.apache.fop.ResourceEventProducer;
  26. import org.apache.fop.apps.FOPException;
  27. import org.apache.fop.apps.FOUserAgent;
  28. import org.apache.fop.datatypes.Length;
  29. import org.apache.fop.datatypes.URISpecification;
  30. import org.apache.fop.fo.FONode;
  31. import org.apache.fop.fo.PropertyList;
  32. import org.apache.fop.fo.ValidationException;
  33. import org.apache.fop.fo.properties.FixedLength;
  34. /**
  35. * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_external-graphic">
  36. * <code>fo:external-graphic</code></a> object.
  37. * This FO node handles the external graphic. It creates an image
  38. * inline area that can be added to the area tree.
  39. */
  40. public class ExternalGraphic extends AbstractGraphics {
  41. // The value of properties relevant for fo:external-graphic.
  42. // All but one of the e-g properties are kept in AbstractGraphics
  43. private String src;
  44. // End of property values
  45. //Additional values
  46. private String url;
  47. private int intrinsicWidth;
  48. private int intrinsicHeight;
  49. private Length intrinsicAlignmentAdjust;
  50. /**
  51. * Create a new ExternalGraphic node that is a child
  52. * of the given {@link FONode}.
  53. *
  54. * @param parent the parent of this node
  55. */
  56. public ExternalGraphic(FONode parent) {
  57. super(parent);
  58. }
  59. /** {@inheritDoc} */
  60. public void bind(PropertyList pList) throws FOPException {
  61. super.bind(pList);
  62. src = pList.get(PR_SRC).getString();
  63. //Additional processing: obtain the image's intrinsic size and baseline information
  64. url = URISpecification.getURL(src);
  65. FOUserAgent userAgent = getUserAgent();
  66. ImageManager manager = userAgent.getFactory().getImageManager();
  67. ImageInfo info = null;
  68. try {
  69. info = manager.getImageInfo(url, userAgent.getImageSessionContext());
  70. } catch (ImageException e) {
  71. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  72. getUserAgent().getEventBroadcaster());
  73. eventProducer.imageError(this, url, e, getLocator());
  74. } catch (FileNotFoundException fnfe) {
  75. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  76. getUserAgent().getEventBroadcaster());
  77. eventProducer.imageNotFound(this, url, fnfe, getLocator());
  78. } catch (IOException ioe) {
  79. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  80. getUserAgent().getEventBroadcaster());
  81. eventProducer.imageIOError(this, url, ioe, getLocator());
  82. }
  83. if (info != null) {
  84. this.intrinsicWidth = info.getSize().getWidthMpt();
  85. this.intrinsicHeight = info.getSize().getHeightMpt();
  86. int baseline = info.getSize().getBaselinePositionFromBottom();
  87. if (baseline != 0) {
  88. this.intrinsicAlignmentAdjust
  89. = FixedLength.getInstance(-baseline);
  90. }
  91. }
  92. }
  93. /** {@inheritDoc} */
  94. protected void startOfNode() throws FOPException {
  95. super.startOfNode();
  96. getFOEventHandler().image(this);
  97. }
  98. /**
  99. * {@inheritDoc}
  100. * <br>XSL Content Model: empty
  101. */
  102. protected void validateChildNode(Locator loc, String nsURI, String localName)
  103. throws ValidationException {
  104. if (FO_URI.equals(nsURI)) {
  105. invalidChildError(loc, nsURI, localName);
  106. }
  107. }
  108. /** @return the "src" property */
  109. public String getSrc() {
  110. return src;
  111. }
  112. /** @return Get the resulting URL based on the src property */
  113. public String getURL() {
  114. return url;
  115. }
  116. /** {@inheritDoc} */
  117. public String getLocalName() {
  118. return "external-graphic";
  119. }
  120. /**
  121. * {@inheritDoc}
  122. * @return {@link org.apache.fop.fo.Constants#FO_EXTERNAL_GRAPHIC}
  123. */
  124. public int getNameId() {
  125. return FO_EXTERNAL_GRAPHIC;
  126. }
  127. /** {@inheritDoc} */
  128. public int getIntrinsicWidth() {
  129. return this.intrinsicWidth;
  130. }
  131. /** {@inheritDoc} */
  132. public int getIntrinsicHeight() {
  133. return this.intrinsicHeight;
  134. }
  135. /** {@inheritDoc} */
  136. public Length getIntrinsicAlignmentAdjust() {
  137. return this.intrinsicAlignmentAdjust;
  138. }
  139. }