Author: msahyoun
Date: Tue Aug 25 19:00:14 2026
New Revision: 1937445

Log:
PDFBOX-5660: refactor XML escaping

Added:
   
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFUtils.java
Modified:
   
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDictionary.java
   
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java

Modified: 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDictionary.java
==============================================================================
--- 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDictionary.java
   Tue Aug 25 18:59:50 2026        (r1937444)
+++ 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDictionary.java
   Tue Aug 25 19:00:14 2026        (r1937445)
@@ -232,9 +232,9 @@ public class FDFDictionary implements CO
     public void writeXML(Writer output) throws IOException
     {
         PDFileSpecification fs = this.getFile();
-        if (fs != null)
+        if (fs != null && fs.getFile() != null)
         {
-            output.write("<f href=\"" + fs.getFile() + "\" />\n");
+            output.write("<f href=\"" + FDFUtils.escapeXML10(fs.getFile()) + 
"\" />\n");
         }
         COSArray ids = this.getID();
         if (ids != null)

Modified: 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java
==============================================================================
--- 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java
        Tue Aug 25 18:59:50 2026        (r1937444)
+++ 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java
        Tue Aug 25 19:00:14 2026        (r1937445)
@@ -117,7 +117,7 @@ public class FDFField implements COSObje
     public void writeXML(Writer output) throws IOException
     {
         output.write("<field name=\"");
-        output.write(getPartialFieldName());
+        output.write(FDFUtils.escapeXML10(getPartialFieldName()));
         output.write("\">\n");
 
         Object value = getValue();
@@ -125,7 +125,7 @@ public class FDFField implements COSObje
         if (value instanceof String)
         {
             output.write("<value>");
-            output.write(escapeXML((String) value));
+            output.write(FDFUtils.escapeXML10((String) value));
             output.write("</value>\n");
         }
         else if (value instanceof List)
@@ -134,7 +134,7 @@ public class FDFField implements COSObje
             for (String item : items)
             {
                 output.write("<value>");
-                output.write(escapeXML(item));
+                output.write(FDFUtils.escapeXML10(item));
                 output.write("</value>\n");
             }
         }
@@ -143,7 +143,7 @@ public class FDFField implements COSObje
         if (rt != null)
         {
             output.write("<value-richtext>");
-            output.write(escapeXML(rt));
+            output.write(FDFUtils.escapeXML10(rt));
             output.write("</value-richtext>\n");
         }
         List<FDFField> kids = getKids();
@@ -776,48 +776,4 @@ public class FDFField implements COSObje
     {
         field.setItem(COSName.RV, rv);
     }
-
-    /**
-     * Escape special characters.
-     *
-     * @param input the string to be escaped.
-     *
-     * @return the resulting string
-     */
-    private String escapeXML(String input)
-    {
-        StringBuilder escapedXML = new StringBuilder();
-        for (int i = 0; i < input.length(); i++)
-        {
-            char c = input.charAt(i);
-            switch (c)
-            {
-            case '<':
-                escapedXML.append("&lt;");
-                break;
-            case '>':
-                escapedXML.append("&gt;");
-                break;
-            case '\"':
-                escapedXML.append("&quot;");
-                break;
-            case '&':
-                escapedXML.append("&amp;");
-                break;
-            case '\'':
-                escapedXML.append("&apos;");
-                break;
-            default:
-                if (c > 0x7e)
-                {
-                    escapedXML.append("&#").append((int) c).append(';');
-                }
-                else
-                {
-                    escapedXML.append(c);
-                }
-            }
-        }
-        return escapedXML.toString();
-    }
 }

Added: 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFUtils.java
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFUtils.java
        Tue Aug 25 19:00:14 2026        (r1937445)
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.fdf;
+
+public class FDFUtils {
+
+    /**
+     * Escape special characters.
+     *
+     * @param input the string to be escaped.
+     *
+     * @return the resulting string
+     */
+    static String escapeXML10(String input)
+    {
+        StringBuilder escapedXML = new StringBuilder();
+        for (int i = 0; i < input.length(); i++)
+        {
+            char c = input.charAt(i);
+            switch (c)
+            {
+            case '<':
+                escapedXML.append("&lt;");
+                break;
+            case '>':
+                escapedXML.append("&gt;");
+                break;
+            case '\"':
+                escapedXML.append("&quot;");
+                break;
+            case '&':
+                escapedXML.append("&amp;");
+                break;
+            case '\'':
+                escapedXML.append("&apos;");
+                break;
+            default:
+                if (c > 0x7e)
+                {
+                    escapedXML.append("&#").append((int) c).append(';');
+                }
+                else
+                {
+                    escapedXML.append(c);
+                }
+            }
+        }
+        return escapedXML.toString();
+    }
+}

Reply via email to