1.1 restoration
[mir.git] / source / mir / rss / RSS091Reader.java
index 3f8d446..a6556b7 100755 (executable)
@@ -31,13 +31,15 @@ package mir.rss;
 
 import java.io.InputStream;
 import java.net.URL;
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
-import java.util.*;
+import java.util.Map;
 import java.util.Vector;
-import java.text.*;
 
-import mir.util.XMLReader;
-import mir.util.*;
+import mir.util.HTTPClientHelper;
+import mir.util.xml.*;
+import mir.util.xml.XMLParserEngine;
 
 /**
  *
@@ -84,9 +86,8 @@ public class RSS091Reader {
 
   public RSSData parseInputStream(InputStream aStream) throws RSSExc, RSSFailure {
     try {
-      XMLReader xmlReader = new XMLReader(false);
       RSSData result = new RSSData();
-      xmlReader.parseInputStream(aStream, new RootSectionHandler(result));
+      XMLParserEngine.getInstance().parse("html", aStream, new RootSectionHandler(result));
 
       return result;
     }
@@ -95,48 +96,82 @@ public class RSS091Reader {
     }
   }
 
-  public RSSData parseUrl(String anUrl) throws RSSExc, RSSFailure {
+  public RSSData parseInputStream(InputStream aStream, String anEncoding) throws RSSExc, RSSFailure {
     try {
-      InputStream inputStream = (InputStream) new URL(anUrl).getContent(new Class[] {InputStream.class});
+      RSSData result = new RSSData();
+      XMLParserEngine.getInstance().parse("html", aStream, anEncoding, new RootSectionHandler(result));
 
+      return result;
+    }
+    catch (Throwable t) {
+      throw new RSSFailure(t);
+    }
+  }
+
+  public RSSData parseUrl(String anUrl) throws RSSExc, RSSFailure {
+    try {
+      HTTPClientHelper httpClientHelper = new HTTPClientHelper();      
+      InputStream inputStream = httpClientHelper.getUrl(anUrl);
       if (inputStream==null)
         throw new RSSExc("RSSChannel.parseUrl: Can't get url content");
 
-      return parseInputStream(inputStream);
+      RSSData theRSSData =  parseInputStream(inputStream);
+      httpClientHelper.releaseHTTPConnection();
+      return theRSSData;
+
     }
     catch (Throwable t) {
       throw new RSSFailure(t);
     }
   }
 
-  private class RootSectionHandler extends XMLReader.AbstractSectionHandler {
+  public RSSData parseUrl(String anUrl, String anEncoding) throws RSSExc, RSSFailure {
+    try {
+      HTTPClientHelper httpClientHelper = new HTTPClientHelper();      
+      InputStream inputStream = httpClientHelper.getUrl(anUrl);
+
+      if (inputStream==null)
+        throw new RSSExc("RSSChannel.parseUrl: Can't get url content");
+      
+      RSSData theRSSData = parseInputStream(inputStream, anEncoding);
+      httpClientHelper.releaseHTTPConnection();
+      return theRSSData;
+    }
+
+    catch (Throwable t) {
+      throw new RSSFailure(t);
+    }
+  }
+
+  private class RootSectionHandler extends mir.util.xml.AbstractSectionHandler {
     private RSSData data;
 
     public RootSectionHandler(RSSData aData) {
       data = aData;
     }
 
-    public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
-//      if (aTag.getLocalName().equals("rss")) {
+    public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
+      if (aTag.getLocalName().equals("rss")) {
         return new RSS091SectionHandler(data);
-//      }
-//      else
-//        throw new XMLReader.XMLReaderFailure(new RSSExc("'rss' tag expected"));
+      }
+      else
+        throw new XMLParserFailure(new RSSExc("'rss' tag expected"));
     };
 
-    public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
+    public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
     };
 
-    public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
+    public void characters(String aCharacters) throws XMLParserExc {
       if (aCharacters.trim().length()>0)
-        throw new XMLReader.XMLReaderExc("No character data allowed here");
+        throw new XMLParserExc("No character data allowed here");
     };
 
-    public void finishSection() throws XMLReader.XMLReaderExc {
+    public void finishSection() throws XMLParserExc {
     };
   }
 
-  private class RSS091SectionHandler extends XMLReader.AbstractSectionHandler {
+  private class RSS091SectionHandler extends mir.util.xml.AbstractSectionHandler {
     private RSSData data;
 
 
@@ -144,23 +179,23 @@ public class RSS091Reader {
       data = aData;
     }
 
-    public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
+    public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
       if (aTag.getLocalName().equals("channel"))
         return new RSS091ChannelSectionHandler(data);
       else
-        throw new XMLReader.XMLReaderExc("channel tag expected, " + aTag.getLocalName() + " found");
+        throw new XMLParserExc("channel tag expected, " + aTag.getLocalName() + " found");
     };
 
-    public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
+    public void characters(String aCharacters) throws XMLParserExc {
       if (aCharacters.trim().length()>0)
-        throw new XMLReader.XMLReaderExc("No character data allowed here");
+        throw new XMLParserExc("No character data allowed here");
     };
 
-    public void finishSection() throws XMLReader.XMLReaderExc {
+    public void finishSection() throws XMLParserExc {
     };
   }
 
-  private class RSS091ChannelSectionHandler extends XMLReader.AbstractSectionHandler {
+  private class RSS091ChannelSectionHandler extends mir.util.xml.AbstractSectionHandler {
     private String currentTag;
 
     private RSSData data;
@@ -175,7 +210,7 @@ public class RSS091Reader {
       attributes = new HashMap();
     }
 
-    public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
+    public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
       String tag = aTag.getLocalName();
 
       if (tag.equals("item"))
@@ -188,7 +223,7 @@ public class RSS091Reader {
         return new DiscardingSectionHandler();
     };
 
-    public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
+    public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
       if (aHandler instanceof PCDATASectionHandler) {
         attributes.put(currentTag, (((PCDATASectionHandler) aHandler).getData()));
       }
@@ -197,12 +232,12 @@ public class RSS091Reader {
       }
     };
 
-    public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
+    public void characters(String aCharacters) throws XMLParserExc {
       if (aCharacters.trim().length()>0)
-        throw new XMLReader.XMLReaderExc("No character data allowed here");
+        throw new XMLParserExc("No character data allowed here");
     };
 
-    public void finishSection() throws XMLReader.XMLReaderExc {
+    public void finishSection() throws XMLParserExc {
       Iterator i = items.iterator();
 
       while (i.hasNext()) {
@@ -211,7 +246,7 @@ public class RSS091Reader {
     };
   }
 
-  private class RSS091ItemSectionHandler extends XMLReader.AbstractSectionHandler {
+  private class RSS091ItemSectionHandler extends mir.util.xml.AbstractSectionHandler {
     private String currentTag;
 
     private RDFResource item;
@@ -221,9 +256,8 @@ public class RSS091Reader {
       attributes = new HashMap();
     }
 
-    public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
+    public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
       String tag = aTag.getLocalName();
-      System.out.println(tag);
 
       if (mappedItemProperties.containsKey(tag)) {
         currentTag=(String) mappedItemProperties.get(tag);
@@ -233,18 +267,18 @@ public class RSS091Reader {
         return new DiscardingSectionHandler();
     };
 
-    public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
+    public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
       if (aHandler instanceof PCDATASectionHandler) {
         attributes.put(currentTag, (((PCDATASectionHandler) aHandler).getData()));
       }
     };
 
-    public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
+    public void characters(String aCharacters) throws XMLParserExc {
       if (aCharacters.trim().length()>0)
-        throw new XMLReader.XMLReaderExc("No character data allowed here");
+        throw new XMLParserExc("No character data allowed here");
     };
 
-    public void finishSection() throws XMLReader.XMLReaderExc {
+    public void finishSection() throws XMLParserExc {
       item = new RDFResource("rss:item", (String) attributes.get("rss:link"));
 
       Iterator i = attributes.entrySet().iterator();
@@ -261,25 +295,25 @@ public class RSS091Reader {
   }
 
 
-  private class PCDATASectionHandler extends XMLReader.AbstractSectionHandler {
+  private class PCDATASectionHandler extends mir.util.xml.AbstractSectionHandler {
     private StringBuffer data;
 
     public PCDATASectionHandler() {
       data = new StringBuffer();
     }
 
-    public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
-      throw new XMLReader.XMLReaderFailure(new RSSExc("No subtags allowed here"));
+    public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
+      throw new XMLParserFailure(new RSSExc("No subtags allowed here"));
     };
 
-    public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
+    public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
     };
 
-    public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
+    public void characters(String aCharacters) throws XMLParserExc {
       data.append(aCharacters);
     };
 
-    public void finishSection() throws XMLReader.XMLReaderExc {
+    public void finishSection() throws XMLParserExc {
     };
 
     public String getData() {
@@ -288,14 +322,14 @@ public class RSS091Reader {
   }
 
 
-  private class RDFSequenceSectionHandler extends XMLReader.AbstractSectionHandler {
+  private class RDFSequenceSectionHandler extends mir.util.xml.AbstractSectionHandler {
     private List items;
 
     public RDFSequenceSectionHandler() {
       items = new Vector();
     }
 
-    public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
+    public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
       if (aTag.equals("rdf:li")) {
         String item = (String) anAttributes.get("rdf:resource");
 
@@ -306,13 +340,13 @@ public class RSS091Reader {
       return new DiscardingSectionHandler();
     };
 
-    public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
+    public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
     };
 
-    public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
+    public void characters(String aCharacters) throws XMLParserExc {
     };
 
-    public void finishSection() throws XMLReader.XMLReaderExc {
+    public void finishSection() throws XMLParserExc {
     };
 
     public List getItems() {
@@ -320,18 +354,18 @@ public class RSS091Reader {
     }
   }
 
-  private class DiscardingSectionHandler extends XMLReader.AbstractSectionHandler {
-    public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
+  private class DiscardingSectionHandler extends mir.util.xml.AbstractSectionHandler {
+    public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
       return this;
     };
 
-    public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
+    public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
     };
 
-    public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
+    public void characters(String aCharacters) throws XMLParserExc {
     };
 
-    public void finishSection() throws XMLReader.XMLReaderExc {
+    public void finishSection() throws XMLParserExc {
     };
   }
 }