Jsouper { public <T> ElementAdapter<T> adapter(Type type) { return adapter(type, Util.NO_ANNOTATIONS); } Jsouper(Builder builder); ElementAdapter<T> adapter(Type type); ElementAdapter<T> adapter(Type type, Set<? extends Annotation> annotations);  }
@Test public void customAdapter() throws Exception { Jsouper jsouper = new Jsouper.Builder() .add(Movie.class, new MovieAdapter()) .build(); ElementAdapter<Movie> elementAdapter = jsouper.adapter(Movie.class); Movie movie = elementAdapter.fromElement(document); assertThat(movie).isEqualTo(new Movie("Deadpool", "Sample description", 4)); }
@Test public void registerAdapterWithAnnotation() throws Exception { Jsouper jsouper = new Jsouper.Builder().build(); ElementAdapter<Movie> elementAdapter = jsouper.adapter(AnnotatedMovie.class); Movie movie = elementAdapter.fromElement(document); assertThat(movie).isEqualTo(new Movie("Deadpool", "Sample description", 4)); }
@Test public void composingAdapterFactory() throws Exception { Jsouper jsouper = new Jsouper.Builder().add(new MovieReviewAdapterFactory()) .add(Movie.class, new MovieAdapter()) .build(); ElementAdapter<MovieReview> movieReviewAdapter = jsouper.adapter(MovieReview.class); MovieReview movieReview = movieReviewAdapter.fromElement(document); assertThat(movieReview.movie).isEqualTo(new Movie("Deadpool", "Sample description", 4)); assertThat(movieReview.review).isEqualTo("Ryan Reynolds is adorable!"); }
@Test public void stringAdapter() throws Exception { Jsouper jsouper = new Jsouper.Builder().build(); ElementAdapter<String> stringAdapter = jsouper.adapter(String.class); String text = "Lorem ipsum dolor sit amet."; Document document = Jsoup.parse(String.format("<div class=\"lorem\">%s</div>", text)); assertThat(stringAdapter.fromElement(document)).isEqualTo(text); assertThat(stringAdapter.fromElement(document, ".lorem")).isEqualTo(text); }