1 31 32 package org.opencms.validation; 33 34 import org.opencms.db.CmsDbContext; 35 import org.opencms.db.CmsDbUtil; 36 import org.opencms.db.CmsDriverManager; 37 import org.opencms.file.CmsObject; 38 import org.opencms.file.CmsProject; 39 import org.opencms.file.CmsResource; 40 import org.opencms.file.types.I_CmsResourceType; 41 import org.opencms.main.CmsException; 42 import org.opencms.main.CmsLog; 43 import org.opencms.main.OpenCms; 44 import org.opencms.report.CmsShellReport; 45 import org.opencms.report.I_CmsReport; 46 47 import java.util.ArrayList ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 import java.util.List ; 51 import java.util.Map ; 52 53 import org.apache.commons.logging.Log; 54 55 71 public class CmsXmlDocumentLinkValidator { 72 73 74 private static final Log LOG = CmsLog.getLog(CmsXmlDocumentLinkValidator.class); 75 76 79 protected CmsDriverManager m_driverManager; 80 81 86 public CmsXmlDocumentLinkValidator(CmsDriverManager driverManager) { 87 88 m_driverManager = driverManager; 89 } 90 91 100 public Map validateResources(CmsObject cms, List offlineResources) { 101 102 return validateResources(cms, offlineResources, new CmsShellReport(cms.getRequestContext().getLocale())); 103 } 104 105 115 public Map validateResources(CmsObject cms, List offlineResources, I_CmsReport report) { 116 117 CmsResource resource = null; 118 List brokenLinks = null; 119 Map offlineFilesLookup = null; 120 List links = null; 121 List validatableResources = null; 122 Map invalidResources = new HashMap (); 123 String resourceName = null; 124 int i = CmsDbUtil.UNKNOWN_ID, j = CmsDbUtil.UNKNOWN_ID; 125 I_CmsResourceType resourceType = null; 126 boolean foundBrokenLinks = false; 127 128 report.println(Messages.get().container(Messages.RPT_HTMLLINK_VALIDATOR_BEGIN_0), I_CmsReport.FORMAT_HEADLINE); 129 130 offlineFilesLookup = new HashMap (); 134 validatableResources = new ArrayList (); 135 for (i = 0; i < offlineResources.size(); i++) { 136 resource = (CmsResource)offlineResources.get(i); 137 offlineFilesLookup.put(resource.getRootPath(), resource); 138 139 try { 140 resourceType = OpenCms.getResourceManager().getResourceType(resource.getTypeId()); 141 if ((resourceType instanceof I_CmsXmlDocumentLinkValidatable) 142 && (resource.getState() != CmsResource.STATE_DELETED)) { 143 validatableResources.add(resource); 145 } 146 } catch (CmsException e) { 147 LOG.error(Messages.get().getBundle().key(Messages.LOG_RETRIEVAL_RESOURCE_1, resourceName), e); 148 } 149 } 150 151 foundBrokenLinks = false; 152 for (i = 0, j = validatableResources.size(); i < j; i++) { 153 try { 154 brokenLinks = null; 155 resource = (CmsResource)validatableResources.get(i); 156 resourceName = resource.getRootPath(); 157 resourceType = OpenCms.getResourceManager().getResourceType(resource.getTypeId()); 158 159 report.print(org.opencms.report.Messages.get().container( 160 org.opencms.report.Messages.RPT_SUCCESSION_2, 161 new Integer (i + 1), 162 new Integer (j)), I_CmsReport.FORMAT_NOTE); 163 report.print(Messages.get().container(Messages.RPT_HTMLLINK_VALIDATING_0), I_CmsReport.FORMAT_NOTE); 164 report.print(org.opencms.report.Messages.get().container( 165 org.opencms.report.Messages.RPT_ARGUMENT_1, 166 cms.getRequestContext().removeSiteRoot(resourceName))); 167 report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 168 169 links = ((I_CmsXmlDocumentLinkValidatable)resourceType).findLinks(cms, resource); 170 171 if (links.size() > 0) { 172 brokenLinks = validateLinks(links, offlineFilesLookup); 173 } 174 175 if (brokenLinks != null && brokenLinks.size() > 0) { 176 invalidResources.put(resourceName, brokenLinks); 178 foundBrokenLinks = true; 179 report.println( 180 Messages.get().container(Messages.RPT_HTMLLINK_FOUND_BROKEN_LINKS_0), 181 I_CmsReport.FORMAT_WARNING); 182 } else { 183 report.println( 185 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 186 I_CmsReport.FORMAT_OK); 187 } 188 } catch (CmsException e) { 189 LOG.error(Messages.get().getBundle().key(Messages.LOG_LINK_SEARCH_1, resourceName), e); 190 } 191 } 192 193 if (foundBrokenLinks) { 194 report.println( 196 Messages.get().container(Messages.RPT_BROKEN_LINKS_SUMMARY_BEGIN_0), 197 I_CmsReport.FORMAT_HEADLINE); 198 199 Iterator outer = invalidResources.keySet().iterator(); 200 while (outer.hasNext()) { 201 resourceName = (String )outer.next(); 202 brokenLinks = (List )invalidResources.get(resourceName); 203 204 report.println( 205 Messages.get().container(Messages.RPT_BROKEN_LINKS_IN_1, resourceName), 206 I_CmsReport.FORMAT_WARNING); 207 Iterator inner = brokenLinks.iterator(); 208 while (inner.hasNext()) { 209 report.print(org.opencms.report.Messages.get().container( 210 org.opencms.report.Messages.RPT_ARGUMENT_1, 211 inner.next()), I_CmsReport.FORMAT_WARNING); 212 } 213 report.println(); 214 } 215 216 report.println( 217 Messages.get().container(Messages.RPT_BROKEN_LINKS_SUMMARY_END_0), 218 I_CmsReport.FORMAT_HEADLINE); 219 report.println(Messages.get().container(Messages.RPT_HTMLLINK_VALIDATOR_ERROR_0), I_CmsReport.FORMAT_ERROR); 220 } 221 222 report.println(Messages.get().container(Messages.RPT_HTMLLINK_VALIDATOR_END_0), I_CmsReport.FORMAT_HEADLINE); 223 224 return invalidResources; 225 } 226 227 230 protected void finalize() throws Throwable { 231 232 try { 233 m_driverManager = null; 234 } catch (Throwable t) { 235 } 237 238 super.finalize(); 239 } 240 241 248 protected List validateLinks(List links, Map offlineFileLookup) { 249 250 List brokenLinks = new ArrayList (); 251 String link = null; 252 boolean isValidLink = true; 253 CmsResource unpublishedResource = null; 254 List validatedLinks = new ArrayList (); 255 256 Iterator i = links.iterator(); 257 while (i.hasNext()) { 258 link = ((String )i.next()).trim(); 259 isValidLink = true; 260 261 if (validatedLinks.contains(link) || "".equals(link)) { 262 continue; 264 } 265 266 268 try { 269 m_driverManager.getVfsDriver().readResource( 271 new CmsDbContext(), 272 CmsProject.ONLINE_PROJECT_ID, 273 link, 274 true); 275 276 if (offlineFileLookup.containsKey(link)) { 278 unpublishedResource = (CmsResource)offlineFileLookup.get(link); 279 280 if (unpublishedResource.getState() == CmsResource.STATE_DELETED) { 281 isValidLink = false; 282 } 283 } 284 } catch (CmsException e) { 285 if (!offlineFileLookup.containsKey(link)) { 287 isValidLink = false; 288 } 289 } 290 291 if (!isValidLink) { 292 brokenLinks.add(link); 293 } 294 295 validatedLinks.add(link); 296 } 297 298 return brokenLinks; 299 } 300 } | Popular Tags |