2
* Copyright (C) 2001-2012 Michael Fuchs
4
* This file is part of herold.
6
* herold is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* herold is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with herold. If not, see <http://www.gnu.org/licenses/>.
19
package org.dbdoclet.herold;
21
import java.io.BufferedReader;
23
import java.io.FileNotFoundException;
24
import java.io.IOException;
25
import java.io.InputStreamReader;
27
import java.util.ResourceBundle;
29
import org.dbdoclet.log.Logger;
30
import org.dbdoclet.option.BooleanOption;
31
import org.dbdoclet.option.FileOption;
32
import org.dbdoclet.option.Option;
33
import org.dbdoclet.option.OptionException;
34
import org.dbdoclet.option.OptionList;
35
import org.dbdoclet.option.SelectOption;
36
import org.dbdoclet.option.StringOption;
37
import org.dbdoclet.service.FileServices;
38
import org.dbdoclet.service.MessageServices;
39
import org.dbdoclet.service.ResourceServices;
40
import org.dbdoclet.service.StringServices;
41
import org.dbdoclet.trafo.TrafoException;
42
import org.dbdoclet.trafo.TrafoScriptManager;
43
import org.dbdoclet.trafo.html.docbook.DBTConstants;
44
import org.dbdoclet.trafo.html.docbook.DocBookTransformer;
45
import org.dbdoclet.trafo.script.Script;
48
* The class <code>Herold</code> implements a console converter to transform
49
* HTML code to DocBook XML.
51
* @author <a href ="mailto:mfuchs@unico-consulting.com">Michael Fuchs</a>
56
private static Logger logger = Logger.getLogger(Herold.class);
57
private static FileOption optIn;
58
private static FileOption optOut;
59
private static ResourceBundle res = ResourceBundle
60
.getBundle("org/dbdoclet/herold/Resources");
61
private boolean verbose = false;
62
private static boolean systemExitEnabled = true;
64
public static void main(String[] args) {
66
OptionList options = null;
72
options = new OptionList(args);
75
option = new BooleanOption("help", "h");
76
option.setMediumName("?");
80
option = new BooleanOption("license", "L");
84
option = new BooleanOption("version", "V");
87
options.validate(true);
89
if (options.getFlag("help", false)) {
94
if (options.getFlag("version", false)) {
99
if (options.getFlag("license", false)) {
104
options = createOptionList(args);
106
if (options.validate() == false) {
107
throw new OptionException(options.getError());
110
Logger.setRootLogLevel(Logger.ERROR);
111
Logger.setLoggerLevel(DocBookTransformer.class,
112
getLoggingLevel(options.getTextOption("logging-level")
114
Logger.setRootOutputFormat(Logger.SHORT);
116
Herold converter = new Herold();
118
FileOption optProfile = (FileOption) options.getOption("profile");
122
if (optProfile.isUnset() == false) {
123
script = TrafoScriptManager.parseScript(optProfile.getValue());
125
script = new Script();
128
File outFile = converter.processCommandLineOptions(options, script);
129
script.setOutputFile(outFile);
130
script.setInputFile(optIn.getValue());
132
// System.out.println(script.dump());
133
converter.convert(script);
135
} catch (OptionException oops) {
137
if ((options != null) && (options.getFlag("help", false) == false)) {
141
String msg = oops.getMessage();
144
System.err.println(msg);
148
oops.printStackTrace();
154
} catch (FileNotFoundException oops) {
156
oops.printStackTrace();
159
} catch (IOException oops) {
161
oops.printStackTrace();
164
} catch (Exception oops) {
166
oops.printStackTrace();
170
if (systemExitEnabled == true) {
175
private static OptionList createOptionList(String[] args) {
182
OptionList options = new OptionList(args);
185
bopt = new BooleanOption(DBTConstants.SECTION_DOCBOOK.toLowerCase()
186
+ "-" + DBTConstants.PARAM_DOCBOOK_ADD_INDEX, "x");
187
bopt.setValue(false);
190
// destination-encoding
191
sopt = new StringOption(DBTConstants.SECTION_DOCBOOK.toLowerCase()
192
+ "-" + DBTConstants.PARAM_DOCBOOK_DESTINATION_ENCODING, "d");
193
sopt.setDefault("UTF-8");
197
bopt = new BooleanOption(DBTConstants.SECTION_DOCBOOK.toLowerCase()
198
+ "-" + DBTConstants.PARAM_DOCBOOK_DECOMPOSE_TABLES,
200
bopt.setDefault(false);
204
selopt = new SelectOption(DBTConstants.SECTION_DOCBOOK.toLowerCase()
205
+ "-" + DBTConstants.PARAM_DOCBOOK_DOCUMENT_ELEMENT,
208
String[] optv2 = { "article", "book", "reference" };
209
selopt.setList(optv2);
210
selopt.setDefault("article");
214
sopt = new StringOption(DBTConstants.SECTION_DOCBOOK.toLowerCase()
215
+ "-" + DBTConstants.PARAM_DOCBOOK_TITLE, "t");
216
sopt.setDefault("http://www.dbdoclet.org/herold");
220
sopt = new StringOption(DBTConstants.SECTION_HTML.toLowerCase()
221
+ "-" + DBTConstants.PARAM_HTML_SOURCE_ENCODING, "s");
222
sopt.setDefault("UTF-8");
226
optIn = new FileOption("in", "i");
227
optIn.isRequired(true);
228
optIn.isExisting(true);
232
SelectOption optLogLevel = new SelectOption("logging-level", "l");
233
optLogLevel.setList(new String[] { "debug5", "debug4", "debug3",
234
"debug2", "debug", "info", "warn", "error", "fatal" });
235
optLogLevel.setDefault("error");
236
options.add(optLogLevel);
239
optOut = new FileOption("out", "o");
242
FileOption optProfile = new FileOption("profile", "p");
243
optProfile.isExisting(true);
244
options.add(optProfile);
247
option = new BooleanOption("verbose", "v");
253
private static int getLoggingLevel(String value) {
259
value = value.trim().toLowerCase();
261
if (value.equals("fatal")) {
265
if (value.equals("error")) {
269
if (value.equals("warn")) {
273
if (value.equals("info")) {
277
if (value.equals("debug")) {
281
if (value.equals("debug2")) {
282
return Logger.DEBUG2;
285
if (value.equals("debug3")) {
286
return Logger.DEBUG3;
289
if (value.equals("debug4")) {
290
return Logger.DEBUG4;
293
if (value.equals("debug5")) {
294
return Logger.DEBUG5;
300
private static String getVersion() {
302
Package p = Herold.class.getPackage();
303
return p.getImplementationVersion();
306
private static void printLicense() throws IOException {
308
URL url = ResourceServices
309
.getResourceAsUrl("/org/dbdoclet/herold/COPYING");
311
BufferedReader reader = new BufferedReader(new InputStreamReader(
313
String line = reader.readLine();
315
while (line != null) {
317
line = reader.readLine();
323
private static void println(String str) {
324
System.out.println(str);
327
private static void printUsage() {
331
String resname = ResourceServices.getString(res, "C_USAGE");
332
String buffer = ResourceServices.getResourceAsString(resname);
334
println(MessageServices.format(buffer, getVersion()));
336
} catch (IOException oops) {
338
logger.fatal("Printing usage message failed!", oops);
342
private static void printVersion() {
344
println("herold version \"" + getVersion() + "\"");
347
public void convert(Script script) throws TrafoException {
349
DocBookTransformer trafo = new DocBookTransformer(
350
script.getOutputFile());
352
if (verbose == true) {
353
trafo.addProgressListener(new ConsoleProgressListener(false));
356
trafo.setScript(script);
357
trafo.convert(script.getInputFile(), script.getOutputFile());
359
if (verbose == true) {
360
System.out.println();
364
public boolean isCanceled() {
368
public File processCommandLineOptions(OptionList options, Script script) {
370
for (Option<?> option : options) {
372
String name = option.getLongName();
374
if (name.startsWith(DBTConstants.SECTION_DOCBOOK.toLowerCase())) {
375
script.selectSection(DBTConstants.SECTION_DOCBOOK);
376
name = StringServices.cutPrefix(name,
377
DBTConstants.SECTION_DOCBOOK.toLowerCase());
378
} else if (name.startsWith(DBTConstants.SECTION_HTML.toLowerCase())) {
379
script.selectSection(DBTConstants.SECTION_HTML);
380
name = StringServices.cutPrefix(name,
381
DBTConstants.SECTION_HTML.toLowerCase());
382
} else if (name.startsWith(DBTConstants.SECTION_GROOVY
384
script.selectSection(DBTConstants.SECTION_GROOVY);
385
name = StringServices.cutPrefix(name,
386
DBTConstants.SECTION_GROOVY.toLowerCase());
389
name = StringServices.cutPrefix(name, "-");
391
if (option.isUnset() == false) {
392
switch (option.getType()) {
395
script.addBoolParam(name, (Boolean) option.getValue());
399
script.addTextParam(name, option.getValue().toString());
405
setVerbose(options.getFlag("verbose", false));
409
if (optOut.isUnset()) {
411
String outFileName = FileServices.getFileBase(optIn.getValue())
413
// outFile = FileServices.createUniqueFile(new File(outFileName));
414
outFile = new File(outFileName);
417
outFile = optOut.getValue();
423
public void setVerbose(boolean verbose) {
424
this.verbose = verbose;
427
public void convert(File htmlFile, File xmlFile) throws TrafoException {
429
Script script = new Script();
430
script.setInputFile(htmlFile);
431
script.setOutputFile(xmlFile);
435
public static void setSystemExitEnabled(boolean systemExitEnabled) {
436
Herold.systemExitEnabled = systemExitEnabled;