1 17 18 19 20 package org.apache.lenya.cms.publication.xsp; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Map ; 26 import java.util.regex.Pattern ; 27 28 import org.apache.cocoon.ProcessingException; 29 import org.apache.lenya.cms.publication.Document; 30 import org.apache.lenya.cms.publication.DocumentBuildException; 31 import org.apache.lenya.cms.publication.DocumentBuilder; 32 import org.apache.lenya.cms.publication.DocumentDoesNotExistException; 33 import org.apache.lenya.cms.publication.DocumentIdToPathMapper; 34 import org.apache.lenya.cms.publication.PageEnvelope; 35 import org.apache.lenya.cms.publication.PageEnvelopeException; 36 import org.apache.lenya.cms.publication.PageEnvelopeFactory; 37 import org.apache.lenya.cms.publication.PathToDocumentIdMapper; 38 import org.apache.lenya.cms.publication.Publication; 39 import org.apache.lenya.cms.publication.SiteTree; 40 import org.apache.lenya.cms.publication.SiteTreeException; 41 import org.apache.lenya.cms.publication.SiteTreeNode; 42 import org.apache.lenya.search.Grep; 43 import org.apache.log4j.Category; 44 45 48 public class DocumentReferencesHelper { 49 50 private static final Category log = Category.getInstance(DocumentReferencesHelper.class); 51 52 private PageEnvelope pageEnvelope = null; 53 54 61 public DocumentReferencesHelper(Map objectModel) 62 throws ProcessingException { 63 try { 64 this.pageEnvelope = 65 PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel); 66 } catch (PageEnvelopeException e) { 67 throw new ProcessingException(e); 68 } 69 } 70 71 80 protected String getReferencesSearchString() { 81 return "href\\s*=\\s*\"" 82 + pageEnvelope.getContext() 83 + "/" 84 + pageEnvelope.getPublication().getId() 85 + "/" 86 + pageEnvelope.getDocument().getArea() 87 + pageEnvelope.getDocument().getId(); 88 } 89 90 99 protected Pattern getInternalLinkPattern() { 100 103 111 115 return Pattern.compile( 116 "href\\s*=\\s*\"" 117 + pageEnvelope.getContext() 118 + "/" 119 + pageEnvelope.getPublication().getId() 120 + "/" 121 + pageEnvelope.getDocument().getArea() 122 + "(/[-a-zA-Z0-9_/]+?)(_[a-z][a-z])?\\.html"); 123 } 124 125 134 public Document[] getReferences(String area) throws ProcessingException { 135 136 ArrayList documents = new ArrayList (); 137 Publication publication = pageEnvelope.getPublication(); 138 DocumentIdToPathMapper mapper = publication.getPathMapper(); 139 if (mapper instanceof PathToDocumentIdMapper) { 140 PathToDocumentIdMapper fileMapper = (PathToDocumentIdMapper)mapper; 141 String documentId = null; 142 String language = null; 143 DocumentBuilder builder = publication.getDocumentBuilder(); 144 File [] inconsistentFiles; 145 try { 146 inconsistentFiles = 147 Grep.find( 148 publication.getContentDirectory(area), 149 getReferencesSearchString()); 150 for (int i = 0; i < inconsistentFiles.length; i++) { 151 String languageOfCurrentDocument = 156 pageEnvelope.getDocument().getLanguage(); 157 String defaultLanguage = 158 pageEnvelope.getPublication().getDefaultLanguage(); 159 Pattern referencesSearchStringWithLanguage = 160 Pattern.compile( 161 getReferencesSearchString() 162 + "_" 163 + languageOfCurrentDocument); 164 Pattern referencesSearchStringWithOutLanguage = 165 Pattern.compile( 166 getReferencesSearchString() + "\\.html"); 167 log.debug( 168 "languageOfCurrentDocument: " 169 + languageOfCurrentDocument); 170 log.debug("defaultLanguage: " + defaultLanguage); 171 log.debug( 172 "referencesSearchStringWithOutLanguage: " 173 + referencesSearchStringWithOutLanguage.pattern()); 174 log.debug( 175 "referencesSearchStringWithLanguage: " 176 + referencesSearchStringWithLanguage.pattern()); 177 if (!Grep 186 .containsPattern( 187 inconsistentFiles[i], 188 referencesSearchStringWithLanguage) 189 && !(Grep 190 .containsPattern( 191 inconsistentFiles[i], 192 referencesSearchStringWithOutLanguage) 193 && languageOfCurrentDocument.equals( 194 defaultLanguage))) { 195 continue; 201 } 202 203 documentId = 204 fileMapper.getDocumentId( 205 publication, 206 area, 207 inconsistentFiles[i]); 208 log.debug("documentId: " + documentId); 209 language = fileMapper.getLanguage(inconsistentFiles[i]); 210 log.debug("language: " + language); 211 212 String url = null; 213 if (language != null) { 214 url = 215 builder.buildCanonicalUrl( 216 publication, 217 area, 218 documentId, 219 language); 220 log.debug("url: " + url); 221 } else { 222 url = 223 builder.buildCanonicalUrl( 224 publication, 225 area, 226 documentId); 227 log.debug("url: " + url); 228 } 229 documents.add(builder.buildDocument(publication, url)); 230 } 231 } catch (IOException e) { 232 throw new ProcessingException(e); 233 } catch (DocumentDoesNotExistException e) { 234 throw new ProcessingException(e); 235 } catch (DocumentBuildException e) { 236 throw new ProcessingException(e); 237 } 238 } 239 return (Document[])documents.toArray(new Document[documents.size()]); 240 } 241 242 251 public Document[] getInternalReferences() throws ProcessingException { 252 ArrayList unpublishedReferences = new ArrayList (); 253 SiteTree sitetree; 254 Pattern internalLinkPattern = getInternalLinkPattern(); 255 Publication publication = pageEnvelope.getPublication(); 256 DocumentBuilder builder = publication.getDocumentBuilder(); 257 try { 258 sitetree = publication.getTree(Publication.LIVE_AREA); 259 String [] internalLinks = 260 Grep.findPattern( 261 pageEnvelope.getDocument().getFile(), 262 internalLinkPattern, 263 1); 264 String [] internalLinksLanguages = 265 Grep.findPattern( 266 pageEnvelope.getDocument().getFile(), 267 internalLinkPattern, 268 2); 269 270 for (int i = 0; i < internalLinks.length; i++) { 271 String docId = internalLinks[i]; 272 String language = null; 273 274 log.debug("docId: " + docId); 275 if (internalLinksLanguages[i] != null) { 276 language = internalLinksLanguages[i].substring(1); 278 } 279 280 log.debug("language: " + language); 281 SiteTreeNode documentNode = sitetree.getNode(docId); 282 283 if (language == null) { 284 String url = 285 "/" 286 + publication.getId() 287 + "/" 288 + pageEnvelope.getDocument().getArea() 289 + docId 290 + ".html"; 291 language = 292 builder.buildDocument(publication, url).getLanguage(); 293 } 294 log.debug("language: " + language); 295 if (documentNode == null 296 || documentNode.getLabel(language) == null) { 297 String url = null; 299 if (language != null) { 300 url = 301 builder.buildCanonicalUrl( 302 publication, 303 Publication.AUTHORING_AREA, 304 docId, 305 language); 306 log.debug("url: " + url); 307 } else { 308 url = 309 builder.buildCanonicalUrl( 310 publication, 311 Publication.AUTHORING_AREA, 312 docId); 313 log.debug("url: " + url); 314 } 315 unpublishedReferences.add( 316 builder.buildDocument(publication, url)); 317 } 318 } 319 } catch (SiteTreeException e) { 320 throw new ProcessingException(e); 321 } catch (IOException e) { 322 throw new ProcessingException(e); 323 } catch (DocumentBuildException e) { 324 throw new ProcessingException(e); 325 } 326 return (Document[])unpublishedReferences.toArray( 327 new Document[unpublishedReferences.size()]); 328 } 329 } 330 | Popular Tags |