1 package org.readium.r2_streamer.parser;
3 import org.readium.r2_streamer.model.container.Container;
4 import org.readium.r2_streamer.model.publication.Encryption;
6 import org.w3c.dom.Document;
7 import org.w3c.dom.Element;
8 import org.w3c.dom.NodeList;
10 import java.util.ArrayList;
11 import java.util.List;
14 * Created by gautam chibde on 31/5/17.
17 public class EncryptionParser {
19 private static final String TAG = EncryptionParser.class.getSimpleName();
22 * parse file encryption.xml located at META-INF/encryption.xml
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}
28 public static List<Encryption> parseEncryption(Container container) {
29 String containerPath = "META-INF/encryption.xml";
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");
36 NodeList element = encryptionDocument.getDocumentElement().getElementsByTagName("EncryptedData");
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"));
48 if (pathElement != null) {
49 if (pathElement.hasAttribute("URI")) {
50 encryption.setProfile(pathElement.getAttribute("URI"));
55 encryptions.add(encryption);
58 } catch (EpubParserException e) {
61 } catch (NullPointerException e) {
62 System.out.println(TAG + " META-INF/encryption.xml not found " + e);