/* * Copyright 2004 Gunn Software Ltd. http://www.gunnsoft.com.au/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.tapestrypublisher; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.util.List; import org.wanto.sxp.ParseException; import org.wanto.sxp.SXPParser; /** * @author Paul Stanton * Main class */ public class Publisher { public static void main(String[] args) { try { List sites = getSites(args[0]); for (int s = 0; s < sites.size(); s++) captureSite((Site) sites.get(s)); } catch (Throwable t) { t.printStackTrace(); System.exit(1); } } /** * performs the publish process for a site * @param site site to be published * @throws MalformedURLException * @throws IOException */ private static void captureSite(Site site) throws MalformedURLException, IOException { for (int p = 0; p < site.getPages().size(); p++) { Page page = (Page) site.getPages().get(p); String content = relink(IOUtils.getWebDocument(site.getUrl().toString() + "?service=page/" + page.getName()), site); String newFilePath = site.getDest() + File.separator + removeVirt(page.getDest()); IOUtils.writeToFile(new File(newFilePath), content.getBytes()); System.out.println("exported " + newFilePath); } for (int r = 0; r < site.getResources().size(); r++) { Resource res = (Resource) site.getResources().get(r); String newFilePath = site.getDest() + File.separator + removeVirt(res.getDest()); recursiveCopy(new File(res.getSource()), newFilePath, res.getExcludes()); } } /** * @param dest * @return the name of the new file minus any virtual directory marker */ private static String removeVirt(String dest) { if (dest.charAt(0) == '/') return dest.substring(1); return dest; } /** * Translates links to html counterparts for registered pages * @param webDocument html source * @param site * @return translated html source */ private static String relink(String webDocument, Site site) { for (int p = 0; p < site.getPages().size(); p++) { Page page = (Page) site.getPages().get(p); webDocument = webDocument.replaceAll(site.getUrl().getPath() + "\\?service=page/" + page.getName(), page.getDest()); } for (int p = 0; p < site.getPathTranslations().size(); p++) { PathTranslation path = (PathTranslation) site.getPathTranslations().get(p); webDocument = webDocument.replaceAll("=\\\"" + path.getRelative(), "=\"" + path.getVirtual()); } return webDocument; } /** * Reads the configuration xml and returns the list of sites to be published * @param xmlPath path to the configuration xml file * @return the list of sites to be published * @throws ParseException * @throws IOException */ public static List getSites(String xmlPath) throws ParseException, IOException { ConfigParseListener listen = new ConfigParseListener(); SXPParser parser = new SXPParser(listen); parser.parse(new File(xmlPath)); return listen.getSites(); } /** * copies source and it's children if a directory * @param source * @param dest * @throws IOException */ private static void recursiveCopy(File source, String dest, String[] excludes) throws IOException { if (!source.exists()) return; if (!isIncluded(source.getName(), excludes)) return; if (source.isDirectory()) { File[] files = source.listFiles(); for (int f = 0; f < files.length; f++) recursiveCopy(files[f], dest + File.separator + files[f].getName(), excludes); } else { IOUtils.copy(source, new File(dest)); System.out.println("exported " + dest); } } /** * TODO a work in progress. this is intended to eventually emmulate the excludes functionality in ant file copy tasks * @param name * @param excludes * @return true if the file has been excluded by one of the patterns in excludes */ private static boolean isIncluded(String name, String[] excludes) { for (int e = 0; e < excludes.length; e++) { if (excludes[e].indexOf('*') != -1) { String reg = excludes[e].replaceAll("\\*", ".*"); if (name.matches(reg)) return false; } else { if (name.equals(excludes[e].trim())) return false; } } return true; } }