<commit_before>package org.jasig.portal.channels;

import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.http.*;

import java.io.*;
import java.util.*;
import org.jasig.portal.*;
import org.jasig.portal.channels.rss.*;
import com.objectspace.xml.*;

import java.net.*;


/**
 * This is a channel for displaying an RSS broadcast.
 * The first time an RSS file is requested, it is retrieved 
 * from its URL, and then stored in a memory cache for future requests.
 * After one hour from the initial request, it is flushed from the
 * cache and retrieved again.
 * 
 * @author <NAME>
 * @version $Revision$
 */
public class CRSSChannel implements org.jasig.portal.IChannel                           
{ 
  private static RSSCache m_RSSCache = new RSSCache (3600);
  private ChannelConfig chConfig = null;
  
  public void init (ChannelConfig chConfig) {this.chConfig = chConfig;}
  
  public String getName () 
  {
    try
    {
      // Check if RSSXml is already in the cache
      String sUrl = (String) chConfig.get ("url");
      IXml xml = (IXml) m_RSSCache.get (sUrl);
      
      // If the RSSXml isn't already cached, download it and put it in the cache
      if (xml == null)
      {
        URL url = new URL (sUrl);
        String sXmlPackage = "org.jasig.portal.channels.rss";
        InputStream xmlStream = url.openStream();
        xml = Xml.openDocument (sXmlPackage, xmlStream);
        m_RSSCache.put (sUrl, xml);
        Logger.log (Logger.INFO, "Caching RSS at URL: " + sUrl);
      }
      
      IRss rss = (IRss) xml.getRoot ();            
      
      // Get Channel
      org.jasig.portal.channels.rss.IChannel channel = (org.jasig.portal.channels.rss.IChannel) rss.getChannel ();
      
      ITitleOrDescriptionOrLinkOrLanguageOrOneOrMoreItemOrRatingOrImageOrTextinputOrCopyrightOrPubDateOrLastBuildDateOrDocsOrManagingEditorOrWebMasterOrSkipHoursOrSkipDays[] channelAttr = channel.getTitleOrDescriptionOrLinkOrLanguageOrOneOrMoreItemOrRatingOrImageOrTextinputOrCopyrightOrPubDateOrLastBuildDateOrDocsOrManagingEditorOrWebMasterOrSkipHoursOrSkipDayss ();
      String sTitle = null;
      
      for (int i = 0; i < channelAttr.length; i++)
      {
        if (channelAttr[i].getTitle () != null)
          sTitle = channelAttr[i].getTitle ();  
      }
      return sTitle;
    }
    catch (Exception e)
    {
      Logger.log (Logger.ERROR, e);
    }
    
    return "&nbsp;";
  }
  
  public boolean isMinimizable () {return true;}
  public boolean isDetachable () {return true;}
  public boolean isRemovable () {return true;}
  public boolean isEditable () {return false;}
  public boolean hasHelp () {return false;}  
  
  public int getDefaultDetachWidth () {return 550;}
  public int getDefaultDetachHeight () {return 450;}
      
  public void render (HttpServletRequest req, HttpServletResponse res, JspWriter out)
  {    
    try 
    {
      // Check if RSSXml is already in the cache
      String sUrl = (String) chConfig.get ("url");
      IXml xml = (IXml) m_RSSCache.get (sUrl);
      
      // If the RSSXml isn't already cached, download it and put it in the cache
      if (xml == null)
      {
        URL url = new URL (sUrl);
        String sXmlPackage = "org.jasig.portal.channels.rss";
        InputStream xmlStream = url.openStream();
        xml = Xml.openDocument (sXmlPackage, xmlStream);
        m_RSSCache.put (sUrl, xml);
      }
      
      IRss rss = (IRss) xml.getRoot ();            
      
      // Get Channel
      org.jasig.portal.channels.rss.IChannel channel = (org.jasig.portal.channels.rss.IChannel) rss.getChannel ();
      
      ITitleOrDescriptionOrLinkOrLanguageOrOneOrMoreItemOrRatingOrImageOrTextinputOrCopyrightOrPubDateOrLastBuildDateOrDocsOrManagingEditorOrWebMasterOrSkipHoursOrSkipDays[] channelAttr = channel.getTitleOrDescriptionOrLinkOrLanguageOrOneOrMoreItemOrRatingOrImageOrTextinputOrCopyrightOrPubDateOrLastBuildDateOrDocsOrManagingEditorOrWebMasterOrSkipHoursOrSkipDayss ();
      String sTitleLink = null;
      String sDescription = null;
      IImage image = null;
      
      for (int i = 0; i < channelAttr.length; i++)
      {
        if (channelAttr[i].getLink () != null)
          sTitleLink = channelAttr[i].getLink ();  
          
        if (channelAttr[i].getDescription () != null)
          sDescription = channelAttr[i].getDescription ();  
          
        if (channelAttr[i].getImage () != null)
          image = (IImage) channelAttr[i].getImage ();          
      }
            
      ITitleOrUrlOrLinkOrWidthOrHeightOrDescription[] imageAttr = image.getTitleOrUrlOrLinkOrWidthOrHeightOrDescriptions ();
      
      String sImageUrl = null;
      
      for (int i = 0; i < imageAttr.length; i++)
      {
        if (imageAttr[i].getUrl () != null)
          sImageUrl = imageAttr[i].getUrl ();  
      }
      
      out.println ("<p><a href=\"" + sTitleLink + "\"><img src=\"" + sImageUrl + "\" border=0 align=right></a>");
      out.println ("<p><em>" + sDescription + "</em>");
      
      for (int i = 0; i < channelAttr.length; i++)
      {
        if (channelAttr[i].getItems () != null)
        {
          IItem[] items = channelAttr[i].getItems ();
                    
          ITitleOrLinkOrDescription[] itemAttr = null;
          
          for (int j = 0; j < items.length; j++)
          {
            if (items[j].getTitleOrLinkOrDescriptions () != null)
            {
              itemAttr = items[j].getTitleOrLinkOrDescriptions ();
              
              String sItemTitle = null;
              String sItemLink = null;
              String sItemDescription = null;
                        
              for (int k = 0; k < itemAttr.length; k++)
              {
                if (itemAttr[k].getTitle () != null)
                  sItemTitle = itemAttr[k].getTitle ();
                  
                if (itemAttr[k].getLink () != null)
                  sItemLink = itemAttr[k].getLink ();
                  
                if (itemAttr[k].getDescription () != null)
                  sItemDescription = itemAttr[k].getDescription ();
              }
              
              out.println ("<ul>");
              
              if (sItemTitle.length() > 0)
                out.println ("<li><a href=\"" + sItemLink + "\">" + sItemTitle + "</a><br>");
              else
                out.println ("<p>" + sItemTitle);
                
              if (sItemDescription != null)
                out.println (sItemDescription);
              
              out.println ("</ul>");
            }
          }
        }          
      }
    }
    catch (Exception e)
    {
      Logger.log (Logger.ERROR, e);
      
      try
      {
        out.println ("Problem rendering channel.");<commit_msg>Fix bug which led to null ptr ex when no image was in XML. Add a little icon to follow to rss source if the provider omitted an image. This is because in R1 we don't have a good way to put the link on the title which is outside the channel's scope. Finally, clean up he list of items html tags.<commit_after>package org.jasig.portal.channels;

import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.http.*;

import java.io.*;
import java.util.*;
import org.jasig.portal.*;
import org.jasig.portal.channels.rss.*;
import com.objectspace.xml.*;

import java.net.*;


/**
 * This is a channel for displaying an RSS broadcast.
 * The first time an RSS file is requested, it is retrieved 
 * from its URL, and then stored in a memory cache for future requests.
 * After one hour from the initial request, it is flushed from the
 * cache and retrieved again.
 * 
 * @author <NAME>
 * @version $Revision$
 */
public class CRSSChannel implements org.jasig.portal.IChannel                           
{ 
  private static RSSCache m_RSSCache = new RSSCache (3600);
  private ChannelConfig chConfig = null;
  
  public void init (ChannelConfig chConfig) {this.chConfig = chConfig;}
  
  public String getName () 
  {
    try
    {
      // Check if RSSXml is already in the cache
      String sUrl = (String) chConfig.get ("url");
      IXml xml = (IXml) m_RSSCache.get (sUrl);
      
      // If the RSSXml isn't already cached, download it and put it in the cache
      if (xml == null)
      {
        URL url = new URL (sUrl);
        String sXmlPackage = "org.jasig.portal.channels.rss";
        InputStream xmlStream = url.openStream();
        xml = Xml.openDocument (sXmlPackage, xmlStream);
        m_RSSCache.put (sUrl, xml);
        Logger.log (Logger.INFO, "Caching RSS at URL: " + sUrl);
      }
      
      IRss rss = (IRss) xml.getRoot ();            
      
      // Get Channel
      org.jasig.portal.channels.rss.IChannel channel = (org.jasig.portal.channels.rss.IChannel) rss.getChannel ();
      
      ITitleOrDescriptionOrLinkOrLanguageOrOneOrMoreItemOrRatingOrImageOrTextinputOrCopyrightOrPubDateOrLastBuildDateOrDocsOrManagingEditorOrWebMasterOrSkipHoursOrSkipDays[] channelAttr = channel.getTitleOrDescriptionOrLinkOrLanguageOrOneOrMoreItemOrRatingOrImageOrTextinputOrCopyrightOrPubDateOrLastBuildDateOrDocsOrManagingEditorOrWebMasterOrSkipHoursOrSkipDayss ();
      String sTitle = null;
      
      for (int i = 0; i < channelAttr.length; i++)
      {
        if (channelAttr[i].getTitle () != null)
          sTitle = channelAttr[i].getTitle ();  
      }
      return sTitle;
    }
    catch (Exception e)
    {
      Logger.log (Logger.ERROR, e);
    }
    
    return "&nbsp;";
  }
  
  public boolean isMinimizable () {return true;}
  public boolean isDetachable () {return true;}
  public boolean isRemovable () {return true;}
  public boolean isEditable () {return false;}
  public boolean hasHelp () {return false;}  
  
  public int getDefaultDetachWidth () {return 550;}
  public int getDefaultDetachHeight () {return 450;}
      
  public void render (HttpServletRequest req, HttpServletResponse res, JspWriter out)
  {    
    try 
    {
      // Check if RSSXml is already in the cache
      String sUrl = (String) chConfig.get ("url");
      IXml xml = (IXml) m_RSSCache.get (sUrl);
      
      // If the RSSXml isn't already cached, download it and put it in the cache
      if (xml == null)
      {
        URL url = new URL (sUrl);
        String sXmlPackage = "org.jasig.portal.channels.rss";
        InputStream xmlStream = url.openStream();
        xml = Xml.openDocument (sXmlPackage, xmlStream);
        m_RSSCache.put (sUrl, xml);
      }
      
      IRss rss = (IRss) xml.getRoot ();

      // Get Channel
      org.jasig.portal.channels.rss.IChannel channel = (org.jasig.portal.channels.rss.IChannel) rss.getChannel ();
      
      ITitleOrDescriptionOrLinkOrLanguageOrOneOrMoreItemOrRatingOrImageOrTextinputOrCopyrightOrPubDateOrLastBuildDateOrDocsOrManagingEditorOrWebMasterOrSkipHoursOrSkipDays[] channelAttr = channel.getTitleOrDescriptionOrLinkOrLanguageOrOneOrMoreItemOrRatingOrImageOrTextinputOrCopyrightOrPubDateOrLastBuildDateOrDocsOrManagingEditorOrWebMasterOrSkipHoursOrSkipDayss ();
      String sTitleLink = null;
      String sDescription = null;
      IImage image = null;
      
      for (int i = 0; i < channelAttr.length; i++)
      {
        if (channelAttr[i].getLink () != null)
          sTitleLink = channelAttr[i].getLink ();

        if (channelAttr[i].getDescription () != null)
          sDescription = channelAttr[i].getDescription ();

        if (channelAttr[i].getImage () != null)
          image = (IImage) channelAttr[i].getImage ();          
      }
// An image is optional but there should be a description and a link for the channel
      if (image != null)
      {
        ITitleOrUrlOrLinkOrWidthOrHeightOrDescription[] imageAttr = image.getTitleOrUrlOrLinkOrWidthOrHeightOrDescriptions ();
        String sImageUrl = null;
        String sImageLink = null;

        for (int i = 0; i < imageAttr.length; i++)
        {
          if (imageAttr[i].getUrl () != null)
            sImageUrl = imageAttr[i].getUrl ();
          if (imageAttr[i].getLink () != null)
            sImageLink = imageAttr[i].getLink ();
        }
        // if there is an image and an image-link use it as intended
        if (sImageLink!= null) out.println ("<a href=\"" + sImageLink + "\"><img src=\"" + sImageUrl + "\" border=0 align=right /></a>");
        else
          // if no image-link but have title-link put title-link on the image
          if (sTitleLink != null) out.println ("<a href=\"" + sTitleLink + "\"><img src=\"" + sImageUrl + "\" border=0 align=right /></a>");
          // if no image-link and no title-link just put the image out with no link
          else out.println ("<img src=\"" + sImageUrl + "\" border=0 align=right />");
      }
      // if there is a title link but no image then insert our own little icon for the link
      else
        if (sTitleLink != null) out.println ("<a href=\"" + sTitleLink + "\"><img src=\"images/rsslink.gif\" border=0 align=right /></a>");

      out.println ("<p><em>" + sDescription + "</em>");

      out.println ("<ul>");
      for (int i = 0; i < channelAttr.length; i++)
      {
        if (channelAttr[i].getItems () != null)
        {
          IItem[] items = channelAttr[i].getItems ();

          ITitleOrLinkOrDescription[] itemAttr = null;

          for (int j = 0; j < items.length; j++)
          {
            if (items[j].getTitleOrLinkOrDescriptions () != null)
            {
              itemAttr = items[j].getTitleOrLinkOrDescriptions ();

              String sItemTitle = null;
              String sItemLink = null;
              String sItemDescription = null;

              for (int k = 0; k < itemAttr.length; k++)
              {
                if (itemAttr[k].getTitle () != null)
                  sItemTitle = itemAttr[k].getTitle ();

                if (itemAttr[k].getLink () != null)
                  sItemLink = itemAttr[k].getLink ();

                if (itemAttr[k].getDescription () != null)
                  sItemDescription = itemAttr[k].getDescription ();
              }


              if (sItemTitle.length() > 0)
                out.println ("<p><li /><a href=\"" + sItemLink + "\">" + sItemTitle + "</a>");
              else
                out.println ("<p>" + sItemTitle );

              if (sItemDescription != null)
                out.println ("<br />" + sItemDescription + "<p />");
              else out.println ("<br /><p />");

            }
          }
        }
      }
        out.println ("</ul>");
    }
    catch (Exception e)
    {
      Logger.log (Logger.ERROR, e);
      
      try
      {
        out.println ("Problem rendering channel.");
{'fraction_non_alphanumeric': 0.11297156916347731, 'fraction_numerical': 0.002733734281027884, 'mean_word_length': 2.2288172992056485, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 30}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4146674', 'n_tokens_mistral': 4342, 'n_tokens_neox': 4211, 'n_words': 1115}