X-Git-Url: https://git.mdrn.pl/wl-app.git/blobdiff_plain/48b2fe9f7c2dc3d9aeaaa6dbfb27c7da4f3235ff..269195b3729c1bdc22e9053ee4ebca667ea8549d:/Android/r2-streamer/r2-parser/src/main/java/org/readium/r2_streamer/parser/EncryptionParser.java diff --git a/Android/r2-streamer/r2-parser/src/main/java/org/readium/r2_streamer/parser/EncryptionParser.java b/Android/r2-streamer/r2-parser/src/main/java/org/readium/r2_streamer/parser/EncryptionParser.java new file mode 100755 index 0000000..6d34082 --- /dev/null +++ b/Android/r2-streamer/r2-parser/src/main/java/org/readium/r2_streamer/parser/EncryptionParser.java @@ -0,0 +1,66 @@ +package org.readium.r2_streamer.parser; + +import org.readium.r2_streamer.model.container.Container; +import org.readium.r2_streamer.model.publication.Encryption; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by gautam chibde on 31/5/17. + */ + +public class EncryptionParser { + + private static final String TAG = EncryptionParser.class.getSimpleName(); + + /** + * parse file encryption.xml located at META-INF/encryption.xml + *

+ * content of the encryption file are saved as {@link Encryption} where path to + * encrypted file is saved in {@link Encryption#profile} and + * encryption algorithm in {@link Encryption#algorithm} + */ + public static List parseEncryption(Container container) { + String containerPath = "META-INF/encryption.xml"; + try { + String containerData = container.rawData(containerPath); + Document encryptionDocument = EpubParser.xmlParser(containerData); + if (encryptionDocument == null) { + throw new EpubParserException("Error while paring META-INF/encryption.xml"); + } + NodeList element = encryptionDocument.getDocumentElement().getElementsByTagName("EncryptedData"); + + List encryptions = new ArrayList<>(); + for (int i = 0; i < element.getLength(); i++) { + Encryption encryption = new Encryption(); + Element algorithmElement = (Element) ((Element) element.item(i)).getElementsByTagName("EncryptionMethod").item(0); + Element pathElement = (Element) ((Element) ((Element) element.item(i)).getElementsByTagName("CipherData").item(0)).getElementsByTagName("CipherReference").item(0); + if (algorithmElement != null) { + if (algorithmElement.hasAttribute("Algorithm")) { + encryption.setAlgorithm(algorithmElement.getAttribute("Algorithm")); + } + } + if (pathElement != null) { + if (pathElement.hasAttribute("URI")) { + encryption.setProfile(pathElement.getAttribute("URI")); + } + } + //TODO properties + //TODO LCP + encryptions.add(encryption); + } + return encryptions; + } catch (EpubParserException e) { + e.printStackTrace(); + return null; + } catch (NullPointerException e) { + System.out.println(TAG + " META-INF/encryption.xml not found " + e); + return null; + } + } +}