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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.apps.FOPException;
  26. import org.apache.fop.apps.FOUserAgent;
  27. import org.apache.fop.datatypes.Length;
  28. import org.apache.fop.datatypes.URISpecification;
  29. import org.apache.fop.events.ResourceEventProducer;
  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 External graphic node.
  52. *
  53. * @param parent the parent of this node
  54. */
  55. public ExternalGraphic(FONode parent) {
  56. super(parent);
  57. }
  58. /** {@inheritDoc} */
  59. public void bind(PropertyList pList) throws FOPException {
  60. super.bind(pList);
  61. src = pList.get(PR_SRC).getString();
  62. //Additional processing: obtain the image's intrinsic size and baseline information
  63. url = URISpecification.getURL(src);
  64. FOUserAgent userAgent = getUserAgent();
  65. ImageManager manager = userAgent.getFactory().getImageManager();
  66. ImageInfo info = null;
  67. try {
  68. info = manager.getImageInfo(url, userAgent.getImageSessionContext());
  69. } catch (ImageException e) {
  70. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  71. getUserAgent().getEventBroadcaster());
  72. eventProducer.imageError(this, url, e, getLocator());
  73. } catch (FileNotFoundException fnfe) {
  74. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  75. getUserAgent().getEventBroadcaster());
  76. eventProducer.imageNotFound(this, url, fnfe, getLocator());
  77. } catch (IOException ioe) {
  78. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  79. getUserAgent().getEventBroadcaster());
  80. eventProducer.imageIOError(this, url, ioe, getLocator());
  81. }
  82. if (info != null) {
  83. this.intrinsicWidth = info.getSize().getWidthMpt();
  84. this.intrinsicHeight = info.getSize().getHeightMpt();
  85. int baseline = info.getSize().getBaselinePositionFromBottom();
  86. if (baseline != 0) {
  87. this.intrinsicAlignmentAdjust
  88. = FixedLength.getInstance(-baseline);
  89. }
  90. }
  91. }
  92. /** {@inheritDoc} */
  93. protected void startOfNode() throws FOPException {
  94. super.startOfNode();
  95. getFOEventHandler().image(this);
  96. }
  97. /**
  98. * {@inheritDoc}
  99. * <br>XSL Content Model: empty
  100. */
  101. protected void validateChildNode(Locator loc, String nsURI, String localName)
  102. throws ValidationException {
  103. if (FO_URI.equals(nsURI)) {
  104. invalidChildError(loc, nsURI, localName);
  105. }
  106. }
  107. /** @return the "src" property */
  108. public String getSrc() {
  109. return src;
  110. }
  111. /** @return Get the resulting URL based on the src property */
  112. public String getURL() {
  113. return url;
  114. }
  115. /** {@inheritDoc} */
  116. public String getLocalName() {
  117. return "external-graphic";
  118. }
  119. /** {@inheritDoc} */
  120. public int getNameId() {
  121. return FO_EXTERNAL_GRAPHIC;
  122. }
  123. /** {@inheritDoc} */
  124. public int getIntrinsicWidth() {
  125. return this.intrinsicWidth;
  126. }
  127. /** {@inheritDoc} */
  128. public int getIntrinsicHeight() {
  129. return this.intrinsicHeight;
  130. }
  131. /** {@inheritDoc} */
  132. public Length getIntrinsicAlignmentAdjust() {
  133. return this.intrinsicAlignmentAdjust;
  134. }
  135. }