View Javadoc
1   package fr.paris.lutece.plugins.newsletter.modules.htmldocs.util;
2   
3   import org.apache.commons.lang.StringUtils;
4   
5   /**
6    * Utility class for newsletter document
7    */
8   public final class NewsletterHtmlDocUtils
9   {
10      /**
11       * Image file type
12       */
13      public static final String CONSTANT_IMG_FILE_TYPE = "image";
14  
15      private static final String CONSTANT_ZERO = "0";
16  
17      /**
18       * Private constructor
19       */
20      private NewsletterHtmlDocUtils( )
21      {
22  
23      }
24  
25      /**
26       * Rewrite secured img urls to absolutes urls
27       * 
28       * @param strContent
29       *            The content to analyze
30       * @param strBaseUrl
31       *            The base url
32       * @param strUnsecuredBaseUrl
33       *            The unsecured base URL
34       * @param strUnsecuredFolderPath
35       *            The unsecured folder path
36       * @param strUnsecuredFolder
37       *            The unsecured folder
38       * @return The converted content
39       */
40      public static String rewriteImgUrls( String strContent, String strBaseUrl, String strUnsecuredBaseUrl, String strUnsecuredFolderPath,
41              String strUnsecuredFolder )
42      {
43          if ( strContent == null )
44          {
45              return StringUtils.EMPTY;
46          }
47          HtmlDomDocNewsletterDocument doc = new HtmlDomDocNewsletterDocument( strContent, strBaseUrl );
48          doc.convertUrlsToUnsecuredUrls( HtmlDomDocNewsletterDocument.ELEMENT_IMG, strUnsecuredBaseUrl, strUnsecuredFolderPath, strUnsecuredFolder );
49          doc.convertUrlsToUnsecuredUrls( HtmlDomDocNewsletterDocument.ELEMENT_A, strUnsecuredBaseUrl, strUnsecuredFolderPath, strUnsecuredFolder );
50  
51          return doc.getContent( );
52      }
53  
54      /**
55       * Get the string representation of an integer with a specified number of digits.
56       * 
57       * @param nNbToformat
58       *            The number to format
59       * @param nNbDigits
60       *            The number of digits to add
61       * @return The string representing the integer
62       */
63      public static String formatInteger( int nNbToformat, int nNbDigits )
64      {
65          String strNumber = Integer.toString( nNbToformat );
66          if ( strNumber.length( ) > nNbDigits )
67          {
68              return strNumber;
69          }
70          StringBuffer sbNumber = new StringBuffer( nNbDigits );
71          int i = strNumber.length( );
72          while ( i < nNbDigits )
73          {
74              sbNumber.append( CONSTANT_ZERO );
75              i++;
76          }
77          sbNumber.append( strNumber );
78          return sbNumber.toString( );
79      }
80  }