Question: *Written in java USING eclipse IDE* - I have finished most ofthe code but cannot figure out why I am getting this error: Exception in

*Written in java USING eclipse IDE* - I have finished most ofthe code but cannot figure out why I am getting this error:

Exception in thread "main" java.lang.AssertionError: Violationof: k < the number of subtrees of the root of this

at components.xmltree.XMLTree1.child(SourceFile:293)

at RSSAggregator.processItem(RSSAggregator.java:284)

at RSSAggregator.outputHeader(RSSAggregator.java:179)

at RSSAggregator.processFeed(RSSAggregator.java:90)

at RSSAggregator.indexOutputHeader(RSSAggregator.java:56)

at RSSAggregator.main(RSSAggregator.java:342)

The problem can be locatedat: http://web.cse.ohio-state.edu/software/2221/web-sw1/assignments/projects/rss-aggregator/rss-aggregator.html

Use this feed totest: http://glengainer.com/CSE2221-RSS_Feeds/metaRSSFeedsXML_sampleNews.xml

My code so far:

import components.simplereader.SimpleReader;
import components.simplereader.SimpleReader1L;
import components.simplewriter.SimpleWriter;
import components.simplewriter.SimpleWriter1L;
import components.xmltree.XMLTree;
import components.xmltree.XMLTree1;

/**
* Program to convert an XML RSS (version 2.0) feed from agiven URL into the
* corresponding HTML output file.
*
* @author Erik Anderson
*
*/
public final class RSSAggregator {

/**
* Private constructor so this utility classcannot be instantiated.
*/
private RSSAggregator() {
}

/**
* Outputs the "opening" tags in the generatedHTML file. These are the
* expected elements generated by thismethod:
*
* feedstitle tag as title
*


    * list of feeds for the index page link
    *

      *
      *
      * @param feeds
      * thefeeds element XMLTree
      * @param out
      * theoutput stream
      * @updates out.content
      * @requires [the root of channel is a tag] and out.is_open
      * @ensures out.content = #out.content * [theHTML "opening" tags]
      */
      private static void indexOutputHeader(XMLTree feeds,SimpleWriter out) {

      String title =feeds.attributeValue("title");

      out.println("");
      out.println("");
      out.println("" + title +"");
      out.println(title);
      out.println("");
      out.println("");
      out.println("

        ");

        for (int i = 0;feeds.numberOfChildren() > i; i++) {
        if(feeds.child(i).isTag()
        && feeds.child(i).label().equals("feed")) {
        processFeed(feeds.child(i).attributeValue("url"),
        feeds.child(i).attributeValue("file"),out);
        out.println("

      • + feeds.child(i).attributeValue("url") +"">"
        + feeds.child(i).attributeValue("name") +"
      • ");
        }

        }
        out.println("

      ");
      out.println("");
      out.println("");
      }

      /**
      * Processes one XML RSS (version 2.0) feed froma given URL converting it
      * into the corresponding HTML outputfile.
      *
      * @param url
      * theURL of the RSS feed
      * @param file
      * thename of the HTML output file
      * @param out
      * theoutput stream to report progress or errors
      * @updates out.content
      * @requires out.is_open
      * @ensures


      * [reads RSS feed from url, saves HTML documentwith table of news items
      * to file, appends to out.contentany needed messages]
      *

      */
      private static void processFeed(String url, Stringfile, SimpleWriter out) {
      XMLTree xml = new XMLTree1(url);
      SimpleWriter fileOut = newSimpleWriter1L(file);

      outputHeader(xml.child(0),fileOut);
      outputFooter(fileOut);
      }

      /**
      * Outputs the "opening" tags in the generatedHTML file. These are the
      * expected elements generated by thismethod:
      *
      * thechannel tag title as the page title
      *
      *

      the page title inside a link to the link
      *


      *


      * the channel description
      *


      *
      *
      *
      *
      *
      *
      *
      * @param channel
      * thechannel element XMLTree
      * @param out
      * theoutput stream
      * @updates out.content
      * @requires [the root of channel is a tag] and out.is_open
      * @ensures out.content = #out.content * [theHTML "opening" tags]
      */
      private static void outputHeader(XMLTree channel,SimpleWriter out) {
      assert channel != null : "Violation of:channel is not null";
      assert out != null : "Violation of: outis not null";
      assert channel.isTag() &&channel.label().equals("channel") : ""
      +"Violation of: the label root of channel is a tag";
      assert out.isOpen() : "Violation of:out.is_open";

      /*
      * Assigns title variable eitherdata from 'title' child, or
      * 'description child if 'title'is empty. If neither exist, assigns
      * value of "No information" totitle.
      */
      String title = "";
      if (getChildElement(channel, "title")> 0) {
      title =channel.child(getChildElement(channel, "title")).label();
      } else if (getChildElement(channel,"description") > 0) {
      title =channel.child(getChildElement(channel, "description"))
      .label();
      } else {
      title = "Noinformation";
      }

      /*
      * runs getChildElement to assignvalue to String description. If there
      * is no description value, "NoDescription" is assigned to the
      * description string
      */
      String description = "";
      if(channel.child(getChildElement(channel, "description"))
      .numberOfChildren() < 1) {
      description = "NoDescription.";
      } else {
      description =channel.child(getChildElement(channel, "description"))
      .child(0).label();
      }

      // Prints all the necessary htmlformat to the outfile
      out.println("");
      out.println("");
      out.println("" + title +"");
      out.println("");
      out.println("");
      out.println(
      "

      + channel.child(getChildElement(channel,"link"))
      .child(0).label()
      + "">" + title +"

      ");
      out.println("

      " + description +"

      ");
      out.println("");
      out.println("
      ");
      out.println("");
      out.println("");
      out.println("");
      out.println("");

      // Goes through each of 'channel'children and creates a table in html via the processItemmethod
      int i = 0;
      while (channel.numberOfChildren() >i) {
      if(channel.child(i).label().equals("item")) {
      processItem(channel.child(i), out);
      }
      i++;
      }

      }

      /**
      * Finds the first occurrence of the given tagamong the children of the
      * given {@code XMLTree} and return its index;returns -1 if not found.
      *
      * @param xml
      * the{@code XMLTree} to search
      * @param tag
      * thetag to look for
      * @return the index of the first child of typetag of the {@code XMLTree}
      * or -1 if notfound
      * @requires [the label of the root of xml is atag]
      * @ensures


      * getChildElement =
      * [the index of the first child of typetag of the {@code XMLTree} or
      * -1 if not found]
      *

      */
      private static int getChildElement(XMLTree xml,String tag) {
      assert xml != null : "Violation of: xmlis not null";
      assert tag != null : "Violation of: tagis not null";
      assert xml.isTag() : "Violation of: thelabel root of xml is a tag";

      /*
      * If xml has an attribute called'tag', it will provide the index value
      * for the position of the 'tag'attribute. If there is no attribute
      * called 'tag', then it returns-1.
      */
      int i = xml.numberOfChildren(), index =-1;

      while (i > 0 && index< 0) {
      i--;
      if(xml.child(i).label().equals(tag)) {
      index =i;
      }
      }

      return index;
      }

      /**
      * Processes one news item and outputs one tablerow. The row contains three
      * elements: the publication date, the source,and the title (or
      * description) of the item.
      *
      * @param item
      * thenews item
      * @param out
      * theoutput stream
      * @updates out.content
      * @requires


      * [the label of the root of item is an tag] and out.is_open
      *

      * @ensures

      * out.content = #out.content *
      * [an HTML table row withpublication date, source, and title of news item]
      *

      */
      private static void processItem(XMLTree item,SimpleWriter out) {
      assert item != null : "Violation of:item is not null";
      assert out != null : "Violation of: outis not null";
      assert item.isTag() &&item.label().equals("item") : ""
      +"Violation of: the label root of item is an tag";
      assert out.isOpen() : "Violation of:out.is_open";

      out.println("

      ");

      // pubDate string is assigned avalue if pubDate returns a value, otherwise it is assigned a valueof "No Date Available"
      String pubDate = "";
      if (item.child(getChildElement(item,"pubDate"))
      .numberOfChildren() < 1) {
      pubDate = "No DateAvailable";
      } else {
      pubDate =item.child(getChildElement(item, "pubDate")).child(0)
      .label();
      }
      out.println("

      ");

      // source string is assigned a valueif a label of one of item's children returns a value "source",otherwise it is assigned an empty value. The results are thenprinted to the outfile
      String source = "No SourceAvailable.";
      String sourceURL = "";
      int i = 0, x = -1;
      while (item.numberOfChildren() > i){
      if(item.child(i).label().equals("source")) {
      source =item.child(i).child(0).label();
      sourceURL =item.child(i).attributeValue("url");
      out.println("

      ");
      x =0;
      }
      i++;
      }
      if (x != 0) {
      out.println("");
      }

      // news string is assigned a valueif "description" returns a value greater than 1 fromgetChildElement. If "description" does not return a value, "title"will be run in getChildElement.If neither return any value, thedefault "No description" will be printed
      String news = "No description";
      if (getChildElement(item,"description") > 0) {
      news =item.child(getChildElement(item, "description")).child(0)
      .label();
      } else if (getChildElement(item,"title") > 0) {
      news =item.child(getChildElement(item, "title")).child(0).label();
      }

      String link = "";
      if (getChildElement(item, "link") >0) {
      link =item.child(getChildElement(item, "link")).child(0).label();
      }

      out.println("

      ");
      out.println("");
      }

      /**
      * Outputs the "closing" tags in the generatedHTML file. These are the
      * expected elements generated by thismethod:
      *
      *

      DateSourceNews
      DateSourceNews
      " + pubDate +"" +source
      + "
      "+ source + "" + news + "

      *
      *
      * @param out
      * theoutput stream
      * @updates out.contents
      * @requires out.is_open
      * @ensures out.content = #out.content * [theHTML "closing" tags]
      */
      private static void outputFooter(SimpleWriter out){
      assert out != null : "Violation of: outis not null";
      assert out.isOpen() : "Violation of:out.is_open";

      // Prints the closing html data tothe outfile
      out.println("");
      out.println("");
      out.println("");
      }

      /**
      * Main method.
      *
      * @param args
      * thecommand line arguments; unused here
      */
      public static void main(String[] args) {
      SimpleReader in = newSimpleReader1L();
      SimpleWriter out = newSimpleWriter1L();

      // Requests user input xml file thatcontains url feeds and then asks user to name output file.
      out.print("Please enter a XML file withURL feeds: ");
      String inputXML = in.nextLine();
      XMLTree xml = newXMLTree1(inputXML);

      out.print("Please name the outputfile: ");
      String outfileName = in.nextLine();

      SimpleWriter fileOut = newSimpleWriter1L(outfileName);

      indexOutputHeader(xml, fileOut);

      in.close();
      out.close();
      }

      }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!