content: package org.alfresco.transform.pii.transformer;
  
  import org.alfresco.transform.base.CustomTransformer;
  import org.alfresco.transform.base.TransformManager;
  import org.alfresco.transform.pii.service.PresidioService;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  
  import java.io.*;
  import java.util.Map;
  
  @Component
  public class PIIRedactionTransformer implements CustomTransformer {
  
      private static final Logger logger = LoggerFactory.getLogger(PIIRedactionTransformer.class);
  
      @Autowired
      private PresidioService presidioService;
  
      @Override
      public String getTransformerName() {
          return "pdf-pii-redaction";
      }
  
      @Override
      public void transform(
              String sourceMimetype,
              InputStream inputStream,
              String targetMimetype,
              OutputStream outputStream,
              Map<String, String> transformOptions,
              TransformManager transformManager)
              throws Exception {
  
          File tempFile = File.createTempFile("pii-redaction-", ".pdf");
          try (OutputStream tempOut = new FileOutputStream(tempFile)) {
              inputStream.transferTo(tempOut);
          }
  
          Map<String, ?> opt = (transformOptions == null) ? Map.of() : transformOptions;
          String entitiesRaw = (opt.get("entities") == null ? null : opt.get("entities") .toString().trim());
          String scoreRaw = (opt.get("score-threshold") == null ? null : opt.get("score-threshold").toString().trim());
          String label = (opt.get("label") == null ? null : opt.get("label").toString().trim());
  
          java.util.List<String> entities = parseEntities(entitiesRaw);
          Double scoreThreshold = parseDouble(scoreRaw);
  
          try {
              File redactedPDF = presidioService.convert(tempFile, entities, scoreThreshold, label);
              try (InputStream in = new FileInputStream(redactedPDF)) {
                  in.transferTo(outputStream);
              }
          } finally {
              try { tempFile.delete(); } catch (Exception ignore) {}
          }
      }
  
      private static java.util.List<String> parseEntities(String raw) {
          if (raw == null || raw.isBlank()) return null;
          String[] parts = raw.split("[,;\\s]+");
          java.util.LinkedHashSet<String> set = new java.util.LinkedHashSet<>();
          for (String p : parts) {
              String s = p.trim();
              if (!s.isEmpty()) set.add(s);
          }
          return set.isEmpty() ? null : new java.util.ArrayList<>(set);
      }
  
      private static Double parseDouble(String raw) {
          if (raw == null || raw.isBlank()) return null;
          try { return Double.valueOf(raw); } catch (NumberFormatException e) { return null; }
      }
  
  }