KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > publishing > 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 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.publishing;
21
22 import java.io.File JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.apache.avalon.framework.parameters.Parameters;
27 import org.apache.lenya.cms.task.ExecutionException;
28 import org.apache.log4j.Category;
29
30
31 /**
32  * This Exporter uses WGet to download HTML files from URIs and saves them. The Task parameters
33  * are: <code><strong>server-uri</strong></code>: the server uri<br/>
34  * <code><strong>server-port</strong></code>: the server port<br/>
35  * <code><strong>publication-id</strong></code>: the publication id<br/>
36  * <code><strong>export-path-prefix</strong></code>: the path to save the files to<br/>
37  * <code><strong>uris</strong></code>: a comma-separated list of uris to download (without server
38  * + port)<br/>
39  * <code><strong>substitute-regexp</strong></code>: a regular expression to substitute a part of
40  * the path<br/>
41  */

42 public class StaticHTMLExporter extends AbstractExporter {
43     private static Category log = Category.getInstance(StaticHTMLExporter.class);
44     public static final String JavaDoc PARAMETER_URIS = "uris";
45
46     /**
47      * DOCUMENT ME!
48      *
49      * @param serverURI DOCUMENT ME!
50      * @param serverPort DOCUMENT ME!
51      * @param publicationPath DOCUMENT ME!
52      * @param exportPath DOCUMENT ME!
53      * @param uris DOCUMENT ME!
54      * @param substituteExpression DOCUMENT ME!
55      *
56      * @throws ExportException DOCUMENT ME!
57      */

58     public void export(URL JavaDoc serverURI, int serverPort, String JavaDoc publicationPath, String JavaDoc exportPath,
59         String JavaDoc[] uris, String JavaDoc substituteExpression, String JavaDoc substituteReplacement)
60         throws ExportException {
61         try {
62             String JavaDoc exportDirectory = publicationPath + exportPath;
63
64             if (new File JavaDoc(exportPath).isAbsolute()) {
65                 exportDirectory = exportPath;
66             }
67
68             log.info(".export(): Export directory: " + exportDirectory + " (" + publicationPath +
69                 " , " + exportPath + ")");
70
71             org.apache.lenya.net.WGet wget = new org.apache.lenya.net.WGet();
72             wget.setDirectoryPrefix(exportDirectory);
73
74             String JavaDoc fullServerURI = serverURI + ":" + serverPort;
75
76             for (int i = 0; i < uris.length; i++) {
77                 URL JavaDoc uri = new URL JavaDoc(fullServerURI + uris[i]);
78                 log.info(".export(): Export static HTML: " + uri);
79
80                 wget.download(uri, substituteExpression, substituteReplacement);
81             }
82         } catch (Exception JavaDoc e) {
83             throw new ExportException(e);
84         }
85     }
86
87     /**
88      * DOCUMENT ME!
89      *
90      * @param contextPath DOCUMENT ME!
91      */

92     public void execute(String JavaDoc contextPath) throws ExecutionException {
93         try {
94             String JavaDoc publicationId = getParameters().getParameter(PARAMETER_PUBLICATION_ID);
95
96             Parameters taskParameters = new Parameters();
97
98             PublishingEnvironment environment = new PublishingEnvironment(contextPath, publicationId);
99
100             // read default parameters from PublishingEnvironment
101
taskParameters.setParameter(PublishingEnvironment.PARAMETER_EXPORT_PATH,
102                 environment.getExportDirectory());
103             taskParameters.setParameter(PublishingEnvironment.PARAMETER_SUBSTITUTE_REGEXP,
104                 environment.getSubstituteExpression());
105             taskParameters.setParameter(PublishingEnvironment.PARAMETER_SUBSTITUTE_REPLACEMENT,
106                 environment.getSubstituteReplacement());
107
108             taskParameters.merge(getParameters());
109             parameterize(taskParameters);
110
111             String JavaDoc publicationPath = PublishingEnvironment.getPublicationPath(contextPath,
112                     publicationId);
113
114             int serverPort = getParameters().getParameterAsInteger(PARAMETER_SERVER_PORT);
115             log.debug(".execute(): Server Port: " + serverPort);
116
117             String JavaDoc serverURI = getParameters().getParameter(PARAMETER_SERVER_URI);
118
119             String JavaDoc urisString = getParameters().getParameter(PARAMETER_URIS);
120             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(urisString, ",");
121             String JavaDoc[] uris = new String JavaDoc[st.countTokens()];
122             int i = 0;
123
124             while (st.hasMoreTokens()) {
125                 uris[i++] = st.nextToken();
126             }
127
128             export(new URL JavaDoc(serverURI), serverPort, publicationPath,
129                 getParameters().getParameter(PublishingEnvironment.PARAMETER_EXPORT_PATH), uris,
130                 getParameters().getParameter(PublishingEnvironment.PARAMETER_SUBSTITUTE_REGEXP),
131                 getParameters().getParameter(PublishingEnvironment.PARAMETER_SUBSTITUTE_REPLACEMENT));
132         } catch (Exception JavaDoc e) {
133             throw new ExecutionException(e);
134         }
135     }
136 }
137
Popular Tags