Added Android code
[wl-app.git] / Android / r2-streamer / r2-parser / src / main / java / org / readium / r2_streamer / parser / EncryptionParser.java
1 package org.readium.r2_streamer.parser;
2
3 import org.readium.r2_streamer.model.container.Container;
4 import org.readium.r2_streamer.model.publication.Encryption;
5
6 import org.w3c.dom.Document;
7 import org.w3c.dom.Element;
8 import org.w3c.dom.NodeList;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 /**
14  * Created by gautam chibde on 31/5/17.
15  */
16
17 public class EncryptionParser {
18
19     private static final String TAG = EncryptionParser.class.getSimpleName();
20
21     /**
22      * parse file encryption.xml located at META-INF/encryption.xml
23      * <p>
24      * content of the encryption file are saved as {@link Encryption} where path to
25      * encrypted file is saved in {@link Encryption#profile} and
26      * encryption algorithm in {@link Encryption#algorithm}
27      */
28     public static List<Encryption> parseEncryption(Container container) {
29         String containerPath = "META-INF/encryption.xml";
30         try {
31             String containerData = container.rawData(containerPath);
32             Document encryptionDocument = EpubParser.xmlParser(containerData);
33             if (encryptionDocument == null) {
34                 throw new EpubParserException("Error while paring META-INF/encryption.xml");
35             }
36             NodeList element = encryptionDocument.getDocumentElement().getElementsByTagName("EncryptedData");
37
38             List<Encryption> encryptions = new ArrayList<>();
39             for (int i = 0; i < element.getLength(); i++) {
40                 Encryption encryption = new Encryption();
41                 Element algorithmElement = (Element) ((Element) element.item(i)).getElementsByTagName("EncryptionMethod").item(0);
42                 Element pathElement = (Element) ((Element) ((Element) element.item(i)).getElementsByTagName("CipherData").item(0)).getElementsByTagName("CipherReference").item(0);
43                 if (algorithmElement != null) {
44                     if (algorithmElement.hasAttribute("Algorithm")) {
45                         encryption.setAlgorithm(algorithmElement.getAttribute("Algorithm"));
46                     }
47                 }
48                 if (pathElement != null) {
49                     if (pathElement.hasAttribute("URI")) {
50                         encryption.setProfile(pathElement.getAttribute("URI"));
51                     }
52                 }
53                 //TODO properties
54                 //TODO LCP
55                 encryptions.add(encryption);
56             }
57             return encryptions;
58         } catch (EpubParserException e) {
59             e.printStackTrace();
60             return null;
61         } catch (NullPointerException e) {
62             System.out.println(TAG + " META-INF/encryption.xml not found " + e);
63             return null;
64         }
65     }
66 }