View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.dila.utils;
35  
36  import fr.paris.lutece.plugins.dila.business.enums.AudienceCategoryEnum;
37  import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalCardChapterDTO;
38  import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalCardDTO;
39  import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalFolderDTO;
40  import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalFolderLinkDTO;
41  import fr.paris.lutece.plugins.dila.service.DilaLocalTypeEnum;
42  import fr.paris.lutece.plugins.dila.service.IDilaLocalService;
43  import fr.paris.lutece.plugins.dila.service.IDilaXmlService;
44  import fr.paris.lutece.portal.service.spring.SpringContextService;
45  
46  import java.text.SimpleDateFormat;
47  import java.util.Date;
48  import java.util.List;
49  
50  import org.apache.commons.lang.StringEscapeUtils;
51  import org.apache.commons.lang.StringUtils;
52  
53  
54  /**
55   * XML convertor utility class
56   */
57  public final class XMLConvertor
58  {
59      // XML Properties
60      private static final String LINE_DATE = "<dc:date>modified <%DATE%></dc:date>";
61      private static final String LINE_FORMAT = "<dc:format>text/xml</dc:format>";
62      private static final String LINE_ID = "<dc:identifier><%ID%></dc:identifier>";
63      private static final String LINE_AUDIENCE = "<Audience top=\"false\"><%AUDIENCE%></Audience>";
64      private static final String LINE_BEGIN_FIL_ARIANE = "<FilDAriane>";
65      private static final String LINE_END_FIL_ARIANE = "</FilDAriane>";
66      private static final String LINE_BEGIN_NIVEAU = "<Niveau ID=\"<%NIVEAU_ID%>\">";
67      private static final String LINE_END_NIVEAU = "</Niveau>";
68      private static final String LINE_BEGIN_THEME = "<Theme ID=\"<%THEME_ID%>\">";
69      private static final String LINE_END_THEME = "</Theme>";
70      private static final String CARD_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet href=\"../StyleSheets/VDD11.xsl\" type=\"text/xsl\"?>";
71      private static final String CARD_LINE_PUBLICATION = "<Publication xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"../Schemas/2.2/Publication.xsd\" type=\"Fiche d'information\" ID=\"<%ID%>\">";
72      private static final String CARD_LINE_END_PUBLICATION = "</Publication>";
73      private static final String CARD_LINE_TITLE = "<dc:title><%CARD_TITLE%></dc:title>";
74      private static final String CARD_LINE_AUTEUR = "<dc:creator><%CARD_AUTEUR%></dc:creator>";
75      private static final String CARD_LINE_TYPE = "<dc:type>Fiche</dc:type>";
76      private static final String CARD_LINE_BEGIN_FOLDER_PERE = "<DossierPere ID=\"<%CARD_PERE_ID%>\">";
77      private static final String CARD_LINE_END_FOLDER_PERE = "</DossierPere>";
78      private static final String CARD_LINE_BEGIN_TEXTE = "<Texte>";
79      private static final String CARD_LINE_END_TEXTE = "</Texte>";
80      private static final String CARD_LINE_BEGIN_CHAPITRE = "<Chapitre>";
81      private static final String CARD_LINE_END_CHAPITRE = "</Chapitre>";
82      private static final String CARD_LINE_BEGIN_TITRE = "<Titre>";
83      private static final String CARD_LINE_END_TITRE = "</Titre>";
84      private static final String CARD_LINE_BEGIN_PARAGRAPHE = "<Paragraphe>";
85      private static final String CARD_LINE_END_PARAGRAPHE = "</Paragraphe>";
86      private static final String FOLDER_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet href=\"../StyleSheets/VDD09.xsl\" type=\"text/xsl\"?>";
87      private static final String FOLDER_LINE_PUBLICATION = "<Publication xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"../Schemas/2.2/Publication.xsd\" type=\"Dossier\" ID=\"<%ID%>\">";
88      private static final String FOLDER_LINE_END_PUBLICATION = "</Publication>";
89      private static final String FOLDER_LINE_TITLE = "<dc:title><%FOLDER_TITLE%></dc:title>";
90      private static final String FOLDER_LINE_AUTEUR = "<dc:creator><%FOLDER_AUTEUR%></dc:creator>";
91      private static final String FOLDER_LINE_TYPE = "<dc:type>Dossier</dc:type>";
92      private static final String FOLDER_LINE_BEGIN_THEME_PERE = "<Theme ID=\"<%THEME_PERE_ID%>\">";
93      private static final String FOLDER_LINE_END_THEME_PERE = "</Theme>";
94      private static final String FOLDER_LINE_BEGIN_PRESENTATION = "<Presentation>";
95      private static final String FOLDER_LINE_END_PRESENTATION = "</Presentation>";
96      private static final String FOLDER_LINE_BEGIN_CARD = "<Fiche seq=\"<%SEQ_ID%>\" ID=\"<%CARD_ID%>\">";
97      private static final String FOLDER_LINE_END_CARD = "</Fiche>";
98  
99      // Service
100     private static final IDilaLocalService _dilaLocalService = SpringContextService.getBean( "dilaLocalService" );
101     private static final IDilaXmlService _dilaXmlService = SpringContextService.getBean( "dilaXmlService" );
102 
103     /**
104      * Private constructor
105      */
106     private XMLConvertor( )
107     {
108     }
109 
110     /**
111      * Convert a {@link LocalCardDTO} into xml string
112      * @param card the card to convert
113      * @param chapters list of {@link LocalCardChapterDTO} linked to the
114      *            {@link LocalCardDTO}
115      * @return the formatted XML
116      */
117     public static String convertCardToXML( LocalCardDTO card, List<LocalCardChapterDTO> chapters )
118     {
119         SimpleDateFormat formatDate = new SimpleDateFormat( "yyyy-MM-dd" );
120         String date = formatDate.format( new Date( ) );
121 
122         StringBuilder result = new StringBuilder( );
123 
124         result.append( CARD_HEADER );
125         result.append( CARD_LINE_PUBLICATION );
126 
127         // Construct header
128         result.append( CARD_LINE_TITLE.replace( "<%CARD_TITLE%>", card.getLocalDTO( ).getTitle( ) ) );
129         result.append( CARD_LINE_AUTEUR.replace( "<%CARD_AUTEUR%>", card.getLocalDTO( ).getAuthor( ) ) );
130         result.append( LINE_DATE.replace( "<%DATE%>", date ) );
131         result.append( CARD_LINE_TYPE );
132         result.append( LINE_FORMAT );
133         result.append( LINE_ID );
134         result.append( LINE_AUDIENCE.replace( "<%AUDIENCE%>",
135                 AudienceCategoryEnum.fromId( card.getLocalDTO( ).getIdAudience( ) ).getLabel( ) ) );
136 
137         // Start construct the breadcrumb
138         result.append( LINE_BEGIN_FIL_ARIANE );
139 
140         String strBreadcrumb = "";
141 
142         strBreadcrumb = card.getLocalDTO( ).getBreadCrumb( );
143 
144         String[] breadcrumbs = strBreadcrumb.split( ";" );
145 
146         String theme = null;
147 
148         for ( String breadcrumb : breadcrumbs )
149         {
150             if ( StringUtils.isNotBlank( breadcrumb ) )
151             {
152                 if ( theme == null )
153                 {
154                     theme = breadcrumb;
155                 }
156 
157                 result.append( LINE_BEGIN_NIVEAU.replace( "<%NIVEAU_ID%>", breadcrumb ) );
158 
159                 if ( StringUtils.isNumeric( breadcrumb ) )
160                 {
161                     result.append( _dilaLocalService.findTitleByIdAndTypeAndAudience( Long.valueOf( breadcrumb ),
162                             DilaLocalTypeEnum.FOLDER.getId( ), card.getLocalDTO( ).getIdAudience( ) ) );
163                 }
164                 else
165                 {
166                     result.append( _dilaXmlService.findTitleById( breadcrumb ) );
167                 }
168 
169                 result.append( LINE_END_NIVEAU );
170             }
171         }
172 
173         result.append( LINE_END_FIL_ARIANE );
174 
175         // Start construct theme
176         if ( theme != null )
177         {
178             result.append( LINE_BEGIN_THEME.replace( "<%THEME_ID%>", theme ) );
179             result.append( _dilaXmlService.findTitleById( theme ) );
180             result.append( LINE_END_THEME );
181         }
182 
183         // Start construct the parent folder
184         result.append( CARD_LINE_BEGIN_FOLDER_PERE.replace( "<%CARD_PERE_ID%>", card.getParentFolderId( ) ) );
185         result.append( CARD_LINE_BEGIN_TITRE );
186 
187         if ( StringUtils.isNumeric( card.getParentFolderId( ) ) )
188         {
189             result.append( card.getLocalParentFolder( ).getLocalDTO( ).getTitle( ) );
190         }
191         else
192         {
193             result.append( card.getNationalParentFolder( ).getTitle( ) );
194         }
195 
196         result.append( CARD_LINE_END_TITRE );
197         result.append( CARD_LINE_END_FOLDER_PERE );
198 
199         // Start construct the chapters
200         result.append( CARD_LINE_BEGIN_TEXTE );
201 
202         for ( LocalCardChapterDTO chapter : chapters )
203         {
204             result.append( CARD_LINE_BEGIN_CHAPITRE );
205 
206             // set title
207             result.append( CARD_LINE_BEGIN_TITRE );
208             result.append( CARD_LINE_BEGIN_PARAGRAPHE );
209             result.append( chapter.getTitle( ) );
210             result.append( CARD_LINE_END_PARAGRAPHE );
211             result.append( CARD_LINE_END_TITRE );
212 
213             // set content
214             result.append( CARD_LINE_BEGIN_PARAGRAPHE );
215             result.append( StringEscapeUtils.unescapeHtml( chapter.getContent( ) ) );
216             result.append( CARD_LINE_END_PARAGRAPHE );
217 
218             result.append( CARD_LINE_END_CHAPITRE );
219         }
220 
221         result.append( CARD_LINE_END_TEXTE );
222 
223         result.append( CARD_LINE_END_PUBLICATION );
224 
225         return result.toString( );
226     }
227 
228     /**
229      * Convert a {@link LocalFolderDTO} into xml string
230      * @param folder the folder to convert
231      * @param links list of {@link LocalFolderLinkDTO} linked to the
232      *            {@link LocalFolderDTO}
233      * @return the formatted XML
234      */
235     public static String convertFolderInXML( LocalFolderDTO folder, List<LocalFolderLinkDTO> links )
236     {
237         SimpleDateFormat formatDate = new SimpleDateFormat( "yyyy-MM-dd" );
238         String date = formatDate.format( new Date( ) );
239 
240         StringBuilder result = new StringBuilder( );
241 
242         result.append( FOLDER_HEADER );
243         result.append( FOLDER_LINE_PUBLICATION );
244 
245         // Construct header
246         result.append( FOLDER_LINE_TITLE.replace( "<%FOLDER_TITLE%>", folder.getLocalDTO( ).getTitle( ) ) );
247         result.append( FOLDER_LINE_AUTEUR.replace( "<%FOLDER_AUTEUR%>", folder.getLocalDTO( ).getAuthor( ) ) );
248         result.append( LINE_DATE.replace( "<%DATE%>", date ) );
249         result.append( FOLDER_LINE_TYPE );
250         result.append( LINE_FORMAT );
251         result.append( LINE_ID );
252         result.append( LINE_AUDIENCE.replace( "<%AUDIENCE%>",
253                 AudienceCategoryEnum.fromId( folder.getLocalDTO( ).getIdAudience( ) ).getLabel( ) ) );
254 
255         // Start construct the breadcrumb
256         result.append( LINE_BEGIN_FIL_ARIANE );
257 
258         String strBreadcrumb = folder.getLocalDTO( ).getBreadCrumb( );
259         String theme = null;
260 
261         if ( StringUtils.isNotBlank( strBreadcrumb ) )
262         {
263             String[] breadcrumbs = strBreadcrumb.split( ";" );
264 
265             for ( String breadcrumb : breadcrumbs )
266             {
267                 //avoid empty breadcrumb
268                 if ( StringUtils.isNotBlank( breadcrumb ) )
269                 {
270 
271                     if ( theme == null )
272                     {
273                         theme = breadcrumb;
274                     }
275 
276                     result.append( LINE_BEGIN_NIVEAU.replace( "<%NIVEAU_ID%>", breadcrumb ) );
277 
278                     if ( StringUtils.isNumeric( breadcrumb ) )
279                     {
280                         result.append( _dilaLocalService.findTitleByIdAndTypeAndAudience( Long.valueOf( breadcrumb ),
281                                 DilaLocalTypeEnum.FOLDER.getId( ), folder.getLocalDTO( ).getIdAudience( ) ) );
282                     }
283                     else
284                     {
285                         result.append( _dilaXmlService.findTitleById( breadcrumb ) );
286                     }
287                     result.append( LINE_END_NIVEAU );
288                 }
289 
290             }
291         }
292 
293         result.append( LINE_END_FIL_ARIANE );
294 
295         // Start construct theme
296         if ( theme != null )
297         {
298             result.append( LINE_BEGIN_THEME.replace( "<%THEME_ID%>", theme ) );
299             result.append( _dilaXmlService.findTitleById( theme ) );
300             result.append( LINE_END_THEME );
301         }
302 
303         // Start construct the parent theme
304         result.append( FOLDER_LINE_BEGIN_THEME_PERE.replace( "<%THEME_PERE_ID%>", folder.getParentThemeId( ) ) );
305         result.append( _dilaXmlService.findTitleById( folder.getParentThemeId( ) ) );
306         result.append( FOLDER_LINE_END_THEME_PERE );
307 
308         result.append( FOLDER_LINE_BEGIN_PRESENTATION );
309         result.append( folder.getPresentation( ) );
310         result.append( FOLDER_LINE_END_PRESENTATION );
311 
312         // construct links
313         if ( links.size( ) > 0 )
314         {
315             for ( LocalFolderLinkDTO link : links )
316             {
317                 result.append( FOLDER_LINE_BEGIN_CARD.replace( "<%SEQ_ID%>", "" + link.getPosition( ) ).replace(
318                         "<%CARD_ID%>", link.getCardId( ) ) );
319                 result.append( link.getTitle( ) );
320                 result.append( FOLDER_LINE_END_CARD );
321             }
322         }
323 
324         result.append( FOLDER_LINE_END_PUBLICATION );
325 
326         return result.toString( );
327     }
328 }