Skip to content

Commit ea542b7

Browse files
committed
Fix javadocs
1 parent 1e149ca commit ea542b7

File tree

11 files changed

+278
-115
lines changed

11 files changed

+278
-115
lines changed

src/main/java/webGrude/OkHttpBrowser.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public class OkHttpBrowser implements LinkVisitor {
4747
private OkHttpClient client;
4848
private final Webgrude webgrude = new Webgrude();
4949

50+
/**
51+
* Creates a new instance of OkHttpBrowser using the default Webgrude and OkHttpClient.
52+
*/
53+
public OkHttpBrowser() {
54+
// Default constructor
55+
}
56+
5057
/**
5158
* Loads content from url from the Page annotation on pageClass onto an instance of pageClass.
5259
*

src/main/java/webGrude/Webgrude.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,45 @@
2424
import java.util.regex.Matcher;
2525
import java.util.regex.Pattern;
2626

27+
/**
28+
* Core class responsible for mapping HTML or XML content to Java objects annotated with Webgrude annotations.
29+
* <p>
30+
* Use this to parse a page's contents and populate annotated fields with selected data.
31+
*/
2732
public class Webgrude {
2833

34+
35+
/**
36+
* Synthetic root tag used when parsing XML and HTML fragments so it always have a root.
37+
*/
2938
public static final String ROOT_FAKE = "rootFake";
39+
3040
private final boolean debug;
3141

32-
public Webgrude(){
42+
43+
/**
44+
* Creates a new Webgrude instance.
45+
*/
46+
public Webgrude() {
3347
debug = false;
3448
}
3549

36-
public Webgrude(boolean debug){
50+
/**
51+
* Creates a new Webgrude instance.
52+
*
53+
* @param debug whether to enable debug logging
54+
*/
55+
public Webgrude(boolean debug) {
3756
this.debug = debug;
3857
}
3958

40-
/***
41-
* Maps a Html string to a class with {@literal @}Selector annotations
42-
* @param pageContents
43-
* @param pageClass
44-
* @return the page instance
45-
* @param <T> The page class
59+
/**
60+
* Maps an HTML string to a class with {@literal @}Selector annotations.
61+
*
62+
* @param pageContents the HTML content to be parsed
63+
* @param pageClass the class with annotated fields to populate
64+
* @param <T> the type of the page class
65+
* @return an instance of the page class populated with data from the HTML
4666
*/
4767
public <T> T map(
4868
final String pageContents,
@@ -52,13 +72,14 @@ public <T> T map(
5272
return map(pageContents, pageClass, url);
5373
}
5474

55-
/***
56-
* Maps a Html string to a class with {@literal @}Selector annotations
57-
* @param pageContents
58-
* @param pageClass
59-
* @param baseUrl Used to populate links with the full href
60-
* @return the page instance
61-
* @param <T> The page class
75+
/**
76+
* Maps an HTML string to a class with {@literal @}Selector annotations.
77+
*
78+
* @param pageContents the HTML content to be parsed
79+
* @param pageClass the class with annotated fields to populate
80+
* @param baseUrl base URL used to resolve relative links
81+
* @param <T> the type of the page class
82+
* @return an instance of the page class populated with data from the HTML
6283
*/
6384
public <T> T map(
6485
final String pageContents,
@@ -405,6 +426,12 @@ private static String encodeOrThrow(final String p) {
405426
return URLEncoder.encode(p, StandardCharsets.UTF_8);
406427
}
407428

429+
/**
430+
* Checks whether the provided class is a type natively supported by Webgrude (String, Integer, Float, etc.).
431+
*
432+
* @param c the class to check
433+
* @return true if the class is known and directly mappable, false otherwise
434+
*/
408435
public boolean typeIsKnown(final Class c) {
409436
return c.equals(String.class) ||
410437
c.equals(Integer.class) || c.getSimpleName().equals("int") ||
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
package webGrude.http;
22

3-
3+
/**
4+
* Exception thrown when an error occurs during an HTTP GET operation.
5+
*/
46
public class GetException extends RuntimeException {
57

8+
/**
9+
* Constructs a new GetException with a specified cause and the resource that was being fetched.
10+
*
11+
* @param e the underlying exception that caused the failure
12+
* @param get the resource being fetched when the error occurred
13+
*/
614
public GetException(Exception e, String get) {
715
super("Error while getting " + get, e);
816
}
917

18+
/**
19+
* Constructs a new GetException with a message describing the resource that failed to be fetched.
20+
*
21+
* @param error description of the error or resource
22+
*/
1023
public GetException(String error) {
1124
super("Error while getting " + error);
1225
}
13-
14-
}
26+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package webGrude.http;
22

3+
/**
4+
* An interface for visiting links and retrieving their content.
5+
* <p>
6+
* Implementations of this interface define how to fetch the content of a given URL,
7+
* it can be an http fetch or a custom way.
8+
*/
39
public interface LinkVisitor {
10+
11+
/**
12+
* Visits the provided URL and returns its contents as a string.
13+
*
14+
* @param href the URL to visit
15+
* @return the contents of the page at the given URL
16+
*/
417
String visitLink(final String href);
518
}
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
package webGrude.mapping;
22

33
import org.jsoup.nodes.Element;
4-
54
import webGrude.mapping.annotations.Selector;
65

6+
/**
7+
* Exception thrown when a selected node cannot be mapped to the expected field type,
8+
* indicating incompatible types between the HTML element and the annotated field.
9+
*/
710
public class IncompatibleTypes extends Exception {
811

912
/**
13+
* Constructs a new IncompatibleTypes exception with details about the mapping failure.
1014
*
15+
* @param newInstance the object instance being populated
16+
* @param selectedNode the HTML element selected during parsing
17+
* @param selectorAnnotation the annotation used to select the element
18+
* @param fieldClass the expected type of the field
19+
* @param e the underlying exception that caused the type incompatibility
1120
*/
12-
private static final long serialVersionUID = 751653411036904421L;
13-
1421
public IncompatibleTypes(
15-
final Object newInstance,
16-
final Element selectedNode,
17-
final Selector selectorAnnotation,
18-
final Class<?> fieldClass,
19-
final Exception e
22+
final Object newInstance,
23+
final Element selectedNode,
24+
final Selector selectorAnnotation,
25+
final Class<?> fieldClass,
26+
final Exception e
2027
){
2128
super(
22-
"{\n"+
23-
"\tInstance type: "+newInstance.getClass().getName()+
24-
"\n\tField Type: "+fieldClass+
25-
"\n\tNode: "+selectedNode+
26-
"\n\tAnnotation: "+selectorAnnotation+
27-
"\n}",e);
29+
"{\n"+
30+
"\tInstance type: "+newInstance.getClass().getName()+
31+
"\n\tField Type: "+fieldClass+
32+
"\n\tNode: "+selectedNode+
33+
"\n\tAnnotation: "+selectorAnnotation+
34+
"\n}", e);
2835
}
29-
}
36+
}

src/main/java/webGrude/mapping/TooManyResultsException.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,30 @@
22

33
import org.jsoup.select.Elements;
44

5+
/**
6+
* Thrown when a CSS selector matches more than one element but the corresponding field
7+
* is not a collection type.
8+
* <p>
9+
* This exception is intended to alert the developer that a single-value field (e.g., {@code String})
10+
* is annotated with a selector that yields multiple elements in the HTML document.
11+
* In such cases, the field should be defined as a {@code List} of the appropriate type.
12+
*/
513
public class TooManyResultsException extends RuntimeException {
614

15+
/**
16+
* Constructs a new TooManyResultsException with details about the selector and matched elements.
17+
*
18+
* @param cssQuery the CSS selector used to find the elements
19+
* @param size the number of elements matched
20+
* @param elements the matched elements
21+
*/
722
public TooManyResultsException(
8-
final String cssQuery,
9-
final int size,
10-
final Elements elements
23+
final String cssQuery,
24+
final int size,
25+
final Elements elements
1126
) {
1227
super("The query '" + cssQuery + "' should return one result but returned "
13-
+ size + ". For more than one result a list should be used as the field type."
14-
+ elements);
28+
+ size + ". For more than one result a list should be used as the field type."
29+
+ elements);
1530
}
16-
17-
}
31+
}

src/main/java/webGrude/mapping/annotations/Selector.java

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,29 @@
33
import webGrude.OkHttpBrowser;
44

55
import java.lang.annotation.ElementType;
6-
import java.lang.annotation.Repeatable;
76
import java.lang.annotation.Retention;
87
import java.lang.annotation.RetentionPolicy;
98
import java.lang.annotation.Target;
109

1110
/**
12-
* Annotates a field to be mapped using a css selector.
11+
* Annotates a field to be mapped using a CSS selector.
1312
* <p>
14-
* A field annotated with this will receive the value corresponding to it's css
13+
* A field annotated with this will receive the value corresponding to its CSS
1514
* selector when mapped.
16-
* The field can be any of the following types (or its primitive):
15+
* The field can be one of the following types (or its primitive counterpart):
1716
* <ul>
18-
* <li>String</li>
19-
* <li>Float</li>
20-
* <li>Integer</li>
21-
* <li>Boolean</li>
22-
* <li>webGrude.mapping.elements.Link</li>
23-
* <li>org.jsoup.nodes.Element</li>
17+
* <li>String</li>
18+
* <li>Float</li>
19+
* <li>Integer</li>
20+
* <li>Boolean</li>
21+
* <li>{@code webGrude.mapping.elements.Link}</li>
22+
* <li>{@code org.jsoup.nodes.Element}</li>
2423
* </ul>
25-
* Or a List of any of these types.<br>
26-
* You can also set the field as an attribute value instead of it's text using
27-
* the attr annotation value. html and outerHtml are also valid values for attr.
28-
* For example:
24+
* Or a {@code List} of any of these types.
2925
* <p>
30-
* A field mapping a link with id foo
31-
* </p>
32-
* <pre>
33-
* {@code @Selector("#foo") String fooText;}
34-
* </pre>
35-
* <p>
36-
* A field mapping a link with id foo but receiving the href value
37-
* </p>
38-
* <pre>
39-
* {@code @Selector(value = "#foo", attr="href") String fooText;}
40-
* </pre>
41-
* <p>
42-
* A field mapping a link with id foo but receiving the href value
43-
* </p>
44-
* <pre>
45-
* {@code @Selector(value = "#foo", attr="href") String fooText;}
46-
* </pre>
47-
* <p>
48-
* A field mapping a the inner html code of a link with id foo
49-
* </p>
50-
* <pre>
51-
* {@code @Selector(value = "#foo", attr="html") String fooHtml;}
52-
* </pre>
53-
* <p>
54-
* A field mapping a the outer html code of a link with id foo
55-
* </p>
56-
* <pre>
57-
* {@code @Selector(value = "#foo", attr="outerHtml") String fooOuterHtml;}
58-
* </pre>
26+
* You can also extract an attribute's value instead of the element's text using
27+
* the {@code attr} attribute. Special values for {@code attr} include:
28+
* {@code "html"} (inner HTML) and {@code "outerHtml"} (entire HTML of the element).
5929
*
6030
* @author beothorn
6131
* @see webGrude.mapping.annotations.Page
@@ -65,9 +35,42 @@
6535
@Retention(RetentionPolicy.RUNTIME)
6636
@Target({ElementType.FIELD})
6737
public @interface Selector {
38+
39+
/**
40+
* The CSS selector used to locate the element(s).
41+
*
42+
* @return the CSS selector string
43+
*/
6844
String value();
45+
46+
/**
47+
* The attribute to extract from the selected element.
48+
* If empty, the text content is used.
49+
* Special values include "html" and "outerHtml".
50+
*
51+
* @return the attribute name or empty for text content
52+
*/
6953
String attr() default "";
54+
55+
/**
56+
* A formatting pattern (e.g., for dates or numbers) to apply when mapping the value.
57+
*
58+
* @return the format string
59+
*/
7060
String format() default "";
61+
62+
/**
63+
* The locale to use for formatting and parsing.
64+
* Should be in the form of a language tag, e.g., "en-US".
65+
*
66+
* @return the locale string
67+
*/
7168
String locale() default "";
69+
70+
/**
71+
* The default value to assign to the field if the selector does not match anything.
72+
*
73+
* @return the default value
74+
*/
7275
String defValue() default "";
7376
}

src/main/java/webGrude/mapping/annotations/XPath.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,19 @@
55
import java.lang.annotation.RetentionPolicy;
66
import java.lang.annotation.Target;
77

8+
/**
9+
* Indicates that a field should be mapped using an XPath expression instead of a CSS selector.
10+
* <p>
11+
* This annotation can be used in combination with {@code @Selector} to enable XPath-based node selection.
12+
*/
813
@Retention(RetentionPolicy.RUNTIME)
914
@Target({ElementType.FIELD})
10-
public @interface XPath{
15+
public @interface XPath {
16+
17+
/**
18+
* The XPath expression to use for selecting the element(s).
19+
*
20+
* @return the XPath query string
21+
*/
1122
String value() default "";
1223
}

0 commit comments

Comments
 (0)