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         } else {
122           return true;
123         }
124       }
125     }
126     return false;
127   }
128 
129   /**
130    * @see org.opencastproject.metadata.mpeg7.Segment#getTextAnnotationCount()
131    */
132   public int getTextAnnotationCount() {
133     return annotations.size();
134   }
135 
136   /**
137    * @see org.opencastproject.metadata.mpeg7.Segment#textAnnotationsByConfidence()
138    */
139   public Iterator<TextAnnotation> textAnnotationsByConfidence() {
140     SortedSet<TextAnnotation> set = new TreeSet<TextAnnotation>(new Comparator<TextAnnotation>() {
141       public int compare(TextAnnotation a1, TextAnnotation a2) {
142         if (a1.getConfidence() > a2.getConfidence())
143           return 1;
144         else if (a1.getConfidence() > a2.getConfidence())
145           return -1;
146         return 0;
147       }
148     });
149     set.addAll(annotations);
150     return set.iterator();
151   }
152 
153   /**
154    * @see org.opencastproject.metadata.mpeg7.Segment#textAnnotationsByRelevance()
155    */
156   public Iterator<TextAnnotation> textAnnotationsByRelevance() {
157     SortedSet<TextAnnotation> set = new TreeSet<TextAnnotation>(new Comparator<TextAnnotation>() {
158       public int compare(TextAnnotation a1, TextAnnotation a2) {
159         if (a1.getRelevance() > a2.getRelevance())
160           return 1;
161         else if (a1.getRelevance() > a2.getRelevance())
162           return -1;
163         return 0;
164       }
165     });
166     set.addAll(annotations);
167     return set.iterator();
168   }
169 
170   /**
171    * @see org.opencastproject.metadata.mpeg7.Segment#createTextAnnotation(float, float, String)
172    */
173   public TextAnnotation createTextAnnotation(float relevance, float confidence, String language) {
174     TextAnnotationImpl annotation = new TextAnnotationImpl(relevance, confidence, language);
175     annotations.add(annotation);
176     return annotation;
177   }
178 
179   /**
180    * @see org.opencastproject.metadata.mpeg7.Segment#textAnnotations()
181    */
182   public Iterator<TextAnnotation> textAnnotations() {
183     return annotations.iterator();
184   }
185 
186   /**
187    * {@inheritDoc}
188    *
189    * @see org.opencastproject.metadata.mpeg7.VideoSegment#createSpatioTemporalDecomposition(boolean, boolean)
190    */
191   @Override
192   public SpatioTemporalDecomposition createSpatioTemporalDecomposition(boolean gap, boolean overlap)
193           throws IllegalStateException {
194     if (spatioTemporalDecomposition != null)
195       throw new IllegalStateException("A spatio temporal decomposition has already been created");
196     spatioTemporalDecomposition = new SpatioTemporalDecompositionImpl(true, false);
197     return spatioTemporalDecomposition;
198   }
199 
200   /**
201    * {@inheritDoc}
202    *
203    * @see org.opencastproject.metadata.mpeg7.AudioVisualSegment#getSpatioTemporalDecomposition()
204    */
205   @Override
206   public SpatioTemporalDecomposition getSpatioTemporalDecomposition() {
207     return spatioTemporalDecomposition;
208   }
209 
210   /**
211    * {@inheritDoc}
212    *
213    * @see org.opencastproject.metadata.mpeg7.AudioVisualSegment#hasSpatioTemporalDecomposition()
214    */
215   @Override
216   public boolean hasSpatioTemporalDecomposition() {
217     return spatioTemporalDecomposition != null;
218   }
219 
220   /**
221    * @see java.lang.Object#hashCode()
222    */
223   @Override
224   public int hashCode() {
225     return id.hashCode();
226   }
227 
228   /**
229    * @see java.lang.Object#equals(java.lang.Object)
230    */
231   @Override
232   public boolean equals(Object obj) {
233     if (obj instanceof Segment) {
234       return id.equals(((Segment) obj).getIdentifier());
235     }
236     return super.equals(obj);
237   }
238 
239   /**
240    * @see org.opencastproject.mediapackage.XmlElement#toXml(org.w3c.dom.Document)
241    */
242   public Node toXml(Document document) {
243     Element node = document.createElement(type.toString());
244     node.setAttribute("id", id);
245     node.appendChild(mediaTime.toXml(document));
246     if (spatioTemporalDecomposition != null)
247       node.appendChild(spatioTemporalDecomposition.toXml(document));
248     for (TextAnnotation annotation : annotations) {
249       node.appendChild(annotation.toXml(document));
250     }
251     return node;
252   }
253 
254 }