1 32 33 package websphinx; 34 35 import rcm.util.Str; 36 import java.io.*; 37 import java.net.URL ; 38 import java.net.MalformedURLException ; 39 import java.util.Hashtable ; 40 41 65 66 public class Concatenator extends RewritableLinkTransformer { 68 69 boolean needRewrite = false; 70 71 public static String defaultProlog = "<HTML><HEAD><TITLE>Concatenation</TITLE></HEAD><BODY>\n"; 72 public static String defaultHeader = "<TABLE WIDTH=\"100%\"><TR>\n" 73 +"<TD ALIGN=left><A NAME=\"%a\">%t [%u]</A>\n" 74 +"<TD ALIGN=right>Page %p</TABLE>\n"; 75 public static String defaultFooter = ""; 76 public static String defaultDivider = "\n<DIV STYLE=\"page-break-after: always;\"><HR></DIV>\n"; 77 public static String defaultEpilog = "\n</BODY></HTML>\n"; 78 79 String prolog = defaultProlog; 80 String header = defaultHeader; 81 String footer = defaultFooter; 82 String divider = defaultDivider; 83 String epilog = defaultEpilog; 84 85 int nPages = 0; 86 87 92 public Concatenator (String filename) throws IOException { 93 super (makeDirs(filename)); 94 } 95 96 private static String makeDirs (String filename) throws IOException { 97 File file = new File (filename); 98 File parent = new File (file.getParent ()); 99 if (parent != null) 100 Access.getAccess ().makeDir (parent); 101 return filename; 102 } 103 104 105 111 public synchronized void setProlog (String prolog) { 112 this.prolog = prolog; 113 } 114 119 public String getProlog () { 120 return prolog; 121 } 122 123 142 public synchronized void setPageHeader (String header) { 143 this.header = header; 144 } 145 150 public String getPageHeader () { 151 return header; 152 } 153 154 161 public synchronized void setPageFooter (String footer) { 162 this.footer = footer; 163 } 164 169 public String getPageFooter () { 170 return footer; 171 } 172 177 public synchronized void setDivider (String divider) { 178 this.divider = divider; 179 } 180 185 public String getDivider () { 186 return divider; 187 } 188 189 194 public synchronized void setEpilog (String epilog) { 195 this.epilog = epilog; 196 } 197 202 public String getEpilog () { 203 return epilog; 204 } 205 206 210 public synchronized int getPageCount () { 211 return nPages; 212 } 213 214 218 public synchronized void rewrite () throws IOException { 219 if (needRewrite) { 220 super.rewrite (); 221 needRewrite = false; 222 } 223 } 224 225 230 public synchronized void close () throws IOException { 231 if (nPages == 0) 232 write (prolog); 233 emit (epilog); 234 rewrite (); 235 super.close (); 236 } 237 238 242 public synchronized void writePage (Page page) throws IOException { 243 ++nPages; 244 245 emit ((nPages == 1) ? prolog : divider); 246 247 String title = page.getTitle (); 248 URL url = page.getURL (); 249 String urlString = url.toExternalForm (); 250 String anchor = "page" + nPages; 251 map (url, "#" + anchor); 252 253 emitTemplate (header, title, urlString, anchor, nPages); 254 if (page.isImage () && page.getURL() != null) 255 super.write ("<IMG SRC='" + page.getURL() + "'>"); 256 else if (page.isHTML()) 257 super.writePage (page); 259 else 260 super.write (page.toHTML()); 261 emitTemplate (footer, title, urlString, anchor, nPages); 262 263 needRewrite = nPages > 1; 264 } 265 266 private void emitTemplate (String template, 267 String title, String url, 268 String anchor, int pages) throws IOException { 269 if (template == null || template.length() == 0) 270 return; 271 272 template = Str.replace (template, "%t", title != null ? title : ""); 273 template = Str.replace (template, "%u", url != null ? url : ""); 274 template = Str.replace (template, "%a", anchor != null ? anchor : ""); 275 template = Str.replace (template, "%p", String.valueOf (pages)); 276 emit (template); 277 } 278 279 287 protected void handleElement (Element elem) throws IOException { 288 String name = elem.getTagName (); 289 if ( name == Tag.TITLE 290 || name == Tag.STYLE 291 || name == Tag.BASE 292 || name == Tag.ISINDEX 293 || name == Tag.FRAMESET 294 || name == Tag.FRAME) { 295 } 297 else if ( name == Tag.HTML 298 || name == Tag.HEAD 299 || name == Tag.BODY 300 || name == Tag.NOFRAMES) { 301 transformContents (elem); 303 } 304 else 305 super.handleElement (elem); 306 } 307 308 314 public static void main (String [] args) throws Exception { 315 HTMLTransformer out = new Concatenator (args[args.length-1]); 316 for (int i=0; i<args.length-1; ++i) { 317 Link link = new Link (args[i]); 318 Page page = new Page (link); 319 out.writePage (page); 320 } 321 out.close (); 322 } 323 } 324 325 | Popular Tags |