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(org.opencastproject.metadata.mpeg7.KeywordAnnotation)
105    */
106   public void addKeywordAnnotation(KeywordAnnotation keywordAnnotation) {
107     keywordAnnotations.add(keywordAnnotation);
108   }
109 
110   /**
111    * Adds free text to this annotation.
112    *
113    * @param text
114    *          the free text
115    */
116   public void addFreeText(String text) {
117     addFreeTextAnnotation(new FreeTextAnnotationImpl(text));
118   }
119 
120   /**
121    * @see org.opencastproject.metadata.mpeg7.TextAnnotation#addFreeTextAnnotation(org.opencastproject.metadata.mpeg7.FreeTextAnnotation)
122    */
123   public void addFreeTextAnnotation(FreeTextAnnotation freeTextAnnotation) {
124     freeTextAnnotations.add(freeTextAnnotation);
125   }
126 
127   /**
128    * @see org.opencastproject.metadata.mpeg7.TextAnnotation#getConfidence()
129    */
130   public float getConfidence() {
131     return confidence;
132   }
133 
134   /**
135    * @see org.opencastproject.metadata.mpeg7.TextAnnotation#getLanguage()
136    */
137   public String getLanguage() {
138     return language;
139   }
140 
141   /**
142    * @see org.opencastproject.metadata.mpeg7.TextAnnotation#getRelevance()
143    */
144   public float getRelevance() {
145     return relevance;
146   }
147 
148   /**
149    * @see org.opencastproject.metadata.mpeg7.TextAnnotation#keywordAnnotations()
150    */
151   public Iterator<KeywordAnnotation> keywordAnnotations() {
152     return keywordAnnotations.iterator();
153   }
154 
155   /**
156    * @see org.opencastproject.metadata.mpeg7.TextAnnotation#freeTextAnnotations()
157    */
158   public Iterator<FreeTextAnnotation> freeTextAnnotations() {
159     return freeTextAnnotations.iterator();
160   }
161 
162   /**
163    * @see org.opencastproject.mediapackage.XmlElement#toXml(org.w3c.dom.Document)
164    */
165   public Node toXml(Document document) {
166     DecimalFormat format = new DecimalFormat("0.0");
167     format.setDecimalFormatSymbols(standardSymbols);
168     Element node = document.createElement("TextAnnotation");
169     if (confidence >= 0.0)
170       node.setAttribute("confidence", format.format(confidence));
171     if (relevance >= 0.0)
172       node.setAttribute("relevance", format.format(relevance));
173     if (language != null)
174       node.setAttribute("xml:lang", language);
175 
176     // Keyword anntiations
177     if (keywordAnnotations.size() > 0) {
178       Element kwAnnotationNode = document.createElement("KeywordAnnotation");
179       for (KeywordAnnotation annotation : keywordAnnotations) {
180         kwAnnotationNode.appendChild(annotation.toXml(document));
181       }
182       node.appendChild(kwAnnotationNode);
183     }
184 
185     // Free text anntiations
186     for (FreeTextAnnotation annotation : freeTextAnnotations) {
187       node.appendChild(annotation.toXml(document));
188     }
189 
190     return node;
191   }
192 
193 }