View Javadoc
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   * Dom document parser for newsletter document.
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       * Instantiates an HtmlDocument after having built the DOM tree.
28       * 
29       * @param strHtml
30       *            The Html code to be parsed.
31       * @param strBaseUrl
32       *            The Base url used to retrieve urls.
33       */
34      public HtmlDomDocNewsletterDocument( String strHtml, String strBaseUrl )
35      {
36          super( strHtml, strBaseUrl );
37      }
38  
39      /**
40       * Get the urls of all html elements specified by elementType and convert its to unsecured urls and copy this elements into an unsecured folder
41       * 
42       * @param elementType
43       *            the type of element to get
44       * @param strUnsecuredBaseUrl
45       *            The unsecured base URL
46       * @param strUnsecuredFolderPath
47       *            The unsecured folder path
48       * @param strUnsecuredFolder
49       *            The unsecured folder
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              // Test if the element matches the required attribute
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 }