Advertisement
jdalbey

DBC problem - extractFilename

Mar 2nd, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. /** A function that extracts the filename from an URL (taken from the Mozilla project).
  2. * Examine the code and explain whether the code represents an "error checking" solution,
  3. * a Design by Contract solution, or neither.
  4. */
  5. public class DBCdemo
  6. {
  7. // function that extracts the filename from a url
  8. public static String getFileNameFromUrl(String urlstr)
  9. {
  10. if (urlstr.length() == 0)
  11. {
  12. return null;
  13. }
  14. // For "http://foo/bar/cheese.jpg", return "cheese.jpg".
  15. // For "imap://[email protected]:143/fetch>UID>/INBOX>951?part=1.2&type=image/gif&filename=foo.jpeg", return "foo.jpeg".
  16. // The 2nd url (ie, "imap://...") is generated for inline images by MimeInlineImage_parse_begin() in mimeiimg.cpp.
  17.  
  18. String tail = urlstr.substring(urlstr.lastIndexOf("/")+1);
  19.  
  20. if (tail.length()>0)
  21. {
  22. int nameIndex = tail.lastIndexOf( "filename=" );
  23. if (nameIndex != -1)
  24. {
  25. return (tail.substring(nameIndex+9));
  26. }
  27. else
  28. {
  29. return tail;
  30. }
  31. }
  32. return null;
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement