From 95471a2fba891a55ff0205cfcd9c64546296f6e1 Mon Sep 17 00:00:00 2001 From: "pixeebot[bot]" <23113631+pixeebot@users.noreply.github.com> Date: Fri, 2 Feb 2024 00:29:18 +0000 Subject: [PATCH] Switch order of literals to prevent NullPointerException --- scripts/PropSync.java | 2 +- .../software/SPDF/SPdfApplication.java | 2 +- .../SPDF/config/ConfigInitializer.java | 2 +- .../converters/ConvertImgPDFController.java | 4 ++-- .../api/misc/ExtractImageScansController.java | 2 +- .../api/misc/ExtractImagesController.java | 6 +++--- .../api/misc/MetadataController.java | 18 +++++++++--------- .../controller/api/misc/OCRController.java | 4 ++-- .../controller/api/misc/StampController.java | 6 +++--- .../api/pipeline/PipelineProcessor.java | 2 +- .../api/security/WatermarkController.java | 6 +++--- .../SPDF/controller/web/MetricsController.java | 8 ++++---- .../software/SPDF/utils/FileToPdf.java | 2 +- .../software/SPDF/utils/GeneralUtils.java | 6 +++--- .../software/SPDF/utils/PDFToFile.java | 2 +- .../stirling/software/SPDF/utils/PdfUtils.java | 8 ++++---- 16 files changed, 40 insertions(+), 40 deletions(-) diff --git a/scripts/PropSync.java b/scripts/PropSync.java index 741712b5..41228b5b 100644 --- a/scripts/PropSync.java +++ b/scripts/PropSync.java @@ -16,7 +16,7 @@ public class PropSync { Map enProps = linesToProps(enLines); for (File file : files) { - if (!file.getName().equals("messages_en_GB.properties")) { + if (!"messages_en_GB.properties".equals(file.getName())) { System.out.println("Processing file: " + file.getName()); List lines; try { diff --git a/src/main/java/stirling/software/SPDF/SPdfApplication.java b/src/main/java/stirling/software/SPDF/SPdfApplication.java index 8cdcc701..f37dfb6e 100644 --- a/src/main/java/stirling/software/SPDF/SPdfApplication.java +++ b/src/main/java/stirling/software/SPDF/SPdfApplication.java @@ -25,7 +25,7 @@ public class SPdfApplication { public void init() { // Check if the BROWSER_OPEN environment variable is set to true String browserOpenEnv = env.getProperty("BROWSER_OPEN"); - boolean browserOpen = browserOpenEnv != null && browserOpenEnv.equalsIgnoreCase("true"); + boolean browserOpen = browserOpenEnv != null && "true".equalsIgnoreCase(browserOpenEnv); if (browserOpen) { try { diff --git a/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java b/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java index 6435c955..a5d05999 100644 --- a/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java +++ b/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java @@ -84,7 +84,7 @@ public class ConfigInitializer for (String line : templateLines) { String key = extractKey.apply(line); - if (line.trim().equalsIgnoreCase("AutomaticallyGenerated:")) { + if ("AutomaticallyGenerated:".equalsIgnoreCase(line.trim())) { insideAutoGenerated = true; mergedLines.add(line); continue; diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java index 548e4937..c131d883 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertImgPDFController.java @@ -55,7 +55,7 @@ public class ConvertImgPDFController { colorTypeResult = ImageType.BINARY; } // returns bytes for image - boolean singleImage = singleOrMultiple.equals("single"); + boolean singleImage = "single".equals(singleOrMultiple); byte[] result = null; String filename = Filenames.toSimpleFileName(file.getOriginalFilename()).replaceFirst("[.][^.]+$", ""); try { @@ -114,6 +114,6 @@ public class ConvertImgPDFController { private String getMediaType(String imageFormat) { String mimeType = URLConnection.guessContentTypeFromName("." + imageFormat); - return mimeType.equals("null") ? "application/octet-stream" : mimeType; + return "null".equals(mimeType) ? "application/octet-stream" : mimeType; } } diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImageScansController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImageScansController.java index 510607e8..da684280 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImageScansController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImageScansController.java @@ -74,7 +74,7 @@ public class ExtractImageScansController { List images = new ArrayList<>(); // Check if input file is a PDF - if (extension.equalsIgnoreCase("pdf")) { + if ("pdf".equalsIgnoreCase(extension)) { // Load PDF document try (PDDocument document = Loader.loadPDF(form.getFileInput().getBytes())) { PDFRenderer pdfRenderer = new PDFRenderer(document); diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java index 24e7262c..cb9071d8 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/ExtractImagesController.java @@ -86,19 +86,19 @@ public class ExtractImagesController { // Convert image to desired format RenderedImage renderedImage = image.getImage(); BufferedImage bufferedImage = null; - if (format.equalsIgnoreCase("png")) { + if ("png".equalsIgnoreCase(format)) { bufferedImage = new BufferedImage( renderedImage.getWidth(), renderedImage.getHeight(), BufferedImage.TYPE_INT_ARGB); - } else if (format.equalsIgnoreCase("jpeg") || format.equalsIgnoreCase("jpg")) { + } else if ("jpeg".equalsIgnoreCase(format) || "jpg".equalsIgnoreCase(format)) { bufferedImage = new BufferedImage( renderedImage.getWidth(), renderedImage.getHeight(), BufferedImage.TYPE_INT_RGB); - } else if (format.equalsIgnoreCase("gif")) { + } else if ("gif".equalsIgnoreCase(format)) { bufferedImage = new BufferedImage( renderedImage.getWidth(), diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/MetadataController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/MetadataController.java index 97119404..8e4ae664 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/MetadataController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/MetadataController.java @@ -110,15 +110,15 @@ public class MetadataController { for (Entry entry : allRequestParams.entrySet()) { String key = entry.getKey(); // Check if the key is a standard metadata key - if (!key.equalsIgnoreCase("Author") - && !key.equalsIgnoreCase("CreationDate") - && !key.equalsIgnoreCase("Creator") - && !key.equalsIgnoreCase("Keywords") - && !key.equalsIgnoreCase("modificationDate") - && !key.equalsIgnoreCase("Producer") - && !key.equalsIgnoreCase("Subject") - && !key.equalsIgnoreCase("Title") - && !key.equalsIgnoreCase("Trapped") + if (!"Author".equalsIgnoreCase(key) + && !"CreationDate".equalsIgnoreCase(key) + && !"Creator".equalsIgnoreCase(key) + && !"Keywords".equalsIgnoreCase(key) + && !"modificationDate".equalsIgnoreCase(key) + && !"Producer".equalsIgnoreCase(key) + && !"Subject".equalsIgnoreCase(key) + && !"Title".equalsIgnoreCase(key) + && !"Trapped".equalsIgnoreCase(key) && !key.contains("customKey") && !key.contains("customValue")) { info.setCustomMetadataValue(key, entry.getValue()); diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/OCRController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/OCRController.java index 685a6526..143ae1da 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/OCRController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/OCRController.java @@ -75,7 +75,7 @@ public class OCRController { throw new IOException("Please select at least one language."); } - if (!ocrRenderType.equals("hocr") && !ocrRenderType.equals("sandwich")) { + if (!"hocr".equals(ocrRenderType) && !"sandwich".equals(ocrRenderType)) { throw new IOException("ocrRenderType wrong"); } @@ -128,7 +128,7 @@ public class OCRController { if (cleanFinal != null && cleanFinal) { command.add("--clean-final"); } - if (ocrType != null && !ocrType.equals("")) { + if (ocrType != null && !"".equals(ocrType)) { if ("skip-text".equals(ocrType)) { command.add("--skip-text"); } else if ("force-ocr".equals(ocrType)) { diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java index 9a01cf4d..52e8d970 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java @@ -96,7 +96,7 @@ public class StampController { graphicsState.setNonStrokingAlphaConstant(opacity); contentStream.setGraphicsStateParameters(graphicsState); - if (watermarkType.equalsIgnoreCase("text")) { + if ("text".equalsIgnoreCase(watermarkType)) { addTextStamp( contentStream, watermarkText, @@ -110,7 +110,7 @@ public class StampController { overrideY, marginFactor, customColor); - } else if (watermarkType.equalsIgnoreCase("image")) { + } else if ("image".equalsIgnoreCase(watermarkType)) { addImageStamp( contentStream, watermarkImage, @@ -167,7 +167,7 @@ public class StampController { break; } - if (!resourceDir.equals("")) { + if (!"".equals(resourceDir)) { ClassPathResource classPathResource = new ClassPathResource(resourceDir); String fileExtension = resourceDir.substring(resourceDir.lastIndexOf(".")); File tempFile = Files.createTempFile("NotoSansFont", fileExtension).toFile(); diff --git a/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java b/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java index 4fbc2aa9..c4e98bdb 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java +++ b/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java @@ -98,7 +98,7 @@ public class PipelineProcessor { for (Resource file : outputFiles) { boolean hasInputFileType = false; for (String extension : inputFileTypes) { - if (extension.equals("ALL") || file.getFilename().endsWith(extension)) { + if ("ALL".equals(extension) || file.getFilename().endsWith(extension)) { hasInputFileType = true; MultiValueMap body = new LinkedMultiValueMap<>(); body.add("fileInput", file); diff --git a/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java b/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java index 469f32be..91a8a265 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java @@ -77,7 +77,7 @@ public class WatermarkController { graphicsState.setNonStrokingAlphaConstant(opacity); contentStream.setGraphicsStateParameters(graphicsState); - if (watermarkType.equalsIgnoreCase("text")) { + if ("text".equalsIgnoreCase(watermarkType)) { addTextWatermark( contentStream, watermarkText, @@ -88,7 +88,7 @@ public class WatermarkController { heightSpacer, fontSize, alphabet); - } else if (watermarkType.equalsIgnoreCase("image")) { + } else if ("image".equalsIgnoreCase(watermarkType)) { addImageWatermark( contentStream, watermarkImage, @@ -141,7 +141,7 @@ public class WatermarkController { break; } - if (!resourceDir.equals("")) { + if (!"".equals(resourceDir)) { ClassPathResource classPathResource = new ClassPathResource(resourceDir); String fileExtension = resourceDir.substring(resourceDir.lastIndexOf(".")); File tempFile = Files.createTempFile("NotoSansFont", fileExtension).toFile(); diff --git a/src/main/java/stirling/software/SPDF/controller/web/MetricsController.java b/src/main/java/stirling/software/SPDF/controller/web/MetricsController.java index 5b079042..155fcdbd 100644 --- a/src/main/java/stirling/software/SPDF/controller/web/MetricsController.java +++ b/src/main/java/stirling/software/SPDF/controller/web/MetricsController.java @@ -84,7 +84,7 @@ public class MetricsController { for (Meter meter : meterRegistry.getMeters()) { if (meter.getId().getName().equals("http.requests")) { String method = meter.getId().getTag("method"); - if (method != null && method.equals("GET")) { + if (method != null && "GET".equals(method)) { if (endpoint.isPresent() && !endpoint.get().isBlank()) { if (!endpoint.get().startsWith("/")) { @@ -129,7 +129,7 @@ public class MetricsController { for (Meter meter : meterRegistry.getMeters()) { if (meter.getId().getName().equals("http.requests")) { String method = meter.getId().getTag("method"); - if (method != null && method.equals("GET")) { + if (method != null && "GET".equals(method)) { String uri = meter.getId().getTag("uri"); if (uri != null) { double currentCount = counts.getOrDefault(uri, 0.0); @@ -197,7 +197,7 @@ public class MetricsController { for (Meter meter : meterRegistry.getMeters()) { if (meter.getId().getName().equals("http.requests")) { String method = meter.getId().getTag("method"); - if (method != null && method.equals("POST")) { + if (method != null && "POST".equals(method)) { if (endpoint.isPresent() && !endpoint.get().isBlank()) { if (!endpoint.get().startsWith("/")) { endpoint = Optional.of("/" + endpoint.get()); @@ -235,7 +235,7 @@ public class MetricsController { for (Meter meter : meterRegistry.getMeters()) { if (meter.getId().getName().equals("http.requests")) { String method = meter.getId().getTag("method"); - if (method != null && method.equals("POST")) { + if (method != null && "POST".equals(method)) { String uri = meter.getId().getTag("uri"); if (uri != null) { double currentCount = counts.getOrDefault(uri, 0.0); diff --git a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java index 816cdef6..09ce430c 100644 --- a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java +++ b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java @@ -173,7 +173,7 @@ public class FileToPdf { // Prioritize 'index.html' if it exists, otherwise use the first .html file for (Path htmlFile : htmlFiles) { - if (htmlFile.getFileName().toString().equals("index.html")) { + if ("index.html".equals(htmlFile.getFileName().toString())) { return htmlFile; } } diff --git a/src/main/java/stirling/software/SPDF/utils/GeneralUtils.java b/src/main/java/stirling/software/SPDF/utils/GeneralUtils.java index 7b6905db..1737b27e 100644 --- a/src/main/java/stirling/software/SPDF/utils/GeneralUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/GeneralUtils.java @@ -122,7 +122,7 @@ public class GeneralUtils { // loop through the page order array for (String element : pageOrderArr) { - if (element.equalsIgnoreCase("all")) { + if ("all".equalsIgnoreCase(element)) { for (int i = 0; i < totalPages; i++) { newPageOrder.add(i); } @@ -137,11 +137,11 @@ public class GeneralUtils { if (element.contains("n")) { String[] parts = element.split("n"); - if (!parts[0].equals("") && parts[0] != null) { + if (!"".equals(parts[0]) && parts[0] != null) { coefficient = Integer.parseInt(parts[0]); coefficientExists = true; } - if (parts.length > 1 && !parts[1].equals("") && parts[1] != null) { + if (parts.length > 1 && !"".equals(parts[1]) && parts[1] != null) { constant = Integer.parseInt(parts[1]); constantExists = true; } diff --git a/src/main/java/stirling/software/SPDF/utils/PDFToFile.java b/src/main/java/stirling/software/SPDF/utils/PDFToFile.java index e210d8a9..7afe6a49 100644 --- a/src/main/java/stirling/software/SPDF/utils/PDFToFile.java +++ b/src/main/java/stirling/software/SPDF/utils/PDFToFile.java @@ -88,7 +88,7 @@ public class PDFToFile { if (outputFiles.size() == 1) { // Return single output file File outputFile = outputFiles.get(0); - if (outputFormat.equals("txt:Text")) { + if ("txt:Text".equals(outputFormat)) { outputFormat = "txt"; } fileName = pdfBaseName + "." + outputFormat; diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index 03e03671..748f8aad 100644 --- a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java @@ -133,7 +133,7 @@ public class PdfUtils { PDFTextStripper textStripper = new PDFTextStripper(); String pdfText = ""; - if (pagesToCheck == null || pagesToCheck.equals("all")) { + if (pagesToCheck == null || "all".equals(pagesToCheck)) { pdfText = textStripper.getText(pdfDocument); } else { // remove whitespaces @@ -219,8 +219,8 @@ public class PdfUtils { ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (singleImage) { - if (imageType.toLowerCase().equals("tiff") - || imageType.toLowerCase().equals("tif")) { + if ("tiff".equals(imageType.toLowerCase()) + || "tif".equals(imageType.toLowerCase())) { // Write the images to the output stream as a TIFF with multiple frames ImageWriter writer = ImageIO.getImageWritersByFormatName("tiff").next(); ImageWriteParam param = writer.getDefaultWriteParam(); @@ -321,7 +321,7 @@ public class PdfUtils { ImageProcessingUtils.convertColorType(image, colorType); // Use JPEGFactory if it's JPEG since JPEG is lossy PDImageXObject pdImage = - (contentType != null && contentType.equals("image/jpeg")) + (contentType != null && "image/jpeg".equals(contentType)) ? JPEGFactory.createFromImage(doc, convertedImage) : LosslessFactory.createFromImage(doc, convertedImage); addImageToDocument(doc, pdImage, fitOption, autoRotate);