Author: msahyoun
Date: Tue Aug 25 18:59:23 2026
New Revision: 1937443
Log:
PDFBOX-5660: refactor XML escaping
Added:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFUtils.java
Modified:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDictionary.java
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java
Modified:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDictionary.java
==============================================================================
---
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDictionary.java
Tue Aug 25 18:44:15 2026 (r1937442)
+++
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDictionary.java
Tue Aug 25 18:59:23 2026 (r1937443)
@@ -248,9 +248,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/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java
==============================================================================
---
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java
Tue Aug 25 18:44:15 2026 (r1937442)
+++
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java
Tue Aug 25 18:59:23 2026 (r1937443)
@@ -114,7 +114,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();
@@ -122,7 +122,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)
@@ -131,7 +131,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");
}
}
@@ -140,7 +140,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();
@@ -786,48 +786,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("<");
- break;
- case '>':
- escapedXML.append(">");
- break;
- case '\"':
- escapedXML.append(""");
- break;
- case '&':
- escapedXML.append("&");
- break;
- case '\'':
- escapedXML.append("'");
- break;
- default:
- if (c > 0x7e)
- {
- escapedXML.append("&#").append((int) c).append(';');
- }
- else
- {
- escapedXML.append(c);
- }
- }
- }
- return escapedXML.toString();
- }
}
Added:
pdfbox/branches/2.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/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFUtils.java
Tue Aug 25 18:59:23 2026 (r1937443)
@@ -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("<");
+ break;
+ case '>':
+ escapedXML.append(">");
+ break;
+ case '\"':
+ escapedXML.append(""");
+ break;
+ case '&':
+ escapedXML.append("&");
+ break;
+ case '\'':
+ escapedXML.append("'");
+ break;
+ default:
+ if (c > 0x7e)
+ {
+ escapedXML.append("&#").append((int) c).append(';');
+ }
+ else
+ {
+ escapedXML.append(c);
+ }
+ }
+ }
+ return escapedXML.toString();
+ }
+}