Solved

Help refactoring a java code

Posted on 2016-08-09
8
53 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 16

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
 
LVL 16

Expert Comment

by:krakatoa
ID: 41749216
So then only the first condition applies.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

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 350 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 35

Assisted Solution

by:mccarl
mccarl earned 150 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Receive file in Servlet 1 36
strCopies  challenge 17 73
mapBully challenge 6 89
HashMap Vs TreeMap 12 48
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now