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.text.DecimalFormat;
30  import java.text.DecimalFormatSymbols;
31  import java.util.ArrayList;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Locale;
35  
36  /**
37   * Implementation of text annotations.
38   */
39  public class TextAnnotationImpl implements TextAnnotation {
40  
41    /** Number formatter, used to deal with relevance values in a locale independent way */
42    private static DecimalFormatSymbols standardSymbols = new DecimalFormatSymbols(Locale.US);
43  
44    /** Confidence value */
45    protected float confidence = -1.0f;
46  
47    /** Relevance value */
48    protected float relevance = -1.0f;
49  
50    /** Language identifier */
51    protected String language = null;
52  
53    /** Keyword annotations */
54    protected List<KeywordAnnotation> keywordAnnotations = null;
55  
56    /** Free text annotations */
57    protected List<FreeTextAnnotation> freeTextAnnotations = null;
58  
59    static {
60      standardSymbols.setDecimalSeparator('.');
61    }
62  
63    /**
64     * Creates a new text annotation.
65     *
66     * @param confidence
67     *          the confidence value <code>[0.0..1.0]</code>
68     * @param relevance
69     *          the relevance value <code>[0.0..1.0]</code>
70     * @param language
71     *          the language identifier
72     */
73    public TextAnnotationImpl(float confidence, float relevance, String language) {
74      this.confidence = confidence;
75      this.relevance = relevance;
76      this.language = language;
77      keywordAnnotations = new ArrayList<KeywordAnnotation>();
78      freeTextAnnotations = new ArrayList<FreeTextAnnotation>();
79    }
80  
81    /**
82     * Adds the keyword to this annotation.
83     *
84     * @param keyword
85     *          the keyword
86     */
87    public void addKeyword(String keyword) {
88      addKeywordAnnotation(new KeywordAnnotationImpl(keyword));
89    }
90  
91    /**
92     * Adds the keyword to this annotation.
93     *
94     * @param keyword
95     *          the keyword
96     * @param type
97     *          the keyword type
98     */
99    public void addKeyword(String keyword, KeywordAnnotation.Type type) {
100     addKeywordAnnotation(new KeywordAnnotationImpl(keyword, type));
101   }
102 
103   /**
104    * @see org.opencastproject.metadata.mpeg7.TextAnnotation#addKeywordAnnotation(
105    * org.opencastproject.metadata.mpeg7.KeywordAnnotation)
106    */
107   public void addKeywordAnnotation(KeywordAnnotation keywordAnnotation) {
108     keywordAnnotations.add(keywordAnnotation);
109   }
110 
111   /**
112    * Adds free text to this annotation.
113    *
114    * @param text
115    *          the free text
116    */
117   public void addFreeText(String text) {
118     addFreeTextAnnotation(new FreeTextAnnotationImpl(text));
119   }
120 
121   /**
122    * @see org.opencastproject.metadata.mpeg7.TextAnnotation#addFreeTextAnnotation(
123    * org.opencastproject.metadata.mpeg7.FreeTextAnnotation)
124    */
125   public void addFreeTextAnnotation(FreeTextAnnotation freeTextAnnotation) {
126     freeTextAnnotations.add(freeTextAnnotation);
127   }
128 
129   /**
130    * @see org.opencastproject.metadata.mpeg7.TextAnnotation#getConfidence()
131    */
132   public float getConfidence() {
133     return confidence;
134   }
135 
136   /**
137    * @see org.opencastproject.metadata.mpeg7.TextAnnotation#getLanguage()
138    */
139   public String getLanguage() {
140     return language;
141   }
142 
143   /**
144    * @see org.opencastproject.metadata.mpeg7.TextAnnotation#getRelevance()
145    */
146   public float getRelevance() {
147     return relevance;
148   }
149 
150   /**
151    * @see org.opencastproject.metadata.mpeg7.TextAnnotation#keywordAnnotations()
152    */
153   public Iterator<KeywordAnnotation> keywordAnnotations() {
154     return keywordAnnotations.iterator();
155   }
156 
157   /**
158    * @see org.opencastproject.metadata.mpeg7.TextAnnotation#freeTextAnnotations()
159    */
160   public Iterator<FreeTextAnnotation> freeTextAnnotations() {
161     return freeTextAnnotations.iterator();
162   }
163 
164   /**
165    * @see org.opencastproject.mediapackage.XmlElement#toXml(org.w3c.dom.Document)
166    */
167   public Node toXml(Document document) {
168     DecimalFormat format = new DecimalFormat("0.0");
169     format.setDecimalFormatSymbols(standardSymbols);
170     Element node = document.createElement("TextAnnotation");
171     if (confidence >= 0.0) {
172       node.setAttribute("confidence", format.format(confidence));
173     }
174     if (relevance >= 0.0) {
175       node.setAttribute("relevance", format.format(relevance));
176     }
177     if (language != null) {
178       node.setAttribute("xml:lang", language);
179     }
180 
181     // Keyword anntiations
182     if (keywordAnnotations.size() > 0) {
183       Element kwAnnotationNode = document.createElement("KeywordAnnotation");
184       for (KeywordAnnotation annotation : keywordAnnotations) {
185         kwAnnotationNode.appendChild(annotation.toXml(document));
186       }
187       node.appendChild(kwAnnotationNode);
188     }
189 
190     // Free text anntiations
191     for (FreeTextAnnotation annotation : freeTextAnnotations) {
192       node.appendChild(annotation.toXml(document));
193     }
194 
195     return node;
196   }
197 
198 }