KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > js > util > AntLinkChecker


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.js.util;
18
19 import java.io.File JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
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 /**
33  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
34  */

35 public class AntLinkChecker extends TaskAdapter {
36   public class CheckedURL {
37     private URL JavaDoc name;
38     public void setName(URL JavaDoc pName) {
39       if (name != null) {
40         throw new IllegalStateException JavaDoc("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 JavaDoc pName) throws MalformedURLException JavaDoc {
45       if (name != null) {
46         throw new IllegalStateException JavaDoc("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 JavaDoc getName() {
51       return name;
52     }
53     public void finish() {
54       if (name == null) {
55         throw new IllegalStateException JavaDoc("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 JavaDoc urls = new ArrayList JavaDoc();
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 JavaDoc 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