From 8c15ed432c95a1232144c822c640efaa325dd0f2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Sun, 15 Mar 2015 10:47:14 +0200 Subject: [PATCH] Clean up compiler warnings in Design.java (#19299) * Remove unused code * Properly close streams * Avoid unsafe casts * Fix inconsistent javadocs Change-Id: I20bb25c5925b6d5d56df2aedc5c4eb624b65e483 --- .../src/com/vaadin/ui/declarative/Design.java | 95 ++++++------------- 1 file changed, 31 insertions(+), 64 deletions(-) diff --git a/server/src/com/vaadin/ui/declarative/Design.java b/server/src/com/vaadin/ui/declarative/Design.java index 63a2638423..a2bd3e082e 100644 --- a/server/src/com/vaadin/ui/declarative/Design.java +++ b/server/src/com/vaadin/ui/declarative/Design.java @@ -22,6 +22,8 @@ import java.io.OutputStream; import java.io.Serializable; import java.lang.annotation.Annotation; import java.util.Collection; +import java.util.logging.Level; +import java.util.logging.Logger; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -360,59 +362,6 @@ public class Design implements Serializable { } - /** - * Constructs a component hierarchy from the design specified as an html - * document. The hierarchy must contain at most one top-level component, - * which should be located under <body>. Also invalid html containing - * the hierarchy without <html>, <head> and <body> tags is - * accepted. You can optionally pass an instance for the root component with - * some uninitialized instance fields. The fields will be automatically - * populated when parsing the design based on the component ids, local ids, - * and captions of the components in the design. - * - * @param html - * the html document describing the component design - * @param rootInstance - * the root instance with fields to be mapped to components in - * the design - * @return the DesignContext created while traversing the tree. The - * top-level component of the created component hierarchy can be - * accessed using result.getRootComponent(), where result is the - * object returned by this method. - * @throws IOException - */ - private static DesignContext parse(InputStream html, Component rootInstance) { - Document doc = parse(html); - return designToComponentTree(doc, rootInstance); - } - - /** - * Constructs a component hierarchy from the design specified as an html - * document given as a string. The hierarchy must contain at most one - * top-level component, which should be located under <body>. Also - * invalid html containing the hierarchy without <html>, <head> - * and <body> tags is accepted. You can optionally pass an instance - * for the root component with some uninitialized instance fields. The - * fields will be automatically populated when parsing the design based on - * the component ids, local ids, and captions of the components in the - * design. - * - * @param html - * the html document describing the component design - * @param rootInstance - * the root instance with fields to be mapped to components in - * the design - * @return the DesignContext created while traversing the tree. The - * top-level component of the created component hierarchy can be - * accessed using result.getRootComponent(), where result is the - * object returned by this method. - * @throws IOException - */ - private static DesignContext parse(String html, Component rootInstance) { - Document doc = Jsoup.parse(html); - return designToComponentTree(doc, rootInstance); - } - /** * Constructs a component hierarchy from the design specified as an html * tree. @@ -602,13 +551,23 @@ public class Design implements Serializable { throw new DesignException("Unable to find design file " + filename + " in " + annotatedClass.getPackage().getName()); } + try { + Document doc = parse(stream); + DesignContext context = designToComponentTree(doc, rootComponent, + annotatedClass); - Document doc = parse(stream); - DesignContext context = designToComponentTree(doc, rootComponent, - annotatedClass); - - return context; + return context; + } finally { + try { + stream.close(); + } catch (IOException e) { + getLogger().log(Level.FINE, "Error closing design stream", e); + } + } + } + private static Logger getLogger() { + return Logger.getLogger(Design.class.getName()); } /** @@ -638,7 +597,7 @@ public class Design implements Serializable { return null; } - return findClassWithAnnotation((Class) superClass, + return findClassWithAnnotation(superClass.asSubclass(Component.class), annotationClass); } @@ -670,7 +629,15 @@ public class Design implements Serializable { + " was not found in the package " + rootComponent.getClass().getPackage().getName()); } - return read(stream, rootComponent); + try { + return read(stream, rootComponent); + } finally { + try { + stream.close(); + } catch (IOException e) { + getLogger().log(Level.FINE, "Error closing design stream", e); + } + } } /** @@ -693,11 +660,11 @@ public class Design implements Serializable { * @throws DesignException * If the design could not be loaded */ - public static DesignContext read(InputStream design, Component rootComponent) { - if (design == null) { + public static DesignContext read(InputStream stream, Component rootComponent) { + if (stream == null) { throw new DesignException("Stream cannot be null"); } - Document doc = parse(design); + Document doc = parse(stream); DesignContext context = designToComponentTree(doc, rootComponent); return context; @@ -726,6 +693,7 @@ public class Design implements Serializable { * the output stream to write the design to. The design is always * written as UTF-8 * @throws IOException + * if writing fails */ public static void write(Component component, OutputStream outputStream) throws IOException { @@ -771,7 +739,6 @@ public class Design implements Serializable { doc.outputSettings().indentAmount(4); doc.outputSettings().syntax(Syntax.html); doc.outputSettings().prettyPrint(true); - doc.outputSettings(); outputStream.write(doc.html().getBytes(UTF8)); } -- 2.39.5