Husi012

KMPS / Pratkikum 1 / Aufgabe 1

Oct 12th, 2023
981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.nio.ByteBuffer;
  3. import java.nio.CharBuffer;
  4. import java.nio.charset.CharacterCodingException;
  5. import java.nio.charset.StandardCharsets;
  6. import java.nio.file.Files;
  7. import java.nio.file.Paths;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class Funktional {
  12.     public static void main(String[] args) throws IOException {
  13. //        byte[] input = Files.readAllBytes(Paths.get("alben.xml"));
  14.         byte[] input = Files.readAllBytes(Paths.get("alben-a1.xml"));
  15.         System.out.println(createTokenList(input));
  16.     }
  17.  
  18.     public static List<String> createTokenList(CharBuffer cb, List<String> tokens, String captured, boolean onlyWhitespace) {
  19.         if (!cb.hasRemaining()) {
  20.             return tokens;
  21.         }
  22.         char c = cb.get();
  23.         if (c == '<' || c == '>') {
  24.             if (!onlyWhitespace) {
  25.                 tokens.add(captured);
  26.             }
  27.             createTokenList(cb, tokens, "", true);
  28.         } else {
  29.             onlyWhitespace &= c == ' ' || c == '\n' || c == '\t' || c == '\r';
  30.             createTokenList(cb, tokens, captured + c, onlyWhitespace);
  31.         }
  32.         return tokens;
  33.     }
  34.  
  35.     public static List<String> createTokenList(byte[] input) throws CharacterCodingException {
  36.         CharBuffer cb = StandardCharsets.UTF_8.newDecoder().decode(ByteBuffer.wrap(input));
  37.         return createTokenList(cb, new ArrayList<>(), "", true);
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment