View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.document.modules.metadatadublincore.business;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  import fr.paris.lutece.portal.service.util.AppPropertiesService;
38  import fr.paris.lutece.util.xml.XmlUtil;
39  
40  import org.apache.commons.digester.Digester;
41  import org.apache.commons.digester.xmlrules.DigesterLoader;
42  
43  import org.xml.sax.SAXException;
44  
45  import java.io.FileNotFoundException;
46  import java.io.IOException;
47  import java.io.StringReader;
48  
49  import java.net.URL;
50  
51  
52  /**
53   * This class represents the business object DublinCoreMetadata
54   */
55  public class DublinCoreMetadata
56  {
57      private static final String PROPERTY_DEFAULT_TITLE = "document-metadatadublincore.default.title";
58      private static final String PROPERTY_DEFAULT_CREATOR = "document-metadatadublincore.default.creator";
59      private static final String PROPERTY_DEFAULT_SUBJECT = "document-metadatadublincore.default.subject";
60      private static final String PROPERTY_DEFAULT_DESCRIPTION = "document-metadatadublincore.default.description";
61      private static final String PROPERTY_DEFAULT_PUBLISHER = "document-metadatadublincore.default.publisher";
62      private static final String PROPERTY_DEFAULT_CONTRIBUTOR = "document-metadatadublincore.default.contributor";
63      private static final String PROPERTY_DEFAULT_DATE = "document-metadatadublincore.default.date";
64      private static final String PROPERTY_DEFAULT_TYPE = "document-metadatadublincore.default.type";
65      private static final String PROPERTY_DEFAULT_FORMAT = "document-metadatadublincore.default.format";
66      private static final String PROPERTY_DEFAULT_IDENTIFIER = "document-metadatadublincore.default.identifier";
67      private static final String PROPERTY_DEFAULT_SOURCE = "document-metadatadublincore.default.source";
68      private static final String PROPERTY_DEFAULT_LANGUAGE = "document-metadatadublincore.default.language";
69      private static final String PROPERTY_DEFAULT_RELATION = "document-metadatadublincore.default.relation";
70      private static final String PROPERTY_DEFAULT_COVERAGE = "document-metadatadublincore.default.coverage";
71      private static final String PROPERTY_DEFAULT_RIGTHS = "document-metadatadublincore.default.rigths";
72      private static final String FILE_RULES = "/fr/paris/lutece/plugins/document/modules/metadatadublincore/business/dublincore-digester-rules.xml";
73  
74      // Variables declarations
75      private String _strTitle;
76      private String _strCreator;
77      private String _strSubject;
78      private String _strDescription;
79      private String _strPublisher;
80      private String _strContributor;
81      private String _strDate;
82      private String _strType;
83      private String _strFormat;
84      private String _strIdentifier;
85      private String _strSource;
86      private String _strLanguage;
87      private String _strRelation;
88      private String _strCoverage;
89      private String _strRights;
90  
91      /**
92       * Default constructor
93       */
94      public DublinCoreMetadata(  )
95      {
96          _strTitle = AppPropertiesService.getProperty( PROPERTY_DEFAULT_TITLE );
97          _strCreator = AppPropertiesService.getProperty( PROPERTY_DEFAULT_CREATOR );
98          _strSubject = AppPropertiesService.getProperty( PROPERTY_DEFAULT_SUBJECT );
99          _strDescription = AppPropertiesService.getProperty( PROPERTY_DEFAULT_DESCRIPTION );
100         _strPublisher = AppPropertiesService.getProperty( PROPERTY_DEFAULT_PUBLISHER );
101         _strContributor = AppPropertiesService.getProperty( PROPERTY_DEFAULT_CONTRIBUTOR );
102         _strDate = AppPropertiesService.getProperty( PROPERTY_DEFAULT_DATE );
103         _strType = AppPropertiesService.getProperty( PROPERTY_DEFAULT_TYPE );
104         _strFormat = AppPropertiesService.getProperty( PROPERTY_DEFAULT_FORMAT );
105         _strIdentifier = AppPropertiesService.getProperty( PROPERTY_DEFAULT_IDENTIFIER );
106         _strSource = AppPropertiesService.getProperty( PROPERTY_DEFAULT_SOURCE );
107         _strLanguage = AppPropertiesService.getProperty( PROPERTY_DEFAULT_LANGUAGE );
108         _strRelation = AppPropertiesService.getProperty( PROPERTY_DEFAULT_RELATION );
109         _strCoverage = AppPropertiesService.getProperty( PROPERTY_DEFAULT_COVERAGE );
110         _strRights = AppPropertiesService.getProperty( PROPERTY_DEFAULT_RIGTHS );
111     }
112 
113     /**
114      * Returns the Title
115      * @return The Title
116      */
117     public String getTitle(  )
118     {
119         return _strTitle;
120     }
121 
122     /**
123      * Sets the Title
124      * @param strTitle The Title
125      */
126     public void setTitle( String strTitle )
127     {
128         _strTitle = strTitle;
129     }
130 
131     /**
132      * Returns the Creator
133      * @return The Creator
134      */
135     public String getCreator(  )
136     {
137         return _strCreator;
138     }
139 
140     /**
141      * Sets the Creator
142      * @param strCreator The Creator
143      */
144     public void setCreator( String strCreator )
145     {
146         _strCreator = strCreator;
147     }
148 
149     /**
150      * Returns the Subject
151      * @return The Subject
152      */
153     public String getSubject(  )
154     {
155         return _strSubject;
156     }
157 
158     /**
159      * Sets the Subject
160      * @param strSubject The Subject
161      */
162     public void setSubject( String strSubject )
163     {
164         _strSubject = strSubject;
165     }
166 
167     /**
168      * Returns the Description
169      * @return The Description
170      */
171     public String getDescription(  )
172     {
173         return _strDescription;
174     }
175 
176     /**
177      * Sets the Description
178      * @param strDescription The Description
179      */
180     public void setDescription( String strDescription )
181     {
182         _strDescription = strDescription;
183     }
184 
185     /**
186      * Returns the Publisher
187      * @return The Publisher
188      */
189     public String getPublisher(  )
190     {
191         return _strPublisher;
192     }
193 
194     /**
195      * Sets the Publisher
196      * @param strPublisher The Publisher
197      */
198     public void setPublisher( String strPublisher )
199     {
200         _strPublisher = strPublisher;
201     }
202 
203     /**
204      * Returns the Contributor
205      * @return The Contributor
206      */
207     public String getContributor(  )
208     {
209         return _strContributor;
210     }
211 
212     /**
213      * Sets the Contributor
214      * @param strContributor The Contributor
215      */
216     public void setContributor( String strContributor )
217     {
218         _strContributor = strContributor;
219     }
220 
221     /**
222      * Returns the Date
223      * @return The Date
224      */
225     public String getDate(  )
226     {
227         return _strDate;
228     }
229 
230     /**
231      * Sets the Date
232      * @param strDate The Date
233      */
234     public void setDate( String strDate )
235     {
236         _strDate = strDate;
237     }
238 
239     /**
240      * Returns the Type
241      * @return The Type
242      */
243     public String getType(  )
244     {
245         return _strType;
246     }
247 
248     /**
249      * Sets the Type
250      * @param strType The Type
251      */
252     public void setType( String strType )
253     {
254         _strType = strType;
255     }
256 
257     /**
258      * Returns the Format
259      * @return The Format
260      */
261     public String getFormat(  )
262     {
263         return _strFormat;
264     }
265 
266     /**
267      * Sets the Format
268      * @param strFormat The Format
269      */
270     public void setFormat( String strFormat )
271     {
272         _strFormat = strFormat;
273     }
274 
275     /**
276      * Returns the Identifier
277      * @return The Identifier
278      */
279     public String getIdentifier(  )
280     {
281         return _strIdentifier;
282     }
283 
284     /**
285      * Sets the Identifier
286      * @param strIdentifier The Identifier
287      */
288     public void setIdentifier( String strIdentifier )
289     {
290         _strIdentifier = strIdentifier;
291     }
292 
293     /**
294      * Returns the Source
295      * @return The Source
296      */
297     public String getSource(  )
298     {
299         return _strSource;
300     }
301 
302     /**
303      * Sets the Source
304      * @param strSource The Source
305      */
306     public void setSource( String strSource )
307     {
308         _strSource = strSource;
309     }
310 
311     /**
312      * Returns the Language
313      * @return The Language
314      */
315     public String getLanguage(  )
316     {
317         return _strLanguage;
318     }
319 
320     /**
321      * Sets the Language
322      * @param strLanguage The Language
323      */
324     public void setLanguage( String strLanguage )
325     {
326         _strLanguage = strLanguage;
327     }
328 
329     /**
330      * Returns the Relation
331      * @return The Relation
332      */
333     public String getRelation(  )
334     {
335         return _strRelation;
336     }
337 
338     /**
339      * Sets the Relation
340      * @param strRelation The Relation
341      */
342     public void setRelation( String strRelation )
343     {
344         _strRelation = strRelation;
345     }
346 
347     /**
348      * Returns the Coverage
349      * @return The Coverage
350      */
351     public String getCoverage(  )
352     {
353         return _strCoverage;
354     }
355 
356     /**
357      * Sets the Coverage
358      * @param strCoverage The Coverage
359      */
360     public void setCoverage( String strCoverage )
361     {
362         _strCoverage = strCoverage;
363     }
364 
365     /**
366      * Returns the Rigths
367      * @return The Rigths
368      */
369     public String getRights(  )
370     {
371         return _strRights;
372     }
373 
374     /**
375      * Sets the Rigths
376      * @param strRights The Rigths
377      */
378     public void setRights( String strRights )
379     {
380         _strRights = strRights;
381     }
382 
383     /**
384      * Get the XML of the current dublin core metadata
385      * @return The XML representing this dublin core metadata
386      */
387     public String getXml(  )
388     {
389         StringBuffer sbXml = new StringBuffer(  );
390         XmlUtil.beginElement( sbXml, "metadata" );
391 
392         if ( ( _strTitle != null ) && ( !_strTitle.equals( "" ) ) )
393         {
394             XmlUtil.addElement( sbXml, "title", _strTitle );
395         }
396 
397         if ( ( _strCreator != null ) && ( !_strCreator.equals( "" ) ) )
398         {
399             XmlUtil.addElement( sbXml, "creator", _strCreator );
400         }
401 
402         if ( ( _strSubject != null ) && ( !_strSubject.equals( "" ) ) )
403         {
404             XmlUtil.addElement( sbXml, "subject", _strSubject );
405         }
406 
407         if ( ( _strDescription != null ) && ( !_strDescription.equals( "" ) ) )
408         {
409             XmlUtil.addElement( sbXml, "description", _strDescription );
410         }
411 
412         if ( ( _strPublisher != null ) && ( !_strPublisher.equals( "" ) ) )
413         {
414             XmlUtil.addElement( sbXml, "publisher", _strPublisher );
415         }
416 
417         if ( ( _strContributor != null ) && ( !_strContributor.equals( "" ) ) )
418         {
419             XmlUtil.addElement( sbXml, "contributor", _strContributor );
420         }
421 
422         if ( ( _strDate != null ) && ( !_strDate.equals( "" ) ) )
423         {
424             XmlUtil.addElement( sbXml, "date", _strDate );
425         }
426 
427         if ( ( _strType != null ) && ( !_strType.equals( "" ) ) )
428         {
429             XmlUtil.addElement( sbXml, "type", _strType );
430         }
431 
432         if ( ( _strFormat != null ) && ( !_strFormat.equals( "" ) ) )
433         {
434             XmlUtil.addElement( sbXml, "format", _strFormat );
435         }
436 
437         if ( ( _strIdentifier != null ) && ( !_strIdentifier.equals( "" ) ) )
438         {
439             XmlUtil.addElement( sbXml, "identifier", _strIdentifier );
440         }
441 
442         if ( ( _strSource != null ) && ( !_strSource.equals( "" ) ) )
443         {
444             XmlUtil.addElement( sbXml, "source", _strSource );
445         }
446 
447         if ( ( _strLanguage != null ) && ( !_strLanguage.equals( "" ) ) )
448         {
449             XmlUtil.addElement( sbXml, "language", _strLanguage );
450         }
451 
452         if ( ( _strRelation != null ) && ( !_strRelation.equals( "" ) ) )
453         {
454             XmlUtil.addElement( sbXml, "relation", _strRelation );
455         }
456 
457         if ( ( _strContributor != null ) && ( !_strContributor.equals( "" ) ) )
458         {
459             XmlUtil.addElement( sbXml, "coverage", _strCoverage );
460         }
461 
462         if ( ( _strRights != null ) && ( !_strRights.equals( "" ) ) )
463         {
464             XmlUtil.addElement( sbXml, "rights", _strRights );
465         }
466 
467         XmlUtil.endElement( sbXml, "metadata" );
468 
469         return sbXml.toString(  );
470     }
471 
472     /**
473      * Load dublin core metadata from an XML stream
474      * @param strXmlData The XML content to get the attributes from
475      */
476     public void load( String strXmlData )
477     {
478         // Configure Digester from XML ruleset
479         URL rules = getClass(  ).getResource( FILE_RULES );
480         Digester digester = DigesterLoader.createDigester( rules );
481 
482         // Push empty List onto Digester's Stack
483         digester.push( this );
484 
485         try
486         {
487             if ( strXmlData != null )
488             {
489                 StringReader sr = new StringReader( strXmlData );
490                 digester.parse( sr );
491             }
492         }
493         catch ( FileNotFoundException e )
494         {
495             AppLogService.error( e.getMessage(  ), e );
496         }
497         catch ( SAXException e )
498         {
499             AppLogService.error( e.getMessage(  ), e );
500         }
501         catch ( IOException e )
502         {
503             AppLogService.error( e.getMessage(  ), e );
504         }
505     }
506 }