diff options
Diffstat (limited to 'src/org/apache/fop/image/analyser/SVGReader.java')
-rw-r--r-- | src/org/apache/fop/image/analyser/SVGReader.java | 160 |
1 files changed, 34 insertions, 126 deletions
diff --git a/src/org/apache/fop/image/analyser/SVGReader.java b/src/org/apache/fop/image/analyser/SVGReader.java index bf0a43919..54b25f90f 100644 --- a/src/org/apache/fop/image/analyser/SVGReader.java +++ b/src/org/apache/fop/image/analyser/SVGReader.java @@ -46,18 +46,30 @@ import java.awt.Point; import java.awt.geom.Dimension2D; import java.awt.Dimension; +import org.apache.fop.fo.FOUserAgent; +import org.apache.fop.svg.SVGUserAgent; + /** * ImageReader object for SVG document image type. */ public class SVGReader extends AbstractImageReader { - public boolean verifySignature(String uri, - BufferedInputStream fis) throws IOException { + public static final String SVG_MIME_TYPE = "image/svg+xml"; + FOUserAgent userAgent; + SVGDocument doc; + + public boolean verifySignature(String uri, BufferedInputStream fis, + FOUserAgent ua) throws IOException { this.imageStream = fis; + userAgent = ua; return loadImage(uri); } public String getMimeType() { - return "image/svg+xml"; + return SVG_MIME_TYPE; + } + + public SVGDocument getDocument() { + return doc; } /** @@ -67,156 +79,52 @@ public class SVGReader extends AbstractImageReader { protected boolean loadImage(String uri) { // parse document and get the size attributes of the svg element try { + int length = imageStream.available(); + imageStream.mark(length); SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(SVGImage.getParserName()); - SVGDocument doc = factory.createDocument(uri, imageStream); + doc = factory.createDocument(uri, imageStream); - Element e = ((SVGDocument)doc).getRootElement(); + Element e = ((SVGDocument) doc).getRootElement(); String s; - UserAgent userAgent = new MUserAgent(new AffineTransform()); - BridgeContext ctx = new BridgeContext(userAgent); - UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e); + SVGUserAgent userAg = new SVGUserAgent(new AffineTransform()); + userAg.setLogger(userAgent.getLogger()); + BridgeContext ctx = new BridgeContext(userAg); + UnitProcessor.Context uctx = + UnitProcessor.createContext(ctx, e); // 'width' attribute - default is 100% s = e.getAttributeNS(null, SVGOMDocument.SVG_WIDTH_ATTRIBUTE); if (s.length() == 0) { s = SVGOMDocument.SVG_SVG_WIDTH_DEFAULT_VALUE; } - width = (int)UnitProcessor.svgHorizontalLengthToUserSpace - (s, SVGOMDocument.SVG_WIDTH_ATTRIBUTE, uctx); + width = (int) UnitProcessor.svgHorizontalLengthToUserSpace ( + s, SVGOMDocument.SVG_WIDTH_ATTRIBUTE, uctx); // 'height' attribute - default is 100% s = e.getAttributeNS(null, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE); if (s.length() == 0) { s = SVGOMDocument.SVG_SVG_HEIGHT_DEFAULT_VALUE; } - height = (int)UnitProcessor.svgVerticalLengthToUserSpace - (s, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE, uctx); + height = (int) UnitProcessor.svgVerticalLengthToUserSpace ( + s, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE, uctx); return true; } catch (NoClassDefFoundError ncdfe) { - //log.error("Batik not in class path"); + //userAgent.getLogger().error("Batik not in class path", ncdfe); return false; } catch (Exception e) { - //log.error("Could not load external SVG: " + - // e.getMessage()); + //userAgent.getLogger().error("Could not load external SVG: " + + // e.getMessage(), e); // assuming any exception means this document is not svg // or could not be loaded for some reason - return false; - } - } - - protected class MUserAgent implements UserAgent { - AffineTransform currentTransform = null; - - /** - * Creates a new SVGUserAgent. - */ - protected MUserAgent(AffineTransform at) { - currentTransform = at; - } - - /** - * Displays an error message. - */ - public void displayError(String message) { - System.err.println(message); - } - - /** - * Displays an error resulting from the specified Exception. - */ - public void displayError(Exception ex) { - ex.printStackTrace(System.err); - } - - /** - * Displays a message in the User Agent interface. - * The given message is typically displayed in a status bar. - */ - public void displayMessage(String message) { - System.out.println(message); - } - - /** - * Returns a customized the pixel to mm factor. - */ - public float getPixelToMM() { - // this is set to 72dpi as the values in fo are 72dpi - return 0.35277777777777777778f; // 72 dpi - // return 0.26458333333333333333333333333333f; // 96dpi - } - - /** - * Returns the language settings. - */ - public String getLanguages() { - return "en"; // userLanguages; - } - - public String getMedia() { - return "print"; - } - - /** - * Returns the user stylesheet uri. - * @return null if no user style sheet was specified. - */ - public String getUserStyleSheetURI() { - return null; // userStyleSheetURI; - } + try { + imageStream.reset(); + } catch (IOException ioe) { } - /** - * Returns the class name of the XML parser. - */ - public String getXMLParserClassName() { - return org.apache.fop.apps.Driver.getParserClassName(); - } - - public boolean isXMLParserValidating() { return false; } - - /** - * Opens a link in a new component. - * @param doc The current document. - * @param uri The document URI. - */ - public void openLink(SVGAElement elt) { - } - - public Point getClientAreaLocationOnScreen() { - return new Point(0, 0); - } - - public void setSVGCursor(java.awt.Cursor cursor) {} - - - public AffineTransform getTransform() { - return currentTransform; - } - - public Dimension2D getViewportSize() { - return new Dimension(100, 100); - } - - public EventDispatcher getEventDispatcher() { - return null; - } - - public boolean supportExtension(String str) { - return false; - } - - public boolean hasFeature(String str) { - return false; - } - - public void registerExtension(BridgeExtension be) {} - - public void handleElement(Element elt, Object data) {} - } } |