001    /*
002     * 
003     * $Revision: 14003 $ $Date: 2008-09-16 11:45:07 +0200 (Di, 16 Sep 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 MCRMetaNumber part of
031     * a metadata object. The MCRMetaNumber class present a number value in decimal
032     * format and optional a type and a measurement. The number can has the format
033     * like <em>xxxx.xxx</em> or <em>xxxx,xxx</em>. There was stored three
034     * numbers after the dot and nine befor them. Also you can store an integer.
035     * <p>
036     * &lt;tag class="MCRMetaNumber" heritable="..."&gt; <br>
037     * &lt;subtag type="..." xml:lang="..." measurement="..."&gt; <br>
038     * xxxx.xxx or xxx <br>
039     * &lt;/subtag&gt; <br>
040     * &lt;/tag&gt; <br>
041     * 
042     * @author Jens Kupferschmidt
043     * @version $Revision: 14003 $ $Date: 2008-09-16 11:45:07 +0200 (Di, 16 Sep 2008) $
044     */
045    final public class MCRMetaNumber extends MCRMetaDefault {
046        /** The length of the attributes * */
047        public static final int MAX_DIMENSION_LENGTH = 128;
048    
049        public static final int MAX_MEASUREMENT_LENGTH = 64;
050    
051        // MCRMetaNumber data
052        private double number;
053    
054        private String dimension;
055    
056        private String measurement;
057    
058        /**
059         * This is the constructor. <br>
060         * The language element was set to <b>en </b>. The number was set to zero,
061         * the measurement and the dimension was set to an empty string.
062         */
063        public MCRMetaNumber() {
064            super();
065            number = 0.;
066            dimension = "";
067            measurement = "";
068        }
069    
070        /**
071         * This is the constructor. <br>
072         * The language element was set. If the value of <em>default_lang</em> is
073         * null, empty or false <b>en </b> was set. The subtag element was set to
074         * the value of <em>set_subtag<em>. If the value of <em>set_subtag</em>
075         * is null or empty an exception was throwed. The dimension element was set to
076         * the value of <em>set_dimension<em>, if it is null, an empty string was set
077         * to the type element. The measurement element was set to the value of
078         * <em>set_measurement<em>, if it is null, an empty string was set
079         * to the measurement element.  The number string <em>set_number</em>
080         * was set to the number element, if it is null or not a number, a
081         * MCRException was thowed.
082         *
083         * @param set_datapart     the global part of the elements like 'metadata'
084         *                         or 'service'
085         * @param set_subtag       the name of the subtag
086         * @param default_lang     the default language
087         * @param set_inherted     a value >= 0
088         * @param set_dimension    the optional dimension string
089         * @param set_measurement  the optional measurement string
090         * @param set_number       the number string
091         * @exception MCRException if the set_subtag value is null or empty or if
092         *   the number string is not in a number format
093         */
094        public MCRMetaNumber(String set_datapart, String set_subtag, String default_lang, int set_inherted, String set_dimension, String set_measurement, String set_number) throws MCRException {
095            super(set_datapart, set_subtag, default_lang, "", set_inherted);
096            set_number = set_number.trim();
097            number = 0.;
098    
099            try {
100                if (set_number == null) {
101                    throw new MCRException("The format of a number is false.");
102                }
103    
104                String new_number = set_number.replace(',', '.');
105                number = (new Double(new_number)).doubleValue();
106            } catch (NumberFormatException e) {
107                throw new MCRException("The format of a number is false.");
108            }
109    
110            dimension = "";
111    
112            if (set_dimension != null) {
113                dimension = set_dimension;
114            }
115    
116            if (dimension.length() >= MAX_DIMENSION_LENGTH) {
117                dimension = dimension.substring(0, MAX_DIMENSION_LENGTH);
118            }
119    
120            measurement = "";
121    
122            if (set_measurement != null) {
123                measurement = set_measurement;
124            }
125    
126            if (measurement.length() >= MAX_MEASUREMENT_LENGTH) {
127                measurement = measurement.substring(0, MAX_MEASUREMENT_LENGTH);
128            }
129        }
130    
131        /**
132         * This is the constructor. <br>
133         * The language element was set. If the value of <em>default_lang</em> is
134         * null, empty or false <b>en </b> was set. The subtag element was set to
135         * the value of <em>set_subtag<em>. If the value of <em>set_subtag</em>
136         * is null or empty an exception was throwed. The dimension element was set to
137         * the value of <em>set_dimension<em>, if it is null, an empty string was set
138         * to the type element. The measurement element was set to the value of
139         * <em>set_measurement<em>, if it is null, an empty string was set
140         * to the measurement element.  The number <em>set_number</em>
141         * was set to the number element.
142         *
143         * @param set_datapart     the global part of the elements like 'metadata'
144         *                         or 'service'
145         * @param set_subtag       the name of the subtag
146         * @param default_lang     the default language
147         * @param set_inherted     a value >= 0
148         * @param set_dimension    the optional dimension string
149         * @param set_measurement  the optional measurement string
150         * @param set_number       the number value
151         * @exception MCRException if the set_subtag value is null or empty
152         */
153        public MCRMetaNumber(String set_datapart, String set_subtag, String default_lang, int set_inherted, String set_dimension, String set_measurement, double set_number) throws MCRException {
154            super(set_datapart, set_subtag, default_lang, "", set_inherted);
155            number = set_number;
156            dimension = "";
157    
158            if (set_dimension != null) {
159                dimension = set_dimension;
160            }
161    
162            if (dimension.length() >= MAX_DIMENSION_LENGTH) {
163                dimension = dimension.substring(0, MAX_DIMENSION_LENGTH);
164            }
165    
166            measurement = "";
167    
168            if (set_measurement != null) {
169                measurement = set_measurement;
170            }
171    
172            if (measurement.length() >= MAX_MEASUREMENT_LENGTH) {
173                measurement = measurement.substring(0, MAX_MEASUREMENT_LENGTH);
174            }
175        }
176    
177        /**
178         * This method set the dimension, if it is null, an empty string was set to
179         * the dimension element.
180         * 
181         * @param set_dimension
182         *            the dimension string
183         */
184        public final void setDimension(String set_dimension) {
185            dimension = "";
186    
187            if (set_dimension != null) {
188                dimension = set_dimension;
189            }
190    
191            if (dimension.length() >= MAX_DIMENSION_LENGTH) {
192                dimension = dimension.substring(0, MAX_DIMENSION_LENGTH);
193            }
194        }
195    
196        /**
197         * This method set the measurement, if it is null, an empty string was set
198         * to the measurement element.
199         * 
200         * @param set_measurement
201         *            the measurement string
202         */
203        public final void setMeasurement(String set_measurement) {
204            measurement = "";
205    
206            if (set_measurement != null) {
207                measurement = set_measurement;
208            }
209    
210            if (measurement.length() >= MAX_MEASUREMENT_LENGTH) {
211                measurement = measurement.substring(0, MAX_MEASUREMENT_LENGTH);
212            }
213        }
214    
215        /**
216         * This method set the number, if it is null or not a number, a MCRException
217         * was thowed.
218         * 
219         * @param set_number
220         *            the number string
221         * @exception MCRException
222         *                if the number string is not in a number format
223         */
224        public final void setNumber(String set_number) {
225            String sset_number = set_number.replace(',', '.');
226            sset_number = sset_number.trim();
227            number = 0.;
228    
229            try {
230                if (sset_number == null) {
231                    throw new MCRException("The format of a number is false.");
232                }
233    
234                number = (new Double(sset_number)).doubleValue();
235            } catch (NumberFormatException e) {
236                throw new MCRException("The format of a number is false.");
237            }
238        }
239    
240        /**
241         * This method set the number.
242         * 
243         * @param set_number
244         *            the number value
245         */
246        public final void setNumber(double set_number) {
247            number = set_number;
248        }
249    
250        /**
251         * This method get the dimension element.
252         * 
253         * @return the dimension String
254         */
255        public final String getDimension() {
256            return dimension;
257        }
258    
259        /**
260         * This method get the measurement element.
261         * 
262         * @return the measurement String
263         */
264        public final String getMeasurement() {
265            return measurement;
266        }
267    
268        /**
269         * This method get the number element.
270         * 
271         * @return the number
272         */
273        public final double getNumber() {
274            return number;
275        }
276    
277        /**
278         * This method get the number element as String.
279         * 
280         * @return the number String
281         */
282        public final String getNumberToString() {
283            return (new Double(number)).toString();
284        }
285    
286        /**
287         * This method read the XML input stream part from a DOM part for the
288         * metadata of the document.
289         * 
290         * @param element
291         *            a relevant JDOM element for the metadata
292         */
293        public final void setFromDOM(org.jdom.Element element) {
294            super.setFromDOM(element);
295    
296            org.jdom.Attribute attr;
297            measurement = "";
298            attr = element.getAttribute("measurement");
299    
300            if (attr != null) {
301                String temp_meas = attr.getValue();
302    
303                if ((temp_meas != null) && ((temp_meas = temp_meas.trim()).length() != 0)) {
304                    measurement = temp_meas;
305                }
306    
307                if (measurement.length() >= MAX_MEASUREMENT_LENGTH) {
308                    measurement = measurement.substring(0, MAX_MEASUREMENT_LENGTH);
309                }
310            }
311    
312            dimension = "";
313            attr = element.getAttribute("dimension");
314    
315            if (attr != null) {
316                String temp_dim = attr.getValue();
317    
318                if ((temp_dim != null) && ((temp_dim = temp_dim.trim()).length() != 0)) {
319                    dimension = temp_dim;
320                }
321    
322                if (dimension.length() >= MAX_DIMENSION_LENGTH) {
323                    dimension = dimension.substring(0, MAX_DIMENSION_LENGTH);
324                }
325            }
326    
327            String temp_value = (element.getText()).trim();
328    
329            if (temp_value == null) {
330                number = 0.;
331    
332                return;
333            }
334    
335            setNumber(temp_value);
336        }
337    
338        /**
339         * This method create a XML stream for all data in this class, defined by
340         * the MyCoRe XML MCRNumber definition for the given subtag.
341         * 
342         * @exception MCRException
343         *                if the content of this class is not valid
344         * @return a JDOM Element with the XML MCRNumber part
345         */
346        public final org.jdom.Element createXML() throws MCRException {
347            if (!isValid()) {
348                throw new MCRException("The content of MCRMetaNumber is not valid.");
349            }
350    
351            org.jdom.Element elm = new org.jdom.Element(subtag);
352            elm.setAttribute("lang", lang, Namespace.XML_NAMESPACE);
353            elm.setAttribute("inherited", Integer.toString(inherited));
354    
355            if ((type != null) && ((type = type.trim()).length() != 0)) {
356                elm.setAttribute("type", type);
357            }
358    
359            if ((dimension != null) && ((dimension = dimension.trim()).length() != 0)) {
360                elm.setAttribute("dimension", dimension);
361            }
362    
363            if ((measurement != null) && ((measurement = measurement.trim()).length() != 0)) {
364                elm.setAttribute("measurement", measurement);
365            }
366    
367            elm.addContent(getNumberToString());
368    
369            return elm;
370        }
371    
372        /**
373         * This method check the validation of the content of this class. The method
374         * returns <em>true</em> if
375         * <ul>
376         * <li>the subtag is not null or empty
377         * </ul>
378         * otherwise the method return <em>false</em>
379         * 
380         * @return a boolean value
381         */
382        public final boolean isValid() {
383            if (!super.isValid()) {
384                return false;
385            }
386    
387            return true;
388        }
389    
390        /**
391         * This method make a clone of this class.
392         */
393        public final Object clone() {
394            return new MCRMetaNumber(datapart, subtag, lang, inherited, dimension, measurement, number);
395        }
396    
397        /**
398         * This method put debug data to the logger (for the debug mode).
399         */
400        public final void debug() {
401            super.debugDefault();
402            LOGGER.debug("Measurement        = " + measurement);
403            LOGGER.debug("Dimension          = " + dimension);
404            LOGGER.debug("Value              = " + number);
405            LOGGER.debug("");
406        }
407    }