Skip to content

Commit

Permalink
Add dependency to Apache Commons commons-text. Remove SimpleXMLParser…
Browse files Browse the repository at this point in the history
….escapeXML. Deprecate SimpleXMLParser.
  • Loading branch information
andreasrosdal committed Aug 4, 2018
1 parent a6a5ed3 commit fb4ec29
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 48 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ https://groups.google.com/d/forum/openpdf

### Required: ###

- Apache Commons Text
- RxJava
- PDFRenderer
- DOM4j
Expand Down
6 changes: 6 additions & 0 deletions openpdf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@
<properties>
<bouncycastle.version>1.60</bouncycastle.version>
<rxjava.version>2.2.0</rxjava.version>
<commons-text.version>1.4</commons-text.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
import com.lowagie.text.xml.simpleparser.IanaEncodings;
import com.lowagie.text.xml.simpleparser.SimpleXMLDocHandler;
import com.lowagie.text.xml.simpleparser.SimpleXMLParser;
import org.apache.commons.text.StringEscapeUtils;

/**
* Bookmark processing in a simple way. It has some limitations, mainly the only
* action types supported are GoTo, GoToR, URI and Launch.
Expand Down Expand Up @@ -597,14 +599,14 @@ else if (key.equals("Kids")) {
String value = (String) entry.getValue();
if (key.equals("Named") || key.equals("NamedN"))
value = SimpleNamedDestination.escapeBinaryString(value);
out.write(SimpleXMLParser.escapeXML(value, onlyASCII));
out.write(StringEscapeUtils.escapeXml11(value));
out.write("\" ");
}
}
out.write(">");
if (title == null)
title = "";
out.write(SimpleXMLParser.escapeXML(title, onlyASCII));
out.write(StringEscapeUtils.escapeXml11(title));
if (kids != null) {
out.write("\n");
exportToXMLNode(kids, out, indent + 1, onlyASCII);
Expand Down Expand Up @@ -658,7 +660,7 @@ public static void exportToXML(List list, OutputStream out, String encoding, boo
*/
public static void exportToXML(List list, Writer wrt, String encoding, boolean onlyASCII) throws IOException {
wrt.write("<?xml version=\"1.0\" encoding=\"");
wrt.write(SimpleXMLParser.escapeXML(encoding, onlyASCII));
wrt.write(StringEscapeUtils.escapeXml11(encoding));
wrt.write("\"?>\n<Bookmark>\n");
exportToXMLNode(list, wrt, 1, onlyASCII);
wrt.write("</Bookmark>\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import com.lowagie.text.xml.simpleparser.IanaEncodings;
import com.lowagie.text.xml.simpleparser.SimpleXMLDocHandler;
import com.lowagie.text.xml.simpleparser.SimpleXMLParser;
import org.apache.commons.text.StringEscapeUtils;

/**
*
Expand Down Expand Up @@ -134,16 +135,16 @@ public static void exportToXML(HashMap names, OutputStream out, String encoding,
*/
public static void exportToXML(HashMap names, Writer wrt, String encoding, boolean onlyASCII) throws IOException {
wrt.write("<?xml version=\"1.0\" encoding=\"");
wrt.write(SimpleXMLParser.escapeXML(encoding, onlyASCII));
wrt.write(StringEscapeUtils.escapeXml11(encoding));
wrt.write("\"?>\n<Destination>\n");
for (Iterator it = names.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry)it.next();
String key = (String)entry.getKey();
String value = (String)entry.getValue();
wrt.write(" <Name Page=\"");
wrt.write(SimpleXMLParser.escapeXML(value, onlyASCII));
wrt.write(StringEscapeUtils.escapeXml11(value));
wrt.write("\">");
wrt.write(SimpleXMLParser.escapeXML(escapeBinaryString(key), onlyASCII));
wrt.write(StringEscapeUtils.escapeXml11(escapeBinaryString(key)));
wrt.write("</Name>\n");
}
wrt.write("</Destination>\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
* </ul>
* <p>
*/
@Deprecated
public final class SimpleXMLParser {
/** possible states */
private final static int UNKNOWN = 0;
Expand Down Expand Up @@ -650,48 +651,7 @@ public static void parse(SimpleXMLDocHandler doc,Reader r) throws IOException {
parse(doc, null, r, false);
}

/**
* Escapes a string with the appropriated XML codes.
* @param s the string to be escaped
* @param onlyASCII codes above 127 will always be escaped with &amp;#nn; if <CODE>true</CODE>
* @return the escaped string
*/
public static String escapeXML(String s, boolean onlyASCII) {
char cc[] = s.toCharArray();
int len = cc.length;
StringBuffer sb = new StringBuffer();
for (int k = 0; k < len; ++k) {
int c = cc[k];
switch (c) {
case '<':
sb.append("&lt;");
break;
case '>':
sb.append("&gt;");
break;
case '&':
sb.append("&amp;");
break;
case '"':
sb.append("&quot;");
break;
case '\'':
sb.append("&apos;");
break;
default:
if ((c == 0x9) || (c == 0xA) || (c == 0xD)
|| ((c >= 0x20) && (c <= 0xD7FF))
|| ((c >= 0xE000) && (c <= 0xFFFD))
|| ((c >= 0x10000) && (c <= 0x10FFFF))) {
if (onlyASCII && c > 127)
sb.append("&#").append(c).append(';');
else
sb.append((char)c);
}
}
}
return sb.toString();
}

/**
* Returns the IANA encoding name that is auto-detected from
* the bytes specified, with the endian-ness of that encoding where appropriate.
Expand Down

2 comments on commit fb4ec29

@evernat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, adding a dependency on commons-text in a library like OpenPDF just to remove a method doing xml escaping is overkill.
Think about users who will have to resolve version conflicts or users who don't have clean dependency management. (See this example recommending to explicitly repeat transitive dependencies in their pom, forgetting that it will defeat your library updating its dependencies when needed.)

I would revert this commit if it was me.

@andreasrosdal
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please submit a Pull request to fix this? (I would prefer not to revert, but rather to write some new code)

Please sign in to comment.