001    /*
002     * 
003     * $Revision: 14106 $ $Date: 2008-10-09 11:30:08 +0200 (Do, 09 Okt 2008) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.datamodel.metadata;
025    
026    import org.jdom.Namespace;
027    import org.mycore.common.MCRException;
028    
029    /**
030     * This class implements all method for handling with the MCRMetaLangText part
031     * of a metadata object. The MCRMetaLangText class present a single item, which
032     * has triples of a text and his corresponding language and optional a type.
033     * 
034     * @author Jens Kupferschmidt
035     * @version $Revision: 14106 $ $Date: 2008-10-09 11:30:08 +0200 (Do, 09 Okt 2008) $
036     */
037    public class MCRMetaLangText extends MCRMetaDefault {
038        // MetaLangText data
039        protected String text;
040    
041        protected String form;
042    
043        /**
044         * This is the constructor. <br>
045         * The language element was set to <b>en </b>. All other elemnts was set to
046         * an empty string. The <em>form</em> Attribute is set to 'plain'.
047         */
048        public MCRMetaLangText() {
049            super();
050            text = "";
051            form = "plain";
052        }
053    
054        /**
055         * This is the constructor. <br>
056         * The language element was set. If the value of <em>default_lang</em> is
057         * null, empty or false <b>en </b> was set. The subtag element was set to
058         * the value of <em>set_subtag<em>. If the value of <em>set_subtag</em>
059         * is null or empty an exception was throwed. The type element was set to
060         * the value of <em>set_type<em>, if it is null, an empty string was set
061         * to the type element. The text element was set to the value of
062         * <em>set_text<em>, if it is null, an empty string was set
063         * to the text element.
064         *
065         * @param set_datapart     the global part of the elements like 'metadata'
066         *                         or 'service'
067         * @param set_subtag       the name of the subtag
068         * @param default_lang     the default language
069         * @param set_type         the optional type string
070         * @param set_inherted     a value >= 0
071         * @param set_form         the format string, if it is empty 'plain' is set.
072         * @param set_text         the text string
073         * @exception MCRException if the set_subtag value is null or empty
074         */
075        public MCRMetaLangText(String set_datapart, String set_subtag, String default_lang, String set_type, int set_inherted, String set_form, String set_text) throws MCRException {
076            super(set_datapart, set_subtag, default_lang, set_type, set_inherted);
077            text = "";
078    
079            if (set_text != null) {
080                text = set_text.trim();
081            }
082    
083            form = "plain";
084    
085            if (set_form != null) {
086                form = set_form.trim();
087            }
088        }
089    
090        /**
091         * This method set the languge, type and text.
092         * 
093         * @param set_lang
094         *            the new language string, if this is null or empty, nothing is
095         *            to do
096         * @param set_type
097         *            the optional type syting
098         * @param set_text
099         *            the new text string
100         */
101        public final void set(String set_lang, String set_type, String set_form, String set_text) {
102            setLang(set_lang);
103            setType(set_type);
104    
105            if (set_text != null) {
106                text = set_text.trim();
107            }
108    
109            form = "plain";
110    
111            if (set_form != null) {
112                form = set_form.trim();
113            }
114        }
115    
116        /**
117         * This method set the text.
118         * 
119         * @param set_text
120         *            the new text string
121         */
122        public final void setText(String set_text) {
123            if (set_text != null) {
124                text = set_text.trim();
125            }
126        }
127    
128        /**
129         * This method set the form attribute.
130         * 
131         * @param set_form
132         *            the new form string
133         */
134        public final void setForm(String set_form) {
135            if (set_form != null) {
136                text = set_form.trim();
137            }
138        }
139    
140        /**
141         * This method get the text element.
142         * 
143         * @return the text
144         */
145        public final String getText() {
146            return text;
147        }
148    
149        /**
150         * This method get the form attribute.
151         * 
152         * @return the form attribute
153         */
154        public final String getForm() {
155            return form;
156        }
157    
158        /**
159         * This method read the XML input stream part from a DOM part for the
160         * metadata of the document.
161         * 
162         * @param element
163         *            a relevant JDOM element for the metadata
164         */
165        public void setFromDOM(org.jdom.Element element) {
166            super.setFromDOM(element);
167    
168            String temp_text = element.getText();
169    
170            if (temp_text == null) {
171                temp_text = "";
172            }
173    
174            text = temp_text.trim();
175    
176            String temp_form = element.getAttributeValue("form");
177    
178            if (temp_form == null) {
179                temp_form = "plain";
180            }
181    
182            form = temp_form.trim();
183        }
184    
185        /**
186         * This method create a XML stream for all data in this class, defined by
187         * the MyCoRe XML MCRMetaLangText definition for the given subtag.
188         * 
189         * @exception MCRException
190         *                if the content of this class is not valid
191         * @return a JDOM Element with the XML MCRMetaLangText part
192         */
193        public org.jdom.Element createXML() throws MCRException {
194            if (!isValid()) {
195                debug();
196                throw new MCRException("The content of MCRMetaLangText is not valid.");
197            }
198    
199            org.jdom.Element elm = new org.jdom.Element(subtag);
200            elm.setAttribute("lang", lang, Namespace.XML_NAMESPACE);
201            elm.setAttribute("inherited", Integer.toString(inherited));
202    
203            if ((form != null) && ((form = form.trim()).length() != 0)) {
204                elm.setAttribute("form", form);
205            }
206    
207            if ((type != null) && ((type = type.trim()).length() != 0)) {
208                elm.setAttribute("type", type);
209            }
210    
211            elm.addContent(text);
212    
213            return elm;
214        }
215    
216        /**
217         * This method check the validation of the content of this class. The method
218         * returns <em>true</em> if
219         * <ul>
220         * <li>the subtag is not null or empty
221         * <li>the text is not null or empty
222         * </ul>
223         * otherwise the method return <em>false</em>
224         * 
225         * @return a boolean value
226         */
227        public boolean isValid() {
228            if (!super.isValid()) {
229                return false;
230            }
231    
232            if ((text == null) || ((text = text.trim()).length() == 0)) {
233                return false;
234            }
235    
236            if ((form == null) || ((form = form.trim()).length() == 0)) {
237                form = "plain";
238            }
239    
240            return true;
241        }
242    
243        /**
244         * This method make a clone of this class.
245         */
246        public Object clone() {
247            return new MCRMetaLangText(datapart, subtag, lang, type, inherited, form, text);
248        }
249    
250        /**
251         * This method put debug data to the logger (for the debug mode).
252         */
253        public final void debug() {
254            super.debugDefault();
255            LOGGER.debug("Format             = " + form);
256            LOGGER.debug("Text               = " + text);
257            LOGGER.debug(" ");
258        }
259    }