java.lang.Object
net.i2p.router.news.NewsXMLParser

public class NewsXMLParserextends Object
Parser for I2P news feeds in Atom format with security filtering.

Processes news.xml files following RFC4287 Atom specification using the UPnP XML parser library with enhanced security features. Handles news entries, metadata, CRL entries, and blocklist data with configurable XHTML filtering.

Provides comprehensive validation and sanitization of news content with support for multiple content types and security levels. Includes configurable handling of potentially dangerous XHTML elements and attributes.

  • Field Details

    • _context

      private final I2PAppContext _context
      The app context.
    • _log

      private final Log _log
      Logger for the news parser.
    • _entries

      private List<NewsEntry> _entries
      The news entries parsed from the feed.
    • _crlEntries

      private List<CRLEntry> _crlEntries
      The CRL entries parsed from the feed.
    • _blocklistEntries

      private BlocklistEntries _blocklistEntries
      The blocklist entries parsed from the feed.
    • _metadata

      private NewsMetadata _metadata
      The feed metadata parsed from the feed.
    • _mode

      private NewsXMLParser.XHTMLMode _mode
      The XHTML sanitizing mode of the parser.
    • xhtmlWhitelist

      private static final Set<String> xhtmlWhitelist
      The XHTML tags allowed in feed content.
    • attributeBlacklist

      private static final Set<String> attributeBlacklist
      The HTML attributes stripped from feed content. See also: global HTML attributes
  • Constructor Details

    • NewsXMLParser

      public NewsXMLParser(I2PAppContext ctx)
      Creates a new NewsXMLParser with the given context.
      Parameters:
      ctx - the application context supplying the log manager and property lookups
  • Method Details

    • setXHTMLMode

      public void setXHTMLMode(NewsXMLParser.XHTMLMode mode)
      Sets the action taken when encountering a non-whitelisted XHTML element in the feed content. Must be set before parse(File). Default REMOVE_ELEMENT.
      Parameters:
      mode - the XHTML mode to set
    • parse

      public Node parse(File file) throws IOException
      Process the XML file.
      Parameters:
      file - XML content only. Any su3 or gunzip handling must have already happened.
      Returns:
      the root node
      Throws:
      IOException - on any parse error
    • parse

      public Node parse(InputStream in) throws IOException
      Process the XML input stream.
      Parameters:
      in - XML content only. Any su3 or gunzip handling must have already happened.
      Returns:
      the root node
      Throws:
      IOException - on any parse error
    • getEntries

      public List<NewsEntry> getEntries()
      The news entries. Must call parse() first.
      Returns:
      sorted, newest first, null if parse failed
    • getMetadata

      public NewsMetadata getMetadata()
      The news metatdata. Must call parse() first.
      Returns:
      null if parse failed
    • getCRLEntries

      public List<CRLEntry> getCRLEntries()
      The news CRL entries. Must call parse() first.
      Returns:
      unsorted, null if none
    • getBlocklistEntries

      public BlocklistEntries getBlocklistEntries()
      The blocklist entries. Must call parse() first.
      Returns:
      null if none
    • extract

      private void extract(Node root) throws NewsXMLParser.I2PParserException
      Extracts the feed contents from the root node.
      Parameters:
      root - the feed's root node
      Throws:
      NewsXMLParser.I2PParserException - if the feed structure is invalid
    • extractNewsMetadata

      private static NewsMetadata extractNewsMetadata(Node feed) throws NewsXMLParser.I2PParserException
      Extracts the metadata of the feed.
      Parameters:
      feed - the feed node
      Returns:
      the parsed metadata
      Throws:
      NewsXMLParser.I2PParserException - if the metadata is invalid
    • extractNewsEntries

      private List<NewsEntry> extractNewsEntries(Node feed) throws NewsXMLParser.I2PParserException
      This does not check for any missing values. Any field in any NewsEntry may be null.
      Parameters:
      feed - the feed node containing the entry elements
      Returns:
      the sorted entries, empty if none
      Throws:
      NewsXMLParser.I2PParserException - in ABORT mode if an entry's content contains a node not in the XHTML whitelist
    • extractCRLEntries

      private static List<CRLEntry> extractCRLEntries(Node feed)
      This does not check for any missing values. Any field in any CRLEntry may be null.
      Parameters:
      feed - the feed node containing the i2p:revocations element
      Returns:
      null if none
    • extractBlocklistEntries

      private BlocklistEntries extractBlocklistEntries(Node feed)
      This does not check for any missing values. Any field in a BlocklistEntry may be null. Signature is verified here.
      Parameters:
      feed - the feed node containing the i2p:blocklist element
      Returns:
      null if none
    • getNodes

      public static List<Node> getNodes(Node node, String name)
      Helper to get all Nodes matching the name.
      Parameters:
      node - the parent node to search
      name - the element name a child node must carry to be returned
      Returns:
      non-null
    • validate

      private boolean validate(Node node) throws NewsXMLParser.I2PParserException
      Validate an XML node, throwing if it is invalid.
      Parameters:
      node - the node to check against the XHTML whitelist
      Returns:
      true if node was removed from parent (only for REMOVE_ELEMENT mode)
      Throws:
      NewsXMLParser.I2PParserException - if any node not in whitelist (depends on mode)
    • main

      public static void main(String[] args)
      Command-line entry point for testing.
      Parameters:
      args - the feed file to parse, optionally followed by an XHTMLMode name