~ubuntu-branches/ubuntu/vivid/herold/vivid

« back to all changes in this revision

Viewing changes to java/org/dbdoclet/herold/Herold.java

  • Committer: Package Import Robot
  • Author(s): Mathieu Malaterre
  • Date: 2012-09-20 10:00:14 UTC
  • Revision ID: package-import@ubuntu.com-20120920100014-5pcwbw2err6on8yg
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (C) 2001-2012 Michael Fuchs
 
3
 *
 
4
 * This file is part of herold.
 
5
 * 
 
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.
 
10
 * 
 
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.
 
15
 * 
 
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/>.  
 
18
 */
 
19
package org.dbdoclet.herold;
 
20
 
 
21
import java.io.BufferedReader;
 
22
import java.io.File;
 
23
import java.io.FileNotFoundException;
 
24
import java.io.IOException;
 
25
import java.io.InputStreamReader;
 
26
import java.net.URL;
 
27
import java.util.ResourceBundle;
 
28
 
 
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;
 
46
 
 
47
/**
 
48
 * The class <code>Herold</code> implements a console converter to transform
 
49
 * HTML code to DocBook XML.
 
50
 * 
 
51
 * @author <a href ="mailto:mfuchs@unico-consulting.com">Michael Fuchs</a>
 
52
 * @version 1.0
 
53
 */
 
54
public class Herold {
 
55
 
 
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;
 
63
 
 
64
        public static void main(String[] args) {
 
65
 
 
66
                OptionList options = null;
 
67
                int rc = 0;
 
68
 
 
69
                try {
 
70
 
 
71
                        Option<?> option;
 
72
                        options = new OptionList(args);
 
73
 
 
74
                        // help
 
75
                        option = new BooleanOption("help", "h");
 
76
                        option.setMediumName("?");
 
77
                        options.add(option);
 
78
 
 
79
                        // license
 
80
                        option = new BooleanOption("license", "L");
 
81
                        options.add(option);
 
82
 
 
83
                        // version
 
84
                        option = new BooleanOption("version", "V");
 
85
                        options.add(option);
 
86
 
 
87
                        options.validate(true);
 
88
 
 
89
                        if (options.getFlag("help", false)) {
 
90
                                printUsage();
 
91
                                return;
 
92
                        }
 
93
 
 
94
                        if (options.getFlag("version", false)) {
 
95
                                printVersion();
 
96
                                return;
 
97
                        }
 
98
 
 
99
                        if (options.getFlag("license", false)) {
 
100
                                printLicense();
 
101
                                return;
 
102
                        }
 
103
 
 
104
                        options = createOptionList(args);
 
105
 
 
106
                        if (options.validate() == false) {
 
107
                                throw new OptionException(options.getError());
 
108
                        }
 
109
 
 
110
                        Logger.setRootLogLevel(Logger.ERROR);
 
111
                        Logger.setLoggerLevel(DocBookTransformer.class,
 
112
                                        getLoggingLevel(options.getTextOption("logging-level")
 
113
                                                        .getValue()));
 
114
                        Logger.setRootOutputFormat(Logger.SHORT);
 
115
 
 
116
                        Herold converter = new Herold();
 
117
 
 
118
                        FileOption optProfile = (FileOption) options.getOption("profile");
 
119
 
 
120
                        Script script;
 
121
 
 
122
                        if (optProfile.isUnset() == false) {
 
123
                                script = TrafoScriptManager.parseScript(optProfile.getValue());
 
124
                        } else {
 
125
                                script = new Script();
 
126
                        }
 
127
 
 
128
                        File outFile = converter.processCommandLineOptions(options, script);
 
129
                        script.setOutputFile(outFile);
 
130
                        script.setInputFile(optIn.getValue());
 
131
 
 
132
                        // System.out.println(script.dump());
 
133
                        converter.convert(script);
 
134
 
 
135
                } catch (OptionException oops) {
 
136
 
 
137
                        if ((options != null) && (options.getFlag("help", false) == false)) {
 
138
 
 
139
                                printUsage();
 
140
 
 
141
                                String msg = oops.getMessage();
 
142
 
 
143
                                if (msg != null) {
 
144
                                        System.err.println(msg);
 
145
                                }
 
146
 
 
147
                        } else {
 
148
                                oops.printStackTrace();
 
149
                                printUsage();
 
150
                        }
 
151
 
 
152
                        rc = 1;
 
153
 
 
154
                } catch (FileNotFoundException oops) {
 
155
 
 
156
                        oops.printStackTrace();
 
157
                        rc = 5;
 
158
 
 
159
                } catch (IOException oops) {
 
160
 
 
161
                        oops.printStackTrace();
 
162
                        rc = 6;
 
163
 
 
164
                } catch (Exception oops) {
 
165
 
 
166
                        oops.printStackTrace();
 
167
                        rc = 7;
 
168
                }
 
169
 
 
170
                if (systemExitEnabled == true) {
 
171
                        System.exit(rc);
 
172
                }
 
173
        }
 
174
 
 
175
        private static OptionList createOptionList(String[] args) {
 
176
 
 
177
                Option<?> option;
 
178
                SelectOption selopt;
 
179
                BooleanOption bopt;
 
180
                StringOption sopt;
 
181
 
 
182
                OptionList options = new OptionList(args);
 
183
 
 
184
                // add-index
 
185
                bopt = new BooleanOption(DBTConstants.SECTION_DOCBOOK.toLowerCase()
 
186
                                + "-" + DBTConstants.PARAM_DOCBOOK_ADD_INDEX, "x");
 
187
                bopt.setValue(false);
 
188
                options.add(bopt);
 
189
 
 
190
                // destination-encoding
 
191
                sopt = new StringOption(DBTConstants.SECTION_DOCBOOK.toLowerCase()
 
192
                                + "-" + DBTConstants.PARAM_DOCBOOK_DESTINATION_ENCODING, "d");
 
193
                sopt.setDefault("UTF-8");
 
194
                options.add(sopt);
 
195
 
 
196
                // decompose-tables
 
197
                bopt = new BooleanOption(DBTConstants.SECTION_DOCBOOK.toLowerCase()
 
198
                                + "-" + DBTConstants.PARAM_DOCBOOK_DECOMPOSE_TABLES,
 
199
                                "T");
 
200
                bopt.setDefault(false);
 
201
                options.add(bopt);
 
202
 
 
203
                // document-element
 
204
                selopt = new SelectOption(DBTConstants.SECTION_DOCBOOK.toLowerCase()
 
205
                                + "-" + DBTConstants.PARAM_DOCBOOK_DOCUMENT_ELEMENT,
 
206
                                "r");
 
207
                
 
208
                String[] optv2 = { "article", "book", "reference" };
 
209
                selopt.setList(optv2);
 
210
                selopt.setDefault("article");
 
211
                options.add(selopt);
 
212
 
 
213
                // title
 
214
                sopt = new StringOption(DBTConstants.SECTION_DOCBOOK.toLowerCase()
 
215
                                + "-" + DBTConstants.PARAM_DOCBOOK_TITLE, "t");
 
216
                sopt.setDefault("http://www.dbdoclet.org/herold");
 
217
                options.add(sopt);
 
218
 
 
219
                // source-encoding
 
220
                sopt = new StringOption(DBTConstants.SECTION_HTML.toLowerCase()
 
221
                                + "-" + DBTConstants.PARAM_HTML_SOURCE_ENCODING, "s");
 
222
                sopt.setDefault("UTF-8");
 
223
                options.add(sopt);
 
224
 
 
225
                // in
 
226
                optIn = new FileOption("in", "i");
 
227
                optIn.isRequired(true);
 
228
                optIn.isExisting(true);
 
229
                options.add(optIn);
 
230
 
 
231
                // loglevel
 
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);
 
237
 
 
238
                // out
 
239
                optOut = new FileOption("out", "o");
 
240
                options.add(optOut);
 
241
 
 
242
                FileOption optProfile = new FileOption("profile", "p");
 
243
                optProfile.isExisting(true);
 
244
                options.add(optProfile);
 
245
 
 
246
                // verbose
 
247
                option = new BooleanOption("verbose", "v");
 
248
                options.add(option);
 
249
 
 
250
                return options;
 
251
        }
 
252
 
 
253
        private static int getLoggingLevel(String value) {
 
254
 
 
255
                if (value == null) {
 
256
                        return Logger.WARN;
 
257
                }
 
258
 
 
259
                value = value.trim().toLowerCase();
 
260
 
 
261
                if (value.equals("fatal")) {
 
262
                        return Logger.FATAL;
 
263
                }
 
264
 
 
265
                if (value.equals("error")) {
 
266
                        return Logger.ERROR;
 
267
                }
 
268
 
 
269
                if (value.equals("warn")) {
 
270
                        return Logger.WARN;
 
271
                }
 
272
 
 
273
                if (value.equals("info")) {
 
274
                        return Logger.INFO;
 
275
                }
 
276
 
 
277
                if (value.equals("debug")) {
 
278
                        return Logger.DEBUG;
 
279
                }
 
280
 
 
281
                if (value.equals("debug2")) {
 
282
                        return Logger.DEBUG2;
 
283
                }
 
284
 
 
285
                if (value.equals("debug3")) {
 
286
                        return Logger.DEBUG3;
 
287
                }
 
288
 
 
289
                if (value.equals("debug4")) {
 
290
                        return Logger.DEBUG4;
 
291
                }
 
292
 
 
293
                if (value.equals("debug5")) {
 
294
                        return Logger.DEBUG5;
 
295
                }
 
296
 
 
297
                return Logger.WARN;
 
298
        }
 
299
 
 
300
        private static String getVersion() {
 
301
 
 
302
                Package p = Herold.class.getPackage();
 
303
                return p.getImplementationVersion();
 
304
        }
 
305
 
 
306
        private static void printLicense() throws IOException {
 
307
 
 
308
                URL url = ResourceServices
 
309
                                .getResourceAsUrl("/org/dbdoclet/herold/COPYING");
 
310
 
 
311
                BufferedReader reader = new BufferedReader(new InputStreamReader(
 
312
                                url.openStream()));
 
313
                String line = reader.readLine();
 
314
 
 
315
                while (line != null) {
 
316
                        println(line);
 
317
                        line = reader.readLine();
 
318
                }
 
319
 
 
320
                reader.close();
 
321
        }
 
322
 
 
323
        private static void println(String str) {
 
324
                System.out.println(str);
 
325
        }
 
326
 
 
327
        private static void printUsage() {
 
328
 
 
329
                try {
 
330
 
 
331
                        String resname = ResourceServices.getString(res, "C_USAGE");
 
332
                        String buffer = ResourceServices.getResourceAsString(resname);
 
333
 
 
334
                        println(MessageServices.format(buffer, getVersion()));
 
335
 
 
336
                } catch (IOException oops) {
 
337
 
 
338
                        logger.fatal("Printing usage message failed!", oops);
 
339
                }
 
340
        }
 
341
 
 
342
        private static void printVersion() {
 
343
 
 
344
                println("herold version \"" + getVersion() + "\"");
 
345
        }
 
346
 
 
347
        public void convert(Script script) throws TrafoException {
 
348
 
 
349
                DocBookTransformer trafo = new DocBookTransformer(
 
350
                                script.getOutputFile());
 
351
 
 
352
                if (verbose == true) {
 
353
                        trafo.addProgressListener(new ConsoleProgressListener(false));
 
354
                }
 
355
 
 
356
                trafo.setScript(script);
 
357
                trafo.convert(script.getInputFile(), script.getOutputFile());
 
358
 
 
359
                if (verbose == true) {
 
360
                        System.out.println();
 
361
                }
 
362
        }
 
363
 
 
364
        public boolean isCanceled() {
 
365
                return false;
 
366
        }
 
367
 
 
368
        public File processCommandLineOptions(OptionList options, Script script) {
 
369
 
 
370
                for (Option<?> option : options) {
 
371
 
 
372
                        String name = option.getLongName();
 
373
 
 
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
 
383
                                        .toLowerCase())) {
 
384
                                script.selectSection(DBTConstants.SECTION_GROOVY);
 
385
                                name = StringServices.cutPrefix(name,
 
386
                                                DBTConstants.SECTION_GROOVY.toLowerCase());
 
387
                        }
 
388
 
 
389
                        name = StringServices.cutPrefix(name, "-");
 
390
 
 
391
                        if (option.isUnset() == false) {
 
392
                                switch (option.getType()) {
 
393
 
 
394
                                case BOOLEAN:
 
395
                                        script.addBoolParam(name, (Boolean) option.getValue());
 
396
                                        break;
 
397
 
 
398
                                case TEXT:
 
399
                                        script.addTextParam(name, option.getValue().toString());
 
400
                                        break;
 
401
                                }
 
402
                        }
 
403
                }
 
404
 
 
405
                setVerbose(options.getFlag("verbose", false));
 
406
 
 
407
                File outFile;
 
408
 
 
409
                if (optOut.isUnset()) {
 
410
 
 
411
                        String outFileName = FileServices.getFileBase(optIn.getValue())
 
412
                                        + ".xml";
 
413
                        // outFile = FileServices.createUniqueFile(new File(outFileName));
 
414
                        outFile = new File(outFileName);
 
415
 
 
416
                } else {
 
417
                        outFile = optOut.getValue();
 
418
                }
 
419
 
 
420
                return outFile;
 
421
        }
 
422
 
 
423
        public void setVerbose(boolean verbose) {
 
424
                this.verbose = verbose;
 
425
        }
 
426
 
 
427
        public void convert(File htmlFile, File xmlFile) throws TrafoException {
 
428
 
 
429
                Script script = new Script();
 
430
                script.setInputFile(htmlFile);
 
431
                script.setOutputFile(xmlFile);
 
432
                convert(script);
 
433
        }
 
434
 
 
435
        public static void setSystemExitEnabled(boolean systemExitEnabled) {
 
436
                Herold.systemExitEnabled = systemExitEnabled;
 
437
        }
 
438
}