1 25 26 54 62 63 package net.killingar.forum.comics; 64 65 import net.killingar.StringUtils; 66 import net.killingar.forum.internal.Strip; 67 68 import java.io.PrintWriter ; 69 import java.net.URL ; 70 import java.util.ArrayList ; 71 import java.util.regex.Matcher ; 72 import java.util.regex.Pattern ; 73 74 public class IndirectLinkComicsSystem extends ComicsSystem 75 { 76 final static int 77 plain = 0, 78 reverse = 1, 79 sort = 2; 80 81 static java.sql.Date dummydate = new java.sql.Date (0); 82 83 static class Settings { 84 int position; 85 String linkpage; 86 int link_group; 87 int description_group; 88 String linkpage_regex; 89 int img_group; 90 String imgpage_regex; 91 int sortmode; 92 String startText; 93 String endText; 94 } 95 96 public void update(String arguments[]) 97 { 98 try { 99 try { 100 Settings settings = parseArguments(arguments, comic.position); 101 102 URL linkspage_url = new URL (settings.linkpage); 104 String page = StringUtils.readURLAsString(linkspage_url); 105 int groups[] = new int[2]; 107 groups[0] = settings.link_group; 108 groups[1] = settings.description_group; 109 110 if (settings.startText != null && page.indexOf(settings.startText) != -1) 112 page = page.substring(page.indexOf(settings.startText)); 113 114 if (settings.endText != null && page.indexOf(settings.endText) != -1) 115 page = page.substring(0, page.indexOf(settings.endText)); 116 117 ArrayList matches = findAllPatternMatches(page, 118 settings.linkpage_regex, groups); 119 120 if(settings.sortmode == reverse) 122 java.util.Collections.reverse(matches); 123 else if(settings.sortmode == sort) 124 java.util.Collections.sort(matches); 125 126 Pattern image_ptn = Pattern.compile(settings.imgpage_regex); 129 for(int i=Integer.parseInt(comic.position); i<matches.size(); i++) { 130 131 String [] url_and_desc = (String []) matches.get(i); 133 URL url = new URL (linkspage_url, url_and_desc[0]); 134 page = StringUtils.readURLAsString(url); 135 Matcher matcher = image_ptn.matcher(page); 136 matcher.find(); 137 url = new URL (url, (String ) matcher.group(1)); 138 139 if (!debug) { 141 cmgr.addStrip(new Strip(comic.ID, url.toString(), dummydate, url_and_desc[1], url_and_desc[1])); 142 143 comic = cmgr.getComic(comic.ID); 144 comic.position = Integer.toString(i); 145 cmgr.changeComic(comic); 146 147 log("! "); 148 } 149 else { 150 logln(url_and_desc[1]+"("+url_and_desc[0]+")"); 151 } 152 153 } 155 if (!debug) { 156 comic = cmgr.getComic(comic.ID); 157 comic.position = Integer.toString(matches.size()); 158 cmgr.changeComic(comic); 159 } 160 161 } catch(java.lang.IllegalArgumentException e) { 162 logln("Error: " + e.getMessage()); 163 } catch(java.io.IOException e) { 164 logln("Error: " + e.getMessage()); 165 } 166 } catch(Exception e) { 167 if (out != null) { 168 PrintWriter w = new PrintWriter (out); 169 w.println("exception while trying to find url ("); 170 w.println(arguments[1]); 171 w.println("):"); 172 e.printStackTrace(w); 173 } 174 } 175 } 176 177 Settings parseArguments(String [] arguments, String position) 178 throws IllegalArgumentException 179 { 180 Settings settings = new Settings(); 181 String err; 182 String item = null; 183 try { 184 item = "position"; 185 settings.position = Integer.parseInt(position); 186 item = "link page regex"; 187 settings.linkpage = arguments[1]; 188 int commapos = arguments[2].indexOf(','); 189 int colonpos = arguments[2].indexOf(':'); 190 settings.link_group = Integer.parseInt(arguments[2].substring(0, commapos)); 191 settings.description_group = Integer.parseInt( 192 arguments[2].substring(commapos+1, colonpos)); 193 settings.linkpage_regex = arguments[2].substring(colonpos+1); 194 item = "image page regex"; 195 colonpos = arguments[3].indexOf(':'); 196 settings.img_group = Integer.parseInt(arguments[3].substring(0, colonpos)); 197 settings.imgpage_regex = arguments[3].substring(colonpos+1); 198 199 if(arguments[4].equals("plain")) 200 settings.sortmode = plain; 201 else if(arguments[4].equals("reverse")) 202 settings.sortmode = reverse; 203 else if(arguments[4].equals("sort")) 204 settings.sortmode = sort; 205 else { 206 err = "unsupported ordering method '" 207 + arguments[4] + "'."; 208 throw new IllegalArgumentException (err); 209 } 210 211 if (arguments.length > 5) 213 settings.startText = arguments[5]; 214 if (arguments.length > 6) 215 settings.endText = arguments[6]; 216 217 if(arguments.length > 7) 218 err = " too many arguments."; 219 else 220 return settings; 221 } catch(Exception e) { 222 err = item + " argument is invalid."; 223 e.printStackTrace(); 224 } 225 throw new IllegalArgumentException (err); 226 } 227 228 ArrayList findAllPatternMatches(String text, String patternString, int[] groups) 231 { 232 Pattern pattern = Pattern.compile(patternString); 234 Matcher matcher = pattern.matcher(text); 235 ArrayList array = new ArrayList (); 236 while(matcher.find()) { 237 String [] res = new String [groups.length]; 238 for(int g=0; g!=groups.length; g++) 239 res[g] = matcher.group(groups[g]); 240 array.add(res); 242 } 243 return array; 244 } 245 } 246 247 248 | Popular Tags |