//# 0 errors, 86 messages
//#
/*
    //#blogentrytopdfaction.java:1:1: class: net.sourceforge.pebble.web.action.BlogEntryToPdfAction
 * Copyright (c) 2003-2006, Simon Brown
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *
 *   - Neither the name of Pebble nor the names of its contributors may
 *     be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package net.sourceforge.pebble.web.action;

import net.sourceforge.pebble.Constants;

import net.sourceforge.pebble.util.SecurityUtils;

import net.sourceforge.pebble.domain.Blog;
import net.sourceforge.pebble.domain.BlogEntry;
import net.sourceforge.pebble.domain.BlogService;
import net.sourceforge.pebble.domain.BlogServiceException;

import net.sourceforge.pebble.web.view.NotFoundView;
import net.sourceforge.pebble.web.view.PdfView;
import net.sourceforge.pebble.web.view.View;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Allows the user to export current blog entry as a PDF file.
 *
 * @author    Alexander Zagniotov
 */
public class BlogEntryToPdfAction extends Action {
    //#blogentrytopdfaction.java:59: method: void net.sourceforge.pebble.web.action.BlogEntryToPdfAction.net.sourceforge.pebble.web.action.BlogEntryToPdfAction()
    //#input(void net.sourceforge.pebble.web.action.BlogEntryToPdfAction()): this
    //#blogentrytopdfaction.java:59: end of method: void net.sourceforge.pebble.web.action.BlogEntryToPdfAction.net.sourceforge.pebble.web.action.BlogEntryToPdfAction()

  private static final Log log = LogFactory.getLog(BlogEntryToPdfAction.class);
    //#blogentrytopdfaction.java:61: method: net.sourceforge.pebble.web.action.BlogEntryToPdfAction.net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init
    //#blogentrytopdfaction.java:61: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.BlogEntryToPdfAction
    //#    method: net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): __Descendant_Table[net/sourceforge/pebble/web/action/BlogEntryToPdfAction]
    //#output(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): __Dispatch_Table.buildPermalink(Lnet/sourceforge/pebble/domain/BlogEntry;)Ljava/lang/String;
    //#output(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): __Dispatch_Table.getLocalizedString(Ljava/lang/String;)Ljava/lang/String;
    //#output(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): __Dispatch_Table.getModel()Lnet/sourceforge/pebble/web/model/Model;
    //#output(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): __Dispatch_Table.process(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)Lnet/sourceforge/pebble/web/view/View;
    //#output(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): __Dispatch_Table.setModel(Lnet/sourceforge/pebble/web/model/Model;)V
    //#output(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): log
    //#output(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): net/sourceforge/pebble/web/action/Action.__Descendant_Table[net/sourceforge/pebble/web/action/BlogEntryToPdfAction]
    //#post(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): __Descendant_Table[net/sourceforge/pebble/web/action/BlogEntryToPdfAction] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): net/sourceforge/pebble/web/action/Action.__Descendant_Table[net/sourceforge/pebble/web/action/BlogEntryToPdfAction] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): __Dispatch_Table.buildPermalink(Lnet/sourceforge/pebble/domain/BlogEntry;)Ljava/lang/String; == &buildPermalink
    //#post(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): __Dispatch_Table.getLocalizedString(Ljava/lang/String;)Ljava/lang/String; == &net/sourceforge/pebble/web/action/Action.getLocalizedString
    //#post(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): __Dispatch_Table.getModel()Lnet/sourceforge/pebble/web/model/Model; == &net/sourceforge/pebble/web/action/Action.getModel
    //#post(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): __Dispatch_Table.process(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)Lnet/sourceforge/pebble/web/view/View; == &process
    //#post(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): __Dispatch_Table.setModel(Lnet/sourceforge/pebble/web/model/Model;)V == &net/sourceforge/pebble/web/action/Action.setModel
    //#post(net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init): init'ed(log)
    //#blogentrytopdfaction.java:61: end of method: net.sourceforge.pebble.web.action.BlogEntryToPdfAction.net.sourceforge.pebble.web.action.BlogEntryToPdfAction__static_init

  /**
   * Peforms the processing associated with this action.
   *
   * @param request  the HttpServletRequest instance
   * @param response the HttpServletResponse instance
   * @return the name of the next view
   */
  public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
    //#blogentrytopdfaction.java:71: method: View net.sourceforge.pebble.web.action.BlogEntryToPdfAction.process(HttpServletRequest, HttpServletResponse)
    //#input(View process(HttpServletRequest, HttpServletResponse)): __Descendant_Table[net/sourceforge/pebble/web/action/BlogEntryToPdfAction]
    //#input(View process(HttpServletRequest, HttpServletResponse)): __Descendant_Table[others]
    //#input(View process(HttpServletRequest, HttpServletResponse)): __Dispatch_Table.getModel()Lnet/sourceforge/pebble/web/model/Model;
    //#input(View process(HttpServletRequest, HttpServletResponse)): net/sourceforge/pebble/web/model/Model.__Descendant_Table[net/sourceforge/pebble/web/model/Model]
    //#input(View process(HttpServletRequest, HttpServletResponse)): net/sourceforge/pebble/web/model/Model.__Descendant_Table[others]
    //#input(View process(HttpServletRequest, HttpServletResponse)): net/sourceforge/pebble/web/model/Model.__Dispatch_Table.get(Ljava/lang/String;)Ljava/lang/Object;
    //#input(View process(HttpServletRequest, HttpServletResponse)): request
    //#input(View process(HttpServletRequest, HttpServletResponse)): this
    //#input(View process(HttpServletRequest, HttpServletResponse)): this.__Tag
    //#input(View process(HttpServletRequest, HttpServletResponse)): this.model
    //#input(View process(HttpServletRequest, HttpServletResponse)): this.model.__Tag
    //#input(View process(HttpServletRequest, HttpServletResponse)): this.model.data
    //#output(View process(HttpServletRequest, HttpServletResponse)): new NotFoundView(process#3) num objects
    //#output(View process(HttpServletRequest, HttpServletResponse)): new PdfView(process#4) num objects
    //#output(View process(HttpServletRequest, HttpServletResponse)): new PdfView(process#4).__Tag
    //#output(View process(HttpServletRequest, HttpServletResponse)): new PdfView(process#4).entry
    //#output(View process(HttpServletRequest, HttpServletResponse)): new PdfView(process#4).filename
    //#output(View process(HttpServletRequest, HttpServletResponse)): new PdfView(process#4).length
    //#output(View process(HttpServletRequest, HttpServletResponse)): return_value
    //#new obj(View process(HttpServletRequest, HttpServletResponse)): new NotFoundView(process#3)
    //#new obj(View process(HttpServletRequest, HttpServletResponse)): new PdfView(process#4)
    //#pre[1] (View process(HttpServletRequest, HttpServletResponse)): request != null
    //#pre[3] (View process(HttpServletRequest, HttpServletResponse)): this.__Tag == net/sourceforge/pebble/web/action/BlogEntryToPdfAction
    //#pre[4] (View process(HttpServletRequest, HttpServletResponse)): this.model != null
    //#pre[5] (View process(HttpServletRequest, HttpServletResponse)): this.model.__Tag == net/sourceforge/pebble/web/model/Model
    //#pre[6] (View process(HttpServletRequest, HttpServletResponse)): this.model.data != null
    //#post(View process(HttpServletRequest, HttpServletResponse)): return_value in Addr_Set{&new PdfView(process#4),&new NotFoundView(process#3)}
    //#post(View process(HttpServletRequest, HttpServletResponse)): new NotFoundView(process#3) num objects <= 1
    //#post(View process(HttpServletRequest, HttpServletResponse)): new PdfView(process#4) num objects <= 1
    //#post(View process(HttpServletRequest, HttpServletResponse)): new PdfView(process#4).__Tag == net/sourceforge/pebble/web/view/PdfView
    //#post(View process(HttpServletRequest, HttpServletResponse)): new PdfView(process#4).entry != null
    //#post(View process(HttpServletRequest, HttpServletResponse)): new PdfView(process#4).filename != null
    //#post(View process(HttpServletRequest, HttpServletResponse)): new PdfView(process#4).length == 0
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:net.sourceforge.pebble.domain.BlogEntry:getTitle
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.lang.String:length
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.lang.StringBuilder
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:net.sourceforge.pebble.domain.BlogEntry:getId
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.lang.StringBuilder:append
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.lang.StringBuilder:toString
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.lang.String:toLowerCase
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.lang.String:replaceAll
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.util.HashMap:get
    //#unanalyzed(View process(HttpServletRequest, HttpServletResponse)): Effects-of-calling:net.sourceforge.pebble.web.view.View
    //#test_vector(View process(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletRequest:getParameter(...)@72: Addr_Set{null}, Inverse{null}
    String entryId = request.getParameter("entry");

    BlogEntry blogEntry = null;
    if (entryId != null) {
      BlogService service = new BlogService();
    //#blogentrytopdfaction.java:76: Warning: method not available
    //#    -- call on void net.sourceforge.pebble.domain.BlogService()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.BlogEntryToPdfAction
    //#    method: View process(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: void net.sourceforge.pebble.domain.BlogService()
      try {
        blogEntry = service.getBlogEntry(blog, entryId);
    //#blogentrytopdfaction.java:78: Warning: method not available
    //#    -- call on BlogEntry net.sourceforge.pebble.domain.BlogService:getBlogEntry(Blog, String)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.BlogEntryToPdfAction
    //#    method: View process(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: BlogEntry net.sourceforge.pebble.domain.BlogService:getBlogEntry(Blog, String)
      } catch (BlogServiceException e) {
        throw new ServletException(e);
      }
    }

	if (blogEntry == null) {
      // the entry cannot be found - it may have been removed or the
      // requesting URL was wrong
      return new NotFoundView();
    //#blogentrytopdfaction.java:87: Warning: method not available
    //#    -- call on void net.sourceforge.pebble.web.view.NotFoundView()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.BlogEntryToPdfAction
    //#    method: View process(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: void net.sourceforge.pebble.web.view.NotFoundView()

    } else {
      
	  String filename = this.buildPermalink(blogEntry);
	  return new PdfView(blogEntry, filename + ".pdf");
    //#blogentrytopdfaction.java:92: end of method: View net.sourceforge.pebble.web.action.BlogEntryToPdfAction.process(HttpServletRequest, HttpServletResponse)

	}
  }

  private String buildPermalink(BlogEntry blogEntry) {
    String title = blogEntry.getTitle();
    //#blogentrytopdfaction.java:98: method: String net.sourceforge.pebble.web.action.BlogEntryToPdfAction.buildPermalink(BlogEntry)
    //#blogentrytopdfaction.java:98: Warning: method not available
    //#    -- call on String net.sourceforge.pebble.domain.BlogEntry:getTitle()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.BlogEntryToPdfAction
    //#    method: String buildPermalink(BlogEntry)
    //#    unanalyzed callee: String net.sourceforge.pebble.domain.BlogEntry:getTitle()
    //#input(String buildPermalink(BlogEntry)): blogEntry
    //#output(String buildPermalink(BlogEntry)): return_value
    //#pre[1] (String buildPermalink(BlogEntry)): blogEntry != null
    //#post(String buildPermalink(BlogEntry)): return_value != null
    //#test_vector(String buildPermalink(BlogEntry)): java.lang.String:length(...)@111: {1..4_294_967_295}, {0}
    //#test_vector(String buildPermalink(BlogEntry)): java.lang.String:length(...)@99: {1..4_294_967_295}, {0}
    //#test_vector(String buildPermalink(BlogEntry)): net.sourceforge.pebble.domain.BlogEntry:getTitle(...)@98: Addr_Set{null}, Inverse{null}
    if (title == null || title.length() == 0) {
      title = "" + blogEntry.getId();
    //#blogentrytopdfaction.java:100: Warning: method not available
    //#    -- call on String net.sourceforge.pebble.domain.BlogEntry:getId()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.BlogEntryToPdfAction
    //#    method: String buildPermalink(BlogEntry)
    //#    unanalyzed callee: String net.sourceforge.pebble.domain.BlogEntry:getId()
    } else {
      title = title.toLowerCase();
      title = title.replaceAll("[\\. ,;/\\\\-]", "_");
      title = title.replaceAll("[^a-z0-9_]", "");
      title = title.replaceAll("_+", "_");
      title = title.replaceAll("^_*", "");
      title = title.replaceAll("_*$", "");
    }

    // if the title has been blanked out, use the blog entry instead
    if (title == null || title.length() == 0) {
    //#blogentrytopdfaction.java:111: Warning: test always goes same way
    //#    test predetermined because title != null
    //#    severity: LOW
    //#    class: net.sourceforge.pebble.web.action.BlogEntryToPdfAction
    //#    method: String buildPermalink(BlogEntry)
    //#    from bb: bb_5
    //#    live edge: bb_5-->bb_6
    //#    tested vn: title == null
    //#    tested vn values: {0}
      title = "" + blogEntry.getId();
    //#blogentrytopdfaction.java:112: Warning: method not available
    //#    -- call on String net.sourceforge.pebble.domain.BlogEntry:getId()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.web.action.BlogEntryToPdfAction
    //#    method: String buildPermalink(BlogEntry)
    //#    unanalyzed callee: String net.sourceforge.pebble.domain.BlogEntry:getId()
    }
    return title;
    //#blogentrytopdfaction.java:114: end of method: String net.sourceforge.pebble.web.action.BlogEntryToPdfAction.buildPermalink(BlogEntry)
  }
}    //#blogentrytopdfaction.java:: end of class: net.sourceforge.pebble.web.action.BlogEntryToPdfAction
