1 19 20 package org.jperdian.rss2; 21 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.text.DateFormat ; 25 import java.text.ParseException ; 26 import java.text.SimpleDateFormat ; 27 import java.util.Date ; 28 import java.util.Locale ; 29 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.NamedNodeMap ; 32 import org.w3c.dom.Node ; 33 import org.w3c.dom.NodeList ; 34 35 40 41 public class RssParseHelper { 42 43 private static final DateFormat DF = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US); 44 45 51 public static URL parseURL(String sourceString) throws RssParseException { 52 if(sourceString == null || sourceString.length() < 1) { 53 return null; 54 } else { 55 try { 56 return new URL (sourceString); 57 } catch(MalformedURLException e) { 58 throw new RssParseException("Illegal URL found: " + sourceString); 59 } 60 } 61 } 62 63 72 private static String parseContent(Node node) { 73 74 StringBuffer buffer = new StringBuffer (); 75 short nodeType = node.getNodeType(); 76 77 switch(nodeType) { 78 79 case Node.CDATA_SECTION_NODE: 80 case Node.TEXT_NODE: 81 String value = node.getNodeValue(); 82 if(value.length() > 0) { 83 buffer.append(value); 84 } 85 break; 86 87 case Node.ELEMENT_NODE: 88 Element element = (Element )node; 89 90 buffer.append("<").append(element.getNodeName()); 91 92 NamedNodeMap attributes = element.getAttributes(); 94 for(int i=0; i < attributes.getLength(); i++) { 95 buffer.append(" ").append(attributes.item(i).getNodeName()).append("=\""); 96 buffer.append(attributes.item(i).getNodeValue()).append("\""); 97 } 98 99 if(element.hasChildNodes()) { 100 buffer.append(">"); 101 buffer.append(RssParseHelper.parseContentChildren(element)); 102 buffer.append("</").append(element.getNodeName()).append(">"); 103 } else { 104 105 114 115 String nodeName = element.getNodeName(); 116 if(nodeName.equalsIgnoreCase("br") || 117 nodeName.equalsIgnoreCase("hr") || 118 nodeName.equalsIgnoreCase("input") || 119 nodeName.equalsIgnoreCase("meta") || 120 nodeName.equalsIgnoreCase("frame") 121 ) { 122 buffer.append(">"); 123 } else { 124 buffer.append("></").append(element.getNodeName()).append(">"); 125 } 126 } 127 } 128 return buffer.toString(); 129 } 130 131 137 public static String parseContentChildren(Node node) { 138 StringBuffer result = new StringBuffer (); 139 if(node.hasChildNodes()) { 140 NodeList children = node.getChildNodes(); 141 for(int i=0; i < children.getLength(); i++) { 142 result.append(RssParseHelper.parseContent(children.item(i))); 143 } 144 } 145 return result.toString().trim(); 146 } 147 148 152 public static Date parseContentDate(Element node) throws RssParseException { 153 String content = RssParseHelper.parseContentChildren(node); 154 if(content.length() < 1) { 155 return null; 156 } else { 157 try { 158 return DF.parse(content); 159 } catch(ParseException e) { 160 return null; 162 } 163 } 164 } 165 166 170 public static int parseContentInt(Element node) throws RssParseException { 171 return RssParseHelper.parseContentInt(node, 0); 172 } 173 174 178 public static int parseContentInt(Element node, int defaultValue) throws RssParseException { 179 String content = RssParseHelper.parseContentChildren(node); 180 if(content.length() < 1) { 181 return defaultValue; 182 } else { 183 try { 184 return Integer.parseInt(content); 185 } catch(NumberFormatException e) { 186 throw new RssParseException("Illegal integer value found: " + content); 187 } 188 } 189 } 190 191 194 public static URL parseContentURL(Element node) throws RssParseException { 195 String content = RssParseHelper.parseContentChildren(node); 196 return RssParseHelper.parseURL(content); 197 } 198 199 } | Popular Tags |