001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.help;
023:
024: import java.io.BufferedOutputStream;
025: import java.io.File;
026: import java.io.FileOutputStream;
027: import java.io.PrintWriter;
028: import java.net.URL;
029:
030: import javax.xml.parsers.DocumentBuilder;
031: import javax.xml.parsers.DocumentBuilderFactory;
032: import javax.xml.transform.Transformer;
033: import javax.xml.transform.TransformerFactory;
034: import javax.xml.transform.dom.DOMSource;
035: import javax.xml.transform.stream.StreamResult;
036: import javax.xml.transform.stream.StreamSource;
037:
038: import org.apache.log4j.Logger;
039: import org.beryl.gui.DialogUtils;
040: import org.beryl.gui.GUIUtils;
041: import org.w3c.dom.Document;
042:
043: class HelpException extends Exception {
044: public HelpException(String message) {
045: super (message);
046: }
047:
048: public HelpException(String message, Throwable cause) {
049: super (message, cause);
050: }
051: }
052:
053: public class HelpCompiler {
054: private static Logger log = null;
055:
056: public static void main(String args[]) {
057: try {
058: GUIUtils.initializeLogging();
059: log = Logger.getLogger(HelpCompiler.class);
060:
061: if (args.length != 4) {
062: throw new HelpException(
063: "Syntax: org.beryl.help.HelpCompiler <indir> <outdir> <map file> <language>");
064: }
065:
066: new HelpCompiler().compile(new File(args[0]), new File(
067: args[1]), new File(args[2]), args[3]);
068: } catch (Exception e) {
069: e.printStackTrace();
070: }
071: }
072:
073: private void compile(File inDir, File outDir, File mapFile,
074: String locale) throws HelpException {
075: String xmlFiles[] = inDir.list(new DialogUtils.ExtensionFilter(
076: "xml", false));
077: Transformer transformer = null;
078: DocumentBuilder db = null;
079: PrintWriter mapPrinter = null;
080:
081: log.debug("generating help file with locale [" + locale + "]");
082: try {
083: log.debug("loading xml parser ..");
084: db = DocumentBuilderFactory.newInstance()
085: .newDocumentBuilder();
086: } catch (Exception e) {
087: throw new HelpException(
088: "Error while loading an XML parser", e);
089: }
090:
091: try {
092: log.debug("creating map file [" + mapFile.getPath()
093: + "] ..");
094: mapPrinter = new PrintWriter(new BufferedOutputStream(
095: new FileOutputStream(mapFile)));
096: mapPrinter
097: .println("<?xml version='1.0' encoding='ISO-8859-1' ?>");
098: mapPrinter
099: .println("<!DOCTYPE map PUBLIC \"-//Sun Microsystems Inc.//DTD JavaHelp Map Version 1.0//EN\""
100: + " \"http://java.sun.com/products/javahelp/map_1_0.dtd\">\n");
101: mapPrinter
102: .println("<!-- Do not change, this file is automatically generated -->");
103: mapPrinter.println("<map version=\"1.0\">");
104: } catch (Exception e) {
105: throw new HelpException(
106: "Error while creating the map file", e);
107: }
108:
109: try {
110: log.debug("loading transformer ..");
111: URL xslFile = getClass().getResource(
112: "/resources/xmlgui/help.xsl");
113: transformer = TransformerFactory.newInstance()
114: .newTransformer(
115: new StreamSource(xslFile.openStream()));
116: transformer.setParameter("locale", locale);
117: } catch (Exception e) {
118: throw new HelpException(
119: "Error while constructing the transformer", e);
120: }
121:
122: for (int i = 0; i < xmlFiles.length; i++) {
123: String xmlFile = xmlFiles[i];
124: String idName = xmlFile.substring(0, xmlFile.length() - 4);
125: File sourceFile = new File(inDir.getPath() + "/" + xmlFile);
126: File outputFile = new File(outDir.getPath() + "/" + idName
127: + "." + locale + ".html");
128: Document document = null;
129:
130: log.debug("loading help file [" + xmlFile + "]");
131: try {
132: document = db.parse(sourceFile);
133: mapPrinter.println(" <mapID target=\""
134: + document.getDocumentElement().getAttribute(
135: "id") + "\" url=\""
136: + outputFile.getName() + "\" />");
137: } catch (Exception e) {
138: throw new HelpException("Error while loading ["
139: + xmlFile + "]", e);
140: }
141:
142: if (outputFile.exists()
143: && outputFile.lastModified() >= sourceFile
144: .lastModified())
145: continue;
146:
147: log.debug("transforming help file to ["
148: + outputFile.getPath() + "]");
149: StreamResult result = new StreamResult(outputFile);
150:
151: try {
152: transformer.transform(new DOMSource(document), result);
153: } catch (Exception e) {
154: throw new HelpException(
155: "Error during the transformation of ["
156: + xmlFile + "]", e);
157: }
158: }
159: mapPrinter.println("</map>");
160: mapPrinter.close();
161: }
162: }
|