1 package fr.paris.lutece.plugins.newsletter.modules.htmldocs.util;
2
3 import fr.paris.lutece.plugins.htmldocs.business.HtmlDoc;
4 import fr.paris.lutece.plugins.htmldocs.service.HtmlDocService;
5 import fr.paris.lutece.plugins.newsletter.modules.htmldocs.service.NewsletterHtmlDocService;
6 import fr.paris.lutece.plugins.newsletter.util.HtmlDomDocNewsletter;
7 import fr.paris.lutece.plugins.newsletter.util.NewsLetterConstants;
8
9 import org.apache.commons.lang.StringUtils;
10 import org.w3c.dom.NamedNodeMap;
11 import org.w3c.dom.Node;
12 import org.w3c.dom.NodeList;
13
14
15
16
17 public class HtmlDomDocNewsletterDocument extends HtmlDomDocNewsletter
18 {
19 private static final String CONSTANT_IMG = "img";
20 private static final String CONSTANT_A = "a";
21 private static final String CONSTANT_SUBSTRING_BEGIN = "document?id=";
22 private static final String CONSTANT_SUBSTRING_END = "&";
23
24 private static NewsletterHtmlDocService _newsletterDocumentService = NewsletterHtmlDocService.getInstance( );
25
26
27
28
29
30
31
32
33
34 public HtmlDomDocNewsletterDocument( String strHtml, String strBaseUrl )
35 {
36 super( strHtml, strBaseUrl );
37 }
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public void convertUrlsToUnsecuredUrls( ElementUrl elementType, String strUnsecuredBaseUrl, String strUnsecuredFolderPath, String strUnsecuredFolder )
52 {
53 NodeList nodes = getDomDocument( ).getElementsByTagName( elementType.getTagName( ) );
54
55 for ( int i = 0; i < nodes.getLength( ); i++ )
56 {
57 Node node = nodes.item( i );
58 NamedNodeMap attributes = node.getAttributes( );
59
60
61 if ( elementType.getTestedAttributeName( ) != null )
62 {
63 String strRel = attributes.getNamedItem( elementType.getTestedAttributeName( ) ).getNodeValue( );
64
65 if ( !elementType.getTestedAttributeValue( ).equals( strRel ) )
66 {
67 continue;
68 }
69 }
70
71 Node nodeAttribute = attributes.getNamedItem( elementType.getAttributeName( ) );
72
73 if ( nodeAttribute != null )
74 {
75 String strSrc = nodeAttribute.getNodeValue( );
76
77 if ( strSrc.contains( CONSTANT_SUBSTRING_BEGIN ) && !strSrc.contains( strUnsecuredBaseUrl + strUnsecuredFolder ) )
78 {
79 String strDocumentId = StringUtils.substringBetween( strSrc, CONSTANT_SUBSTRING_BEGIN, CONSTANT_SUBSTRING_END );
80 HtmlDoc document = HtmlDocService.getInstance( ).findByPrimaryKeyWithoutBinaries( Integer.valueOf( strDocumentId ) );
81
82 String strFileName = StringUtils.EMPTY;
83
84 if ( elementType.getTagName( ).equals( CONSTANT_IMG ) )
85 {
86 strFileName = _newsletterDocumentService.copyFileFromDocument( document, NewsletterHtmlDocUtils.CONSTANT_IMG_FILE_TYPE,
87 strUnsecuredFolderPath + strUnsecuredFolder );
88 }
89 else
90 {
91 if ( elementType.getTagName( ).equals( CONSTANT_A ) )
92 {
93 strFileName = _newsletterDocumentService.copyFileFromDocument( document, NewsLetterConstants.CONSTANT_PDF_FILE_TYPE,
94 strUnsecuredFolderPath + strUnsecuredFolder );
95 }
96 }
97
98 nodeAttribute.setNodeValue( strUnsecuredBaseUrl + strUnsecuredFolder + strFileName );
99 }
100 }
101 }
102 }
103 }