PageRenderTime 165ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/codebling/VFSJFileChooser2
Java | 218 lines | 163 code | 24 blank | 31 comment | 18 complexity | 041741e8c2bd9dff99e212fb610ba29e 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.BufferedReader;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.FileNotFoundException;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.InputStreamReader;
  28. import java.io.Reader;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.util.logging.Level;
  32. import java.util.logging.Logger;
  33. import javax.crypto.Cipher;
  34. import javax.crypto.spec.SecretKeySpec;
  35. import org.xml.sax.Attributes;
  36. import org.xml.sax.ContentHandler;
  37. import org.xml.sax.InputSource;
  38. import org.xml.sax.Locator;
  39. import org.xml.sax.SAXException;
  40. import org.xml.sax.SAXParseException;
  41. import org.xml.sax.XMLReader;
  42. import org.xml.sax.helpers.XMLReaderFactory;
  43. /**
  44. * Utility class to read bookmarks
  45. *
  46. * @author Yves Zoundi <yveszoundi at users dot sf dot net>
  47. * @author Stan Love
  48. * @author Alex Arana <alex at arana.net.au>
  49. * @version 0.0.2
  50. */
  51. final class BookmarksReader {
  52. private List<TitledURLEntry> entries;
  53. private Logger logger = Logger.getLogger(Bookmarks.class.getName());
  54. public BookmarksReader(File bookmarksFile) {
  55. entries = new ArrayList<TitledURLEntry>();
  56. Reader reader = null;
  57. InputStream is = null;
  58. try {
  59. XMLReader xmlReader = XMLReaderFactory.createXMLReader();
  60. xmlReader.setContentHandler(new BookmarksHandler());
  61. reader = new BufferedReader(new InputStreamReader(
  62. new FileInputStream(bookmarksFile), "UTF-8"));
  63. // read 1st 2 bytes to support multiple encryptions
  64. char[] code = new char[2];
  65. reader.read(code, 0, 2);
  66. logger.log(Level.FINEST, "code=" + String.valueOf(code) + "=");
  67. if ((code[0] == 'b') && (code[1] == '1')) {
  68. logger.log(Level.FINEST, "in encrypted code section");
  69. // read the encrypted file
  70. is = new FileInputStream(bookmarksFile);
  71. int the_length = (int) bookmarksFile.length() - 2;
  72. logger.log(Level.FINEST, "raw_length=" + (the_length + 2));
  73. if (the_length <= 0)
  74. the_length = 1;
  75. logger.log(Level.FINEST, "fixed_length=" + the_length);
  76. byte[] code2 = new byte[2];
  77. byte[] outhex = new byte[the_length];
  78. try {
  79. is.read(code2);
  80. is.read(outhex);
  81. // is.read(outhex,2,the_length);
  82. is.close();
  83. } catch (Exception e) {
  84. logger.log(Level.INFO, "exception reading encrypted file"
  85. + e);
  86. }
  87. byte[] out = Util.hexByteArrayToByteArray(outhex);
  88. // do the decryption
  89. byte[] raw = new byte[16];
  90. raw[0] = (byte) 1;
  91. raw[2] = (byte) 23;
  92. raw[3] = (byte) 24;
  93. raw[4] = (byte) 2;
  94. raw[5] = (byte) 99;
  95. raw[6] = (byte) 200;
  96. raw[7] = (byte) 202;
  97. raw[8] = (byte) 209;
  98. raw[9] = (byte) 199;
  99. raw[10] = (byte) 181;
  100. raw[11] = (byte) 255;
  101. raw[12] = (byte) 33;
  102. raw[13] = (byte) 210;
  103. raw[14] = (byte) 214;
  104. raw[15] = (byte) 216;
  105. SecretKeySpec skeyspec = new SecretKeySpec(raw, "Blowfish");
  106. Cipher cipher = Cipher.getInstance("Blowfish");
  107. cipher.init(Cipher.DECRYPT_MODE, skeyspec);
  108. byte[] decrypted = cipher.doFinal(out);
  109. // convert decrypted into a bytestream and parse it
  110. ByteArrayInputStream bstream = new ByteArrayInputStream(
  111. decrypted);
  112. InputSource inputSource = new InputSource(bstream);
  113. xmlReader.parse(inputSource);
  114. logger.log(Level.FINEST, "leaving encrypted code section");
  115. } else {
  116. logger.log(Level.FINEST, "in decrypted code section");
  117. InputSource inputSource = new InputSource(reader);
  118. xmlReader.parse(inputSource);
  119. logger.log(Level.FINEST, "leaving decrypted code section");
  120. }
  121. } catch (SAXParseException e) {
  122. StringBuilder sb = new StringBuilder();
  123. sb.append("Error parsing xml bookmarks file").append("\n").append(
  124. e.getLineNumber()).append(":").append(e.getColumnNumber())
  125. .append("\n").append(e.getMessage());
  126. throw new RuntimeException(sb.toString(), e);
  127. } catch (FileNotFoundException e) {
  128. throw new RuntimeException("Bookmarks file doesn't exist!", e);
  129. } catch (Exception e) {
  130. throw new RuntimeException(e);
  131. }
  132. finally{
  133. try{
  134. if(reader != null){
  135. reader.close();
  136. }
  137. if (is != null){
  138. is.close();
  139. }
  140. }
  141. catch(Throwable t){
  142. logger.log(Level.WARNING, "Unable to close bookmarks stream", t);
  143. }
  144. }
  145. }
  146. public List<TitledURLEntry> getParsedEntries() {
  147. return entries;
  148. }
  149. private class BookmarksHandler implements ContentHandler {
  150. public void setDocumentLocator(Locator locator) {
  151. }
  152. public void startDocument() throws SAXException {
  153. }
  154. public void endDocument() throws SAXException {
  155. }
  156. public void startPrefixMapping(String prefix, String uri)
  157. throws SAXException {
  158. }
  159. public void endPrefixMapping(String prefix) throws SAXException {
  160. }
  161. public void startElement(String uri, String localName, String qName,
  162. Attributes atts) throws SAXException {
  163. if ("entry".equals(localName)) {
  164. TitledURLEntry tue = null;
  165. String title = atts.getValue("title");
  166. String url = atts.getValue("url");
  167. String passiveFtp = atts.getValue("passiveFtp");
  168. if ((title != null) && (url != null)) {
  169. if (passiveFtp == null) {
  170. tue = new TitledURLEntry(title, url);
  171. }
  172. else {
  173. tue = new FTPURLEntry(title, url, Boolean.parseBoolean(passiveFtp));
  174. }
  175. entries.add(tue);
  176. }
  177. }
  178. }
  179. public void endElement(String uri, String localName, String qName)
  180. throws SAXException {
  181. }
  182. public void characters(char[] ch, int start, int length)
  183. throws SAXException {
  184. }
  185. public void ignorableWhitespace(char[] ch, int start, int length)
  186. throws SAXException {
  187. }
  188. public void processingInstruction(String target, String data)
  189. throws SAXException {
  190. }
  191. public void skippedEntity(String name) throws SAXException {
  192. }
  193. }
  194. }