1 /* 2 * Copyright (c) 2002-2021, 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.newsletter.modules.blog.util; 35 36 import fr.paris.lutece.plugins.blog.business.Blog; 37 import fr.paris.lutece.plugins.blog.service.BlogService; 38 import fr.paris.lutece.plugins.newsletter.modules.blog.service.NewsletterBlogService; 39 import fr.paris.lutece.plugins.newsletter.util.HtmlDomDocNewsletter; 40 import fr.paris.lutece.plugins.newsletter.util.NewsLetterConstants; 41 42 import org.apache.commons.lang3.StringUtils; 43 import org.w3c.dom.NamedNodeMap; 44 import org.w3c.dom.Node; 45 import org.w3c.dom.NodeList; 46 47 /** 48 * Dom document parser for newsletter document. 49 */ 50 public class BlogNewsletterDocument extends HtmlDomDocNewsletter 51 { 52 private static final String CONSTANT_IMG = "img"; 53 private static final String CONSTANT_A = "a"; 54 private static final String CONSTANT_SUBSTRING_BEGIN = "document?id="; 55 private static final String CONSTANT_SUBSTRING_END = "&"; 56 57 private static NewsletterBlogService _newsletterDocumentService = NewsletterBlogService.getInstance( ); 58 59 /** 60 * Instantiates an Blogument after having built the DOM tree. 61 * 62 * @param strHtml 63 * The Html code to be parsed. 64 * @param strBaseUrl 65 * The Base url used to retrieve urls. 66 */ 67 public BlogNewsletterDocument( String strHtml, String strBaseUrl ) 68 { 69 super( strHtml, strBaseUrl ); 70 } 71 72 /** 73 * Get the urls of all html elements specified by elementType and convert its to unsecured urls and copy this elements into an unsecured folder 74 * 75 * @param elementType 76 * the type of element to get 77 * @param strUnsecuredBaseUrl 78 * The unsecured base URL 79 * @param strUnsecuredFolderPath 80 * The unsecured folder path 81 * @param strUnsecuredFolder 82 * The unsecured folder 83 */ 84 /* 85 * public void convertUrlsToUnsecuredUrls( ElementUrl elementType, String strUnsecuredBaseUrl, String strUnsecuredFolderPath, String strUnsecuredFolder ) { 86 * NodeList nodes = getDomDocument( ).getElementsByTagName( elementType.getTagName( ) ); 87 * 88 * for ( int i = 0; i < nodes.getLength( ); i++ ) { Node node = nodes.item( i ); NamedNodeMap attributes = node.getAttributes( ); 89 * 90 * // Test if the element matches the required attribute if ( elementType.getTestedAttributeName( ) != null ) { String strRel = attributes.getNamedItem( 91 * elementType.getTestedAttributeName( ) ).getNodeValue( ); 92 * 93 * if ( !elementType.getTestedAttributeValue( ).equals( strRel ) ) { continue; } } 94 * 95 * Node nodeAttribute = attributes.getNamedItem( elementType.getAttributeName( ) ); 96 * 97 * if ( nodeAttribute != null ) { String strSrc = nodeAttribute.getNodeValue( ); 98 * 99 * if ( strSrc.contains( CONSTANT_SUBSTRING_BEGIN ) && !strSrc.contains( strUnsecuredBaseUrl + strUnsecuredFolder ) ) { String strDocumentId = 100 * StringUtils.substringBetween( strSrc, CONSTANT_SUBSTRING_BEGIN, CONSTANT_SUBSTRING_END ); Blog document = BlogService.getInstance( 101 * ).findByPrimaryKeyWithoutBinaries( Integer.valueOf( strDocumentId ) ); 102 * 103 * String strFileName = StringUtils.EMPTY; 104 * 105 * if ( elementType.getTagName( ).equals( CONSTANT_IMG ) ) { strFileName = _newsletterDocumentService.copyFileFromDocument( document, 106 * NewsletterBlogUtils.CONSTANT_IMG_FILE_TYPE, strUnsecuredFolderPath + strUnsecuredFolder ); } else { if ( elementType.getTagName( ).equals( CONSTANT_A ) ) 107 * { strFileName = _newsletterDocumentService.copyFileFromDocument( document, NewsLetterConstants.CONSTANT_PDF_FILE_TYPE, strUnsecuredFolderPath + 108 * strUnsecuredFolder ); } } 109 * 110 * nodeAttribute.setNodeValue( strUnsecuredBaseUrl + strUnsecuredFolder + strFileName ); } } } } 111 */ 112 }