1 package org.apache.maven.linkcheck.validation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.io.BufferedInputStream;
21 import java.io.BufferedOutputStream;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.Iterator;
28 import java.util.LinkedList;
29 import java.util.List;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 /***
35 * @author <a href="mailto:bwalding@apache.org">Ben Walding</a>
36 * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
37 * @version $Id: LinkValidatorManager.java 170200 2005-05-15 06:24:19Z brett $
38 */
39
40 public class LinkValidatorManager {
41 /***
42 * Log for debug output
43 */
44 private static Log LOG = LogFactory.getLog(LinkValidatorManager.class);
45
46 private List validators = new LinkedList();
47 private LinkValidatorCache cache = new LinkValidatorCache(this);
48 private String[] excludes = new String[0];
49
50 public LinkValidatorManager() {
51
52 }
53
54 public void addLinkValidator(LinkValidator lv) {
55 validators.add(lv);
56 }
57
58 public List getValidators() {
59 return validators;
60 }
61
62 public LinkValidationResult validateLink(LinkValidationItem lvi)
63 throws Exception {
64 {
65 LinkValidationResult status = cache.getCachedResult(lvi);
66 if (status != null) {
67 return status;
68 }
69 }
70
71 for (int i = 0; i < excludes.length; i++) {
72 if (excludes[i] != null && lvi.getLink().startsWith(excludes[i])) {
73 if (LOG.isDebugEnabled()) {
74 LOG.debug("Excluded " + lvi.getLink());
75 }
76 return new LinkValidationResult(LinkValidationResult.VALID, false);
77 }
78 }
79
80 Iterator iter = validators.iterator();
81 while (iter.hasNext()) {
82 LinkValidator lv = (LinkValidator) iter.next();
83
84 Object resourceKey = lv.getResourceKey(lvi);
85
86 if (resourceKey != null) {
87
88 LinkValidationResult lvr = lv.validateLink(lvi);
89
90 if (lvr.getStatus() == LinkValidationResult.NOTMINE) {
91 continue;
92 }
93
94 cache.setCachedResult(resourceKey, lvr);
95 return lvr;
96
97 }
98
99 }
100
101 LOG.info("Unable to validate link : " + lvi.getLink());
102 return new LinkValidationResult(LinkValidationResult.UNKNOWN, false);
103 }
104
105 public void loadCache(String cacheFilename) {
106 try {
107 File f = new File(cacheFilename);
108 if (f.exists()) {
109 BufferedInputStream is = new BufferedInputStream(
110 new FileInputStream(cacheFilename));
111 this.cache.load(is);
112 is.close();
113 }
114 } catch (FileNotFoundException e) {
115 e.printStackTrace();
116 } catch (IOException e) {
117 e.printStackTrace();
118 }
119 }
120
121 public void saveCache(String cacheFilename) {
122 try {
123 File cacheFile = new File(cacheFilename);
124 File dir = cacheFile.getParentFile();
125 if (dir != null) {
126 dir.mkdirs();
127 }
128 BufferedOutputStream os = new BufferedOutputStream(
129 new FileOutputStream(cacheFilename));
130 this.cache.save(os);
131 os.close();
132 } catch (FileNotFoundException e) {
133 e.printStackTrace();
134 } catch (IOException e) {
135 e.printStackTrace();
136 }
137 }
138
139 /***
140 * Returns the exclude.
141 * @return String
142 * @deprecated use getExcludes()
143 */
144 public String getExclude() {
145 return excludes[0];
146 }
147
148 /***
149 * Sets the exclude.
150 * @param exclude The exclude to set
151 * @deprecated use setExcludes()
152 */
153 public void setExclude(String exclude) {
154 this.excludes = new String[] {exclude};
155 }
156
157 /***
158 * Returns the excludes.
159 * @return String[]
160 */
161 public String[] getExcludes() {
162 return excludes;
163 }
164
165 /***
166 * Sets the excludes.
167 * @param excludes The excludes to set
168 */
169 public void setExcludes(String[] excludes) {
170 this.excludes = excludes;
171 }
172
173 }