KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > ant > StaticHTMLExporter


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: StaticHTMLExporter.java 42616 2004-03-03 12:56:33Z gregor $ */
19
20 package org.apache.lenya.cms.ant;
21
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import org.apache.lenya.cms.publishing.ExportException;
27 import org.apache.tools.ant.BuildException;
28
29
30 /**
31  * Ant task that exports a set of HTML URIs as static HTML files.
32  */

33 public class StaticHTMLExporter extends PublicationTask {
34     /** Creates a new instance of StaticHTMLExporter */
35     public StaticHTMLExporter() {
36     }
37
38     private String JavaDoc serverURL;
39
40     /**
41      * Returns the server URL.
42      */

43     protected URL JavaDoc getServer() throws MalformedURLException JavaDoc {
44         return new URL JavaDoc(serverURL);
45     }
46
47     /**
48      * Sets the server URL.
49      */

50     public void setServer(String JavaDoc serverURL) {
51         this.serverURL = serverURL;
52     }
53
54     private String JavaDoc path;
55
56     /**
57      * Returns the path to the exported files.
58      */

59     protected String JavaDoc getPath() {
60         return path;
61     }
62
63     /**
64      * Sets the path to the exported files.
65      */

66     public void setPath(String JavaDoc path) {
67         this.path = path;
68     }
69
70     private String JavaDoc uris;
71
72     /**
73      * Returns the URIs to export.
74      */

75     protected String JavaDoc[] getUris() {
76         return uris.split(",");
77     }
78
79     /**
80      * Sets the URIs to export.
81      */

82     public void setUris(String JavaDoc uris) {
83         this.uris = uris;
84     }
85
86     private String JavaDoc expression;
87
88     /**
89      * Returns the expression in the URI to be replaced.
90      */

91     protected String JavaDoc getExpression() {
92         return expression;
93     }
94
95     /**
96      * Sets the expression in the URI to be replaced.
97      */

98     public void setExpression(String JavaDoc expression) {
99         this.expression = expression;
100     }
101
102     private String JavaDoc replacement;
103
104     /**
105      * Returns the string in the URI that replaces the expression.
106      */

107     protected String JavaDoc getReplacement() {
108         return replacement;
109     }
110
111     /**
112      * Sets the string in the URI that replaces the expression.
113      */

114     public void setReplacement(String JavaDoc replacement) {
115         this.replacement = replacement;
116     }
117
118     /**
119      * Exports a set of URIs as a HTML file.
120      * @param serverURI The server to download the file from.
121      * @param publicationDirectory The directory of the publication.
122      * @param exportPath The path to export the files to (relative to publicationDirectory).
123      * @param uris The URIs to export (relative to the publication URI).
124      * @param substituteExpression A part of the complete URIs to be substituted.
125      * @param substituteReplacement A string to replace substituteExpression.
126      */

127     public void export(URL JavaDoc serverURI, File JavaDoc publicationDirectory, String JavaDoc exportPath, String JavaDoc[] uris,
128         String JavaDoc substituteExpression, String JavaDoc substituteReplacement)
129         throws ExportException {
130         try {
131             File JavaDoc exportDirectory;
132
133             if (new File JavaDoc(exportPath).isAbsolute()) {
134                 exportDirectory = new File JavaDoc(exportPath);
135             } else {
136                 exportDirectory = new File JavaDoc(publicationDirectory, exportPath);
137             }
138
139             if (!exportDirectory.exists()) {
140                 exportDirectory.mkdirs();
141             }
142
143             org.apache.lenya.net.WGet wget = new org.apache.lenya.net.WGet();
144             wget.setDirectoryPrefix(exportDirectory.getAbsolutePath());
145
146             String JavaDoc fullServerURI = serverURI.toString();
147
148             for (int i = 0; i < uris.length; i++) {
149                 URL JavaDoc uri = new URL JavaDoc(fullServerURI + uris[i]);
150                 wget.download(uri, substituteExpression, substituteReplacement);
151                 log("Exported URI: " + uri);
152             }
153         } catch (Exception JavaDoc e) {
154             throw new ExportException(e);
155         }
156     }
157
158     /**
159      * Executes the task.
160      */

161     public void execute() throws BuildException {
162         try {
163             log("Server URL: " + getServer());
164             log("Publication Directory: " + getPublicationDirectory());
165             log("Export directory: " + getPath());
166             log("URIs: " + uris);
167             log("Substitute expression: " + getExpression());
168             log("Substitute replacement: " + getReplacement());
169
170             export(getServer(), getPublicationDirectory(), getPath(), getUris(), getExpression(),
171                 getReplacement());
172         } catch (Exception JavaDoc e) {
173             throw new BuildException(e);
174         }
175     }
176 }
177
Popular Tags