Skip to content

Instantly share code, notes, and snippets.

@Dimanaux
Created September 16, 2020 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dimanaux/3d741b040fc8c62dc74f0ed4f341c4d8 to your computer and use it in GitHub Desktop.
Save Dimanaux/3d741b040fc8c62dc74f0ed4f341c4d8 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
class Main {
public static void main(String[] args) {
System.out.println(
tokenize(
("<html>\n" +
" <h1>content1</h1>\n" +
" <div>\n" +
" <h2>content2</h2>\n" +
" </div>\n" +
"</html>").replaceAll("\\s*", "")
)
);
}
static List<Token> tokenize(String html) {
List<Token> tokens = new ArrayList<>();
StringBuilder currentToken = new StringBuilder();
for (int i = 0; i < html.length(); i++) {
char symbol = html.charAt(i);
if (symbol == '<') {
if (currentToken.length() != 0) {
tokens.add(Token.text(currentToken.toString()));
}
currentToken = new StringBuilder();
} else if (symbol == '>') {
tokens.add(Token.tag(currentToken.toString()));
currentToken = new StringBuilder();
} else {
currentToken.append(symbol);
}
}
if (currentToken.length() != 0) {
tokens.add(Token.text(currentToken.toString()));
}
return tokens;
}
}
interface Token {
static Token tag(String tag) {
return tag.startsWith("/") ?
new ClosingTag(tag.substring(1)) :
new OpeningTag(tag);
}
static Token text(String text) {
return new Text(text);
}
class Text implements Token {
private final String value;
public Text(String value) {
this.value = value;
}
@Override
public String toString() {
return "(Text " + value + ")";
}
}
class ClosingTag implements Token {
private final String tag;
public ClosingTag(String tag) {
this.tag = tag;
}
@Override
public String toString() {
return "(Tag /" + tag + ")";
}
}
class OpeningTag implements Token {
private final String tag;
public OpeningTag(String tag) {
this.tag = tag;
}
@Override
public String toString() {
return "(Tag " + tag + ")";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment