1 17 package org.apache.ws.jaxme.js.util; 18 19 import java.io.File ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.apache.ws.jaxme.logging.AntProjectLoggerFactory; 27 import org.apache.ws.jaxme.logging.LoggerAccess; 28 import org.apache.ws.jaxme.logging.LoggerFactory; 29 import org.apache.tools.ant.Project; 30 import org.apache.tools.ant.TaskAdapter; 31 32 35 public class AntLinkChecker extends TaskAdapter { 36 public class CheckedURL { 37 private URL name; 38 public void setName(URL pName) { 39 if (name != null) { 40 throw new IllegalStateException ("An 'URL' element must have exactly one 'name' or 'file' attribute. These attributes are mutually exclusive."); 41 } 42 name = pName; 43 } 44 public void setFile(File pName) throws MalformedURLException { 45 if (name != null) { 46 throw new IllegalStateException ("An 'URL' element must have exactly one 'name' or 'file' attribute. These attributes are mutually exclusive."); 47 } 48 name = pName.toURL(); 49 } 50 public URL getName() { 51 return name; 52 } 53 public void finish() { 54 if (name == null) { 55 throw new IllegalStateException ("An 'URL' element must have exactly one 'name' or 'file' attribute."); 56 } 57 } 58 } 59 60 public class MyLinkChecker extends LinkChecker { 61 private boolean isSettingLoggerFactory = true; 62 public CheckedURL createURL() { 63 CheckedURL url = new CheckedURL(); 64 AntLinkChecker.this.addURL(url); 65 return url; 66 } 67 public boolean isSettingLoggerFactory() { 68 return isSettingLoggerFactory; 69 } 70 public void setSettingLoggerFactory(boolean pSettingLoggerFactory) { 71 isSettingLoggerFactory = pSettingLoggerFactory; 72 } 73 } 74 75 private MyLinkChecker checker = new MyLinkChecker(); 76 private List urls = new ArrayList (); 77 78 void addURL(CheckedURL pURL) { 79 urls.add(pURL); 80 } 81 82 public AntLinkChecker() { 83 setProxy(checker); 84 } 85 86 protected LoggerFactory initLogging() { 87 if (!checker.isSettingLoggerFactory()) { 88 return null; 89 } 90 LoggerFactory loggerFactory = LoggerAccess.getLoggerFactory(); 91 if (loggerFactory instanceof AntProjectLoggerFactory) { 92 return null; 93 } 94 LoggerAccess.setLoggerFactory(new AntProjectLoggerFactory(this)); 95 return loggerFactory; 96 } 97 98 protected void stopLogging(LoggerFactory pLoggerFactory) { 99 if (pLoggerFactory != null) { 100 LoggerAccess.setLoggerFactory(pLoggerFactory); 101 } 102 } 103 104 public void execute() { 105 log("execute: ->", Project.MSG_VERBOSE); 106 LoggerFactory loggerFactory = initLogging(); 107 checker.setLogger(LoggerAccess.getLogger(LinkChecker.class)); 108 try { 109 for (Iterator iter = urls.iterator(); iter.hasNext(); ) { 110 CheckedURL url = (CheckedURL) iter.next(); 111 checker.addURL(url.getName()); 112 } 113 checker.parse(); 114 } finally { 115 stopLogging(loggerFactory); 116 } 117 log("execute: <-", Project.MSG_VERBOSE); 118 } 119 } 120 | Popular Tags |