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: InstreamForeignObject.java 554423 2007-07-08 19:47:53Z adelmelle $ */
019:
020: package org.apache.fop.fo.flow;
021:
022: import java.awt.geom.Point2D;
023: import org.apache.fop.apps.FOPException;
024: import org.apache.fop.datatypes.Length;
025: import org.apache.fop.fo.FONode;
026: import org.apache.fop.fo.ValidationException;
027: import org.apache.fop.fo.XMLObj;
028: import org.xml.sax.Locator;
029:
030: /**
031: * The instream-foreign-object flow formatting object.
032: * This is an atomic inline object that contains
033: * xml data.
034: */
035: public class InstreamForeignObject extends AbstractGraphics {
036:
037: // The value of properties relevant for fo:instream-foreign-object.
038: // All property values contained in AbstractGraphics
039: // End of property values
040:
041: //Additional value
042: private Point2D intrinsicDimensions;
043:
044: private Length intrinsicAlignmentAdjust;
045:
046: /**
047: * constructs an instream-foreign-object object (called by Maker).
048: *
049: * @param parent the parent formatting object
050: */
051: public InstreamForeignObject(FONode parent) {
052: super (parent);
053: }
054:
055: /**
056: * Make sure content model satisfied, if so then tell the
057: * FOEventHandler that we are at the end of the flow.
058: * @see org.apache.fop.fo.FONode#endOfNode
059: */
060: protected void endOfNode() throws FOPException {
061: if (firstChild == null) {
062: missingChildElementError("one (1) non-XSL namespace child");
063: }
064: getFOEventHandler().foreignObject(this );
065: }
066:
067: /**
068: * @see org.apache.fop.fo.FONode#validateChildNode(Locator, String, String)
069: * XSL Content Model: one (1) non-XSL namespace child
070: */
071: protected void validateChildNode(Locator loc, String nsURI,
072: String localName) throws ValidationException {
073: if (FO_URI.equals(nsURI)) {
074: invalidChildError(loc, nsURI, localName);
075: } else if (firstChild != null) {
076: tooManyNodesError(loc, "child element");
077: }
078: }
079:
080: /** @see org.apache.fop.fo.FONode#getLocalName() */
081: public String getLocalName() {
082: return "instream-foreign-object";
083: }
084:
085: /**
086: * @see org.apache.fop.fo.FObj#getNameId()
087: */
088: public int getNameId() {
089: return FO_INSTREAM_FOREIGN_OBJECT;
090: }
091:
092: /**
093: * Preloads the image so the intrinsic size is available.
094: */
095: private void prepareIntrinsicSize() {
096: if (intrinsicDimensions == null) {
097: XMLObj child = (XMLObj) firstChild;
098: Point2D csize = new Point2D.Float(-1, -1);
099: intrinsicDimensions = child.getDimension(csize);
100: if (intrinsicDimensions == null) {
101: log
102: .error("Intrinsic dimensions of "
103: + " instream-foreign-object could not be determined");
104: }
105: intrinsicAlignmentAdjust = child
106: .getIntrinsicAlignmentAdjust();
107: }
108: }
109:
110: /**
111: * @see org.apache.fop.fo.flow.AbstractGraphics#getIntrinsicWidth()
112: */
113: public int getIntrinsicWidth() {
114: prepareIntrinsicSize();
115: if (intrinsicDimensions != null) {
116: return (int) (intrinsicDimensions.getX() * 1000);
117: } else {
118: return 0;
119: }
120: }
121:
122: /**
123: * @see org.apache.fop.fo.flow.AbstractGraphics#getIntrinsicHeight()
124: */
125: public int getIntrinsicHeight() {
126: prepareIntrinsicSize();
127: if (intrinsicDimensions != null) {
128: return (int) (intrinsicDimensions.getY() * 1000);
129: } else {
130: return 0;
131: }
132: }
133:
134: /**
135: * @see org.apache.fop.fo.flow.AbstractGraphics#getIntrinsicAlignmentAdjust()
136: */
137: public Length getIntrinsicAlignmentAdjust() {
138: prepareIntrinsicSize();
139: return intrinsicAlignmentAdjust;
140: }
141:
142: /** @see org.apache.fop.fo.FONode#addChildNode(org.apache.fop.fo.FONode) */
143: protected void addChildNode(FONode child) throws FOPException {
144: super .addChildNode(child);
145: }
146:
147: /** @return the XMLObj child node of the instream-foreign-object. */
148: public XMLObj getChildXMLObj() {
149: return (XMLObj) firstChild;
150: }
151:
152: }
|