ddeexxiikk

Narzędzia.java

Oct 27th, 2025
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. package pk.wieik.ti.model;
  2.  
  3. import jakarta.servlet.ServletContext;
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.nio.charset.StandardCharsets;
  9.  
  10. public class Narzedzia {
  11.  
  12.     public static String pobierzSzablon(String plik, ServletContext context) throws IOException {
  13.         StringBuffer wyjscie = new StringBuffer("");
  14.         String tekst = "";
  15.         InputStream is = context.getResourceAsStream("/WEB-INF/widok/" + plik);
  16.         if (is != null) {
  17.             InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
  18.             BufferedReader reader = new BufferedReader(isr);
  19.             while ((tekst = reader.readLine()) != null) {
  20.                 wyjscie.append(tekst).append("\n");
  21.             }
  22.         } else wyjscie.append("Brak pliku " + plik);
  23.         return wyjscie.toString();
  24.     }
  25.  
  26.     public static String uzupelnij(String szablon, String znacznik,
  27.                                    String plik, ServletContext context) throws IOException {
  28.         StringBuffer wyjscie = new StringBuffer("");
  29.         String tekst = "";
  30.         InputStream is = context.getResourceAsStream("/WEB-INF/widok/" + plik);
  31.         if (is != null) {
  32.             InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
  33.             BufferedReader reader = new BufferedReader(isr);
  34.             while ((tekst = reader.readLine()) != null) {
  35.                 wyjscie.append(tekst).append("\n");
  36.             }
  37.         } else wyjscie.append("Brak pliku " + plik);
  38.  
  39.         return szablon.replace("[[" + znacznik + "]]", wyjscie.toString());
  40.     }
  41.  
  42.     public static int parsujInteger(String wejscie, int domyslna) {
  43.         int wyjscie = domyslna;
  44.         try {
  45.             wyjscie = Integer.parseInt(wejscie);
  46.         } catch (NumberFormatException nfe) { // null lub zły format
  47.             wyjscie = domyslna;
  48.         }
  49.         return wyjscie;
  50.     }
  51.  
  52.     public static String parsujStrone(String wejscie, String prawidlowe)
  53.     {
  54.         String wyjscie = "glowna";
  55.         String[] strony = prawidlowe.split(";");
  56.         if (wejscie==null) wejscie="glowna";
  57.  
  58.         for (String poprawna: strony)
  59.         {
  60.             if (wejscie.equals(poprawna)) {
  61.                 wyjscie = wejscie;
  62.                 return wyjscie;
  63.             }
  64.         }
  65.         return wyjscie;
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment