reintroduced StringUtil.regexpReplace
[mir.git] / source / mir / rss / RSS091Reader.java
index 3f8d446..5c832af 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2001, 2002 The Mir-coders group
+ * Copyright (C) 2001-2006 The Mir-coders group
  *
  * This file is part of Mir.
  *
@@ -19,8 +19,6 @@
  *
  * In addition, as a special exception, The Mir-coders gives permission to link
  * the code of this program with  any library licensed under the Apache Software License,
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
- * (or with modified versions of the above that use the same license as the above),
  * and distribute linked combinations including the two.  You must obey the
  * GNU General Public License in all respects for all of the code used other than
  * the above mentioned libraries.  If you modify this file, you may extend this
  */
 package mir.rss;
 
+import mir.util.HTTPClientHelper;
+import mir.util.xml.XMLParserEngine;
+import mir.util.xml.XMLParserExc;
+import mir.util.xml.XMLParserFailure;
+
 import java.io.InputStream;
-import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
-import java.util.*;
-import java.util.Vector;
-import java.text.*;
-
-import mir.util.XMLReader;
-import mir.util.*;
+import java.util.Map;
 
 /**
  *
@@ -84,9 +84,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 +94,81 @@ 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"));
-    };
+      }
+                       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 +176,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;
@@ -170,12 +202,12 @@ public class RSS091Reader {
 
     public RSS091ChannelSectionHandler(RSSData aData) {
       data = aData;
-      items = new Vector();
+      items = new ArrayList();
       channel = new RDFResource("rss:channel");
       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"))
@@ -186,32 +218,32 @@ public class RSS091Reader {
       }
       else
         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()));
       }
       else if (aHandler instanceof RSS091ItemSectionHandler) {
         items.add((((RSS091ItemSectionHandler) aHandler).getItem()));
       }
-    };
+    }
 
-    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()) {
         data.addResource((RDFResource) i.next());
       }
-    };
+    }
   }
 
-  private class RSS091ItemSectionHandler extends XMLReader.AbstractSectionHandler {
+  private class RSS091ItemSectionHandler extends mir.util.xml.AbstractSectionHandler {
     private String currentTag;
 
     private RDFResource item;
@@ -221,30 +253,28 @@ 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);
         return new PCDATASectionHandler();
       }
-      else
-        return new DiscardingSectionHandler();
-    };
+                       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();
@@ -253,7 +283,7 @@ public class RSS091Reader {
 
         item.set((String) entry.getKey(), entry.getValue());
       }
-    };
+    }
 
     public RDFResource getItem() {
       return item;
@@ -261,26 +291,26 @@ 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() {
       return data.toString();
@@ -288,14 +318,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();
+      items = new ArrayList();
     }
 
-    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");
 
@@ -304,34 +334,34 @@ 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() {
       return items;
     }
   }
 
-  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 {
+    }
   }
 }