001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ExternalGraphic.java 554423 2007-07-08 19:47:53Z adelmelle $ */
019:
020: package org.apache.fop.fo.flow;
021:
022: import org.apache.fop.apps.FOPException;
023: import org.apache.fop.apps.FOUserAgent;
024: import org.apache.fop.datatypes.Length;
025: import org.apache.fop.fo.FONode;
026: import org.apache.fop.fo.PropertyList;
027: import org.apache.fop.fo.ValidationException;
028: import org.apache.fop.image.FopImage;
029: import org.apache.fop.image.ImageFactory;
030: import org.xml.sax.Locator;
031:
032: /**
033: * External graphic formatting object.
034: * This FO node handles the external graphic. It creates an image
035: * inline area that can be added to the area tree.
036: */
037: public class ExternalGraphic extends AbstractGraphics {
038:
039: // The value of properties relevant for fo:external-graphic.
040: // All but one of the e-g properties are kept in AbstractGraphics
041: private String src;
042: // End of property values
043:
044: //Additional values
045: private String url;
046: private int intrinsicWidth;
047: private int intrinsicHeight;
048: private Length intrinsicAlignmentAdjust;
049:
050: /**
051: * Create a new External graphic node.
052: *
053: * @param parent the parent of this node
054: */
055: public ExternalGraphic(FONode parent) {
056: super (parent);
057: }
058:
059: /**
060: * @see org.apache.fop.fo.FObj#bind(PropertyList)
061: */
062: public void bind(PropertyList pList) throws FOPException {
063: super .bind(pList);
064: src = pList.get(PR_SRC).getString();
065:
066: //Additional processing: preload image
067: url = ImageFactory.getURL(getSrc());
068: FOUserAgent userAgent = getUserAgent();
069: ImageFactory fact = userAgent.getFactory().getImageFactory();
070: FopImage fopimage = fact.getImage(url, userAgent);
071: if (fopimage == null) {
072: log.error("Image not available: " + getSrc());
073: } else {
074: // load dimensions
075: if (!fopimage.load(FopImage.DIMENSIONS)) {
076: log.error("Cannot read image dimensions: " + getSrc());
077: }
078: this .intrinsicWidth = fopimage.getIntrinsicWidth();
079: this .intrinsicHeight = fopimage.getIntrinsicHeight();
080: this .intrinsicAlignmentAdjust = fopimage
081: .getIntrinsicAlignmentAdjust();
082: }
083: //TODO Report to caller so he can decide to throw an exception
084: }
085:
086: /**
087: * @see org.apache.fop.fo.FONode#startOfNode
088: */
089: protected void startOfNode() throws FOPException {
090: super .startOfNode();
091: getFOEventHandler().image(this );
092: }
093:
094: /**
095: * @see org.apache.fop.fo.FONode#validateChildNode(Locator, String, String)
096: * XSL Content Model: empty
097: */
098: protected void validateChildNode(Locator loc, String nsURI,
099: String localName) throws ValidationException {
100: invalidChildError(loc, nsURI, localName);
101: }
102:
103: /**
104: * @return the "src" property.
105: */
106: public String getSrc() {
107: return src;
108: }
109:
110: /**
111: * @return Get the resulting URL based on the src property.
112: */
113: public String getURL() {
114: return url;
115: }
116:
117: /** @see org.apache.fop.fo.FONode#getLocalName() */
118: public String getLocalName() {
119: return "external-graphic";
120: }
121:
122: /**
123: * @see org.apache.fop.fo.FObj#getNameId()
124: */
125: public int getNameId() {
126: return FO_EXTERNAL_GRAPHIC;
127: }
128:
129: /**
130: * @see org.apache.fop.fo.flow.AbstractGraphics#getIntrinsicWidth()
131: */
132: public int getIntrinsicWidth() {
133: return this .intrinsicWidth;
134: }
135:
136: /**
137: * @see org.apache.fop.fo.flow.AbstractGraphics#getIntrinsicHeight()
138: */
139: public int getIntrinsicHeight() {
140: return this .intrinsicHeight;
141: }
142:
143: /**
144: * @see org.apache.fop.fo.flow.AbstractGraphics#getIntrinsicAlignmentAdjust()
145: */
146: public Length getIntrinsicAlignmentAdjust() {
147: return this.intrinsicAlignmentAdjust;
148: }
149:
150: }
|