Skip to content

Commit 596ea78

Browse files
author
Francesco Cannizzaro
committed
add html annotation
1 parent 8ed3f31 commit 596ea78

File tree

7 files changed

+43
-15
lines changed

7 files changed

+43
-15
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,15 @@ class Content {
6868
String title;
6969
```
7070

71+
## @Html(String: query)
72+
```java
73+
@Html("div.inner")
74+
String html;
75+
```
76+
7177
## @Attr(String: query, String: attr)
7278
```java
73-
@Attr(value="a", attr="href")
79+
@Attr(query="a", attr="href")
7480
String href;
7581
```
7682

app/src/main/java/com/fcannizzaro/jsoup/sample/model/Package.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static class Info {
2424

2525
}
2626

27-
@Attr(value = "h3 a", attr = "href")
27+
@Attr(query = "h3 a", attr = "href")
2828
public String link;
2929

3030
@Text("h3")
@@ -33,7 +33,7 @@ public static class Info {
3333
@Text("p.type-ellipsis")
3434
public String description;
3535

36-
@Attr(value = "img.mts", attr = "src")
36+
@Attr(query = "img.mts", attr = "src")
3737
public String icon;
3838

3939
@Child

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 10
99
targetSdkVersion 25
1010
versionCode 1
11-
versionName "1.0"
11+
versionName "1.0.1"
1212
}
1313
buildTypes {
1414
release {

library/src/main/java/com/fcannizzaro/jsoup/annotations/JsoupProcessor.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fcannizzaro.jsoup.annotations.interfaces.Attr;
44
import com.fcannizzaro.jsoup.annotations.interfaces.Child;
55
import com.fcannizzaro.jsoup.annotations.interfaces.ForEach;
6+
import com.fcannizzaro.jsoup.annotations.interfaces.Html;
67
import com.fcannizzaro.jsoup.annotations.interfaces.Items;
78
import com.fcannizzaro.jsoup.annotations.interfaces.Selector;
89
import com.fcannizzaro.jsoup.annotations.interfaces.Text;
@@ -48,17 +49,18 @@ public static <T> T from(Element container, Class<T> clazz) {
4849
Text text = field.getAnnotation(Text.class);
4950
Child child = field.getAnnotation(Child.class);
5051
Items items = field.getAnnotation(Items.class);
52+
Html html = field.getAnnotation(Html.class);
5153
Attr attr = field.getAnnotation(Attr.class);
5254

5355
Object value = null;
5456

5557
if (items != null) {
58+
5659
ParameterizedType type = (ParameterizedType) field.getGenericType();
5760
Class<?> cz = (Class<?>) type.getActualTypeArguments()[0];
5861
value = fromList(container, cz);
59-
}
6062

61-
if (child != null) {
63+
} else if (child != null) {
6264

6365
Class cz = field.getType();
6466
Selector sel = (Selector) cz.getAnnotation(Selector.class);
@@ -67,25 +69,29 @@ public static <T> T from(Element container, Class<T> clazz) {
6769
value = from(element(container, sel.value()), cz);
6870
}
6971

70-
}
72+
} else if (selector != null) {
7173

72-
if (selector != null) {
7374
value = element(container, selector.value());
74-
}
7575

76-
if (text != null) {
76+
} else if (text != null) {
7777

7878
Element el = element(container, text.value());
7979

8080
if (el != null) {
8181
value = el.text();
8282
}
8383

84-
}
84+
} else if (html != null) {
85+
86+
Element el = element(container, html.value());
87+
88+
if (el != null) {
89+
value = el.html();
90+
}
8591

86-
if (attr != null) {
92+
} else if (attr != null) {
8793

88-
Element el = element(container, attr.value());
94+
Element el = element(container, attr.query());
8995

9096
if (el != null) {
9197
value = el.attr(attr.attr());

library/src/main/java/com/fcannizzaro/jsoup/annotations/interfaces/Attr.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
@Retention(RetentionPolicy.RUNTIME)
1313
@Target(ElementType.FIELD)
1414
public @interface Attr {
15-
String value();
15+
String query();
1616
String attr();
1717
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.fcannizzaro.jsoup.annotations.interfaces;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Created by Francesco Cannizzaro (fcannizzaro)
10+
*/
11+
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Target(ElementType.FIELD)
14+
public @interface Html {
15+
String value();
16+
}

library/src/main/java/com/fcannizzaro/jsoup/annotations/interfaces/Text.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
@Retention(RetentionPolicy.RUNTIME)
1313
@Target(ElementType.FIELD)
14-
public @interface Text{
14+
public @interface Text {
1515
String value();
1616
}

0 commit comments

Comments
 (0)