Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- public class Funktional {
- /**
- * Find the position of the next break character, or the end of the string
- *
- * @param content The string to parse
- * @param currentPos Current position of the pointer in the string
- * @return Position of the next break character or the end of the string
- */
- private static int tokenEndAt(String content, int currentPos) {
- if (currentPos >= content.length())
- return content.length(); // end of the whole string
- char currentChar = content.charAt(currentPos);
- if ((currentChar == '<') || (currentChar == '>')) {
- return currentPos; // return the token's end index
- }
- // else the token not ended, move to the next position
- return tokenEndAt(content, currentPos + 1);
- }
- /**
- * Parse a string and find the tokens inside
- *
- * @param content The string to parse
- * @param startPos Start position of a new token
- * @return An ArrayList containing all found, non-empty tokens.
- */
- public static ArrayList<String> createTokenList(String content, int startPos) {
- if (startPos >= content.length()) {
- return new ArrayList<>(); // end of the whole string, every result will be appended to this
- }
- // start finding a new token
- int endPos = tokenEndAt(content, startPos);
- // append the found token into the list
- ArrayList<String> tokens = createTokenList(content, endPos + 1);
- String tokenFound = content.substring(startPos, endPos)
- .replaceAll("[\\r\\n\\t]", ""); // clean the spaces;
- // Only non-empty counts
- if (tokenFound.length() > 0)
- tokens.add(0, tokenFound);
- return tokens;
- }
- /**
- * Println an ArrayList without using iterative loop
- * @param list The ArrayList to print
- * @param index Current position of the pointer
- */
- private static void printRecursive(ArrayList list, int index) {
- if (index >= list.size())
- return;
- System.out.println(list.get(index));
- printRecursive(list, index + 1);
- }
- /**
- * Find the position of the closing tag of a given opening tag
- * @param tokens The list of token extracted
- * @param openTagName The name of the opening tag
- * @param index Current position of the pointer in the list
- * @return The closing tag's position of the given tag
- */
- private static int getCloseTagPos(ArrayList<String> tokens, String openTagName, int index) {
- if (index >= tokens.size())
- return -1;
- String currentToken = tokens.get(index).substring(1); //remove the "/" char
- if (currentToken.equals(openTagName)) {
- return index;
- }
- return getCloseTagPos(tokens, openTagName, index + 1);
- }
- /**
- * Find the content of a given opening tag
- * @param tokens The list of token extracted
- * @param openTagPos Position of the opening tag in token list
- * @return A sublist of tokens, (inclusive) from the opening tag to its closing tag
- */
- private static ArrayList<String> getTagContent(ArrayList<String> tokens, int openTagPos) {
- String tagName = tokens.get(openTagPos);
- int closeTagPos = getCloseTagPos(tokens, tagName, openTagPos + 1);
- return new ArrayList<>(tokens.subList(openTagPos, closeTagPos + 1));
- }
- private static Track extractTrack(ArrayList<String> tokens, int index, Track currentTrack) {
- if (index >= tokens.size())
- return currentTrack;
- String currentToken = tokens.get(index);
- boolean isClosingTag = currentToken.charAt(0) == '/';
- if (!isClosingTag) {
- String tagContent = getTagContent(tokens, index).get(1);
- switch (currentToken) {
- case "title" -> currentTrack.title = tagContent;
- case "length" -> currentTrack.length = tagContent;
- case "rating" -> currentTrack.rating = Integer.parseInt(tagContent);
- case "feature" -> currentTrack.features.add(tagContent);
- case "writing" -> currentTrack.writers.add(tagContent);
- }
- index += 2; // <tag>content<tag> => each tag contains 3 token => must += 2 after extracting
- }
- return extractTrack(tokens, index + 1, currentTrack);
- }
- public static Album extractAlbum(ArrayList<String> tokens, int index, Album currentAlbum) {
- if (index >= tokens.size())
- return currentAlbum;
- String currentToken = tokens.get(index);
- boolean isClosingTag = currentToken.charAt(0) == '/';
- if (!isClosingTag) {
- if (currentToken.equals("track")) {
- // Tracks should be handled carefully
- ArrayList<String> trackContent = getTagContent(tokens, index);
- // trackContent[0] == "track" => start at 1
- Track currentTrack = extractTrack(trackContent, 1, new Track());
- currentAlbum.tracks.add(currentTrack);
- index = getCloseTagPos(tokens, currentToken, index); // move index to the closing tag
- } else {
- // Normal attributes can be processed in bulk
- String tagContent = getTagContent(tokens, index).get(1);
- switch (currentToken) {
- case "artist" -> currentAlbum.artist = tagContent;
- case "title" -> currentAlbum.title = tagContent;
- case "date" -> currentAlbum.date = tagContent;
- }
- index += 2; // <tag>content<tag> => each tag contains 3 token => must += 2 after extracting
- }
- }
- return extractAlbum(tokens, index + 1, currentAlbum);
- }
- public static ArrayList<Album> parseFile(ArrayList<String> tokens, int index, ArrayList<Album> albumList) {
- if (index >= tokens.size())
- return albumList;
- String currentToken = tokens.get(index);
- if (currentToken.equals("album")) {
- ArrayList<String> albumContent = getTagContent(tokens, index);
- // albumContent[0] == "album" => start at 1
- Album currentAlbum = extractAlbum(albumContent, 1, new Album());
- albumList.add(currentAlbum);
- }
- return parseFile(tokens, index + 1, albumList);
- }
- public static void main(String[] args) {
- try {
- String content = new String(Files.readAllBytes(Paths.get("alben.xml")));
- ArrayList<String> tokens = createTokenList(content, 0);
- ArrayList<Album> albumList = parseFile(tokens, 0, new ArrayList<>());
- printRecursive(albumList, 0);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement