PageRenderTime 286ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/src/net/sf/vfsjfilechooser/accessories/bookmarks/BookmarksWriter.java

https://github.com/codebling/VFSJFileChooser2
Java | 172 lines | 115 code | 24 blank | 33 comment | 16 complexity | 3ce7a1a6749d7d962e4ab0fcdf106fef MD5 | raw file
  1. /*
  2. *
  3. * Copyright (C) 2008-2009 Yves Zoundi
  4. * Copyright (C) 2008-2009 Stan Love
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * under the License.
  18. */
  19. package net.sf.vfsjfilechooser.accessories.bookmarks;
  20. import java.io.BufferedWriter;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.OutputStreamWriter;
  25. import java.io.StringWriter;
  26. import java.io.Writer;
  27. import java.security.InvalidKeyException;
  28. import java.security.NoSuchAlgorithmException;
  29. import java.util.Iterator;
  30. import java.util.List;
  31. import javax.crypto.BadPaddingException;
  32. import javax.crypto.Cipher;
  33. import javax.crypto.IllegalBlockSizeException;
  34. import javax.crypto.NoSuchPaddingException;
  35. import javax.crypto.spec.SecretKeySpec;
  36. /**
  37. * Utility class to save bookmarks
  38. *
  39. * @author Yves Zoundi <yveszoundi at users dot sf dot net>
  40. * @author Stan Love
  41. * @author Alex Arana <alex at arana.net.au>
  42. * @version 0.0.2
  43. */
  44. final class BookmarksWriter {
  45. private Writer writer;
  46. public BookmarksWriter() {
  47. }
  48. private void startAttribute(String name, String value) throws IOException {
  49. writer.write(" ");
  50. writer.write(name);
  51. writer.write(" =");
  52. writer.write("\"");
  53. writer.write(value);
  54. writer.write("\"");
  55. }
  56. private void startTag(String name) throws IOException {
  57. writer.write("<" + name + ">");
  58. }
  59. private void startNewLine() throws IOException {
  60. writer.write("\n");
  61. }
  62. private void endTag(String tagName) throws IOException {
  63. writer.write("</" + tagName + ">");
  64. }
  65. private void writeData(List<TitledURLEntry> entries)
  66. throws java.io.IOException {
  67. startTag("entries");
  68. Iterator<TitledURLEntry> it = entries.iterator();
  69. while (it.hasNext()) {
  70. TitledURLEntry entry = it.next();
  71. if ((entry == null)
  72. || ((entry.getTitle() == null) || (entry.getTitle()
  73. .length() == 0))) {
  74. it.remove();
  75. }
  76. startNewLine();
  77. writer.write("<entry");
  78. startAttribute("title", entry.getTitle());
  79. startAttribute("url", entry.getURL());
  80. if (entry instanceof FTPURLEntry) {
  81. FTPURLEntry ftpEntry = (FTPURLEntry) entry;
  82. startAttribute("passiveFtp", String.valueOf(ftpEntry.isPassiveFtp()));
  83. }
  84. writer.write("/>");
  85. }
  86. startNewLine();
  87. endTag("entries");
  88. }
  89. public void writeToFile(List<TitledURLEntry> entries, File bookmarksFile)
  90. throws IOException, NoSuchAlgorithmException, InvalidKeyException,
  91. NoSuchPaddingException, IllegalBlockSizeException,
  92. BadPaddingException {
  93. if ((entries == null) || (bookmarksFile == null)) {
  94. throw new NullPointerException();
  95. }
  96. // String write_type=""; //allow multiple encryption options
  97. String write_type = "b1"; // allow multiple encryption options
  98. if (write_type.equals("")) {
  99. writer = new BufferedWriter(new OutputStreamWriter(
  100. new FileOutputStream(bookmarksFile), "UTF-8"));
  101. writeData(entries);
  102. writer.flush();
  103. writer.close();
  104. }// if(write_type.equals(""))
  105. else if (write_type.equals("b1")) {
  106. // writer = new BufferedWriter(new OutputStreamWriter(
  107. // new FileOutputStream(bookmarksFile), "UTF-8"));
  108. writer = (new StringWriter());
  109. writeData(entries);
  110. // get the bytes so we can do the encryption
  111. byte[] out = writer.toString().getBytes();
  112. // do the encryption
  113. byte[] raw = new byte[16];
  114. raw[0] = (byte) 1;
  115. raw[2] = (byte) 23;
  116. raw[3] = (byte) 24;
  117. raw[4] = (byte) 2;
  118. raw[5] = (byte) 99;
  119. raw[6] = (byte) 200;
  120. raw[7] = (byte) 202;
  121. raw[8] = (byte) 209;
  122. raw[9] = (byte) 199;
  123. raw[10] = (byte) 181;
  124. raw[11] = (byte) 255;
  125. raw[12] = (byte) 33;
  126. raw[13] = (byte) 210;
  127. raw[14] = (byte) 214;
  128. raw[15] = (byte) 216;
  129. SecretKeySpec skeyspec = new SecretKeySpec(raw, "Blowfish");
  130. Cipher cipher = Cipher.getInstance("Blowfish");
  131. cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
  132. byte[] encrypted = cipher.doFinal(out);
  133. // write out results
  134. BufferedWriter writer2 = new BufferedWriter(new OutputStreamWriter(
  135. new FileOutputStream(bookmarksFile), "UTF-8"));
  136. writer2.write("b1");
  137. // writer2.write(out);
  138. writer2.write(Util.byteArraytoHexString(encrypted));
  139. writer2.flush();
  140. writer2.close();
  141. }// if(write_type.equals("b1"))
  142. else {
  143. System.out
  144. .println("FATAL ERROR -- BookmarksWriter.java unknown write style");
  145. System.exit(10);
  146. }
  147. }
  148. }