You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2018/05/22 15:54:20 UTC
svn commit: r1832038 -
/pdfbox/branches/2.0/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java
Author: tilman
Date: Tue May 22 15:54:19 2018
New Revision: 1832038
URL: http://svn.apache.org/viewvc?rev=1832038&view=rev
Log:
PDFBOX-2941: new internal class to also support password entry loop for URLs but avoid double code
Modified:
pdfbox/branches/2.0/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java
Modified: pdfbox/branches/2.0/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java?rev=1832038&r1=1832037&r2=1832038&view=diff
==============================================================================
--- pdfbox/branches/2.0/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java (original)
+++ pdfbox/branches/2.0/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java Tue May 22 15:54:19 2018
@@ -1261,7 +1261,7 @@ public class PDFDebugger extends JFrame
readPDFFile(file, password);
}
- private void readPDFFile(File file, String password) throws IOException
+ private void readPDFFile(final File file, String password) throws IOException
{
if( document != null )
{
@@ -1273,7 +1273,17 @@ public class PDFDebugger extends JFrame
}
currentFilePath = file.getPath();
recentFiles.removeFile(file.getPath());
- parseDocument( file, password );
+ DocumentOpener documentOpener = new DocumentOpener(password)
+ {
+ @Override
+ PDDocument open() throws IOException
+ {
+ return PDDocument.load(file, password);
+ }
+ };
+ document = documentOpener.parse();
+ printMenuItem.setEnabled(true);
+ reopenMenuItem.setEnabled(true);
initTree();
@@ -1289,7 +1299,7 @@ public class PDFDebugger extends JFrame
addRecentFileItems();
}
- private void readPDFurl(String urlString, String password) throws IOException
+ private void readPDFurl(final String urlString, String password) throws IOException
{
if (document != null)
{
@@ -1300,8 +1310,15 @@ public class PDFDebugger extends JFrame
}
}
currentFilePath = urlString;
- URL url = new URL(urlString);
- document = PDDocument.load(url.openStream(), password);
+ DocumentOpener documentOpener = new DocumentOpener(password)
+ {
+ @Override
+ PDDocument open() throws IOException
+ {
+ return PDDocument.load(new URL(urlString).openStream(), password);
+ }
+ };
+ document = documentOpener.parse();
printMenuItem.setEnabled(true);
reopenMenuItem.setEnabled(true);
@@ -1339,45 +1356,68 @@ public class PDFDebugger extends JFrame
tree.setSelectionPath(treeStatus.getPathForString("Root"));
}
}
-
+
/**
- * This will parse a document.
- *
- * @param file The file addressing the document.
- *
- * @throws IOException If there is an error parsing the document.
+ * Internal class to avoid double code in password entry loop.
*/
- private void parseDocument( File file, String password )throws IOException
+ abstract class DocumentOpener
{
- while (true)
+ String password;
+
+ DocumentOpener(String password)
{
- try
- {
- document = PDDocument.load(file, password);
- }
- catch (InvalidPasswordException ipe)
+ this.password = password;
+ }
+
+ /**
+ * Override to load the actual input type (File, URL, stream), don't call it directly!
+ *
+ * @return
+ * @throws IOException
+ */
+ abstract PDDocument open() throws IOException;
+
+ /**
+ * Call this!
+ *
+ * @return
+ * @throws IOException
+ */
+ final PDDocument parse() throws IOException
+ {
+ PDDocument document;
+ while (true)
{
- // https://stackoverflow.com/questions/8881213/joptionpane-to-get-password
- JPanel panel = new JPanel();
- JLabel label = new JLabel("Password:");
- JPasswordField pass = new JPasswordField(10);
- panel.add(label);
- panel.add(pass);
- String[] options = new String[] {"OK", "Cancel"};
- int option = JOptionPane.showOptionDialog(null, panel, "Enter password",
- JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,
- null, options, "");
- if (option == 0)
+ try
{
- password = new String(pass.getPassword());
- continue;
+ document = open();
}
- throw ipe;
+ catch (InvalidPasswordException ipe)
+ {
+ // https://stackoverflow.com/questions/8881213/joptionpane-to-get-password
+ JPanel panel = new JPanel();
+ JLabel label = new JLabel("Password:");
+ JPasswordField pass = new JPasswordField(10);
+ panel.add(label);
+ panel.add(pass);
+ String[] options = new String[]
+ {
+ "OK", "Cancel"
+ };
+ int option = JOptionPane.showOptionDialog(null, panel, "Enter password",
+ JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,
+ null, options, "");
+ if (option == 0)
+ {
+ password = new String(pass.getPassword());
+ continue;
+ }
+ throw ipe;
+ }
+ break;
}
- break;
- }
- printMenuItem.setEnabled(true);
- reopenMenuItem.setEnabled(true);
+ return document;
+ }
}
private void addRecentFileItems()