From db49ef947e4d1cfeee3eccf2da66ff38997a16f0 Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Fri, 29 Dec 2023 16:20:01 +0200 Subject: [PATCH] add lots of nullability annotations (I must admit that I am not 100% all of them are correct) --- doc/guide/users-guide-r7.xil | 5 +- doc/guide/users-guide-r8.xil | 5 +- .../context/StylesheetFactoryImpl.java | 2 +- .../xhtmlrenderer/css/parser/MakeTokens.java | 1 - .../event/DefaultDocumentListener.java | 15 ++---- .../xhtmlrenderer/event/DocumentListener.java | 16 +++--- .../extend/UserAgentCallback.java | 16 ++++-- .../xhtmlrenderer/layout/SharedContext.java | 3 +- .../resource/AbstractResource.java | 24 ++++----- .../xhtmlrenderer/resource/CSSResource.java | 10 ++-- .../xhtmlrenderer/resource/ImageResource.java | 6 ++- .../org/xhtmlrenderer/resource/Resource.java | 21 ++++---- .../xhtmlrenderer/resource/XMLResource.java | 2 + .../org/xhtmlrenderer/simple/XHTMLPanel.java | 23 ++++----- .../extend/DefaultFormSubmissionListener.java | 6 +-- .../simple/extend/FormSubmissionListener.java | 3 ++ .../org/xhtmlrenderer/swing/BasicPanel.java | 8 +-- .../swing/DelegatingUserAgent.java | 12 ++++- .../xhtmlrenderer/swing/NaiveUserAgent.java | 12 ++++- .../org/xhtmlrenderer/swing/RootPanel.java | 28 ++++------- .../swing/ScalableXHTMLPanel.java | 7 ++- .../org/xhtmlrenderer/swing/UriResolver.java | 29 ++++++++--- .../org/xhtmlrenderer/util/ImageUtil.java | 50 +++++++++++++++++-- .../src/main/java/BrowsePanel.java | 5 +- .../src/main/java/Printer.java | 7 +-- .../demo/aboutbox/DemoUserAgent.java | 24 ++++++--- .../demo/browser/BrowserPanel.java | 28 ++++++----- .../demo/browser/PanelManager.java | 15 +++--- .../fop/nbsp/NonBreakPointsEnhancer.java | 6 +-- .../fop/nbsp/NonBreakPointsLoader.java | 10 +++- .../fop/nbsp/NonBreakPointsLoaderImpl.java | 16 ++++-- .../fop/nbsp/NonBreakPointsEnhancerTest.java | 5 +- .../org/xhtmlrenderer/simple/PDFRenderer.java | 37 ++++++++------ .../xhtmlrenderer/pdf/AbstractFormField.java | 2 + .../xhtmlrenderer/pdf/BookmarkElement.java | 6 ++- .../xhtmlrenderer/pdf/CheckboxFormField.java | 24 +++++---- .../pdf/EmptyReplacedElement.java | 11 ++-- .../xhtmlrenderer/pdf/ITextImageElement.java | 5 +- .../xhtmlrenderer/pdf/ITextOutputDevice.java | 4 +- .../pdf/ITextReplacedElement.java | 8 +-- .../pdf/RadioButtonFormField.java | 2 + .../xhtmlrenderer/pdf/SelectFormField.java | 2 + .../xhtmlrenderer/pdf/TextAreaFormField.java | 3 ++ .../org/xhtmlrenderer/pdf/TextFormField.java | 23 ++++----- .../org/xhtmlrenderer/simple/PDFRenderer.java | 20 ++++---- .../demo/browser/swt/Browser.java | 2 + .../demo/browser/swt/BrowserUserAgent.java | 11 +++- .../org/xhtmlrenderer/swt/NaiveUserAgent.java | 10 +++- 48 files changed, 358 insertions(+), 232 deletions(-) diff --git a/doc/guide/users-guide-r7.xil b/doc/guide/users-guide-r7.xil index 68c827534..3a28ae723 100644 --- a/doc/guide/users-guide-r7.xil +++ b/doc/guide/users-guide-r7.xil @@ -449,10 +449,9 @@ String address = "http://www.w3.org/"; // render try { - BufferedImage buff = null; - buff = Graphics2DRenderer.renderToImage(address, "w3c-homepage.png", 1024); + BufferedImage buff = Graphics2DRenderer.renderToImage(address, "w3c-homepage.png", 1024); } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException(e); } }} diff --git a/doc/guide/users-guide-r8.xil b/doc/guide/users-guide-r8.xil index 82a69215f..4fb8cd464 100644 --- a/doc/guide/users-guide-r8.xil +++ b/doc/guide/users-guide-r8.xil @@ -450,10 +450,9 @@ String address = "http://www.w3.org/"; // render try { - BufferedImage buff = null; - buff = Graphics2DRenderer.renderToImage(address, "w3c-homepage.png", 1024); + BufferedImage buff = Graphics2DRenderer.renderToImage(address, "w3c-homepage.png", 1024); } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException(e); } }} diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/context/StylesheetFactoryImpl.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/context/StylesheetFactoryImpl.java index 145763261..3f1f15cfc 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/context/StylesheetFactoryImpl.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/context/StylesheetFactoryImpl.java @@ -41,7 +41,7 @@ /** * A Factory class for Cascading Style Sheets. Sheets are parsed using a single - * parser instance for all sheets. Sheets are cached by URI using a LRU test, + * parser instance for all sheets. Sheets are cached by URI using LRU test, * but timestamp of file is not checked. * * @author Torbjoern Gannholm diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/parser/MakeTokens.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/parser/MakeTokens.java index 14a096a4e..50d6ff41d 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/parser/MakeTokens.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/parser/MakeTokens.java @@ -44,7 +44,6 @@ public static void main(String[] args) throws IOException { tokens.add(s); } } - // ignore StringBuilder buf = new StringBuilder(); diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/event/DefaultDocumentListener.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/event/DefaultDocumentListener.java index 4c3df2629..d4eb92dcb 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/event/DefaultDocumentListener.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/event/DefaultDocumentListener.java @@ -19,32 +19,23 @@ */ package org.xhtmlrenderer.event; +import javax.annotation.ParametersAreNonnullByDefault; + /** * Default, do-nothing implementation of a {@link DocumentListener}; implements all methods in {@link DocumentListener} * but the methods do nothing. Subclass this class and override whichever methods you need to trap. */ +@ParametersAreNonnullByDefault public class DefaultDocumentListener implements DocumentListener { - /** - * {@inheritDoc} - */ public void documentStarted() { } - /** - * {@inheritDoc} - */ public void documentLoaded() { } - /** - * {@inheritDoc} - */ public void onLayoutException(Throwable t) { } - /** - * {@inheritDoc} - */ public void onRenderException(Throwable t) { } } diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/event/DocumentListener.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/event/DocumentListener.java index 799b6dba4..55e52ae9c 100755 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/event/DocumentListener.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/event/DocumentListener.java @@ -19,12 +19,15 @@ */ package org.xhtmlrenderer.event; +import javax.annotation.ParametersAreNonnullByDefault; + /** * Implementations of this listener interface receive notifications about * various document and layout events. Events are called on the Event Dispatch Thread, and will block * any EDT activity until the methods return; make sure you do as little as possible in each method, or where necessary, * spin the task off to a separate thread. */ +@ParametersAreNonnullByDefault public interface DocumentListener { /** * Indicates document has been requested (e.g. a new document is going to be @@ -43,9 +46,9 @@ public interface DocumentListener { /** * Called when document layout failed with an exception. All - * Throwable objects thrown (except for - * ThreadDeath) during layout and not otherwise handled will - * be provided to this method. If a DocumentListener has been + * {@code Throwable} objects thrown (except for + * {@code ThreadDeath}) during layout and not otherwise handled will + * be provided to this method. If a {@code DocumentListener} has been * defined an XHTML panel, the listener is entirely responsible for * handling the exception. No other action will be taken. */ @@ -53,14 +56,13 @@ public interface DocumentListener { /** * Called when document render failed with an exception. All - * Throwable objects thrown (except for - * ThreadDeath) during render and not otherwise handled will - * be provided to this method. If a DocumentListener has been + * {@code Throwable} objects thrown (except for + * {@code ThreadDeath}) during render and not otherwise handled will + * be provided to this method. If a {@code DocumentListener} has been * defined an XHTML panel, the listener is entirely responsible for * handling the exception. No other action will be taken. */ void onRenderException(Throwable t); - } /* diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/extend/UserAgentCallback.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/extend/UserAgentCallback.java index af9a9cfbd..055c6f969 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/extend/UserAgentCallback.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/extend/UserAgentCallback.java @@ -23,6 +23,10 @@ import org.xhtmlrenderer.resource.ImageResource; import org.xhtmlrenderer.resource.XMLResource; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; + /** *

To be implemented by any user agent using the panel. "User agent" is a @@ -47,6 +51,7 @@ * * @author Torbjoern Gannholm */ +@ParametersAreNonnullByDefault public interface UserAgentCallback { /** * Retrieves the CSS at the given URI. This is a synchronous call. @@ -76,6 +81,8 @@ public interface UserAgentCallback { * Retrieves a binary resource located at a given URI and returns its contents * as a byte array or null if the resource could not be loaded. */ + @Nullable + @CheckReturnValue byte[] getBinaryResource(String uri); /** @@ -85,7 +92,8 @@ public interface UserAgentCallback { * @param uri A URI which may have been visited by this user agent. * @return The visited value */ - boolean isVisited(String uri); + @CheckReturnValue + boolean isVisited(@Nullable String uri); /** * Does not need to be a correct URL, only an identifier that the @@ -93,7 +101,7 @@ public interface UserAgentCallback { * * @param url A URL against which relative references can be resolved. */ - void setBaseURL(String url); + void setBaseURL(@Nullable String url); /** * @return the base uri, possibly in the implementations private uri-space @@ -108,6 +116,8 @@ public interface UserAgentCallback { * @param uri an absolute or relative (to baseURL) uri to be resolved. * @return the full uri in uri-spaces known to the current implementation. */ - String resolveURI(String uri); + @Nullable + @CheckReturnValue + String resolveURI(@Nullable String uri); } diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/SharedContext.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/SharedContext.java index 7ba7c446a..d9fcd0278 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/SharedContext.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/SharedContext.java @@ -46,6 +46,7 @@ import org.xhtmlrenderer.swing.SwingReplacedElementFactory; import org.xhtmlrenderer.util.XRLog; +import javax.annotation.Nullable; import java.awt.*; import java.util.HashMap; import java.util.HashSet; @@ -370,7 +371,7 @@ public String getBaseURL() { * * @param url The new baseURL value */ - public void setBaseURL(String url) { + public void setBaseURL(@Nullable String url) { uac.setBaseURL(url); } diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/AbstractResource.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/AbstractResource.java index 3c04e680d..72eb4acfd 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/AbstractResource.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/AbstractResource.java @@ -21,46 +21,46 @@ import org.xml.sax.InputSource; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import java.io.BufferedInputStream; import java.io.InputStream; /** * @author Patrick Wright */ +@ParametersAreNonnullByDefault public abstract class AbstractResource implements Resource { - private InputSource inputSource; + private final InputSource inputSource; private final long createTimeStamp; private long elapsedLoadTime; - private AbstractResource() { - this.createTimeStamp = System.currentTimeMillis(); - } - - /** - * Creates a new instance of AbstractResource - */ - public AbstractResource(InputSource source) { - this(); + protected AbstractResource(@Nullable InputSource source) { this.inputSource = source; + this.createTimeStamp = System.currentTimeMillis(); } - public AbstractResource(InputStream is) { + protected AbstractResource(@Nullable InputStream is) { this(is == null ? null : new InputSource(new BufferedInputStream(is))); } + @Nullable + @CheckReturnValue public InputSource getResourceInputSource() { return this.inputSource; } + @CheckReturnValue public long getResourceLoadTimeStamp() { return this.createTimeStamp; } + @CheckReturnValue public long getElapsedLoadTime() { return elapsedLoadTime; } - /*package*/ void setElapsedLoadTime(long elapsedLoadTime) { this.elapsedLoadTime = elapsedLoadTime; } diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/CSSResource.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/CSSResource.java index c8ada97af..53f74d8d3 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/CSSResource.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/CSSResource.java @@ -19,20 +19,18 @@ */ package org.xhtmlrenderer.resource; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import java.io.InputStream; /** * @author Administrator */ +@ParametersAreNonnullByDefault public class CSSResource extends AbstractResource { - - /** - * Creates a new instance of CSSResource - */ - public CSSResource(InputStream stream) { + public CSSResource(@Nullable InputStream stream) { super(stream); } - } /* diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/ImageResource.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/ImageResource.java index 259ac7347..d8e3d46be 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/ImageResource.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/ImageResource.java @@ -24,15 +24,19 @@ import org.xhtmlrenderer.swing.MutableFSImage; import org.xml.sax.InputSource; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; + /** * @author Administrator */ +@ParametersAreNonnullByDefault public class ImageResource extends AbstractResource { private final String _imageUri; private final FSImage _img; //HACK: at least for now, till we know what we want to do here - public ImageResource(final String uri, FSImage img) { + public ImageResource(@Nullable final String uri, @Nullable FSImage img) { super((InputSource) null); _imageUri = uri; _img = img; diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/Resource.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/Resource.java index 06077c74b..20c13abf1 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/Resource.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/Resource.java @@ -21,21 +21,20 @@ import org.xml.sax.InputSource; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; + /** * * @author Patrick Wright */ +@ParametersAreNonnullByDefault public interface Resource { + @Nullable + @CheckReturnValue InputSource getResourceInputSource(); - long getResourceLoadTimeStamp(); -} -/* - * $Id$ - * - * $Log$ - * Revision 1.1 2005/02/03 20:39:35 pdoubleya - * Added to CVS. - * - * - */ \ No newline at end of file + @CheckReturnValue + long getResourceLoadTimeStamp(); +} \ No newline at end of file diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/XMLResource.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/XMLResource.java index 4d55fd9d1..f81cd0208 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/XMLResource.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/resource/XMLResource.java @@ -35,6 +35,7 @@ import org.xml.sax.helpers.XMLFilterImpl; import org.xml.sax.helpers.XMLReaderFactory; +import javax.annotation.ParametersAreNonnullByDefault; import javax.xml.XMLConstants; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; @@ -57,6 +58,7 @@ /** * @author Patrick Wright */ +@ParametersAreNonnullByDefault public class XMLResource extends AbstractResource { private Document document; private static final XMLResourceBuilder XML_RESOURCE_BUILDER; diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/XHTMLPanel.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/XHTMLPanel.java index 898e6b680..df0f73018 100755 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/XHTMLPanel.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/XHTMLPanel.java @@ -23,7 +23,6 @@ import org.xhtmlrenderer.extend.UserAgentCallback; import org.xhtmlrenderer.layout.SharedContext; import org.xhtmlrenderer.render.RenderingContext; -import org.xhtmlrenderer.simple.extend.FormSubmissionListener; import org.xhtmlrenderer.simple.extend.XhtmlNamespaceHandler; import org.xhtmlrenderer.swing.BasicPanel; import org.xhtmlrenderer.swing.CursorListener; @@ -31,8 +30,11 @@ import org.xhtmlrenderer.swing.LinkListener; import org.xhtmlrenderer.util.Configuration; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import java.io.File; import java.io.InputStream; +import java.net.MalformedURLException; import java.net.URL; /** @@ -63,7 +65,7 @@ * instances. You should make sure the document you want to render is well-formed. For XHTML, * there is always a default stylesheet available, even if no CSS is attached to the * XHTML you are loading. For XML, there is no default stylesheet, so you should have - * one attached to your XML before trying to render it using the xml-stylesheer processing + * one attached to your XML before trying to render it using the xml-stylesheet processing * instruction. XHTMLPanel has methods to load * documents from a uri ({@link #setDocument(String uri)}), * from a Document instance ({@link #setDocument(Document)}) or from an InputStream @@ -95,6 +97,7 @@ * @see The Flying Saucer Home Page * @see RenderingContext */ +@ParametersAreNonnullByDefault public class XHTMLPanel extends BasicPanel { private static final long serialVersionUID = 1L; @@ -125,11 +128,7 @@ private void setupListeners() { addMouseTrackingListener(new HoverListener()); addMouseTrackingListener(new LinkListener()); addMouseTrackingListener(new CursorListener()); - setFormSubmissionListener(new FormSubmissionListener() { - public void submit(String query) { - XHTMLPanel.this.setDocumentRelative(query); - } - }); + setFormSubmissionListener(query -> XHTMLPanel.this.setDocumentRelative(query)); } } @@ -175,7 +174,7 @@ public void setDocument(Document doc) { * @param url The new document value */ @Override - public void setDocument(Document doc, String url) { + public void setDocument(Document doc, @Nullable String url) { resetListeners(); setDocument(doc, url, new XhtmlNamespaceHandler()); } @@ -189,8 +188,7 @@ public void setDocument(Document doc, String url) { */ // TODO: should throw more specific exception (PWW 25/07/2006) @Override - public void setDocument(InputStream stream, String url) - throws Exception { + public void setDocument(InputStream stream, String url) { resetListeners(); setDocument(stream, url, new XhtmlNamespaceHandler()); } @@ -203,8 +201,7 @@ public void setDocument(InputStream stream, String url) * will be resolved based on the file's parent directory. */ // TODO: should throw more specific exception (PWW 25/07/2006) - public void setDocument(File file) - throws Exception { + public void setDocument(File file) throws MalformedURLException { resetListeners(); File parent = file.getAbsoluteFile().getParentFile(); String parentURL = ( parent == null ? "" : parent.toURI().toURL().toExternalForm()); @@ -264,7 +261,7 @@ public void resetFontSize() { /** * Decrements all rendered fonts on the current document by the current - * scaling factor for the panel. Scaling applies culmulatively, which means that + * scaling factor for the panel. Scaling applies cumulatively, which means that * multiple calls to this method scale fonts smaller and smaller by applying the * current scaling factor against itself. You can modify the scaling factor by * {@link #setFontScalingFactor(float)}, and reset to the document's specified diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/extend/DefaultFormSubmissionListener.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/extend/DefaultFormSubmissionListener.java index 428b5fe70..25a474c52 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/extend/DefaultFormSubmissionListener.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/extend/DefaultFormSubmissionListener.java @@ -19,12 +19,12 @@ */ package org.xhtmlrenderer.simple.extend; +import javax.annotation.ParametersAreNonnullByDefault; + /** * No-op implementation of a FormSubmissionListener; override the submit method in a subclass as needed. */ +@ParametersAreNonnullByDefault public class DefaultFormSubmissionListener implements FormSubmissionListener { - /** - * {@inheritDoc} - */ public void submit(String query) { } } diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/extend/FormSubmissionListener.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/extend/FormSubmissionListener.java index 64a291873..6d0496abd 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/extend/FormSubmissionListener.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/extend/FormSubmissionListener.java @@ -20,6 +20,8 @@ package org.xhtmlrenderer.simple.extend; +import javax.annotation.ParametersAreNonnullByDefault; + /** * FormSubmissionListener is used to receive callbacks when an XhtmlForm has its submit action called. The entire * query string is given over to the {@link #submit(String)} method, which can then be submitted back to the panel @@ -28,6 +30,7 @@ * * @author Christophe Marchand */ +@ParametersAreNonnullByDefault public interface FormSubmissionListener { /** * Called by XhtmlForm when a form is submitted. diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/BasicPanel.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/BasicPanel.java index 545f0fb37..093d13b06 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/BasicPanel.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/BasicPanel.java @@ -37,6 +37,8 @@ import org.xhtmlrenderer.util.XRLog; import org.xml.sax.InputSource; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import javax.swing.*; import java.awt.*; import java.awt.print.PrinterGraphics; @@ -48,15 +50,13 @@ import java.util.List; import java.util.logging.Level; - - - /** * A Swing {@link javax.swing.JPanel} that encloses the Flying Saucer renderer * for easy integration into Swing applications. * * @author Joshua Marinacci */ +@ParametersAreNonnullByDefault public abstract class BasicPanel extends RootPanel implements FormSubmissionListener { private static final int PAGE_PAINTING_CLEARANCE_WIDTH = 10; private static final int PAGE_PAINTING_CLEARANCE_HEIGHT = 10; @@ -342,7 +342,7 @@ public void setDocument(InputStream stream, String url, NamespaceHandler nsh) { setDocument(dom, url, nsh); } - public void setDocumentFromString(String content, String url, NamespaceHandler nsh) { + public void setDocumentFromString(String content, @Nullable String url, NamespaceHandler nsh) { InputSource is = new InputSource(new BufferedReader(new StringReader(content))); Document dom = XMLResource.load(is).getDocument(); diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/DelegatingUserAgent.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/DelegatingUserAgent.java index 7c25987fa..aac58ab7f 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/DelegatingUserAgent.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/DelegatingUserAgent.java @@ -27,6 +27,9 @@ import org.xhtmlrenderer.util.IOUtil; import org.xhtmlrenderer.util.StreamResource; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -50,6 +53,7 @@ * * @author Torbjoern Gannholm */ +@ParametersAreNonnullByDefault public class DelegatingUserAgent implements UserAgentCallback, DocumentListener { private final UriResolver _uriResolver; private ImageResourceLoader _imageResourceLoader; @@ -113,7 +117,7 @@ public ImageResource getImageResource(String uri) { } /** - * Retrieves the XML located at the given URI. It's assumed the URI does point to a XML--the URI will + * Retrieves the XML located at the given URI. It's assumed the URI does point to XML--the URI will * be accessed (using java.io or java.net), opened, read and then passed into the XML parser (XMLReader) * configured for Flying Saucer. The result is packed up into an XMLResource for later consumption. * @@ -134,6 +138,8 @@ public XMLResource getXMLResource(String uri) { } } + @Nullable + @CheckReturnValue public byte[] getBinaryResource(String uri) { String ruri = _uriResolver.resolve(uri); StreamResource sr = new StreamResource(ruri); @@ -182,7 +188,9 @@ public void setBaseURL(String uri) { * @param uri A URI, possibly relative. * @return A URI as String, resolved, or null if there was an exception (for example if the URI is malformed). */ - public String resolveURI(String uri) { + @Nullable + @CheckReturnValue + public String resolveURI(@Nullable String uri) { return _uriResolver.resolve(uri); } diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/NaiveUserAgent.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/NaiveUserAgent.java index 71c5573c2..90dff6a5b 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/NaiveUserAgent.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/NaiveUserAgent.java @@ -28,6 +28,9 @@ import org.xhtmlrenderer.util.ImageUtil; import org.xhtmlrenderer.util.XRLog; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; @@ -63,6 +66,7 @@ * * @author Torbjoern Gannholm */ +@ParametersAreNonnullByDefault public class NaiveUserAgent implements UserAgentCallback, DocumentListener { private static final int DEFAULT_IMAGE_CACHE_SIZE = 16; @@ -275,7 +279,7 @@ public ImageResource getImageResource(String uri) { * * @return An ImageResource containing the image. */ - protected ImageResource createImageResource(String uri, Image img) { + protected ImageResource createImageResource(String uri, @Nullable Image img) { return new ImageResource(uri, AWTFSImage.createImage(img)); } @@ -306,6 +310,8 @@ public XMLResource getXMLResource(String uri) { } @Override + @Nullable + @CheckReturnValue public byte[] getBinaryResource(String uri) { InputStream is = resolveAndOpenStream(uri); if (is==null) return null; @@ -363,7 +369,9 @@ public void setBaseURL(String url) { * @return A URI as String, resolved, or null if there was an exception (for example if the URI is malformed). */ @Override - public String resolveURI(String uri) { + @Nullable + @CheckReturnValue + public String resolveURI(@Nullable String uri) { if (uri == null) return null; if (_baseURL == null) {//first try to set a base URL diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/RootPanel.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/RootPanel.java index 62b8dd4ce..6581da7fc 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/RootPanel.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/RootPanel.java @@ -46,6 +46,8 @@ import org.xhtmlrenderer.util.Configuration; import org.xhtmlrenderer.util.XRLog; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import javax.swing.*; import java.awt.*; import java.awt.event.MouseEvent; @@ -53,7 +55,9 @@ import java.util.Set; import java.util.logging.Level; +import static java.util.Objects.requireNonNull; +@ParametersAreNonnullByDefault public class RootPanel extends JPanel implements Scrollable, UserInterface, FSCanvas, RepaintListener { private Box rootBox; private boolean needRelayout; @@ -94,7 +98,7 @@ public LayoutContext getLayoutContext() { return layoutContext; } - public void setDocument(Document doc, String url, NamespaceHandler nsh) { + public void setDocument(Document doc, @Nullable String url, NamespaceHandler nsh) { fireDocumentStarted(); resetScrollPosition(); setRootBox(null); @@ -152,7 +156,7 @@ public void resetScrollPosition() { * @param scrollPane the enclosing {@link JScrollPane} or null if * the panel is no longer enclosed in a {@link JScrollPane}. */ - protected void setEnclosingScrollPane(JScrollPane scrollPane) { + protected void setEnclosingScrollPane(@Nullable JScrollPane scrollPane) { enclosingScrollPane = scrollPane; @@ -286,7 +290,7 @@ public Rectangle getScreenExtents() { return extents; } - public void doDocumentLayout(Graphics g) { + public void doDocumentLayout(@Nullable Graphics g) { try { removeAll(); if (g == null) { @@ -329,11 +333,7 @@ public void doDocumentLayout(Graphics g) { // if there is a fixed child then we need to set opaque to false // so that the entire viewport will be repainted. this is slower // but that's the hit you get from using fixed layout - if (root.getLayer().containsFixedContent()) { - super.setOpaque(false); - } else { - super.setOpaque(true); - } + super.setOpaque(!root.getLayer().containsFixedContent()); XRLog.layout(Level.FINEST, "after layout: " + root); @@ -431,10 +431,7 @@ private void initFontFromComponent(BlockBox root) { * @param listener Contains the DocumentListener for DocumentEvent data. */ public void addDocumentListener(DocumentListener listener) { - if(listener == null) { - return; - } - documentListeners.add(listener); + documentListeners.add(requireNonNull(listener)); } /** @@ -445,10 +442,7 @@ public void addDocumentListener(DocumentListener listener) { * @param listener Contains the DocumentListener to remove. */ public void removeDocumentListener(DocumentListener listener) { - if(listener == null) { - return; - } - documentListeners.remove(listener); + documentListeners.remove(requireNonNull(listener)); } protected boolean hasDocumentListeners() { @@ -546,7 +540,7 @@ public synchronized Box getRootBox() { return rootBox; } - public synchronized void setRootBox(Box rootBox) { + public synchronized void setRootBox(@Nullable Box rootBox) { this.rootBox = rootBox; } diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/ScalableXHTMLPanel.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/ScalableXHTMLPanel.java index ab930dfbf..8413ab632 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/ScalableXHTMLPanel.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/ScalableXHTMLPanel.java @@ -28,6 +28,8 @@ import org.xhtmlrenderer.render.RenderingContext; import org.xhtmlrenderer.simple.XHTMLPanel; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.print.PrinterGraphics; @@ -39,6 +41,7 @@ * * @author chm */ +@ParametersAreNonnullByDefault public class ScalableXHTMLPanel extends XHTMLPanel { public static final int SCALE_POLICY_NONE = 0; @@ -80,7 +83,7 @@ public ScalableXHTMLPanel(UserAgentCallback uac) { * @param url The new document value */ @Override - public void setDocument(Document doc, String url) { + public void setDocument(Document doc, @Nullable String url) { resetScaleAccordingToPolicy(); lastLayoutSize = null; super.setDocument(doc, url); @@ -94,7 +97,7 @@ public void setDocument(Document doc, String url) { * @param url The URL used to resolve relative path references. */ @Override - public void setDocument(InputStream stream, String url) throws Exception { + public void setDocument(InputStream stream, String url) { resetScaleAccordingToPolicy(); lastLayoutSize = null; super.setDocument(stream, url); diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/UriResolver.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/UriResolver.java index 1c8934deb..1b4decf93 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/UriResolver.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/UriResolver.java @@ -2,17 +2,29 @@ import org.xhtmlrenderer.util.XRLog; -import java.net.URL; -import java.net.MalformedURLException; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; import java.util.logging.Level; +@ParametersAreNonnullByDefault public class UriResolver { private String _baseUri; - public String resolve(final String uri) { + @Nullable + @CheckReturnValue + public String resolve(@Nullable final String uri) { if (uri == null) return null; - String ret = null; + return resolveUri(uri); + } + + @Nonnull + @CheckReturnValue + public String resolveUri(final String uri) { if (_baseUri == null) {//first try to set a base URL try { URL result = new URL(uri); @@ -33,20 +45,21 @@ public String resolve(final String uri) { XRLog.load(Level.FINE, "Could not read " + uri + " as a URL; may be relative. Testing using parent URL " + _baseUri); try { URL result = new URL(new URL(_baseUri), uri); - ret = result.toString(); + String ret = result.toString(); XRLog.load(Level.FINE, "Was able to read from " + uri + " using parent URL " + _baseUri); + return ret; } catch (MalformedURLException e1) { - XRLog.exception("The default NaiveUserAgent cannot resolve the URL " + uri + " with base URL " + _baseUri); + throw new IllegalStateException("The default NaiveUserAgent cannot resolve the URL " + uri + " with base URL " + _baseUri, e1); } } - return ret; - } public void setBaseUri(final String baseUri) { _baseUri = baseUri; } + @Nonnull + @CheckReturnValue public String getBaseUri() { return _baseUri; } diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/util/ImageUtil.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/util/ImageUtil.java index 75c6ff672..0618afaba 100755 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/util/ImageUtil.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/util/ImageUtil.java @@ -19,6 +19,10 @@ */ package org.xhtmlrenderer.util; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; @@ -37,6 +41,7 @@ * * @author pwright */ +@ParametersAreNonnullByDefault public class ImageUtil { private static final Map qualities = new HashMap<>(); @@ -70,6 +75,8 @@ public static void clearImage(BufferedImage image) { clearImage(image, Color.WHITE); } + @Nonnull + @CheckReturnValue public static BufferedImage makeCompatible(BufferedImage image) { BufferedImage cimg; if (GraphicsEnvironment.isHeadless()) { @@ -105,6 +112,8 @@ public static BufferedImage makeCompatible(BufferedImage image) { * in non-headless more. * @return A BufferedImage compatible with the screen (best fit). */ + @Nonnull + @CheckReturnValue public static BufferedImage createCompatibleBufferedImage(int width, int height, int biType) { final BufferedImage image; @@ -124,6 +133,8 @@ public static BufferedImage createCompatibleBufferedImage(int width, int height, return image; } + @Nonnull + @CheckReturnValue private static GraphicsConfiguration getGraphicsConfiguration() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gs = ge.getDefaultScreenDevice(); @@ -140,6 +151,8 @@ private static GraphicsConfiguration getGraphicsConfiguration() { * @param height Target height for the image * @return A BufferedImage compatible with the screen (best fit) supporting transparent pixels. */ + @Nonnull + @CheckReturnValue public static BufferedImage createCompatibleBufferedImage(int width, int height) { return createCompatibleBufferedImage(width, height, Transparency.BITMASK); } @@ -160,6 +173,8 @@ public static BufferedImage createCompatibleBufferedImage(int width, int height) * @param orgImage The image to scale * @return The scaled image instance. */ + @Nonnull + @CheckReturnValue public static BufferedImage getScaledInstance(ScalingOptions opt, BufferedImage orgImage) { int w = orgImage.getWidth(null); int h = orgImage.getHeight(null); @@ -196,6 +211,8 @@ public static BufferedImage getScaledInstance(ScalingOptions opt, BufferedImage * @param targetHeight The target height in pixels * @return The scaled image instance. */ + @Nonnull + @CheckReturnValue public static BufferedImage getScaledInstance(BufferedImage orgImage, int targetWidth, int targetHeight) { String downscaleQuality = Configuration.valueFor("xr.image.scale", DownscaleQuality.HIGH_QUALITY.asString()); DownscaleQuality quality = DownscaleQuality.forString(downscaleQuality, DownscaleQuality.HIGH_QUALITY); @@ -218,14 +235,14 @@ public static BufferedImage getScaledInstance(BufferedImage orgImage, int target * not check for duplicate dimensions. * @return List of buffered images in the given dimensions. */ + @Nonnull + @CheckReturnValue public static List scaleMultiple(ScalingOptions opt, BufferedImage img, List dimensions) { List scaledImages = new ArrayList<>(dimensions.size()); for (Dimension dim : dimensions) { opt.setTargetDimensions(dim); - BufferedImage scaled = getScaledInstance(opt, img); - scaledImages.add(scaled); } return scaledImages; @@ -240,6 +257,8 @@ public static List scaleMultiple(ScalingOptions opt, BufferedImag * {@link java.awt.image.BufferedImage#BufferedImage(int,int,int)} * @return BufferedImage with same content. */ + @Nonnull + @CheckReturnValue public static BufferedImage convertToBufferedImage(Image awtImg, int type) { final BufferedImage image; if (awtImg instanceof BufferedImage) { @@ -253,6 +272,8 @@ public static BufferedImage convertToBufferedImage(Image awtImg, int type) { return image; } + @Nonnull + @CheckReturnValue public static BufferedImage createTransparentImage(int width, int height) { BufferedImage bi = createCompatibleBufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = bi.createGraphics(); @@ -272,8 +293,9 @@ public static BufferedImage createTransparentImage(int width, int height) { * @param uri URI of the image * @return A boolean */ - public static boolean isEmbeddedBase64Image(String uri) { - return (uri != null && uri.startsWith("data:image/")); + @CheckReturnValue + public static boolean isEmbeddedBase64Image(@Nullable String uri) { + return uri != null && uri.startsWith("data:image/"); } /** @@ -282,6 +304,8 @@ public static boolean isEmbeddedBase64Image(String uri) { * @param imageDataUri URI of the embedded image * @return The binary content */ + @Nullable + @CheckReturnValue public static byte[] getEmbeddedBase64Image(String imageDataUri) { int b64Index = imageDataUri.indexOf("base64,"); if (b64Index != -1) { @@ -299,6 +323,8 @@ public static byte[] getEmbeddedBase64Image(String imageDataUri) { * @param imageDataUri URI of the embedded image * @return The BufferedImage */ + @Nullable + @CheckReturnValue public static BufferedImage loadEmbeddedBase64Image(String imageDataUri) { try { byte[] buffer = getEmbeddedBase64Image(imageDataUri); @@ -311,6 +337,7 @@ public static BufferedImage loadEmbeddedBase64Image(String imageDataUri) { return null; } + @ParametersAreNonnullByDefault interface Scaler { /** * Convenience method that returns a scaled instance of the @@ -339,11 +366,16 @@ interface Scaler { * in pixels * @return a scaled version of the original {@code BufferedImage} */ + @Nonnull + @CheckReturnValue BufferedImage getScaledInstance(BufferedImage img, ScalingOptions opt); } + @ParametersAreNonnullByDefault abstract static class AbstractFastScaler implements Scaler { @Override + @Nonnull + @CheckReturnValue public BufferedImage getScaledInstance(BufferedImage img, ScalingOptions opt) { // target is always >= 1 Image scaled = img.getScaledInstance(opt.getTargetWidth(), opt.getTargetHeight(), getImageScalingMethod()); @@ -357,8 +389,10 @@ public BufferedImage getScaledInstance(BufferedImage img, ScalingOptions opt) { /** * Old AWT-style scaling, poor quality */ + @ParametersAreNonnullByDefault static class OldScaler extends AbstractFastScaler { @Override + @CheckReturnValue protected int getImageScalingMethod() { return Image.SCALE_FAST; } @@ -367,8 +401,10 @@ protected int getImageScalingMethod() { /** * AWT-style one-step scaling, using area averaging */ + @ParametersAreNonnullByDefault static class AreaAverageScaler extends AbstractFastScaler { @Override + @CheckReturnValue protected int getImageScalingMethod() { return Image.SCALE_AREA_AVERAGING; } @@ -377,8 +413,11 @@ protected int getImageScalingMethod() { /** * Fast but decent scaling */ + @ParametersAreNonnullByDefault static class FastScaler implements Scaler { @Override + @Nonnull + @CheckReturnValue public BufferedImage getScaledInstance(BufferedImage img, ScalingOptions opt) { int w, h; @@ -400,8 +439,11 @@ public BufferedImage getScaledInstance(BufferedImage img, ScalingOptions opt) { /** * Step-wise downscaling */ + @ParametersAreNonnullByDefault static class HighQualityScaler implements Scaler { @Override + @Nonnull + @CheckReturnValue public BufferedImage getScaledInstance(BufferedImage img, ScalingOptions opt) { int w, h; int width = img.getWidth(null); diff --git a/flying-saucer-examples/src/main/java/BrowsePanel.java b/flying-saucer-examples/src/main/java/BrowsePanel.java index aad5cbeb1..a0ef680a5 100644 --- a/flying-saucer-examples/src/main/java/BrowsePanel.java +++ b/flying-saucer-examples/src/main/java/BrowsePanel.java @@ -28,6 +28,7 @@ import org.xhtmlrenderer.swing.SwingReplacedElementFactory; import org.xhtmlrenderer.util.GeneralUtil; +import javax.annotation.Nonnull; import javax.swing.*; import java.awt.*; import java.io.File; @@ -104,12 +105,12 @@ public void documentLoaded() { frame.setTitle(panel.getDocumentTitle()); } - public void onLayoutException(Throwable t) { + public void onLayoutException(@Nonnull Throwable t) { panel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); panel.setDocument(getErrorDocument("can't layout: " + t.getMessage()).getDocument()); } - public void onRenderException(Throwable t) { + public void onRenderException(@Nonnull Throwable t) { panel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); panel.setDocument(getErrorDocument("can't render: " + t.getMessage()).getDocument()); } diff --git a/flying-saucer-examples/src/main/java/Printer.java b/flying-saucer-examples/src/main/java/Printer.java index d3e3adaeb..02f3d9d97 100644 --- a/flying-saucer-examples/src/main/java/Printer.java +++ b/flying-saucer-examples/src/main/java/Printer.java @@ -23,6 +23,7 @@ import org.xhtmlrenderer.swing.Java2DRenderer; import org.xhtmlrenderer.swing.NaiveUserAgent; +import javax.annotation.ParametersAreNonnullByDefault; import javax.print.Doc; import javax.print.DocFlavor; import javax.print.DocPrintJob; @@ -48,10 +49,9 @@ /** - - * * @author rk */ +@ParametersAreNonnullByDefault public class Printer implements Runnable, DocumentListener, Printable, PrintJobListener { private final static String template = "printingtemplate.xhtml"; @@ -193,7 +193,6 @@ public static void main(String[] args) { } else { System.out.println("usage: "); } - } public void documentStarted() { @@ -205,11 +204,9 @@ public void documentLoaded() { } public void onLayoutException(Throwable t) { - } public void onRenderException(Throwable t) { - } public int print(Graphics graphics, PageFormat pf, int pi) { diff --git a/flying-saucer-examples/src/main/java/org/xhtmlrenderer/demo/aboutbox/DemoUserAgent.java b/flying-saucer-examples/src/main/java/org/xhtmlrenderer/demo/aboutbox/DemoUserAgent.java index f4a332392..a1a0a0829 100644 --- a/flying-saucer-examples/src/main/java/org/xhtmlrenderer/demo/aboutbox/DemoUserAgent.java +++ b/flying-saucer-examples/src/main/java/org/xhtmlrenderer/demo/aboutbox/DemoUserAgent.java @@ -27,6 +27,9 @@ import org.xhtmlrenderer.util.Uu; import org.xhtmlrenderer.util.XRLog; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; @@ -50,6 +53,7 @@ * Date: 2005-jun-15 * Time: 07:38:59 */ +@ParametersAreNonnullByDefault public class DemoUserAgent implements UserAgentCallback { private String baseUrl; private int index = -1; @@ -68,19 +72,18 @@ protected boolean removeEldestEntry(Map.Entry eldest) { }; @Override - public CSSResource getCSSResource(String uri) { - InputStream is = null; - uri = resolveURI(uri); + public CSSResource getCSSResource(String resourceUri) { + String uri = resolveURI(resourceUri); try { URLConnection uc = new URL(uri).openConnection(); uc.connect(); - is = uc.getInputStream(); + return new CSSResource(uc.getInputStream()); } catch (MalformedURLException e) { XRLog.exception("bad URL given: " + uri, e); } catch (IOException e) { XRLog.exception("IO problem for " + uri, e); } - return new CSSResource(is); + return new CSSResource(null); } @Override @@ -114,6 +117,8 @@ public ImageResource getImageResource(String uri) { } @Override + @Nullable + @CheckReturnValue public byte[] getBinaryResource(String uri) { InputStream is = null; try { @@ -180,7 +185,8 @@ public XMLResource getXMLResource(String uri) { } @Override - public boolean isVisited(String uri) { + @CheckReturnValue + public boolean isVisited(@Nullable String uri) { if (uri == null) return false; uri = resolveURI(uri); return history.contains(uri); @@ -201,10 +207,12 @@ public void setBaseURL(String url) { } @Override - public String resolveURI(String uri) { + @Nullable + @CheckReturnValue + public String resolveURI(@Nullable String uri) { URL ref = null; if (uri == null) return baseUrl; - if (uri.trim().equals("")) return baseUrl;//jar URLs don't resolve this right + if (uri.trim().isEmpty()) return baseUrl;//jar URLs don't resolve this right if (uri.startsWith("demo:")) { DemoMarker marker = new DemoMarker(); String short_url = uri.substring(5); diff --git a/flying-saucer-examples/src/main/java/org/xhtmlrenderer/demo/browser/BrowserPanel.java b/flying-saucer-examples/src/main/java/org/xhtmlrenderer/demo/browser/BrowserPanel.java index d30ba77c5..abcf8bbc0 100755 --- a/flying-saucer-examples/src/main/java/org/xhtmlrenderer/demo/browser/BrowserPanel.java +++ b/flying-saucer-examples/src/main/java/org/xhtmlrenderer/demo/browser/BrowserPanel.java @@ -35,6 +35,7 @@ import org.xhtmlrenderer.util.XRLog; import org.xhtmlrenderer.util.XRRuntimeException; +import javax.annotation.ParametersAreNonnullByDefault; import javax.swing.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -49,25 +50,26 @@ import java.util.logging.Level; import java.util.logging.Logger; +@ParametersAreNonnullByDefault public class BrowserPanel extends JPanel implements DocumentListener { private static final long serialVersionUID = 1L; - JButton forward; - JButton backward; - JButton stop; - JButton reload; - JButton goHome; - JButton font_inc; - JButton font_rst; - JButton font_dec; - JButton print; + private JButton forward; + private JButton backward; + private JButton stop; + private JButton reload; + private JButton goHome; + private JButton font_inc; + private JButton font_rst; + private JButton font_dec; + private JButton print; JTextField url; BrowserStatus status; public ScalableXHTMLPanel view; - JScrollPane scroll; - BrowserStartup root; - BrowserPanelListener listener; - JButton print_preview; + private JScrollPane scroll; + private BrowserStartup root; + private BrowserPanelListener listener; + private JButton print_preview; private static final Logger logger = Logger.getLogger("app.browser"); diff --git a/flying-saucer-examples/src/main/java/org/xhtmlrenderer/demo/browser/PanelManager.java b/flying-saucer-examples/src/main/java/org/xhtmlrenderer/demo/browser/PanelManager.java index 5a1805022..5bc23c644 100644 --- a/flying-saucer-examples/src/main/java/org/xhtmlrenderer/demo/browser/PanelManager.java +++ b/flying-saucer-examples/src/main/java/org/xhtmlrenderer/demo/browser/PanelManager.java @@ -26,6 +26,8 @@ import org.xhtmlrenderer.util.XRLog; import org.xml.sax.InputSource; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nullable; import javax.xml.transform.sax.SAXSource; import java.io.File; import java.io.IOException; @@ -53,18 +55,16 @@ public class PanelManager extends DelegatingUserAgent { private int index = -1; private final List history = new ArrayList<>(); - - /** - * {@inheritdoc}. - */ @Override - public String resolveURI(String uri) { + @Nullable + @CheckReturnValue + public String resolveURI(@Nullable String uri) { final String burl = getBaseURL(); URL ref = null; if (uri == null) return burl; - if (uri.trim().equals("")) return burl; //jar URLs don't resolve this right + if (uri.trim().isEmpty()) return burl; //jar URLs don't resolve this right if (uri.startsWith("demo:")) { DemoMarker marker = new DemoMarker(); @@ -106,9 +106,6 @@ public String resolveURI(String uri) { return ref.toExternalForm(); } - /** - * {@inheritDoc} - */ @Override public XMLResource getXMLResource(String uri) { uri = resolveURI(uri); diff --git a/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsEnhancer.java b/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsEnhancer.java index 3bd42b8d0..9aca19ed1 100644 --- a/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsEnhancer.java +++ b/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsEnhancer.java @@ -52,10 +52,7 @@ public NonBreakPointsEnhancer() { this(new NonBreakPointsLoaderImpl()); } - /** - * For test only - */ - /*package*/ NonBreakPointsEnhancer(NonBreakPointsLoader loader) { + NonBreakPointsEnhancer(NonBreakPointsLoader loader) { this.loader = loader; } @@ -64,7 +61,6 @@ public String enhance(String input, String lang) { if (input.isEmpty()) return ""; if (lang == null || lang.isEmpty()) return input; List rules = loader.loadNBSP(lang); - if (rules == null) return input; for (String r : rules) { Matcher m = Pattern.compile(r).matcher(input); if (m.groupCount() != 3) { diff --git a/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoader.java b/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoader.java index f7fb15759..c3bf11008 100644 --- a/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoader.java +++ b/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoader.java @@ -18,14 +18,20 @@ */ package org.xhtmlrenderer.fop.nbsp; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import java.util.List; /** * @author Lukas Zaruba, lukas.zaruba@gmail.com */ +@ParametersAreNonnullByDefault public interface NonBreakPointsLoader { - - List loadNBSP(String lang); + @Nonnull + @CheckReturnValue + List loadNBSP(@Nullable String lang); } diff --git a/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImpl.java b/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImpl.java index b2ed4f6c8..172cd2a13 100644 --- a/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImpl.java +++ b/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImpl.java @@ -18,6 +18,10 @@ */ package org.xhtmlrenderer.fop.nbsp; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -26,26 +30,32 @@ import java.util.List; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Collections.emptyList; /** * @author Lukas Zaruba, lukas.zaruba@gmail.com */ +@ParametersAreNonnullByDefault public class NonBreakPointsLoaderImpl implements NonBreakPointsLoader { @Override - public List loadNBSP(String lang) { + @Nonnull + @CheckReturnValue + public List loadNBSP(@Nullable String lang) { if (lang == null || lang.isEmpty()) { throw new IllegalArgumentException("Lang must be filled to search for file!"); } List result = loadForKey(lang); if (result == null) { int index = lang.indexOf('_'); - if (index < 0) return null; // cannot split key + if (index < 0) return emptyList(); // cannot split key result = loadForKey(lang.substring(0, index)); } - return result; + return result == null ? emptyList() : result; } + @Nullable + @CheckReturnValue private List loadForKey(String lang) { String path = "non-break-spaces/" + lang + ".nbsp"; InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); diff --git a/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsEnhancerTest.java b/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsEnhancerTest.java index cf7e28711..733fd255d 100644 --- a/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsEnhancerTest.java +++ b/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsEnhancerTest.java @@ -22,6 +22,7 @@ import java.util.Arrays; +import static java.util.Collections.emptyList; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -52,7 +53,7 @@ public void testEmptyInput() { @Test public void noDefinition() { - NonBreakPointsLoader nullLoader = lang -> null; + NonBreakPointsLoader nullLoader = lang -> emptyList(); assertEquals("some text with spaces", new NonBreakPointsEnhancer(nullLoader).enhance("some text with spaces", "cs")); } @@ -61,7 +62,7 @@ public void testLoaderConfiguration() { final String[] c = new String[] {null}; NonBreakPointsLoader capturingLoader = lang -> { c[0] = lang; - return null; + return emptyList(); }; new NonBreakPointsEnhancer(capturingLoader).enhance("some text with spaces", "cs"); assertEquals("cs", c[0]); diff --git a/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/simple/PDFRenderer.java b/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/simple/PDFRenderer.java index 279e1ef9a..8378f50ac 100644 --- a/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/simple/PDFRenderer.java +++ b/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/simple/PDFRenderer.java @@ -4,6 +4,10 @@ import com.itextpdf.text.pdf.PdfWriter; import org.xhtmlrenderer.pdf.ITextRenderer; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -13,6 +17,7 @@ import java.util.Map; import static java.nio.file.Files.newOutputStream; +import static java.util.Objects.requireNonNull; /** *

@@ -30,6 +35,7 @@ * @author Pete Brant * @author Patrick Wright */ +@ParametersAreNonnullByDefault public class PDFRenderer { private static final Map versionMap = new HashMap<>(); @@ -52,11 +58,10 @@ public class PDFRenderer { * @throws DocumentException if an error occurred * while building the Document. */ - public static void renderToPDF(String url, String pdf) - throws IOException, DocumentException { - + public static void renderToPDF(String url, String pdf) throws IOException, DocumentException { renderToPDF(url, pdf, null); } + /** * Renders the XML file at the given URL as a PDF file * at the target location. @@ -69,7 +74,7 @@ public static void renderToPDF(String url, String pdf) * @throws DocumentException if an error occurred * while building the Document. */ - public static void renderToPDF(String url, String pdf, Character pdfVersion) + public static void renderToPDF(String url, String pdf, @Nullable Character pdfVersion) throws IOException, DocumentException { ITextRenderer renderer = new ITextRenderer(); @@ -88,9 +93,7 @@ public static void renderToPDF(String url, String pdf, Character pdfVersion) * @throws DocumentException if an error occurred * while building the Document. */ - public static void renderToPDF(File file, String pdf) - throws IOException, DocumentException { - + public static void renderToPDF(File file, String pdf) throws IOException, DocumentException { renderToPDF(file, pdf, null); } @@ -105,7 +108,7 @@ public static void renderToPDF(File file, String pdf) * @throws DocumentException if an error occurred * while building the Document. */ - public static void renderToPDF(File file, String pdf, Character pdfVersion) + public static void renderToPDF(File file, String pdf, @Nullable Character pdfVersion) throws IOException, DocumentException { ITextRenderer renderer = new ITextRenderer(); @@ -123,6 +126,8 @@ public static void renderToPDF(File file, String pdf, Character pdfVersion) * @throws DocumentException if an error occurred * while building the Document. */ + @Nonnull + @CheckReturnValue public static byte[] renderToPDFBytes(byte[] bytes) throws IOException, DocumentException { @@ -139,7 +144,9 @@ public static byte[] renderToPDFBytes(byte[] bytes) * @throws DocumentException if an error occurred * while building the Document. */ - public static byte[] renderToPDFBytes(byte[] bytes, Character pdfVersion) + @Nonnull + @CheckReturnValue + public static byte[] renderToPDFBytes(byte[] bytes, @Nullable Character pdfVersion) throws IOException, DocumentException { ITextRenderer renderer = new ITextRenderer(); @@ -159,9 +166,7 @@ public static byte[] renderToPDFBytes(byte[] bytes, Character pdfVersion) * @throws DocumentException if an error occurred * while building the Document. */ - public static void renderToPDF(byte[] bytes, String pdf) - throws IOException, DocumentException { - + public static void renderToPDF(byte[] bytes, String pdf) throws IOException, DocumentException { renderToPDF(bytes, pdf, null); } @@ -176,7 +181,7 @@ public static void renderToPDF(byte[] bytes, String pdf) * @throws DocumentException if an error occurred * while building the Document. */ - public static void renderToPDF(byte[] bytes, String pdf, Character pdfVersion) + public static void renderToPDF(byte[] bytes, String pdf, @Nullable Character pdfVersion) throws IOException, DocumentException { ITextRenderer renderer = new ITextRenderer(); @@ -258,17 +263,19 @@ public static void main(String[] args) throws IOException, DocumentException { } } + @Nonnull + @CheckReturnValue private static Character checkVersion(String version) { final Character val = versionMap.get(version.trim()); if (val == null) { usage("Invalid PDF version number; use 1.2 through 1.7"); } - return val; + return requireNonNull(val); } /** prints out usage information, with optional error message */ - private static void usage(String err) { + private static void usage(@Nullable String err) { if (err != null && !err.isEmpty()) { System.err.println("==>" + err); } diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/AbstractFormField.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/AbstractFormField.java index eb8e4af22..e03123c88 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/AbstractFormField.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/AbstractFormField.java @@ -28,8 +28,10 @@ import org.xhtmlrenderer.render.BlockBox; import org.xhtmlrenderer.util.Util; +import javax.annotation.ParametersAreNonnullByDefault; import java.awt.*; +@ParametersAreNonnullByDefault public abstract class AbstractFormField implements ITextReplacedElement { protected static final String DEFAULT_CHECKED_STATE = "Yes"; protected static final String OFF_STATE = "Off"; // required per the spec diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/BookmarkElement.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/BookmarkElement.java index e74f06bf3..b8812fead 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/BookmarkElement.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/BookmarkElement.java @@ -19,12 +19,14 @@ */ package org.xhtmlrenderer.pdf; -import java.awt.Point; - import org.xhtmlrenderer.layout.LayoutContext; import org.xhtmlrenderer.render.BlockBox; import org.xhtmlrenderer.render.RenderingContext; +import javax.annotation.ParametersAreNonnullByDefault; +import java.awt.*; + +@ParametersAreNonnullByDefault public class BookmarkElement implements ITextReplacedElement { private Point _location = new Point(0, 0); private String _anchorName; diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/CheckboxFormField.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/CheckboxFormField.java index b7ab4f111..fb3cad5b3 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/CheckboxFormField.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/CheckboxFormField.java @@ -19,23 +19,28 @@ */ package org.xhtmlrenderer.pdf; -import java.awt.Color; -import java.io.IOException; - import com.lowagie.text.DocumentException; import com.lowagie.text.Rectangle; -import com.lowagie.text.pdf.*; +import com.lowagie.text.pdf.BaseField; +import com.lowagie.text.pdf.PdfBorderDictionary; +import com.lowagie.text.pdf.PdfContentByte; +import com.lowagie.text.pdf.PdfFormField; +import com.lowagie.text.pdf.PdfWriter; +import com.lowagie.text.pdf.RadioCheckField; import org.w3c.dom.Element; import org.xhtmlrenderer.layout.LayoutContext; import org.xhtmlrenderer.render.BlockBox; import org.xhtmlrenderer.render.RenderingContext; -public class CheckboxFormField extends AbstractFormField -{ +import javax.annotation.ParametersAreNonnullByDefault; +import java.awt.*; +import java.io.IOException; + +@ParametersAreNonnullByDefault +public class CheckboxFormField extends AbstractFormField { private static final String FIELD_TYPE = "Checkbox"; - public CheckboxFormField(LayoutContext c, BlockBox box, int cssWidth, int cssHeight) - { + public CheckboxFormField(LayoutContext c, BlockBox box, int cssWidth, int cssHeight) { initDimensions(c, box, cssWidth, cssHeight); } @@ -44,8 +49,7 @@ protected String getFieldType() return FIELD_TYPE; } - public void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box) - { + public void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box) { PdfContentByte cb = outputDevice.getCurrentPage(); PdfWriter writer = outputDevice.getWriter(); diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/EmptyReplacedElement.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/EmptyReplacedElement.java index e518454f1..bf32b3ce9 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/EmptyReplacedElement.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/EmptyReplacedElement.java @@ -7,14 +7,15 @@ import org.xhtmlrenderer.render.BlockBox; import org.xhtmlrenderer.render.RenderingContext; +import javax.annotation.ParametersAreNonnullByDefault; import java.awt.*; /** * User: beck * Date: 11/4/11 */ -public class EmptyReplacedElement extends AbstractFormField -{ +@ParametersAreNonnullByDefault +public class EmptyReplacedElement extends AbstractFormField { private static final String FIELD_TYPE = "Hidden"; private final int _width; @@ -22,8 +23,7 @@ public class EmptyReplacedElement extends AbstractFormField private Point _location = new Point(0, 0); - public EmptyReplacedElement(int width, int height) - { + public EmptyReplacedElement(int width, int height) { _width = width; _height = height; } @@ -73,8 +73,7 @@ protected String getFieldType() return FIELD_TYPE; } - public void detach(LayoutContext c) - { + public void detach(LayoutContext c) { } public boolean isRequiresInteractivePaint() diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextImageElement.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextImageElement.java index 43b3c2b43..898fe51b3 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextImageElement.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextImageElement.java @@ -25,8 +25,10 @@ import org.xhtmlrenderer.render.BlockBox; import org.xhtmlrenderer.render.RenderingContext; +import javax.annotation.ParametersAreNonnullByDefault; import java.awt.*; +@ParametersAreNonnullByDefault public class ITextImageElement implements ITextReplacedElement { private final FSImage _image; @@ -64,8 +66,7 @@ public boolean isRequiresInteractivePaint() { return false; } - public void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box) - { + public void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box) { Rectangle contentBounds = box.getContentAreaEdge(box.getAbsX(), box.getAbsY(), c); ReplacedElement element = box.getReplacedElement(); outputDevice.drawImage( diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextOutputDevice.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextOutputDevice.java index 4b63c080e..fdff3b172 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextOutputDevice.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextOutputDevice.java @@ -923,7 +923,7 @@ private void writeOutline(RenderingContext c, Box root) { if (_bookmarks.isEmpty()) { _bookmarks = HTMLOutline.generate(root.getElement(), root); } - if (_bookmarks.size() > 0) { + if (!_bookmarks.isEmpty()) { _writer.setViewerPreferences(PdfWriter.PageModeUseOutlines); writeBookmarks(c, root, _writer.getRootOutline(), _bookmarks); } @@ -1197,7 +1197,7 @@ public void setMetadata(String name, String value) { } // Class for storing metadata element name/content pairs from the head - // section of an xhtml document. + // section of xhtml document. private static class Metadata { private String _name; private String _content; diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextReplacedElement.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextReplacedElement.java index 9225a38a6..918a6d2c0 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextReplacedElement.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextReplacedElement.java @@ -23,7 +23,9 @@ import org.xhtmlrenderer.render.BlockBox; import org.xhtmlrenderer.render.RenderingContext; -public interface ITextReplacedElement extends ReplacedElement -{ - public void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box); +import javax.annotation.ParametersAreNonnullByDefault; + +@ParametersAreNonnullByDefault +public interface ITextReplacedElement extends ReplacedElement { + void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box); } diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/RadioButtonFormField.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/RadioButtonFormField.java index 40ac1dd81..554820ffb 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/RadioButtonFormField.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/RadioButtonFormField.java @@ -34,9 +34,11 @@ import org.xhtmlrenderer.render.PageBox; import org.xhtmlrenderer.render.RenderingContext; +import javax.annotation.ParametersAreNonnullByDefault; import java.awt.*; import java.util.List; +@ParametersAreNonnullByDefault public final class RadioButtonFormField extends AbstractFormField { private static final String FIELD_TYPE = "RadioButton"; diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/SelectFormField.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/SelectFormField.java index c77a2cbb7..9985278db 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/SelectFormField.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/SelectFormField.java @@ -33,12 +33,14 @@ import org.xhtmlrenderer.render.RenderingContext; import org.xhtmlrenderer.util.Util; +import javax.annotation.ParametersAreNonnullByDefault; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import static org.xhtmlrenderer.util.TextUtil.readTextContentOrNull; +@ParametersAreNonnullByDefault public class SelectFormField extends AbstractFormField { private static final String FIELD_TYPE = "Select"; diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/TextAreaFormField.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/TextAreaFormField.java index cf2ae1c35..be27d040f 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/TextAreaFormField.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/TextAreaFormField.java @@ -23,6 +23,9 @@ import org.xhtmlrenderer.render.BlockBox; import org.xhtmlrenderer.render.RenderingContext; +import javax.annotation.ParametersAreNonnullByDefault; + +@ParametersAreNonnullByDefault public class TextAreaFormField extends AbstractFormField { private static final String FIELD_TYPE = "TextArea"; diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/TextFormField.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/TextFormField.java index 135cd77c1..7104e069e 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/TextFormField.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/TextFormField.java @@ -34,10 +34,11 @@ import org.xhtmlrenderer.render.RenderingContext; import org.xhtmlrenderer.util.Util; +import javax.annotation.ParametersAreNonnullByDefault; import java.io.IOException; -public class TextFormField extends AbstractFormField -{ +@ParametersAreNonnullByDefault +public class TextFormField extends AbstractFormField { private static final String FIELD_TYPE = "Text"; private static final int DEFAULT_SIZE = 15; @@ -46,8 +47,7 @@ public class TextFormField extends AbstractFormField private boolean multiline = false; - public TextFormField(LayoutContext c, BlockBox box, int cssWidth, int cssHeight) - { + public TextFormField(LayoutContext c, BlockBox box, int cssWidth, int cssHeight) { initDimensions(c, box, cssWidth, cssHeight); float fontSize = box.getStyle().getFSFont(c).getSize2D(); @@ -55,27 +55,22 @@ public TextFormField(LayoutContext c, BlockBox box, int cssWidth, int cssHeight) _baseline = (int) (getHeight() / 2 + (fontSize * 0.3f)); } - protected void initDimensions(LayoutContext c, BlockBox box, int cssWidth, int cssHeight) - { - if (cssWidth != -1) - { + protected void initDimensions(LayoutContext c, BlockBox box, int cssWidth, int cssHeight) { + if (cssWidth != -1) { setWidth(cssWidth); } - else - { + else { setWidth(c.getTextRenderer().getWidth( c.getFontContext(), box.getStyle().getFSFont(c), spaces(getSize(box.getElement())))); } - if (cssHeight != -1) - { + if (cssHeight != -1) { setHeight(cssHeight); multiline = true; } - else - { + else { setHeight((int) (box.getStyle().getLineHeight(c))); } } diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/simple/PDFRenderer.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/simple/PDFRenderer.java index 34eac3b09..72fc723dc 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/simple/PDFRenderer.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/simple/PDFRenderer.java @@ -4,6 +4,8 @@ import com.lowagie.text.pdf.PdfWriter; import org.xhtmlrenderer.pdf.ITextRenderer; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import java.io.File; import java.io.IOException; import java.io.OutputStream; @@ -29,6 +31,7 @@ * @author Pete Brant * @author Patrick Wright */ +@ParametersAreNonnullByDefault public class PDFRenderer { private static final Map versionMap = new HashMap<>(); @@ -51,9 +54,7 @@ public class PDFRenderer { * @throws DocumentException if an error occurred * while building the Document. */ - public static void renderToPDF(String url, String pdf) - throws IOException, DocumentException { - + public static void renderToPDF(String url, String pdf) throws IOException, DocumentException { renderToPDF(url, pdf, null); } /** @@ -68,7 +69,7 @@ public static void renderToPDF(String url, String pdf) * @throws DocumentException if an error occurred * while building the Document. */ - public static void renderToPDF(String url, String pdf, Character pdfVersion) + public static void renderToPDF(String url, String pdf, @Nullable Character pdfVersion) throws IOException, DocumentException { ITextRenderer renderer = new ITextRenderer(); @@ -87,9 +88,7 @@ public static void renderToPDF(String url, String pdf, Character pdfVersion) * @throws DocumentException if an error occurred * while building the Document. */ - public static void renderToPDF(File file, String pdf) - throws IOException, DocumentException { - + public static void renderToPDF(File file, String pdf) throws IOException, DocumentException { renderToPDF(file, pdf, null); } @@ -104,7 +103,7 @@ public static void renderToPDF(File file, String pdf) * @throws DocumentException if an error occurred * while building the Document. */ - public static void renderToPDF(File file, String pdf, Character pdfVersion) + public static void renderToPDF(File file, String pdf, @Nullable Character pdfVersion) throws IOException, DocumentException { ITextRenderer renderer = new ITextRenderer(); @@ -116,8 +115,7 @@ public static void renderToPDF(File file, String pdf, Character pdfVersion) /** * Internal use, runs the render process */ - private static void doRenderToPDF(ITextRenderer renderer, String pdf) - throws IOException, DocumentException { + private static void doRenderToPDF(ITextRenderer renderer, String pdf) throws IOException, DocumentException { try (OutputStream os = newOutputStream(Paths.get(pdf))){ renderer.layout(); renderer.createPDF(os); @@ -167,7 +165,7 @@ private static Character checkVersion(String version) { /** prints out usage information, with optional error message */ - private static void usage(String err) { + private static void usage(@Nullable String err) { if (err != null && !err.isEmpty()) { System.err.println("==>" + err); } diff --git a/flying-saucer-swt-examples/src/main/java/org/xhtmlrenderer/demo/browser/swt/Browser.java b/flying-saucer-swt-examples/src/main/java/org/xhtmlrenderer/demo/browser/swt/Browser.java index 675488342..04a89434e 100644 --- a/flying-saucer-swt-examples/src/main/java/org/xhtmlrenderer/demo/browser/swt/Browser.java +++ b/flying-saucer-swt-examples/src/main/java/org/xhtmlrenderer/demo/browser/swt/Browser.java @@ -64,6 +64,7 @@ import org.xhtmlrenderer.event.DocumentListener; import org.xhtmlrenderer.simple.SWTXHTMLRenderer; +import javax.annotation.ParametersAreNonnullByDefault; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; @@ -71,6 +72,7 @@ import java.util.HashMap; import java.util.Map; +@ParametersAreNonnullByDefault public class Browser implements DisposeListener, DocumentListener { private final Shell _shell; diff --git a/flying-saucer-swt-examples/src/main/java/org/xhtmlrenderer/demo/browser/swt/BrowserUserAgent.java b/flying-saucer-swt-examples/src/main/java/org/xhtmlrenderer/demo/browser/swt/BrowserUserAgent.java index e4835c614..815d0a02e 100644 --- a/flying-saucer-swt-examples/src/main/java/org/xhtmlrenderer/demo/browser/swt/BrowserUserAgent.java +++ b/flying-saucer-swt-examples/src/main/java/org/xhtmlrenderer/demo/browser/swt/BrowserUserAgent.java @@ -33,6 +33,9 @@ import org.xhtmlrenderer.util.XRLog; import org.xml.sax.InputSource; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import javax.xml.transform.sax.SAXSource; import java.io.File; import java.io.IOException; @@ -44,6 +47,7 @@ import java.net.URL; import java.net.URLConnection; +@ParametersAreNonnullByDefault public class BrowserUserAgent extends NaiveUserAgent { private final DemosNavigation _demos; @@ -56,8 +60,13 @@ public BrowserUserAgent(Device device) { } @Override - public String resolveURI(String uri) { + @Nullable + @CheckReturnValue + public String resolveURI(@Nullable String uri) { + if (uri == null) return null; + final String burl = getBaseURL(); + if (uri.trim().isEmpty()) return burl; //jar URLs don't resolve this right URL ref = null; diff --git a/flying-saucer-swt/src/main/java/org/xhtmlrenderer/swt/NaiveUserAgent.java b/flying-saucer-swt/src/main/java/org/xhtmlrenderer/swt/NaiveUserAgent.java index 45fd65e8e..612f56bee 100644 --- a/flying-saucer-swt/src/main/java/org/xhtmlrenderer/swt/NaiveUserAgent.java +++ b/flying-saucer-swt/src/main/java/org/xhtmlrenderer/swt/NaiveUserAgent.java @@ -29,6 +29,9 @@ import org.xhtmlrenderer.util.ImageUtil; import org.xhtmlrenderer.util.XRLog; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -47,6 +50,7 @@ * * @author Vianney le Clément */ +@ParametersAreNonnullByDefault public class NaiveUserAgent implements UserAgentCallback { /** @@ -187,7 +191,9 @@ public void setBaseURL(String url) { } @Override - public String resolveURI(String uri) { + @Nullable + @CheckReturnValue + public String resolveURI(@Nullable String uri) { if (uri == null) return null; if (_baseURL == null) {//first try to set a base URL @@ -251,6 +257,8 @@ public void disposeCache() { } @Override + @Nullable + @CheckReturnValue public byte[] getBinaryResource(String uri) { InputStream is = getInputStream(uri); if (is==null) return null;