﻿//-----------------------------------------------------------------------------
// RSS2
//
// Copyright 2005-2010 - Xcential Group LLC.
//
//-----------------------------------------------------------------------------

//=============================================================================
// Constructor

function RSS2()
{

   //--------------------------------------------------------------------------
   // Private Interface

   //--------------------------------------------------------------------------
   // Privileged Interface

   this.valueOf = function()
   {

      return "RSS2";
   }

   //--------------------------------------------------------------------------
   // Initialization

}

RSS2.ClassName = "RSS2";

//=============================================================================
// Static Interface

RSS2.NAMESPACE_URI = "http://backend.userland.com/rss2";
RSS2.PREFERRED_PREFIX = "rss";

RSS2.VERIFY = true;

RSS2.MEDIA_TYPE = "application/rss+xml";
RSS2.FORMAT = "rss2";

//-----------------------------------------------------------------------------

RSS2.getValue = function(
   entryXNode,
   childName,
   defaultValue,
   verify
)
{
   defaultValue = (defaultValue == null) ? null : defaultValue;
   verify = (verify == null) ? false : verify;

   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   function retargetLinks(
      text
   )
   {
      text = (text == null) ? null : text;

      if (!text)
         return text;

      var newText = "";
      while (XMatch(text, /(<a\s[^>]*)/))
      {
         newText += XMatch.leftContext;
         text = XMatch.rightContext;
         var link = XMatch.matches[1];
         link = link.replace(/\s+target=[^\s\>]+/,"");
         link += " target=\"_blank\"";
         newText += link;
      }
      newText += text;

      return newText;
   }

   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   function verify(
      type,
      value
   )
   {

      switch (type)
      {
         case "text":
            value = value.replace(/<[^>]+>/g," ");
            break;
         case "html":
            value = value.replace(/\<\!\[CDATA\[/ig,"").replace(/\]\]\>/ig,"");
            break;
         case "xhtml":
            try
            {
               value = XDoc(value).toXML();
            }
            catch (error)
            {
               value = "";
            }
            break
      }

      return value;
   }

   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   var childXNode = entryXNode.getChild(childName);
   if (childXNode)
   {
      var type = "text";
      var value = childXNode.toXML(XNode.INNER_ONLY);
      if ((/http:\/\/www.w3.org\/1999\/xhtml/).test(value))
      {
         type = "xhtml";
         value = XString(value).decode(XString.ENCODE_ENTITY);
         value = value.replace(/^\s*\<\!\[CDATA\[/,"");
         value = value.replace(/\]\]\>\s*$/,"");
      }
      else if ((/&lt;\/[0-9a-z\_\-]+&gt/).test(value) || (/&lt[0-9a-z\_\-]+;\/&gt/).test(value))
      {
         type = "html";
         value = XString(value).decode(XString.ENCODE_ENTITY);
         value = value.replace(/^\s*\<\!\[CDATA\[/,"");
         value = value.replace(/\]\]\>\s*$/,"");
         value = "<![CDATA[" + value + "]]>";
      }
      else if ((/^\<\!\[CDATA\[/).test(value))
      {
         type = "html";
         value = value.replace(/^\s*\<\!\[CDATA\[/,"");
         value = value.replace(/\]\]\>\s*$/,"");
         value = "<![CDATA[" + value + "]]>";
      }
      if (verify)
         value = verify(type, value);
      return {type: type, value: retargetLinks(value)};
   }

   return defaultValue;
}

//-----------------------------------------------------------------------------
