1 package org.apache.maven.plugin.linkcheck;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.maven.plugin.linkcheck.validation.LinkValidationItem;
24 import org.apache.maven.plugin.linkcheck.validation.LinkValidationResult;
25 import org.apache.maven.plugin.linkcheck.validation.LinkValidatorManager;
26
27 import java.io.File;
28 import java.util.Iterator;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.Set;
32
33 /**
34 * @author <a href="mailto:bwalding@apache.org">Ben Walding</a>
35 * @author <a href="mailto:aheritier@apache.org">Arnaud Heritier</a>
36 * @version $Id: FileToCheck.java 532339 2007-04-25 12:28:56Z ltheussl $
37 */
38 public final class FileToCheck
39 {
40 /**
41 * Log for debug output
42 */
43 private static final Log LOG = LogFactory.getLog( FileToCheck.class );
44
45 public static final String STATUS_UNKNOWN = null;
46
47 public static final String STATUS_JTIDY_FAILURE = "Unable to tidy source";
48
49 public static final String STATUS_OK = "OK";
50
51 private String base;
52
53 private File fileToCheck;
54
55 private List links = new LinkedList();
56
57 private String message = "";
58
59 private String status = STATUS_OK;
60
61 private int successful;
62
63 private int unsuccessful;
64
65 /**
66 * Returns the message.
67 *
68 * @return String
69 */
70 public String getMessage()
71 {
72 return this.message;
73 }
74
75 public String getName()
76 {
77 String fileName = this.fileToCheck.getAbsolutePath();
78 if ( fileName.startsWith( this.base ) )
79 {
80 fileName = fileName.substring( this.base.length() + 1 );
81 }
82
83 fileName = fileName.replace( '\\', '/' );
84 return fileName;
85 }
86
87 public List getResults()
88 {
89 return this.links;
90 }
91
92 /**
93 * Returns the status.
94 *
95 * @return int
96 */
97 public String getStatus()
98 {
99 return this.status;
100 }
101
102 /**
103 * Returns the successful.
104 *
105 * @return int
106 */
107 public int getSuccessful()
108 {
109 return this.successful;
110 }
111
112 /**
113 * Returns the unsuccessful.
114 *
115 * @return int
116 */
117 public int getUnsuccessful()
118 {
119 return this.unsuccessful;
120 }
121
122 public FileToCheck( File baseFile, File fileToCheck )
123 {
124 this.base = baseFile.getAbsolutePath();
125 this.fileToCheck = fileToCheck;
126 }
127
128 public void check( LinkValidatorManager lvm ) throws Exception
129 {
130 this.successful = 0;
131 this.unsuccessful = 0;
132 this.status = STATUS_OK;
133 this.message = "";
134 if ( LOG.isDebugEnabled() )
135 {
136 LOG.debug( "Validating " + getName() );
137 }
138 try
139 {
140 final Set hrefs;
141 try
142 {
143 hrefs = LinkMatcher.match( this.fileToCheck );
144 }
145 catch ( Throwable t )
146 {
147
148
149 if ( LOG.isDebugEnabled() )
150 {
151 LOG.error( "Received: [" + t + "] in page [" + getName() + "]", t );
152 }
153 else
154 {
155 LOG.error( "Received: [" + t + "] in page [" + getName() + "]" );
156 }
157 LinkCheckResult lcr = new LinkCheckResult();
158 lcr.setStatus( "PARSE FAILURE" );
159 lcr.setTarget( "N/A" );
160 addResult( lcr );
161 return;
162 }
163 String href;
164 LinkCheckResult lcr;
165 LinkValidationItem lvi;
166 LinkValidationResult result;
167 for ( Iterator iter = hrefs.iterator(); iter.hasNext(); )
168 {
169 href = (String) iter.next();
170 lcr = new LinkCheckResult();
171 lvi = new LinkValidationItem( this.fileToCheck, href );
172 result = lvm.validateLink( lvi );
173 lcr.setTarget( href );
174 lcr.setErrorMessage( result.getErrorMessage() );
175 switch ( result.getStatus() )
176 {
177 case LinkValidationResult.VALID:
178 this.successful++;
179 lcr.setStatus( "valid" );
180 addResult( lcr );
181
182 break;
183 case LinkValidationResult.UNKNOWN:
184 this.unsuccessful++;
185 lcr.setStatus( "unknown" );
186 addResult( lcr );
187 break;
188 case LinkValidationResult.ERROR:
189 this.unsuccessful++;
190 lcr.setStatus( "error" );
191 addResult( lcr );
192 break;
193 case LinkValidationResult.WARNING:
194 this.unsuccessful++;
195 lcr.setStatus( "warning" );
196 addResult( lcr );
197 break;
198 }
199 }
200 href = null;
201 lcr = null;
202 lvi = null;
203 result = null;
204
205 }
206 catch ( Exception e )
207 {
208 LOG.error( this.message );
209 throw e;
210 }
211 }
212
213 public String toXML()
214 {
215 StringBuffer buf = new StringBuffer();
216 buf.append( " <file>\n" );
217 buf.append( " <name><![CDATA[" + getName() + "]]></name>\n" );
218 buf.append( " <successful>" + getSuccessful() + "</successful>\n" );
219 buf.append( " <unsuccessful>" + getUnsuccessful() + "</unsuccessful>\n" );
220 Iterator iter = getResults().iterator();
221 LinkCheckResult result;
222 while ( iter.hasNext() )
223 {
224 result = (LinkCheckResult) iter.next();
225 buf.append( result.toXML() );
226 }
227 buf.append( " </file>\n" );
228 return buf.toString();
229 }
230
231 private void addResult( LinkCheckResult lcr )
232 {
233 this.links.add( lcr );
234 }
235 }