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      this.text = text;
89      this.language = language;
90    }
91  
92    /**
93     * Creates a new textual base element.
94     *
95     * @param words
96     *          the words
97     * @throws IllegalArgumentException
98     *           if the words is <code>null</code> or empty
99     */
100   public TextualImpl(String[] words, String language) {
101     this(StringUtils.join(words, ' '), null);
102   }
103 
104   /**
105    * {@inheritDoc}
106    *
107    * @see org.opencastproject.metadata.mpeg7.Textual#getLanguage()
108    */
109   @Override
110   public String getLanguage() {
111     return language;
112   }
113 
114   /**
115    * {@inheritDoc}
116    *
117    * @see org.opencastproject.metadata.mpeg7.Textual#getPhoneticAlphabet()
118    */
119   @Override
120   public String getPhoneticAlphabet() {
121     return alphabet;
122   }
123 
124   /**
125    * {@inheritDoc}
126    *
127    * @see org.opencastproject.metadata.mpeg7.Textual#getPhoneticTranscription()
128    */
129   @Override
130   public String getPhoneticTranscription() {
131     return transcription;
132   }
133 
134   /**
135    * {@inheritDoc}
136    *
137    * @see org.opencastproject.metadata.mpeg7.Textual#setText(java.lang.String)
138    */
139   @Override
140   public void setText(String text) {
141     if (StringUtils.trimToNull(text) == null) {
142       throw new IllegalArgumentException("The text cannot be empty");
143     } else {
144       this.text = text;
145     }
146   }
147 
148   /**
149    * {@inheritDoc}
150    *
151    * @see org.opencastproject.metadata.mpeg7.Textual#getText()
152    */
153   @Override
154   public String getText() {
155     return text;
156   }
157 
158   /**
159    * {@inheritDoc}
160    *
161    * @see org.opencastproject.metadata.mpeg7.Textual#setLanguage(java.lang.String)
162    */
163   @Override
164   public void setLanguage(String language) {
165     this.language = language;
166   }
167 
168   /**
169    * {@inheritDoc}
170    *
171    * @see org.opencastproject.metadata.mpeg7.Textual#setPhoneticTranscription(java.lang.String, java.lang.String)
172    */
173   @Override
174   public void setPhoneticTranscription(String transcription, String alphabet) {
175     if (transcription != null && alphabet == null)
176       alphabet = DEFAULT_PHONETIC_ALPHABET;
177     this.transcription = transcription;
178     this.alphabet = alphabet;
179   }
180 
181   /**
182    * {@inheritDoc}
183    *
184    * @see org.opencastproject.mediapackage.XmlElement#toXml(org.w3c.dom.Document)
185    */
186   @Override
187   public Node toXml(Document document) {
188     Element node = document.createElement("Text");
189     if (language != null)
190       node.setAttribute("xml:lang", language);
191     if (transcription != null) {
192       node.setAttribute("phoneticTranscription", transcription);
193       node.setAttribute("phoneticAlphabet", alphabet);
194     }
195     node.appendChild(document.createTextNode(text));
196     return node;
197   }
198 
199 }