rebuilding head
[mir.git] / source / mir / rss / RSSReader.java
index bdc850b..6d89d49 100755 (executable)
 package mir.rss;
 
 import java.io.InputStream;
-import java.net.URL;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Vector;
 
 import mir.util.DateTimeFunctions;
-import mir.util.XMLReader;
+import mir.util.HTTPClientHelper;
+import mir.util.xml.XMLParserEngine;
+import mir.util.xml.XMLParserExc;
+import mir.util.xml.XMLParserFailure;
 
 /**
  *
@@ -60,22 +62,22 @@ public class RSSReader {
   public static final String CONTENT_NAMESPACE_URI = "http://purl.org/rss/1.0/modules/content/";
 
   // ML: to be localized:
-  public static final String V2V_NAMESPACE_URI = "http://v2v.indymedia.de/rss/";
+  public static final String V2V_NAMESPACE_URI = "http://v2v.cc/rss/";
 
-  private static final XMLReader.XMLName RDF_ABOUT_PARAMETER = new XMLReader.XMLName(RDF_NAMESPACE_URI, "about");
-  private static final XMLReader.XMLName RDF_SEQUENCE_TAG = new XMLReader.XMLName(RDF_NAMESPACE_URI, "Seq");
-  private static final XMLReader.XMLName RDF_BAG_PARAMETER = new XMLReader.XMLName(RDF_NAMESPACE_URI, "Bag");
+  private static final mir.util.xml.XMLName RDF_ABOUT_PARAMETER = new mir.util.xml.XMLName(RDF_NAMESPACE_URI, "about");
+  private static final mir.util.xml.XMLName RDF_SEQUENCE_TAG = new mir.util.xml.XMLName(RDF_NAMESPACE_URI, "Seq");
+  private static final mir.util.xml.XMLName RDF_BAG_PARAMETER = new mir.util.xml.XMLName(RDF_NAMESPACE_URI, "Bag");
 
-  private static final XMLReader.XMLName RSS_CHANNEL_TAG = new XMLReader.XMLName(RSS_1_0_NAMESPACE_URI, "channel");
-  private static final XMLReader.XMLName RSS_ITEM_TAG = new XMLReader.XMLName(RSS_1_0_NAMESPACE_URI, "item");
-  private static final XMLReader.XMLName RSS_ITEMS_TAG = new XMLReader.XMLName(RSS_1_0_NAMESPACE_URI, "items");
+  private static final mir.util.xml.XMLName RSS_CHANNEL_TAG = new mir.util.xml.XMLName(RSS_1_0_NAMESPACE_URI, "channel");
+  private static final mir.util.xml.XMLName RSS_ITEM_TAG = new mir.util.xml.XMLName(RSS_1_0_NAMESPACE_URI, "item");
+  private static final mir.util.xml.XMLName RSS_ITEMS_TAG = new mir.util.xml.XMLName(RSS_1_0_NAMESPACE_URI, "items");
 
   private List modules;
   private Map namespaceURItoModule;
   private Map moduleToPrefix;
 
   public RSSReader() {
-    modules = new Vector();
+    modules = new ArrayList();
     namespaceURItoModule = new HashMap();
     moduleToPrefix = new HashMap();
 
@@ -115,9 +117,20 @@ public class RSSReader {
 
   public RSSData parseInputStream(InputStream aStream) throws RSSExc, RSSFailure {
     try {
-      XMLReader xmlReader = new XMLReader(true);
       RSSData result = new RSSData();
-      xmlReader.parseInputStream(aStream, new RootSectionHandler(result));
+      XMLParserEngine.getInstance().parse("xml", aStream, new RootSectionHandler(result));
+
+      return result;
+    }
+    catch (Throwable t) {
+      throw new RSSFailure(t);
+    }
+  }
+
+  public RSSData parseInputStream(InputStream aStream, String anEncoding) throws RSSExc, RSSFailure {
+    try {
+      RSSData result = new RSSData();
+      XMLParserEngine.getInstance().parse("xml", aStream, anEncoding, new RootSectionHandler(result));
 
       return result;
     }
@@ -128,46 +141,64 @@ public class RSSReader {
 
   public RSSData parseUrl(String anUrl) throws RSSExc, RSSFailure {
     try {
-      InputStream inputStream = (InputStream) new URL(anUrl).getContent(new Class[] {InputStream.class});
+      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);
+      httpClientHelper.releaseHTTPConnection();
+      return theRSSData;
+    }
+    catch (Throwable t) {
+      throw new RSSFailure(t);
+    }
+  }
 
+  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");
 
-      return parseInputStream(inputStream);
+      RSSData theRSSData =  parseInputStream(inputStream, anEncoding);
+      httpClientHelper.releaseHTTPConnection();
+      return theRSSData;
     }
     catch (Throwable t) {
       throw new RSSFailure(t);
     }
   }
 
-  private class RootSectionHandler extends XMLReader.AbstractSectionHandler {
+  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 {
+    public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
       if (aTag.getLocalName().equals("RDF")) {
         return new RDFSectionHandler(data);
       }
       else
-        throw new XMLReader.XMLReaderFailure(new RSSExc("'RDF' tag expected"));
+        throw new XMLParserFailure(new RSSExc("'RDF' 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 RDFSectionHandler extends XMLReader.AbstractSectionHandler {
+  private class RDFSectionHandler extends mir.util.xml.AbstractSectionHandler {
     private RSSData data;
 
 
@@ -175,29 +206,29 @@ public class RSSReader {
       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 {
       String identifier = (String) anAttributes.get(RDF_ABOUT_PARAMETER);
       String rdfClass = makeQualifiedName(aTag);
 
       return new RDFResourceSectionHandler(rdfClass, identifier);
     };
 
-    public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
+    public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
       if (aHandler instanceof RDFResourceSectionHandler) {
         data.addResource(((RDFResourceSectionHandler) aHandler).getResource());
       }
     };
 
-    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 XMLReader.SectionHandler makePropertyValueSectionHandler(XMLReader.XMLName aTag, Map anAttributes) {
+  private mir.util.xml.SectionHandler makePropertyValueSectionHandler(mir.util.xml.XMLName aTag, Map anAttributes) {
     RSSModule module = (RSSModule) namespaceURItoModule.get(aTag.getNamespaceURI());
 
     if (module!=null) {
@@ -224,7 +255,7 @@ public class RSSReader {
     return new FlexiblePropertyValueSectionHandler();
   }
 
-  private void usePropertyValueSectionHandler(RDFResource aResource, PropertyValueSectionHandler aHandler, XMLReader.XMLName aTag) {
+  private void usePropertyValueSectionHandler(RDFResource aResource, PropertyValueSectionHandler aHandler, mir.util.xml.XMLName aTag) {
     RSSModule module = (RSSModule) namespaceURItoModule.get(aTag.getNamespaceURI());
 
     if (module!=null) {
@@ -234,7 +265,7 @@ public class RSSReader {
         List value = (List) aResource.get(makeQualifiedName(aTag));
 
         if (value==null) {
-          value = new Vector();
+          value = new ArrayList();
           aResource.set(makeQualifiedName(aTag), value);
         }
 
@@ -247,7 +278,7 @@ public class RSSReader {
     aResource.set(makeQualifiedName(aTag), aHandler.getValue());
   }
 
-  private String makeQualifiedName(XMLReader.XMLName aName) {
+  private String makeQualifiedName(mir.util.xml.XMLName aName) {
     String result=aName.getLocalName();
     RSSModule module = (RSSModule) namespaceURItoModule.get(aName.getNamespaceURI());
     if (module!=null) {
@@ -260,38 +291,38 @@ public class RSSReader {
     return result;
   }
 
-  private class RDFResourceSectionHandler extends XMLReader.AbstractSectionHandler {
+  private class RDFResourceSectionHandler extends mir.util.xml.AbstractSectionHandler {
     private String image;
-    private XMLReader.XMLName currentTag;
+    private mir.util.xml.XMLName currentTag;
     private RDFResource resource;
 
     public RDFResourceSectionHandler(String anRDFClass, String anIdentifier) {
       resource = new RDFResource(anRDFClass, anIdentifier);
     }
 
-    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 {
       currentTag = aTag;
 
       return makePropertyValueSectionHandler(aTag, anAttributes);
     };
 
-    public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
+    public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
       if (aHandler instanceof PropertyValueSectionHandler) {
         usePropertyValueSectionHandler(resource, (PropertyValueSectionHandler) aHandler, currentTag);
-//        resource.set(makeQualifiedName(currentTag), ( (PropertyValueSectionHandler) aHandler).getValue());
+//        resource.set(makeQualifiedName(currentTag), ( (PropertyValueSectionHandler) aHandler).getFieldValue());
       }
     };
 
-    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 {
     };
 
     public RDFResource getResource() {
-      if (resource.getIdentifier()==null || resource.getIdentifier().length()==0) {
+      if ((resource.getIdentifier()==null || resource.getIdentifier().length()==0) && resource.get("rss:link")!=null) {
         resource.setIdentifier(resource.get("rss:link").toString());
       }
 
@@ -299,7 +330,7 @@ public class RSSReader {
     }
   }
 
-  private abstract class PropertyValueSectionHandler extends XMLReader.AbstractSectionHandler {
+  private abstract class PropertyValueSectionHandler extends mir.util.xml.AbstractSectionHandler {
     public abstract Object getValue();
   }
 
@@ -312,24 +343,24 @@ public class RSSReader {
       structuredData=null;
     }
 
-    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_SEQUENCE_TAG))
         return new RDFSequenceSectionHandler();
       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 RDFSequenceSectionHandler) {
         structuredData= ((RDFSequenceSectionHandler) aHandler).getItems();
       }
     };
 
-    public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
+    public void characters(String aCharacters) throws XMLParserExc {
       stringData.append(aCharacters);
     };
 
-    public void finishSection() throws XMLReader.XMLReaderExc {
+    public void finishSection() throws XMLParserExc {
     };
 
     public String getData() {
@@ -348,28 +379,28 @@ public class RSSReader {
     private List items;
 
     public RDFCollectionSectionHandler() {
-      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_SEQUENCE_TAG))
         return new RDFSequenceSectionHandler();
       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 RDFSequenceSectionHandler) {
         items.addAll(((RDFSequenceSectionHandler) aHandler).getItems());
       }
     };
 
-    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 {
     };
 
     public List getItems() {
@@ -388,18 +419,18 @@ public class RSSReader {
       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() {
@@ -418,24 +449,20 @@ public class RSSReader {
       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 {
     };
 
-    private final static String SPACE = "[\t\n\r ]*";
-    private final static String NUMBER = "[0-9]*";
-    private final static String SIGN = "[-+]";
-
     public Object getValue() {
       try {
         String expression = data.toString().trim();
@@ -450,14 +477,14 @@ public class RSSReader {
   }
 
 
-  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");
 
@@ -468,13 +495,13 @@ public class RSSReader {
       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() {
@@ -494,23 +521,23 @@ public class RSSReader {
       return data;
     }
 
-    public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
+    public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
       tag=aTag;
       data.append("<"+tag+">");
 
       return new RDFLiteralSectionHandler();
     };
 
-    public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
+    public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
       data.append(((RDFLiteralSectionHandler) aHandler).getData());
       data.append("</"+tag+">");
     };
 
-    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 Object getValue() {
@@ -518,18 +545,18 @@ public class RSSReader {
     }
   }
 
-  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 {
     };
   }
 }