KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > publication > xsp > DocumentReferencesHelper


1 /*
2  * Copyright 1999-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
18 /* $Id: DocumentReferencesHelper.java 160151 2005-04-05 09:59:13Z michi $ */
19
20 package org.apache.lenya.cms.publication.xsp;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
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 /**
46  * Helper class for finding references to the current document.
47  */

48 public class DocumentReferencesHelper {
49
50     private static final Category log = Category.getInstance(DocumentReferencesHelper.class);
51
52     private PageEnvelope pageEnvelope = null;
53
54     /**
55      * Create a new DocumentReferencesHelper
56      *
57      * @param objectModel the objectModel
58      *
59      * @throws ProcessingException if the page envelope could not be created.
60      */

61     public DocumentReferencesHelper(Map JavaDoc 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     /**
72      * Construct a search string for the search of references, i.e.
73      * links from other documents to the current document. This
74      * is done using the assumption that internal links look as if
75      * they were copied directly from the browser,
76      * e.g. /lenya/default/authoring/doctypes/2columns.html
77      *
78      * @return the search string
79      */

80     protected String JavaDoc 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     /**
91      * Construct a search string for the search of internal references,
92      * i.e from the current document to others. This is done using
93      * the assumption that internal links look as if they were copied
94      * directly from the browser, e.g.
95      * /lenya/default/authoring/doctypes/2columns.html
96      *
97      * @return the search string
98      */

99     protected Pattern JavaDoc getInternalLinkPattern() {
100         // FIXME: The following method is not very robust and certainly
101
// will fail if the mapping between URL and document-id changes
102

103         // Link Management now assumes that internal links are of the
104
// form
105
// HREF="$CONTEXT_PREFIX/$PUBLICATION_ID/$AREA$DOCUMENT_ID(_[a-z][a-z])?.html
106
// If there is a match in a document file it is assumed that
107
// this is an internal link and is treated as such (warning if
108
// publish with unpublished internal links and warning if
109
// deactivate with internal references).
110

111         // However this is not coordinated with the
112
// DocumentToPathMapper and will probably fail if the URL
113
// looks different.
114

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     /**
126      * Find a list of document-ids which have references to the current
127      * document.
128      *
129      * @return an <code>array</code> of documents if there are references,
130      * an empty <code>array</code> otherwise
131      *
132      * @throws ProcessingException if the search for references failed.
133      */

134     public Document[] getReferences(String JavaDoc area) throws ProcessingException {
135
136         ArrayList JavaDoc documents = new ArrayList JavaDoc();
137         Publication publication = pageEnvelope.getPublication();
138         DocumentIdToPathMapper mapper = publication.getPathMapper();
139         if (mapper instanceof PathToDocumentIdMapper) {
140             PathToDocumentIdMapper fileMapper = (PathToDocumentIdMapper)mapper;
141             String JavaDoc documentId = null;
142             String JavaDoc language = null;
143             DocumentBuilder builder = publication.getDocumentBuilder();
144             File JavaDoc[] inconsistentFiles;
145             try {
146                 inconsistentFiles =
147                     Grep.find(
148                         publication.getContentDirectory(area),
149                         getReferencesSearchString());
150                 for (int i = 0; i < inconsistentFiles.length; i++) {
151                     // for performance reasons the getReferencesSearchString() is
152
// constructed in a way such that it will catch all files which
153
// have a link to any language version of the current document.
154
// That's why we need to do some additional tests for each hit.
155
String JavaDoc languageOfCurrentDocument =
156                         pageEnvelope.getDocument().getLanguage();
157                     String JavaDoc defaultLanguage =
158                         pageEnvelope.getPublication().getDefaultLanguage();
159                     Pattern JavaDoc referencesSearchStringWithLanguage =
160                         Pattern.compile(
161                             getReferencesSearchString()
162                                 + "_"
163                                 + languageOfCurrentDocument);
164                     Pattern JavaDoc 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                     // a link is indeed to the current document if the following conditions
178
// are met:
179
// 1. the link is to foo_xx and the language of the current
180
// document is xx.
181
// 2. or the link is to foo.html and the language of the current
182
// document is the default language.
183
// Now negate the expression because we continue if above (1) and (2) are
184
// false, and you'll get the following if statement
185
if (!Grep
186                         .containsPattern(
187                             inconsistentFiles[i],
188                             referencesSearchStringWithLanguage)
189                         && !(Grep
190                             .containsPattern(
191                                 inconsistentFiles[i],
192                                 referencesSearchStringWithOutLanguage)
193                             && languageOfCurrentDocument.equals(
194                                 defaultLanguage))) {
195                         // the reference foo_xx is neither to the language of the current
196
// document.
197
// nor is the reference foo.html and the current document is in the
198
// default language.
199
// So the reference is of no importance to us, skip
200
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 JavaDoc 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 JavaDoc 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     /**
243      * Find all internal references in the current document to documents which have
244      * not been published yet.
245      *
246      * @return an <code>array</code> of <code>Document</code> of references
247      * from the current document to documents which have not been published yet.
248      *
249      * @throws ProcessingException if the current document cannot be opened.
250      */

251     public Document[] getInternalReferences() throws ProcessingException {
252         ArrayList JavaDoc unpublishedReferences = new ArrayList JavaDoc();
253         SiteTree sitetree;
254         Pattern JavaDoc internalLinkPattern = getInternalLinkPattern();
255         Publication publication = pageEnvelope.getPublication();
256         DocumentBuilder builder = publication.getDocumentBuilder();
257         try {
258             sitetree = publication.getTree(Publication.LIVE_AREA);
259             String JavaDoc[] internalLinks =
260                 Grep.findPattern(
261                     pageEnvelope.getDocument().getFile(),
262                     internalLinkPattern,
263                     1);
264             String JavaDoc[] internalLinksLanguages =
265                 Grep.findPattern(
266                     pageEnvelope.getDocument().getFile(),
267                     internalLinkPattern,
268                     2);
269
270             for (int i = 0; i < internalLinks.length; i++) {
271                 String JavaDoc docId = internalLinks[i];
272                 String JavaDoc language = null;
273
274                 log.debug("docId: " + docId);
275                 if (internalLinksLanguages[i] != null) {
276                     // trim the leading '_'
277
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 JavaDoc 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                     // the docId has not been published for the given language
298
String JavaDoc 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 JavaDoc 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