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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.fo.flow;
  18. import org.apache.fop.apps.FOPException;
  19. import org.apache.fop.apps.FOUserAgent;
  20. import org.apache.fop.fo.FONode;
  21. import org.apache.fop.fo.PropertyList;
  22. import org.apache.fop.fo.ValidationException;
  23. import org.apache.fop.image.FopImage;
  24. import org.apache.fop.image.ImageFactory;
  25. import org.xml.sax.Locator;
  26. /**
  27. * External graphic formatting object.
  28. * This FO node handles the external graphic. It creates an image
  29. * inline area that can be added to the area tree.
  30. */
  31. public class ExternalGraphic extends AbstractGraphics {
  32. // The value of properties relevant for fo:external-graphic.
  33. // All but one of the e-g properties are kept in AbstractGraphics
  34. private String src;
  35. // End of property values
  36. //Additional values
  37. private String url;
  38. private FopImage fopimage;
  39. /**
  40. * Create a new External graphic node.
  41. *
  42. * @param parent the parent of this node
  43. */
  44. public ExternalGraphic(FONode parent) {
  45. super(parent);
  46. }
  47. /**
  48. * @see org.apache.fop.fo.FObj#bind(PropertyList)
  49. */
  50. public void bind(PropertyList pList) throws FOPException {
  51. super.bind(pList);
  52. src = pList.get(PR_SRC).getString();
  53. //Additional processing: preload image
  54. url = ImageFactory.getURL(getSrc());
  55. FOUserAgent userAgent = getUserAgent();
  56. ImageFactory fact = userAgent.getFactory().getImageFactory();
  57. fopimage = fact.getImage(url, userAgent);
  58. if (fopimage == null) {
  59. getLogger().error("Image not available: " + getSrc());
  60. } else {
  61. // load dimensions
  62. if (!fopimage.load(FopImage.DIMENSIONS)) {
  63. getLogger().error("Cannot read image dimensions: " + getSrc());
  64. }
  65. }
  66. //TODO Report to caller so he can decide to throw an exception
  67. }
  68. /**
  69. * @see org.apache.fop.fo.FONode#startOfNode
  70. */
  71. protected void startOfNode() throws FOPException {
  72. checkId(getId());
  73. getFOEventHandler().image(this);
  74. }
  75. /**
  76. * @see org.apache.fop.fo.FONode#validateChildNode(Locator, String, String)
  77. * XSL Content Model: empty
  78. */
  79. protected void validateChildNode(Locator loc, String nsURI, String localName)
  80. throws ValidationException {
  81. invalidChildError(loc, nsURI, localName);
  82. }
  83. /**
  84. * @return the "src" property.
  85. */
  86. public String getSrc() {
  87. return src;
  88. }
  89. /**
  90. * @return Get the resulting URL based on the src property.
  91. */
  92. public String getURL() {
  93. return url;
  94. }
  95. /** @see org.apache.fop.fo.FONode#getLocalName() */
  96. public String getLocalName() {
  97. return "external-graphic";
  98. }
  99. /**
  100. * @see org.apache.fop.fo.FObj#getNameId()
  101. */
  102. public int getNameId() {
  103. return FO_EXTERNAL_GRAPHIC;
  104. }
  105. /**
  106. * @see org.apache.fop.fo.flow.AbstractGraphics#getIntrinsicWidth()
  107. */
  108. public int getIntrinsicWidth() {
  109. if (fopimage != null) {
  110. return fopimage.getIntrinsicWidth();
  111. } else {
  112. return 0;
  113. }
  114. }
  115. /**
  116. * @see org.apache.fop.fo.flow.AbstractGraphics#getIntrinsicHeight()
  117. */
  118. public int getIntrinsicHeight() {
  119. if (fopimage != null) {
  120. return fopimage.getIntrinsicHeight();
  121. } else {
  122. return 0;
  123. }
  124. }
  125. }