001    /*
002     * 
003     * $Revision: 14000 $ $Date: 2008-09-16 11:21:12 +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.apache.log4j.Logger;
027    
028    import org.mycore.common.MCRConfiguration;
029    import org.mycore.common.MCRException;
030    
031    /**
032     * This class implements any methods for handling the basic data for all
033     * metadata classes of the metadata objects. The methods createXML() and
034     * createTypedContent() and createTextSearch() are abstract methods.
035     * 
036     * @author Jens Kupferschmidt
037     * @version $Revision: 14000 $ $Date: 2008-09-16 11:21:12 +0200 (Di, 16 Sep 2008) $
038     */
039    public abstract class MCRMetaDefault implements MCRMetaInterface {
040        // public data
041        public static final int DEFAULT_LANG_LENGTH = 12;
042    
043        public static final int DEFAULT_TYPE_LENGTH = 256;
044    
045        public static final int DEFAULT_STRING_LENGTH = 4096;
046    
047        // common data
048        protected static final String NL = System.getProperties().getProperty("line.separator");
049    
050        protected static final String DEFAULT_LANGUAGE = MCRConfiguration.instance().getString("MCR.Metadata.DefaultLang","en");
051    
052        protected static final String DEFAULT_DATAPART = "metadata";
053    
054        protected static final int DEFAULT_INHERITED = 0;
055    
056        // logger
057        static Logger LOGGER = Logger.getLogger(MCRMetaDefault.class.getName());
058    
059        // MetaLangText data
060        protected String subtag;
061    
062        protected String lang;
063    
064        protected String type;
065    
066        protected int inherited;
067    
068        protected String datapart;
069    
070        /**
071         * This is the constructor. <br>
072         * The language element was set to <b>en </b>. The datapart element was set
073         * to <b>metadata <b>All other elemnts was set to an empty string. The
074         * inherited value is set to 0!
075         */
076        public MCRMetaDefault() {
077            lang = DEFAULT_LANGUAGE;
078            subtag = "";
079            type = "";
080            inherited = DEFAULT_INHERITED;
081            datapart = DEFAULT_DATAPART;
082        }
083    
084        /**
085         * This is the constructor. <br>
086         * The language element was set. If the value of <em>default_lang</em> is
087         * empty or false <b>en </b> was set. The datapart was set to default. All
088         * other elemnts was set to an empty string. The inherited value is set to
089         * 0!
090         * 
091         * @param default_lang
092         *            the default language
093         */
094        public MCRMetaDefault(String default_lang) {
095            lang = DEFAULT_LANGUAGE;
096    
097            if ((default_lang != null) && ((default_lang = default_lang.trim()).length() != 0)) {
098                lang = default_lang;
099            }
100    
101            subtag = "";
102            type = "";
103            inherited = DEFAULT_INHERITED;
104            datapart = DEFAULT_DATAPART;
105        }
106    
107        /**
108         * This is the constructor. <br>
109         * The language element was set. If the value of <em>default_lang</em> is
110         * null, empty or false <b>en </b> was set. The subtag element was set to
111         * the value of <em>set_subtag<em>. If the value of <em>set_subtag</em>
112         * is null or empty an exception was throwed. The type element was set to
113         * the value of <em>set_type<em>, if it is null, an empty string was set
114         * to the type element. The datapart element was set. If the value of
115         * <em>set_datapart,/em> is null or empty the default was set.
116         *
117         * @param set_datapart     the data part name
118         * @param set_subtag       the name of the subtag
119         * @param default_lang     the default language
120         * @param set_type         the optional type string
121         * @param set_inherited     a int value , > 0 if the data are inherited,
122         *                         else = 0.
123         * @exception MCRException if the set_subtag value is null or empty
124         */
125        public MCRMetaDefault(String set_datapart, String set_subtag, String default_lang, String set_type, int set_inherited) throws MCRException {
126            lang = DEFAULT_LANGUAGE;
127            subtag = "";
128            type = "";
129            setInherited(set_inherited);
130    
131            if ((set_subtag == null) || ((set_subtag = set_subtag.trim()).length() == 0)) {
132                throw new MCRException("The set_subtag is null or empty.");
133            }
134    
135            subtag = set_subtag;
136    
137            if ((default_lang != null) && ((default_lang = default_lang.trim()).length() != 0)) {
138                lang = default_lang;
139            }
140    
141            if (set_type != null) {
142                type = set_type;
143            }
144    
145            if ((set_datapart != null) && ((set_datapart = set_datapart.trim()).length() != 0)) {
146                datapart = set_datapart;
147            }
148        }
149    
150        /**
151         * This method set the inherited level. This can be 0 or an integer higher
152         * 0.
153         * 
154         * @param value
155         *            the inherited level value, if it is < 0, 0 was set
156         */
157        public final void setInherited(int value) {
158            if (value < 0) {
159                inherited = 0;
160            } else {
161                inherited = value;
162            }
163        }
164    
165        /**
166         * This method increments the inherited value with 1.
167         */
168        public final void incrementInherited() {
169            inherited++;
170        }
171    
172        /**
173         * This method decrements the inherited value with 1.
174         */
175        public final void decrementInherited() {
176            if (inherited > 0) {
177                inherited--;
178            }
179        }
180    
181        /**
182         * This method set the language element. If the value of
183         * <em>default_lang</em> is null, empty or false nothing was changed.
184         * 
185         * @param default_lang
186         *            the default language
187         */
188        public final void setLang(String default_lang) {
189            if ((default_lang == null) || ((default_lang = default_lang.trim()).length() == 0)) {
190                lang = DEFAULT_LANGUAGE;
191            } else {
192                lang = default_lang;
193            }
194        }
195    
196        /**
197         * This method set the subtag element. If the value of <em>set_subtag</em>
198         * is null or empty an exception was throwed.
199         * 
200         * @param set_subtag
201         *            the subtag
202         * @exception MCRException
203         *                if the set_subtag value is null or empty
204         */
205        public final void setSubTag(String set_subtag) throws MCRException {
206            if ((set_subtag == null) || ((set_subtag = set_subtag.trim()).length() == 0)) {
207                throw new MCRException("The set_subtag is null or empty.");
208            }
209    
210            subtag = set_subtag;
211        }
212    
213        /**
214         * This method set the type element. If the value of <em>set_type</em> is
215         * null or empty nothing was changed.
216         * 
217         * @param set_type
218         *            the optional type
219         */
220        public final void setType(String set_type) {
221            if (set_type == null) {
222                return;
223            }
224    
225            type = set_type;
226        }
227    
228        /**
229         * This method set the datapart element. If the value of
230         * <em>set_datapart</em> is null, empty or false nothing was changed.
231         * 
232         * @param set_datapart
233         *            the data part name
234         */
235        public final void setDataPart(String set_datapart) {
236            if ((set_datapart == null) || ((set_datapart = set_datapart.trim()).length() == 0)) {
237                datapart = DEFAULT_DATAPART;
238            } else {
239                datapart = set_datapart;
240            }
241        }
242    
243        /**
244         * This method get the inherited element.
245         * 
246         * @return the inherited flag as int
247         */
248        public final int getInherited() {
249            return inherited;
250        }
251    
252        /**
253         * This method get the inherited element.
254         * 
255         * @return the inherited flag as string
256         */
257        public final String getInheritedToString() {
258            return Integer.toString(inherited);
259        }
260    
261        /**
262         * This method get the language element.
263         * 
264         * @return the language
265         */
266        public final String getLang() {
267            return lang;
268        }
269    
270        /**
271         * This method get the subtag element.
272         * 
273         * @return the subtag
274         */
275        public final String getSubTag() {
276            return subtag;
277        }
278    
279        /**
280         * This method get the type element.
281         * 
282         * @return the type
283         */
284        public final String getType() {
285            return type;
286        }
287    
288        /**
289         * This method get the datapart element.
290         * 
291         * @return the datapart
292         */
293        public final String getDataPart() {
294            return datapart;
295        }
296    
297        /**
298         * This method read the XML input stream part from a DOM part for the
299         * metadata of the document.
300         * 
301         * @param element
302         *            a relevant DOM element for the metadata
303         * @exception MCRException
304         *                if the set_subtag value is null or empty
305         */
306        public void setFromDOM(org.jdom.Element element) throws MCRException {
307            if (element == null) {
308                return;
309            }
310    
311            String temp_subtag = element.getName();
312    
313            if ((temp_subtag == null) || ((temp_subtag = temp_subtag.trim()).length() == 0)) {
314                throw new MCRException("The subtag is null or empty.");
315            }
316    
317            subtag = temp_subtag;
318    
319            String temp_lang = element.getAttributeValue("lang", org.jdom.Namespace.XML_NAMESPACE);
320    
321            if ((temp_lang != null) && ((temp_lang = temp_lang.trim()).length() != 0)) {
322                lang = temp_lang;
323            }
324    
325            String temp_type = element.getAttributeValue("type");
326    
327            if ((temp_type != null) && ((temp_type = temp_type.trim()).length() != 0)) {
328                type = temp_type;
329            }
330    
331            String temp_herit = element.getAttributeValue("inherited");
332    
333            if ((temp_herit != null) && ((temp_herit = temp_herit.trim()).length() != 0)) {
334                try {
335                    inherited = Integer.parseInt(temp_herit);
336                } catch (NumberFormatException e) {
337                    inherited = 0;
338                }
339            }
340        }
341    
342        /**
343         * This abstract method create a XML stream for all data in this class,
344         * defined by the MyCoRe XML MCRMeta... definition for the given subtag.
345         * 
346         * @exception MCRException
347         *                if the content of this class is not valid
348         * @return a JDOM Element with the XML MCRMeta... part
349         */
350        public abstract org.jdom.Element createXML() throws MCRException;
351    
352        /**
353         * This method check the validation of the content of this class. The method
354         * returns <em>true</em> if
355         * <ul>
356         * <li>the subtag is not null or empty
357         * <li>the lang value was supported
358         * </ul>
359         * otherwise the method return <em>false</em>
360         * 
361         * @return a boolean value
362         */
363        public boolean isValid() {
364            if ((subtag == null) || ((subtag = subtag.trim()).length() == 0)) {
365                LOGGER.warn("Error while checking subtag: "+subtag);
366                return false;
367            }
368            return true;
369        }
370    
371        /**
372         * This method put debug data to the logger (for the debug mode).
373         */
374        public void debug() {
375            debugDefault();
376            LOGGER.debug(" ");
377        }
378        
379        /**
380         * This method put common debug data to the logger (for the debug mode).
381         */
382        public final void debugDefault() {
383            LOGGER.debug("SubTag             = " + subtag);
384            LOGGER.debug("Language           = " + lang);
385            LOGGER.debug("Type               = " + type);
386            LOGGER.debug("DataPart           = " + datapart);
387            LOGGER.debug("Inhreited          = " + String.valueOf(inherited));
388        }
389        
390        public abstract Object clone();
391    }