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  package org.opencastproject.metadata.mpeg7;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.w3c.dom.Document;
26  import org.w3c.dom.Element;
27  import org.w3c.dom.Node;
28  
29  /**
30   * Default implementation of a <code>TextualBase</code> mpeg-7 element.
31   */
32  public class TextualImpl implements Textual {
33  
34    /** The default phonetic alphabet */
35    public static final String DEFAULT_PHONETIC_ALPHABET = "sampa";
36  
37    /** The text */
38    protected String text = null;
39  
40    /** The text */
41    protected String language = null;
42  
43    /** The phonetic transcription */
44    protected String transcription = null;
45  
46    /** The phonetic alphabet */
47    protected String alphabet = DEFAULT_PHONETIC_ALPHABET;
48  
49    /**
50     * Creates a new textual base element.
51     */
52    public TextualImpl() {
53    }
54  
55    /**
56     * Creates a new textual base element.
57     *
58     * @param text
59     *          the text
60     * @throws IllegalArgumentException
61     *           if the text is <code>null</code> or empty
62     */
63    public TextualImpl(String text) {
64      this(text, null);
65    }
66  
67    /**
68     * Creates a new Textual element from a number of words.
69     *
70     * @param words
71     *          the words
72     */
73    public TextualImpl(String[] words) {
74      this(words, null);
75    }
76  
77    /**
78     * Creates a new textual base element.
79     *
80     * @param text
81     *          the text
82     * @throws IllegalArgumentException
83     *           if the text is <code>null</code> or empty
84     */
85    public TextualImpl(String text, String language) {
86      if (StringUtils.trimToNull(text) == null) {
87        throw new IllegalArgumentException("The text cannot be empty");
88      }
89      this.text = text;
90      this.language = language;
91    }
92  
93    /**
94     * Creates a new textual base element.
95     *
96     * @param words
97     *          the words
98     * @throws IllegalArgumentException
99     *           if the words is <code>null</code> or empty
100    */
101   public TextualImpl(String[] words, String language) {
102     this(StringUtils.join(words, ' '), null);
103   }
104 
105   /**
106    * {@inheritDoc}
107    *
108    * @see org.opencastproject.metadata.mpeg7.Textual#getLanguage()
109    */
110   @Override
111   public String getLanguage() {
112     return language;
113   }
114 
115   /**
116    * {@inheritDoc}
117    *
118    * @see org.opencastproject.metadata.mpeg7.Textual#getPhoneticAlphabet()
119    */
120   @Override
121   public String getPhoneticAlphabet() {
122     return alphabet;
123   }
124 
125   /**
126    * {@inheritDoc}
127    *
128    * @see org.opencastproject.metadata.mpeg7.Textual#getPhoneticTranscription()
129    */
130   @Override
131   public String getPhoneticTranscription() {
132     return transcription;
133   }
134 
135   /**
136    * {@inheritDoc}
137    *
138    * @see org.opencastproject.metadata.mpeg7.Textual#setText(java.lang.String)
139    */
140   @Override
141   public void setText(String text) {
142     if (StringUtils.trimToNull(text) == null) {
143       throw new IllegalArgumentException("The text cannot be empty");
144     } else {
145       this.text = text;
146     }
147   }
148 
149   /**
150    * {@inheritDoc}
151    *
152    * @see org.opencastproject.metadata.mpeg7.Textual#getText()
153    */
154   @Override
155   public String getText() {
156     return text;
157   }
158 
159   /**
160    * {@inheritDoc}
161    *
162    * @see org.opencastproject.metadata.mpeg7.Textual#setLanguage(java.lang.String)
163    */
164   @Override
165   public void setLanguage(String language) {
166     this.language = language;
167   }
168 
169   /**
170    * {@inheritDoc}
171    *
172    * @see org.opencastproject.metadata.mpeg7.Textual#setPhoneticTranscription(java.lang.String, java.lang.String)
173    */
174   @Override
175   public void setPhoneticTranscription(String transcription, String alphabet) {
176     if (transcription != null && alphabet == null) {
177       alphabet = DEFAULT_PHONETIC_ALPHABET;
178     }
179     this.transcription = transcription;
180     this.alphabet = alphabet;
181   }
182 
183   /**
184    * {@inheritDoc}
185    *
186    * @see org.opencastproject.mediapackage.XmlElement#toXml(org.w3c.dom.Document)
187    */
188   @Override
189   public Node toXml(Document document) {
190     Element node = document.createElement("Text");
191     if (language != null) {
192       node.setAttribute("xml:lang", language);
193     }
194     if (transcription != null) {
195       node.setAttribute("phoneticTranscription", transcription);
196       node.setAttribute("phoneticAlphabet", alphabet);
197     }
198     node.appendChild(document.createTextNode(text));
199     return node;
200   }
201 
202 }