diff options
Diffstat (limited to 'examples/plan/src')
4 files changed, 130 insertions, 45 deletions
diff --git a/examples/plan/src/org/apache/fop/plan/PlanElement.java b/examples/plan/src/org/apache/fop/plan/PlanElement.java index 9e4f7bb16..d910076c6 100644 --- a/examples/plan/src/org/apache/fop/plan/PlanElement.java +++ b/examples/plan/src/org/apache/fop/plan/PlanElement.java @@ -21,14 +21,15 @@ package org.apache.fop.plan; import java.awt.geom.Point2D; -import org.apache.fop.apps.FOPException; -import org.apache.fop.fo.FONode; -import org.apache.fop.fo.PropertyList; - import org.w3c.dom.Document; + import org.xml.sax.Attributes; import org.xml.sax.Locator; +import org.apache.fop.apps.FOPException; +import org.apache.fop.fo.FONode; +import org.apache.fop.fo.PropertyList; + /** * This class defines the plan element. */ @@ -46,9 +47,7 @@ public class PlanElement extends PlanObj { super(parent); } - /** - * @see org.apache.fop.fo.FONode#processNode - */ + /** {@inheritDoc} */ public void processNode(String elementName, Locator locator, Attributes attlist, PropertyList propertyList) throws FOPException { @@ -79,15 +78,13 @@ public class PlanElement extends PlanObj { } - /** - * @see org.apache.fop.fo.XMLObj#getDOMDocument() - */ + /** {@inheritDoc} */ public Document getDOMDocument() { convertToSVG(); return doc; } - /** @see org.apache.fop.fo.FONode#getNamespaceURI() */ + /** {@inheritDoc} */ public String getNamespaceURI() { if (svgDoc == null) { return PlanElementMapping.NAMESPACE; @@ -95,9 +92,7 @@ public class PlanElement extends PlanObj { return "http://www.w3.org/2000/svg"; } - /** - * @see org.apache.fop.fo.XMLObj#getDimension(Point2D) - */ + /** {@inheritDoc} */ public Point2D getDimension(Point2D view) { convertToSVG(); return new Point2D.Float(width, height); diff --git a/examples/plan/src/org/apache/fop/plan/PlanElementMapping.java b/examples/plan/src/org/apache/fop/plan/PlanElementMapping.java index 556518d42..c33c25edf 100644 --- a/examples/plan/src/org/apache/fop/plan/PlanElementMapping.java +++ b/examples/plan/src/org/apache/fop/plan/PlanElementMapping.java @@ -19,12 +19,10 @@ package org.apache.fop.plan; -import org.apache.fop.fo.FONode; -import org.apache.fop.fo.ElementMapping; -import org.apache.fop.image.analyser.XMLReader; -import org.apache.fop.image.FopImage; import org.w3c.dom.DOMImplementation; -import org.w3c.dom.Document; + +import org.apache.fop.fo.ElementMapping; +import org.apache.fop.fo.FONode; /** * This class provides the element mapping for FOP. @@ -39,19 +37,17 @@ public class PlanElementMapping extends ElementMapping { this.namespaceURI = NAMESPACE; } - /** @see org.apache.fop.fo.ElementMapping#getDOMImplementation() */ + /** {@inheritDoc} */ public DOMImplementation getDOMImplementation() { return getDefaultDOMImplementation(); } - /** @see org.apache.fop.fo.ElementMapping#initialize() */ + /** {@inheritDoc} */ protected void initialize() { if (foObjs == null) { foObjs = new java.util.HashMap(); foObjs.put("plan", new PE()); foObjs.put(DEFAULT, new PlanMaker()); - - XMLReader.setConverter(this.namespaceURI, new PlanConverter()); } } @@ -67,24 +63,4 @@ public class PlanElementMapping extends ElementMapping { } } - static class PlanConverter implements XMLReader.Converter { - public FopImage.ImageInfo convert(Document doc) { - try { - PlanRenderer pr = new PlanRenderer(); - pr.setFontInfo("Helvetica", 12); - FopImage.ImageInfo info = new FopImage.ImageInfo(); - info.data = pr.createSVGDocument(doc); - info.width = (int)pr.getWidth(); - info.height = (int)pr.getHeight(); - info.mimeType = "image/svg+xml"; - info.str = "http://www.w3.org/2000/svg"; - - return info; - } catch (Throwable t) { - /**@todo Log this properly! */ - } - return null; - } - } - } diff --git a/examples/plan/src/org/apache/fop/plan/PlanObj.java b/examples/plan/src/org/apache/fop/plan/PlanObj.java index 32ad522b1..36f3243a6 100644 --- a/examples/plan/src/org/apache/fop/plan/PlanObj.java +++ b/examples/plan/src/org/apache/fop/plan/PlanObj.java @@ -38,12 +38,12 @@ public class PlanObj extends XMLObj { super(parent); } - /** @see org.apache.fop.fo.FONode#getNamespaceURI() */ + /** {@inheritDoc} */ public String getNamespaceURI() { return PlanElementMapping.NAMESPACE; } - /** @see org.apache.fop.fo.FONode#getNormalNamespacePrefix() */ + /** {@inheritDoc} */ public String getNormalNamespacePrefix() { return "plan"; } diff --git a/examples/plan/src/org/apache/fop/plan/PreloaderPlan.java b/examples/plan/src/org/apache/fop/plan/PreloaderPlan.java new file mode 100644 index 000000000..58d5be477 --- /dev/null +++ b/examples/plan/src/org/apache/fop/plan/PreloaderPlan.java @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.plan; + +import java.io.IOException; +import java.io.InputStream; + +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.stream.StreamSource; + +import org.w3c.dom.Document; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.apache.xmlgraphics.image.loader.Image; +import org.apache.xmlgraphics.image.loader.ImageContext; +import org.apache.xmlgraphics.image.loader.ImageInfo; +import org.apache.xmlgraphics.image.loader.ImageSize; +import org.apache.xmlgraphics.image.loader.impl.AbstractImagePreloader; +import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM; +import org.apache.xmlgraphics.image.loader.util.ImageUtil; + +import org.apache.fop.util.UnclosableInputStream; + +/** + * Image preloader for Plan images. + */ +public class PreloaderPlan extends AbstractImagePreloader { + + /** Logger instance */ + private static Log log = LogFactory.getLog(PreloaderPlan.class); + + /** {@inheritDoc} */ + public ImageInfo preloadImage(String uri, Source src, ImageContext context) + throws IOException { + if (!ImageUtil.hasInputStream(src)) { + //TODO Remove this and support DOMSource and possibly SAXSource + return null; + } + ImageInfo info = getImage(uri, src, context); + if (info != null) { + ImageUtil.closeQuietly(src); //Image is fully read + } + return info; + } + + private ImageInfo getImage(String uri, Source src, ImageContext context) { + + InputStream in = new UnclosableInputStream(ImageUtil.needInputStream(src)); + try { + TransformerFactory tFactory = TransformerFactory.newInstance(); + Transformer transformer = tFactory.newTransformer(); + Source source = new StreamSource(in); + DOMResult res = new DOMResult(); + transformer.transform(source, res); + + //Have to render the plan to know its size + PlanRenderer pr = new PlanRenderer(); + Document svgDoc = pr.createSVGDocument((Document)res.getNode()); + float width = pr.getWidth(); + float height = pr.getHeight(); + + //Return converted SVG image + ImageInfo info = new ImageInfo(uri, "image/svg+xml"); + final ImageSize size = new ImageSize(); + size.setSizeInMillipoints( + Math.round(width * 1000), + Math.round(height * 1000)); + //Set the resolution to that of the FOUserAgent + size.setResolution(context.getSourceResolution()); + size.calcPixelsFromSize(); + info.setSize(size); + + //The whole image had to be loaded for this, so keep it + Image image = new ImageXMLDOM(info, svgDoc, + svgDoc.getDocumentElement().getNamespaceURI()); + info.getCustomObjects().put(ImageInfo.ORIGINAL_IMAGE, image); + + return info; + } catch (TransformerException e) { + try { + in.reset(); + } catch (IOException ioe) { + // we're more interested in the original exception + } + log.debug("Error while trying to parsing a Plan file: " + + e.getMessage()); + return null; + } + } + +} |