1 26 27 29 package de.nava.informa.parsers; 30 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.Reader ; 35 import java.net.URL ; 36 import java.util.Collection ; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 import org.jdom.Document; 41 import org.jdom.Element; 42 import org.jdom.JDOMException; 43 import org.jdom.input.SAXBuilder; 44 import org.xml.sax.InputSource ; 45 46 import de.nava.informa.core.ParseException; 47 import de.nava.informa.core.UnsupportedFormatException; 48 import de.nava.informa.utils.NoOpEntityResolver; 49 50 60 public class OPMLParser { 61 62 private static Log logger = LogFactory.getLog(OPMLParser.class); 63 64 private OPMLParser() { 65 } 66 67 public static Collection parse(URL aURL) throws IOException , ParseException { 68 return parse(new InputSource (aURL.toExternalForm()), aURL); 69 } 70 71 75 public static Collection parse(String url) throws IOException , ParseException { 76 URL aURL = null; 77 try { 78 aURL = new URL (url); 79 } catch (java.net.MalformedURLException e) { 80 logger.warn("Could not create URL for " + url); 81 } 82 return parse(new InputSource (url), aURL); 83 } 84 85 public static Collection parse(Reader reader) throws IOException , ParseException { 86 return parse(new InputSource (reader), null); 87 } 88 89 public static Collection parse(InputStream stream) throws IOException , ParseException { 90 return parse(new InputSource (stream), null); 91 } 92 93 public static Collection parse(File aFile) throws IOException , ParseException { 94 URL aURL = null; 95 try { 96 aURL = aFile.toURL(); 97 } catch (java.net.MalformedURLException e) { 98 throw new IOException ("File " + aFile + " had invalid URL " + 99 "representation."); 100 } 101 return parse(new InputSource (aURL.toExternalForm()), aURL); 102 } 103 104 public static Collection parse(InputSource inpSource, 105 URL baseLocation) throws IOException , ParseException { 106 SAXBuilder saxBuilder = new SAXBuilder(false); 108 saxBuilder.setEntityResolver(new NoOpEntityResolver()); 110 try { 111 Document doc = saxBuilder.build(inpSource); 112 return parse(doc); 113 } catch (JDOMException e) { 114 throw new ParseException(e); 115 } 116 } 117 118 122 private static synchronized Collection parse(Document doc) throws ParseException { 123 124 logger.debug("start parsing."); 125 Element root = doc.getRootElement(); 127 String rootElement = root.getName().toLowerCase(); 128 if (rootElement.startsWith("opml")) { 130 String opmlVersion = root.getAttribute("version").getValue(); 131 if (opmlVersion.indexOf("1.1") >= 0) { 132 logger.info("Collection uses OPML root element (Version 1.1)."); 133 return OPML_1_1_Parser.parse(root); 134 } 135 } 136 137 throw new UnsupportedFormatException("Unsupported OPML root element [" + 139 rootElement + "]."); 140 } 141 142 } 143 | Popular Tags |