Added Android code
[wl-app.git] / Android / r2-streamer / r2-parser / src / main / java / org / readium / r2_streamer / model / publication / SMIL / MediaOverlays.java
1 package org.readium.r2_streamer.model.publication.SMIL;
2
3 import com.fasterxml.jackson.annotation.JsonProperty;
4
5 import java.io.Serializable;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 /**
10  * Created by gautam chibde on 23/5/17.
11  */
12
13 public class MediaOverlays implements Serializable {
14
15     private static final long serialVersionUID = 335418192699543070L;
16
17     @JsonProperty("media-overlay")
18     public List<MediaOverlayNode> mediaOverlayNodes;
19
20     public MediaOverlays() {
21         this.mediaOverlayNodes = new ArrayList<>();
22     }
23
24     @Override
25     public String toString() {
26         return "MediaOverlays{" +
27                 ", mediaOverlayNodes=" + mediaOverlayNodes +
28                 '}';
29     }
30
31     /**
32      * Function return the path of the audio file for the
33      * given page href
34      *
35      * @param href page href
36      * @return audio file path for given SMIL
37      */
38     public String getAudioPath(String href) {
39         // extract file name
40         if (href.contains("/")) {
41             int startIndex = href.lastIndexOf("/");
42             href = href.substring(startIndex + 1, href.length());
43         }
44         String path = findAudioPath(href, this.mediaOverlayNodes);
45         if (path != null) {
46             return path;
47         }
48         return null;
49     }
50
51     /**
52      * [RECURSIVE]
53      * <p>
54      * Return the file path from the first node element
55      *
56      * @param href  href of page
57      * @param nodes media overlay nodes
58      * @return audio file path
59      */
60     private String findAudioPath(String href,
61                                  List<MediaOverlayNode> nodes) {
62         // For each node of the current scope..
63         for (MediaOverlayNode node : nodes) {
64             if (node.audio != null) {
65                 if (node.text.contains(href)) {
66                     if (node.audio.contains("#")) {
67                         return node.audio.split("#")[0];
68                     }
69                 }
70             }
71             if (node.role.contains("section")) {
72                 return findAudioPath(href, node.children);
73             }
74         }
75         return null;
76     }
77
78     /**
79      * <p>
80      * Get the audio `Clip` associated to an audio Fragment id.
81      * The fragment id can be found in the HTML document in <p> & <span> tags,
82      * it refer to a element of one of the SMIL files, providing information
83      * This function returns the clip representing this element from SMIL.
84      * about the synchronized audio.
85      * </p>
86      *
87      * @param forFragmentId The audio fragment id.
88      * @return The `Clip`, representation of the associated SMIL element.
89      */
90     public Clip clip(String forFragmentId) {
91         MediaOverlayNode node = findNode(forFragmentId, this.mediaOverlayNodes);
92         if (node != null) {
93             return node.clip();
94         }
95         return new Clip();
96     }
97
98     /**
99      * [RECURSIVE]
100      * <p>
101      * Find the node (<par>) corresponding to "fragment" ?? nil.
102      * </p>
103      *
104      * @param fragment The current fragment name for which we are looking the
105      * @param nodes    The set of MediaOverlayNodes where to search. Default to  self children.
106      * @return node corresponding to the fragment id, null if not found
107      */
108     private MediaOverlayNode findNode(String fragment,
109                                       List<MediaOverlayNode> nodes) {
110         // For each node of the current scope..
111         for (MediaOverlayNode node : nodes) {
112             if (node.text.contains(fragment)) {
113                 return node;
114             }
115             if (node.role.contains("section")) {
116                 return findNode(fragment, node.children);
117             }
118         }
119         return null;
120     }
121 }