Please disable your adblock and script blockers to view this page

Search this blog

Wednesday, 5 December 2018

Get All URLs From a Website Using Java Code


In this post, I am going to show you a Java class using that we can get all URLs from a website URL like a web crawler.

Here goes the Java Class





  1. package client;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.Reader;
  6. import java.net.MalformedURLException;
  7. import java.net.URI;
  8. import java.net.URISyntaxException;
  9. import java.net.URL;
  10. import java.util.Enumeration;
  11. import javax.swing.text.MutableAttributeSet;
  12. import javax.swing.text.html.HTML;
  13. import javax.swing.text.html.HTMLEditorKit;
  14. import javax.swing.text.html.parser.ParserDelegator;
  15. public class GetAllUrls {
  16. public GetAllUrls() {
  17. super();
  18. }
  19. public static void main(String[] args) {
  20. final String name = "http://www.awasthiashish.com";
  21. Reader r = null;
  22. try {
  23. URL u = new URL(name);
  24. InputStream in = u.openStream();
  25. r = new InputStreamReader(in);
  26. ParserDelegator hp = new ParserDelegator();
  27. hp.parse(r, new HTMLEditorKit.ParserCallback() {
  28. public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
  29. if (t == HTML.Tag.A) {
  30. Enumeration attrNames = a.getAttributeNames();
  31. while (attrNames.hasMoreElements()) {
  32. String exactUrl = "";
  33. Object key = attrNames.nextElement();
  34. if ("href".equals(key.toString())) {
  35. exactUrl = a.getAttribute(key).toString();
  36. if (!a.getAttribute(key).toString().startsWith("http://") &&
  37. !a.getAttribute(key).toString().startsWith("https://")) {
  38. if (a.getAttribute(key).toString().startsWith("/")) {
  39. exactUrl = name + a.getAttribute(key);
  40. } else {
  41. exactUrl = name.concat("/").concat(a.getAttribute(key).toString());
  42. }
  43. URI uri;
  44. try {
  45. uri = new java.net.URI(exactUrl);
  46. System.out.println(uri);
  47. } catch (URISyntaxException e) {
  48. }
  49. } else {
  50. URI uri;
  51. try {
  52. uri = new java.net.URI(exactUrl);
  53. System.out.println(uri);
  54. } catch (URISyntaxException e) {
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }, true);
  62. } catch (MalformedURLException e) {
  63. } catch (IOException e) {
  64. } finally {
  65. if (r != null) {
  66. try {
  67. r.close();
  68. } catch (IOException e) {
  69. }
  70. }
  71. }
  72. }
  73. }

and output on the console

Here you can see all the URLs linked to the home page of this website


Cheer 🙂 Happy Learning

Friday, 30 November 2018

Switching ADF Skin at run time using trinidad-config.xml

 This post talks about switching ADF skin at runtime and uses a managed bean variable to change skin name at runtime. Sometimes we want to use multiple themes for our application and in that case, we need this.

Created an ADF application, 2 different skins and a page in view controller project


Learn more about creating ADF skin in JDeveloper 

This page has a button to switch between skins and an outputText. Create a variable in the managed bean to hold skin name and button action listener to change skin variable value.

Here goes the managed bean code

  1. package switchadfskin.view.bean;
  2. import javax.faces.application.ViewHandler;
  3. import javax.faces.component.UIViewRoot;
  4. import javax.faces.context.FacesContext;
  5. import javax.faces.event.ActionEvent;
  6. public class SwitchADFSkinBean {
  7. public SwitchADFSkinBean() {
  8. }
  9. //Set default skin name
  10. private String skinName = "RedButton";
  11. public void setSkinName(String skinName) {
  12. this.skinName = skinName;
  13. }
  14. public String getSkinName() {
  15. return skinName;
  16. }
  17. /**Method to change skin at runtime
  18. * @param actionEvent
  19. */
  20. public void switchSkinAction(ActionEvent actionEvent) {
  21. //Change skin name
  22. if (skinName.equalsIgnoreCase("RedButton")) {
  23. this.setSkinName("GreenButton");
  24. } else {
  25. this.setSkinName("RedButton");
  26. }
  27. //Reload page
  28. refreshPage();
  29. }
  30. /**Method to refresh/reload page
  31. */
  32. protected void refreshPage() {
  33. FacesContext fctx = FacesContext.getCurrentInstance();
  34. String page = fctx.getViewRoot().getViewId();
  35. ViewHandler ViewH = fctx.getApplication().getViewHandler();
  36. UIViewRoot UIV = ViewH.createView(fctx, page);
  37. UIV.setViewId(page);
  38. fctx.setViewRoot(UIV);
  39. }
  40. }

The next step is to change the trinidad-config.xml file, set skin-family variable to refer to its value from managed bean so that when the user changes the bean variable then skin (CSS) is updated in the application.

  1. <?xml version="1.0" encoding="windows-1252"?>
  2. <trinidad-config xmlns="http://myfaces.apache.org/trinidad/config">
  3. <skin-family>#{SwitchADFSkinBean.skinName}</skin-family>
  4. </trinidad-config>

Now run and check application

The default skin is "RedButton" so it looks like this


and on click of the button

Sample ADF Application (JDeveloper 12.1.3) - Download

Cheers 🙂 Happy Learning