presariohg

Funktional.java

Oct 21st, 2021
698
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.IOException;
  2. import java.nio.file.Files;
  3. import java.nio.file.Paths;
  4. import java.util.ArrayList;
  5.  
  6. public class Funktional {
  7.  
  8.     /**
  9.      * Find the position of the next break character, or the end of the string
  10.      *
  11.      * @param content The string to parse
  12.      * @param currentPos Current position of the pointer in the string
  13.      * @return Position of the next break character or the end of the string
  14.      */
  15.     private static int tokenEndAt(String content, int currentPos) {
  16.         if (currentPos >= content.length())
  17.             return content.length(); // end of the whole string
  18.  
  19.         char currentChar = content.charAt(currentPos);
  20.         if ((currentChar == '<') || (currentChar == '>')) {
  21.             return currentPos; // return the token's end index
  22.         }
  23.  
  24.         // else the token not ended, move to the next position
  25.         return tokenEndAt(content, currentPos + 1);
  26.     }
  27.  
  28.  
  29.     /**
  30.      * Parse a string and find the tokens inside
  31.      *
  32.      * @param content The string to parse
  33.      * @param startPos Start position of a new token
  34.      * @return An ArrayList containing all found, non-empty tokens.
  35.      */
  36.     public static ArrayList<String> createTokenList(String content, int startPos) {
  37.         if (startPos >= content.length()) {
  38.             return new ArrayList<>(); // end of the whole string, every result will be appended to this
  39.         }
  40.         // start finding a new token
  41.         int endPos = tokenEndAt(content, startPos);
  42.  
  43.         // append the found token into the list
  44.         ArrayList<String> tokens = createTokenList(content, endPos + 1);
  45.         String tokenFound = content.substring(startPos, endPos)
  46.                                    .replaceAll("[\\r\\n\\t]", ""); // clean the spaces;
  47.         // Only non-empty counts
  48.         if (tokenFound.length() > 0)
  49.             tokens.add(0, tokenFound);
  50.  
  51.         return tokens;
  52.     }
  53.  
  54.  
  55.     /**
  56.      * Println an ArrayList without using iterative loop
  57.      * @param list The ArrayList to print
  58.      * @param index Current position of the pointer
  59.      */
  60.     private static void printRecursive(ArrayList list, int index) {
  61.         if (index >= list.size())
  62.             return;
  63.  
  64.         System.out.println(list.get(index));
  65.         printRecursive(list, index + 1);
  66.     }
  67.  
  68.  
  69.     /**
  70.      * Find the position of the closing tag of a given opening tag
  71.      * @param tokens The list of token extracted
  72.      * @param openTagName The name of the opening tag
  73.      * @param index Current position of the pointer in the list
  74.      * @return The closing tag's position of the given tag
  75.      */
  76.     private static int getCloseTagPos(ArrayList<String> tokens, String openTagName, int index) {
  77.         if (index >= tokens.size())
  78.             return -1;
  79.  
  80.         String currentToken = tokens.get(index).substring(1); //remove the "/" char
  81.         if (currentToken.equals(openTagName)) {
  82.             return index;
  83.         }
  84.  
  85.         return getCloseTagPos(tokens, openTagName, index + 1);
  86.     }
  87.  
  88.  
  89.     /**
  90.      * Find the content of a given opening tag
  91.      * @param tokens The list of token extracted
  92.      * @param openTagPos Position of the opening tag in token list
  93.      * @return A sublist of tokens, (inclusive) from the opening tag to its closing tag
  94.      */
  95.     private static ArrayList<String> getTagContent(ArrayList<String> tokens, int openTagPos) {
  96.         String tagName = tokens.get(openTagPos);
  97.         int closeTagPos = getCloseTagPos(tokens, tagName, openTagPos + 1);
  98.  
  99.         return new ArrayList<>(tokens.subList(openTagPos, closeTagPos + 1));
  100.     }
  101.  
  102.  
  103.     private static Track extractTrack(ArrayList<String> tokens, int index, Track currentTrack) {
  104.         if (index >= tokens.size())
  105.             return currentTrack;
  106.  
  107.         String currentToken = tokens.get(index);
  108.         boolean isClosingTag = currentToken.charAt(0) == '/';
  109.  
  110.         if (!isClosingTag) {
  111.             String tagContent = getTagContent(tokens, index).get(1);
  112.  
  113.             switch (currentToken) {
  114.                 case "title" -> currentTrack.title = tagContent;
  115.                 case "length" -> currentTrack.length = tagContent;
  116.                 case "rating" -> currentTrack.rating = Integer.parseInt(tagContent);
  117.                 case "feature" -> currentTrack.features.add(tagContent);
  118.                 case "writing" -> currentTrack.writers.add(tagContent);
  119.             }
  120.             index += 2; // <tag>content<tag> => each tag contains 3 token => must += 2 after extracting
  121.         }
  122.  
  123.         return extractTrack(tokens, index + 1, currentTrack);
  124.     }
  125.  
  126.  
  127.     public static Album extractAlbum(ArrayList<String> tokens, int index, Album currentAlbum) {
  128.         if (index >= tokens.size())
  129.             return currentAlbum;
  130.  
  131.         String currentToken = tokens.get(index);
  132.         boolean isClosingTag = currentToken.charAt(0) == '/';
  133.  
  134.         if (!isClosingTag) {
  135.             if (currentToken.equals("track")) {
  136.                 // Tracks should be handled carefully
  137.                 ArrayList<String> trackContent = getTagContent(tokens, index);
  138.  
  139.                 // trackContent[0] == "track" => start at 1
  140.                 Track currentTrack = extractTrack(trackContent, 1, new Track());
  141.  
  142.                 currentAlbum.tracks.add(currentTrack);
  143.                 index = getCloseTagPos(tokens, currentToken, index); // move index to the closing tag
  144.             } else {
  145.                 // Normal attributes can be processed in bulk
  146.  
  147.                 String tagContent = getTagContent(tokens, index).get(1);
  148.                 switch (currentToken) {
  149.                     case "artist" -> currentAlbum.artist = tagContent;
  150.                     case "title" -> currentAlbum.title = tagContent;
  151.                     case "date" -> currentAlbum.date = tagContent;
  152.                 }
  153.                 index += 2; // <tag>content<tag> => each tag contains 3 token => must += 2 after extracting
  154.             }
  155.         }
  156.  
  157.         return extractAlbum(tokens, index + 1, currentAlbum);
  158.     }
  159.  
  160.  
  161.     public static ArrayList<Album> parseFile(ArrayList<String> tokens, int index, ArrayList<Album> albumList) {
  162.         if (index >= tokens.size())
  163.             return albumList;
  164.  
  165.         String currentToken = tokens.get(index);
  166.         if (currentToken.equals("album")) {
  167.             ArrayList<String> albumContent = getTagContent(tokens, index);
  168.             // albumContent[0] == "album" => start at 1
  169.             Album currentAlbum = extractAlbum(albumContent, 1, new Album());
  170.             albumList.add(currentAlbum);
  171.         }
  172.  
  173.         return parseFile(tokens, index + 1, albumList);
  174.     }
  175.  
  176.  
  177.     public static void main(String[] args) {
  178.         try {
  179.             String content = new String(Files.readAllBytes(Paths.get("alben.xml")));
  180.             ArrayList<String> tokens = createTokenList(content, 0);
  181.  
  182.             ArrayList<Album> albumList = parseFile(tokens, 0, new ArrayList<>());
  183.             printRecursive(albumList, 0);
  184.  
  185.         } catch (IOException e) {
  186.             e.printStackTrace();
  187.         }
  188.     }
  189. }
  190.  
RAW Paste Data