/plugins/FTP/tags/ftp_0_7_4/ftp/FtpAddress.java

# · Java · 100 lines · 62 code · 11 blank · 27 comment · 14 complexity · 9db72f4133848596158fc36a3f54f0e2 MD5 · raw file

  1. /*
  2. * FtpAddress.java - FTP addressing encapsulator
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 2002 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package ftp;
  23. import org.gjt.sp.jedit.jEdit;
  24. public class FtpAddress
  25. {
  26. public boolean secure;
  27. public String host;
  28. public String user;
  29. public String path;
  30. //{{{ FtpAddress constructor
  31. public FtpAddress(String url)
  32. {
  33. if(url.startsWith(FtpVFS.FTP_PROTOCOL + ":"))
  34. secure = false;
  35. else if(url.startsWith(FtpVFS.SFTP_PROTOCOL + ":"))
  36. secure = true;
  37. else
  38. throw new IllegalArgumentException();
  39. // remove any leading slashes, and ftp: from URL
  40. int trimAt = url.indexOf(':') + 1;
  41. for(int i = trimAt; i < url.length(); i++)
  42. {
  43. if(url.charAt(i) != '/')
  44. {
  45. trimAt = i;
  46. break;
  47. }
  48. }
  49. url = url.substring(trimAt);
  50. // get username
  51. int index = url.lastIndexOf('@');
  52. if(index != -1)
  53. {
  54. user = url.substring(0,index);
  55. url = url.substring(index + 1);
  56. }
  57. // get host name and path
  58. index = url.indexOf('/');
  59. if(index == -1)
  60. index = url.length();
  61. host = url.substring(0,index);
  62. path = url.substring(index);
  63. if(path.length() == 0)
  64. path = "/";
  65. } //}}}
  66. //{{{ FtpAddress constructor
  67. public FtpAddress(boolean secure, String host, String user, String path)
  68. {
  69. this.secure = secure;
  70. this.host = host;
  71. this.user = user;
  72. this.path = path;
  73. } //}}}
  74. //{{{ toString() method
  75. public String toString()
  76. {
  77. StringBuffer buf = new StringBuffer();
  78. buf.append(secure ? FtpVFS.SFTP_PROTOCOL : FtpVFS.FTP_PROTOCOL);
  79. buf.append("://");
  80. if(user != null)
  81. {
  82. buf.append(user);
  83. buf.append('@');
  84. }
  85. buf.append(host);
  86. buf.append(path);
  87. return buf.toString();
  88. } //}}}
  89. }