KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > bean > Target


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 package org.apache.cocoon.bean;
17
18 import java.util.TreeMap JavaDoc;
19
20 import org.apache.cocoon.Constants;
21 import org.apache.cocoon.util.MIMEUtils;
22 import org.apache.cocoon.util.NetUtils;
23 import org.apache.cocoon.ProcessingException;
24
25 /**
26  * A Target is a single page for generation. It encapsulates the URI
27  * arithmetic required to transform the URI of the page to be generated
28  * (the source URI) into the URI to which the resulting page should be
29  * written (the destination URI).
30  *
31  * @author <a HREF="mailto:uv@upaya.co.uk">Upayavira</a>
32  * @version CVS $Id: Target.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

34 public class Target {
35     // Defult type is append
36
private static final String JavaDoc APPEND_TYPE = "append";
37     private static final String JavaDoc REPLACE_TYPE = "replace";
38     private static final String JavaDoc INSERT_TYPE = "insert";
39
40     private final String JavaDoc type;
41     private final String JavaDoc root;
42     private final String JavaDoc sourceURI;
43     private final String JavaDoc destURI;
44     private final String JavaDoc deparameterizedSourceURI;
45     private final TreeMap JavaDoc parameters;
46     
47     private String JavaDoc parentURI = null;
48     private String JavaDoc originalURI = null;
49     private String JavaDoc mimeType = null;
50     private String JavaDoc defaultFilename = Constants.INDEX_URI;
51     private String JavaDoc finalDestinationURI = null;
52     private String JavaDoc extension = null;
53
54     private boolean followLinks;
55     private boolean confirmExtension;
56     private String JavaDoc logger;
57                  
58     private transient int _hashCode;
59     private transient String JavaDoc _toString;
60
61     public Target(String JavaDoc type,
62                   String JavaDoc root,
63                   String JavaDoc sourceURI,
64                   String JavaDoc destURI)
65     throws IllegalArgumentException JavaDoc {
66         this.type = type;
67         this.root = root;
68         
69         if (destURI == null || destURI.length() == 0) {
70             throw new IllegalArgumentException JavaDoc("You must specify a destination directory when defining a target");
71         }
72         if (!destURI.endsWith("/")) {
73             destURI += "/";
74         }
75         this.destURI = destURI;
76         
77         this.parameters = new TreeMap JavaDoc();
78         
79         // Normalize sourceURI, and make sure that parameters is always in the same order
80
sourceURI = NetUtils.normalize(root + sourceURI);
81         this.deparameterizedSourceURI = NetUtils.deparameterize(sourceURI, this.parameters);
82         this.sourceURI = NetUtils.parameterize(this.deparameterizedSourceURI, this.parameters);
83     }
84
85     public Target(String JavaDoc type, String JavaDoc sourceURI, String JavaDoc destURI)
86         throws IllegalArgumentException JavaDoc {
87         this(type, "", sourceURI, destURI);
88     }
89
90     public Target(String JavaDoc sourceURI, String JavaDoc destURI)
91         throws IllegalArgumentException JavaDoc {
92         this(APPEND_TYPE, "", sourceURI, destURI);
93     }
94
95     public Target getDerivedTarget(String JavaDoc originalLinkURI)
96         throws IllegalArgumentException JavaDoc {
97
98         String JavaDoc linkURI = originalLinkURI;
99         // Fix relative links starting with "?"
100
if (linkURI.startsWith("?")) {
101             linkURI = this.getPageURI() + linkURI;
102         }
103         linkURI =
104             NetUtils.normalize(NetUtils.absolutize(this.getPath(), linkURI));
105
106         // Ignore pages outside the root folder
107
if (!linkURI.startsWith(this.root)) {
108             return null;
109         }
110         linkURI = linkURI.substring(root.length());
111         
112         Target target = new Target(this.type, this.root, linkURI, this.destURI);
113         target.setOriginalURI(originalLinkURI);
114         target.setParentURI(this.sourceURI);
115         target.setConfirmExtension(this.confirmExtension);
116         target.setFollowLinks(this.followLinks);
117         target.setDefaultFilename(this.defaultFilename);
118         target.setLogger(this.logger);
119         return target;
120     }
121
122     /**
123      * Sets the original URI. This is used to record the URI that
124      * caused the creation of this Target, for example as a link
125      * in another page. It is needed for doing link translation, as
126      * this is the URI that must be replaced by the translated one.
127      */

128     public void setOriginalURI(String JavaDoc uri) {
129         this.originalURI = uri;
130     }
131     
132     /**
133      * Sets the URI of the page that contained the link to this
134      * URI. Used for reporting purposes.
135      */

136     public void setParentURI(String JavaDoc uri) {
137         this.parentURI = uri;
138     }
139     
140     /**
141      * Sets the mime type for the resource referenced by this target.
142      * If a mime type is specified, the file extension of the
143      * destination URI will be checked to see that it matches the
144      * default extension for the specified mime type. If it doesn't,
145      * the default extension will be appended to the destination URI.
146      *
147      * This URI change will be taken into account in pages that link
148      * to the current page.
149      *
150      * If the mime type is not specified (and thus null), no extension
151      * checking will take place.
152      */

153     public void setMimeType(String JavaDoc mimeType) {
154         this.mimeType = mimeType;
155         this.finalDestinationURI = null;
156     }
157     
158     /**
159      * Sets a file extension to be appended to the end of the destination
160      * URI. The main use of this is to create broken link error files that
161      * stand out, within the file structure of the generated site, by, for
162      * example, adding '.error' to the end of the filename.
163      */

164     public void setExtraExtension(String JavaDoc extension) {
165         this.extension = extension;
166         this.finalDestinationURI = null;
167     }
168     /**
169      * Sets the default filename. This filename is appended to URIs
170      * that refer to a directory, i.e. end with a slash, as resources
171      * referred to by such a URI cannot be written to a file system
172      * without a filename.
173      *
174      * This URI change will be taken into account in pages that link
175      * to the current page.
176      *
177      * If no default is specified, the Cocoon constants value will
178      * be used.
179      */

180     public void setDefaultFilename(String JavaDoc filename) {
181         this.defaultFilename = filename;
182     }
183     
184     /**
185      * Gets the filename from the source URI, without the path.
186      * This is used to fill out relative URIs that have
187      * parameters but no filename such as ?page=123
188      */

189     public String JavaDoc getPageURI() {
190         String JavaDoc pageURI = this.getSourceURI();
191         if (pageURI.indexOf("/") != -1) {
192             pageURI = pageURI.substring(pageURI.lastIndexOf("/") + 1);
193             if (pageURI.length() == 0) {
194                 pageURI = "./";
195             }
196         }
197         return pageURI;
198     }
199
200     /**
201      * Gets the path from the source URI, without the filename.
202      * This is used when absolutizing/relativizing link URIs.
203      */

204     public String JavaDoc getPath() {
205         return NetUtils.getPath(this.getSourceURI());
206     }
207
208     /**
209      * Gets the file extension for the source URI
210      */

211     public String JavaDoc getExtension() {
212         return NetUtils.getExtension(this.getSourceURI());
213     }
214     
215     /**
216      * Gets the parent URI (the URI of the page that contained
217      * a link to this URI). null is returned if this page was
218      * not referred to in a link.
219      */

220     public String JavaDoc getParentURI() {
221         return this.parentURI;
222     }
223     
224     /**
225      * Calculates the destination URI - the URI to which the generated
226      * page should be written. This will be a URI that, when resolved
227      * by a SourceResolver, will return a modifiableSource.
228      *
229      * This calculation is only done once per target. It is therefore
230      * necessary to ensure that the mime type has been set (if required)
231      * before this method is called.
232      */

233     public String JavaDoc getDestinationURI()
234         throws ProcessingException {
235         
236         if (this.finalDestinationURI == null) {
237             
238             String JavaDoc actualSourceURI = this.sourceURI;
239             if (!actualSourceURI.startsWith(root)) {
240                 throw new ProcessingException(
241                     "Derived target does not share same root: "
242                         + actualSourceURI);
243             }
244             actualSourceURI = actualSourceURI.substring(root.length());
245             actualSourceURI = mangle(actualSourceURI);
246             
247             String JavaDoc destinationURI;
248             if (APPEND_TYPE.equals(this.type)) {
249                 destinationURI = destURI + actualSourceURI;
250             } else if (REPLACE_TYPE.equals(this.type)) {
251                 destinationURI = destURI;
252             } else if (INSERT_TYPE.equals(this.type)) {
253                 int starPos = destURI.indexOf("*");
254                 if (starPos == -1) {
255                     throw new ProcessingException("Missing * in replace mapper uri");
256                 } else if (starPos == destURI.length() - 1) {
257                     destinationURI = destURI.substring(0, starPos) + actualSourceURI;
258                 } else {
259                     destinationURI = destURI.substring(0, starPos)
260                         + actualSourceURI
261                         + destURI.substring(starPos + 1);
262                 }
263             } else {
264                 throw new ProcessingException(
265                     "Unknown mapper type: " + this.type);
266             }
267             if (mimeType != null) {
268                 final String JavaDoc ext = NetUtils.getExtension(destinationURI);
269                 final String JavaDoc defaultExt = MIMEUtils.getDefaultExtension(mimeType);
270                 if (defaultExt != null) {
271                     if ((ext == null) || (!ext.equals(defaultExt))) {
272                         destinationURI += defaultExt;
273                     }
274                 }
275             }
276             if (this.extension != null) {
277                 destinationURI += this.extension;
278             }
279             this.finalDestinationURI = destinationURI;
280         }
281         return this.finalDestinationURI;
282     }
283
284     /**
285      * Gets a translated version of a link, ready for insertion
286      * into another page as a link. This link needs to be
287      * relative to the original page.
288      */

289     public String JavaDoc getTranslatedURI(String JavaDoc path)
290         throws ProcessingException {
291                     
292         String JavaDoc actualSourceURI = this.sourceURI;
293         if (!actualSourceURI.startsWith(root)) {
294             return actualSourceURI;
295         }
296         actualSourceURI = mangle(actualSourceURI);
297         
298         if (mimeType != null) {
299             final String JavaDoc ext = NetUtils.getExtension(actualSourceURI);
300             final String JavaDoc defaultExt = MIMEUtils.getDefaultExtension(mimeType);
301             if (defaultExt != null) {
302                 if ((ext == null) || (!ext.equals(defaultExt))) {
303                     actualSourceURI += defaultExt;
304                 }
305             }
306         }
307         return NetUtils.relativize(path, actualSourceURI);
308     }
309
310     /**
311      *
312      * @return destination URI after all authentication details have been
313      * removed
314      */

315     public String JavaDoc getAuthlessDestURI() throws ProcessingException {
316         return NetUtils.removeAuthorisation(this.getDestinationURI());
317     }
318     
319     /**
320      * Gets the original URI used to create this Target.
321      * This URI is completely unprocessed.
322      */

323     public String JavaDoc getOriginalSourceURI() {
324         return this.originalURI;
325     }
326
327     /**
328      * Gets the source URI for this target, after
329      * the URI has been 'prepared' by normalisation,
330      * absolutization and deparameterization followed
331      * by reparameterization. This final step is to
332      * ensure that all parameters appear in a consistent
333      * order. For example page?a=1&b=2 and page?b=2&a=1
334      * should be considered the same resource, and thus
335      * have the same sourceURI.
336      */

337     public String JavaDoc getSourceURI() {
338         return this.sourceURI;
339     }
340     
341     /**
342      * Gets the source URI for this target, with
343      * parameters removed. This is the URI that is
344      * to be passed to Cocoon in order to generate
345      * the page.
346      */

347     public String JavaDoc getDeparameterizedSourceURI() {
348         return this.deparameterizedSourceURI;
349     }
350
351     /**
352      * Gets the parameters that have been removed from
353      * the URI. These need to be passed to Cocoon when
354      * generating a page.
355      */

356     public TreeMap JavaDoc getParameters() {
357         return this.parameters;
358     }
359
360     /**
361      * Mangle a URI.
362      *
363      * @param uri a URI to mangle
364      * @return a mangled URI
365      */

366     private String JavaDoc mangle(String JavaDoc uri) {
367         if (uri.length()==0 || uri.charAt(uri.length() - 1) == '/') {
368             uri += defaultFilename;
369         }
370         uri = uri.replace('"', '\'');
371         uri = uri.replace('?', '_');
372         uri = uri.replace(':', '_');
373
374         return uri;
375     }
376
377     public boolean equals(Object JavaDoc o) {
378         return (o instanceof Target) && o.toString().equals(toString());
379     }
380
381     public int hashCode() {
382         if (_hashCode == 0) {
383             return _hashCode = toString().hashCode();
384         }
385         return _hashCode;
386     }
387
388     public String JavaDoc toString() {
389         if (_toString == null) {
390             return _toString =
391                 "<"
392                     + type
393                     + "|"
394                     + root
395                     + "|"
396                     + sourceURI
397                     + "|"
398                     + destURI
399                     + ">";
400         }
401         return _toString;
402     }
403     /**
404      * @return boolean
405      */

406     public boolean confirmExtensions() {
407         return confirmExtension;
408     }
409
410     public boolean followLinks() {
411         return followLinks;
412     }
413
414     public String JavaDoc getLogger() {
415         return logger;
416     }
417
418     public void setConfirmExtension(boolean b) {
419         confirmExtension = b;
420     }
421
422     public void setFollowLinks(boolean b) {
423         followLinks = b;
424     }
425
426     public void setLogger(String JavaDoc string) {
427         logger = string;
428     }
429 }
430
Popular Tags