View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  
23  package org.opencastproject.metadata.mpeg7;
24  
25  import org.w3c.dom.Document;
26  import org.w3c.dom.Element;
27  import org.w3c.dom.Node;
28  
29  import java.util.ArrayList;
30  import java.util.Comparator;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.SortedSet;
34  import java.util.TreeSet;
35  
36  /**
37   * Default implementation of the <code>SegmentType</code>.
38   */
39  public class SegmentImpl implements Segment, AudioSegment, VideoSegment, AudioVisualSegment {
40  
41    /** The content type */
42    protected Segment.Type type = null;
43  
44    /** The content element identifier */
45    protected String id = null;
46  
47    /** The content time contraints */
48    protected MediaTime mediaTime = null;
49  
50    /** The text annotations */
51    protected List<TextAnnotation> annotations = null;
52  
53    /** An optional spatio-temporal decomposition */
54    protected SpatioTemporalDecomposition spatioTemporalDecomposition = null;
55  
56    /**
57     * Creates a new content segment.
58     *
59     * @param type
60     *          the segment type
61     * @param id
62     *          the segment identifier
63     */
64    public SegmentImpl(Segment.Type type, String id) {
65      this.type = type;
66      this.id = id;
67      annotations = new ArrayList<TextAnnotation>();
68    }
69  
70    /**
71     * @see org.opencastproject.metadata.mpeg7.Segment#getIdentifier()
72     */
73    public String getIdentifier() {
74      return id;
75    }
76  
77    /**
78     * @see org.opencastproject.metadata.mpeg7.Segment#setMediaTime(org.opencastproject.metadata.mpeg7.MediaTime)
79     */
80    public void setMediaTime(MediaTime mediaTime) {
81      this.mediaTime = mediaTime;
82    }
83  
84    /**
85     * @see org.opencastproject.metadata.mpeg7.Segment#getMediaTime()
86     */
87    public MediaTime getMediaTime() {
88      return mediaTime;
89    }
90  
91    /**
92     * @see org.opencastproject.metadata.mpeg7.Segment#hasTextAnnotations()
93     */
94    public boolean hasTextAnnotations() {
95      return annotations.size() > 0;
96    }
97  
98    /**
99     * @see org.opencastproject.metadata.mpeg7.Segment#hasTextAnnotations(java.lang.String)
100    */
101   public boolean hasTextAnnotations(String language) {
102     return hasTextAnnotations(0.0f, 0.0f, language);
103   }
104 
105   /**
106    * @see org.opencastproject.metadata.mpeg7.Segment#hasTextAnnotations(float, float)
107    */
108   public boolean hasTextAnnotations(float relevance, float confidence) {
109     return hasTextAnnotations(relevance, confidence, null);
110   }
111 
112   /**
113    * @see org.opencastproject.metadata.mpeg7.Segment#hasTextAnnotations(float, float, java.lang.String)
114    */
115   public boolean hasTextAnnotations(float relevance, float confidence, String language) {
116     for (TextAnnotation annotation : annotations) {
117       if (annotation.getRelevance() >= relevance && annotation.getConfidence() >= confidence) {
118         if (language != null) {
119           if (language.equals(annotation.getLanguage())) {
120             return true;
121           }
122         } else {
123           return true;
124         }
125       }
126     }
127     return false;
128   }
129 
130   /**
131    * @see org.opencastproject.metadata.mpeg7.Segment#getTextAnnotationCount()
132    */
133   public int getTextAnnotationCount() {
134     return annotations.size();
135   }
136 
137   /**
138    * @see org.opencastproject.metadata.mpeg7.Segment#textAnnotationsByConfidence()
139    */
140   public Iterator<TextAnnotation> textAnnotationsByConfidence() {
141     SortedSet<TextAnnotation> set = new TreeSet<TextAnnotation>(new Comparator<TextAnnotation>() {
142       public int compare(TextAnnotation a1, TextAnnotation a2) {
143         if (a1.getConfidence() > a2.getConfidence()) {
144           return 1;
145         } else if (a1.getConfidence() > a2.getConfidence()) {
146           return -1;
147         }
148         return 0;
149       }
150     });
151     set.addAll(annotations);
152     return set.iterator();
153   }
154 
155   /**
156    * @see org.opencastproject.metadata.mpeg7.Segment#textAnnotationsByRelevance()
157    */
158   public Iterator<TextAnnotation> textAnnotationsByRelevance() {
159     SortedSet<TextAnnotation> set = new TreeSet<TextAnnotation>(new Comparator<TextAnnotation>() {
160       public int compare(TextAnnotation a1, TextAnnotation a2) {
161         if (a1.getRelevance() > a2.getRelevance()) {
162           return 1;
163         } else if (a1.getRelevance() > a2.getRelevance()) {
164           return -1;
165         }
166         return 0;
167       }
168     });
169     set.addAll(annotations);
170     return set.iterator();
171   }
172 
173   /**
174    * @see org.opencastproject.metadata.mpeg7.Segment#createTextAnnotation(float, float, String)
175    */
176   public TextAnnotation createTextAnnotation(float relevance, float confidence, String language) {
177     TextAnnotationImpl annotation = new TextAnnotationImpl(relevance, confidence, language);
178     annotations.add(annotation);
179     return annotation;
180   }
181 
182   /**
183    * @see org.opencastproject.metadata.mpeg7.Segment#textAnnotations()
184    */
185   public Iterator<TextAnnotation> textAnnotations() {
186     return annotations.iterator();
187   }
188 
189   /**
190    * {@inheritDoc}
191    *
192    * @see org.opencastproject.metadata.mpeg7.VideoSegment#createSpatioTemporalDecomposition(boolean, boolean)
193    */
194   @Override
195   public SpatioTemporalDecomposition createSpatioTemporalDecomposition(boolean gap, boolean overlap)
196           throws IllegalStateException {
197     if (spatioTemporalDecomposition != null) {
198       throw new IllegalStateException("A spatio temporal decomposition has already been created");
199     }
200     spatioTemporalDecomposition = new SpatioTemporalDecompositionImpl(true, false);
201     return spatioTemporalDecomposition;
202   }
203 
204   /**
205    * {@inheritDoc}
206    *
207    * @see org.opencastproject.metadata.mpeg7.AudioVisualSegment#getSpatioTemporalDecomposition()
208    */
209   @Override
210   public SpatioTemporalDecomposition getSpatioTemporalDecomposition() {
211     return spatioTemporalDecomposition;
212   }
213 
214   /**
215    * {@inheritDoc}
216    *
217    * @see org.opencastproject.metadata.mpeg7.AudioVisualSegment#hasSpatioTemporalDecomposition()
218    */
219   @Override
220   public boolean hasSpatioTemporalDecomposition() {
221     return spatioTemporalDecomposition != null;
222   }
223 
224   /**
225    * @see java.lang.Object#hashCode()
226    */
227   @Override
228   public int hashCode() {
229     return id.hashCode();
230   }
231 
232   /**
233    * @see java.lang.Object#equals(java.lang.Object)
234    */
235   @Override
236   public boolean equals(Object obj) {
237     if (obj instanceof Segment) {
238       return id.equals(((Segment) obj).getIdentifier());
239     }
240     return super.equals(obj);
241   }
242 
243   /**
244    * @see org.opencastproject.mediapackage.XmlElement#toXml(org.w3c.dom.Document)
245    */
246   public Node toXml(Document document) {
247     Element node = document.createElement(type.toString());
248     node.setAttribute("id", id);
249     node.appendChild(mediaTime.toXml(document));
250     if (spatioTemporalDecomposition != null) {
251       node.appendChild(spatioTemporalDecomposition.toXml(document));
252     }
253     for (TextAnnotation annotation : annotations) {
254       node.appendChild(annotation.toXml(document));
255     }
256     return node;
257   }
258 
259 }