Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help refactoring a java code

Posted on 2016-08-09
8
Medium Priority
?
91 Views
Last Modified: 2016-08-13
HI,
I came across a code like :
public class HTMLSanitizer {
    private static Whitelist whitelist;
    private static Document.OutputSettings settings;

    static {
        whitelist = Whitelist.basicWithImages();
        whitelist.addTags("div", "s", "span");
        whitelist.addAttributes(":all", "style");
        settings = new Document.OutputSettings();
        settings.prettyPrint(false);
    }

    public static String sanitizeHTML(String unsafeHTML) {
        return Jsoup.clean(unsafeHTML, "", whitelist, settings);
    }
}

Open in new window


I am basically using this code in my spring web application. to clean html before saving it into the Database.
I am using Jsoup and using the above class like : HTMLSanitizer.sanitizeHTML(text)

I need advice from experts on the way this class is designed. Do you see any problems here. This is the only class where i have used static blocks...
Other approach will be  to make a constructor of this class and annotate this class with @Component and then autowire it where i have to use it...
Please suggest what are the advantages or disadvantages of this approach ?

Thanks
0
Comment
Question by:Rohit Bajaj
8 Comments
 
LVL 17

Expert Comment

by:krakatoa
ID: 41749161
I don't think we can help with actual code - you've got to do that bit yourself. Experts can only make suggestions. Sadly, I don't have one for your particular case. And if it's not your own code anyway, then there's no chance, since it could be an infringement of copyright or patent.
0
 

Author Comment

by:Rohit Bajaj
ID: 41749179
there is no copy right infringment.. its something me and my friend both working on.. and this is a modified code... not the actual one...just to give idea about the approach..
0
 

Author Comment

by:Rohit Bajaj
ID: 41749180
there is no copy right infringment.. its something me and my friend both working on.. and this is a modified code... not the actual one...just to give idea about the approach..
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:krakatoa
ID: 41749216
So then only the first condition applies.
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 41749221
By the way it's not " a code" (that is a term concerned with secrecy or cryptography) it's just "code".
0
 

Author Comment

by:Rohit Bajaj
ID: 41749330
HI,
I am not looking for coding help here.
I am looking for alternative approach and comparison between two approaches which i mentioned above..
0
 
LVL 3

Accepted Solution

by:
Mark Olsen earned 1400 total points
ID: 41749427
You could move the static block into a constructor, then instantiate the object and call a member function to perform the work.

Something like this:

public class HTMLSanitizer {
    private Whitelist whitelist;
    private Document.OutputSettings settings;

   public HTMLSanitizer () {
      // do stuff that was in the static block
    }

    public String sanitizeHTML(String unsafeHTML) {
        // do the work that was in the static version of this function
    }
}

Open in new window



Then a class that uses this would do something like this:

String htmlString = "<html><body>Hello world!</body></html>";

HTMLSanitizer sanitizer = new HTMLSanitizer();

String sanitized = sanitizer.sanitizeHTML(htmlString);

Open in new window


The downside is the member variables are created and initialized every time the sanitizer is instantiated. If it's going to be used often the created object could be stored for reuse. There is an argument for the static version the code is only initialized once and re-used throughout the life of the application.
0
 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 600 total points
ID: 41749668
If you think that it is very unlikely that you would ever have a situation that you would need to use multiple different settings/whitelist within the same app, then I would say the above is fine to use. But if you would need to use different settings or think that you may need this in the future, then it is probably better to refactor now to something like what you are talking about, ie. set the whitelist and settings up as beans, and inject them into an Object of the above class, and then inject that where you need to use it.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question