java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLI.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 08 Jun 2014 15:00:19 +0200
changeset 39 a5020340362b
parent 31 c9f1e497132e
child 40 4afb00b7b1a9
permissions -rw-r--r--
LexicalHandler (for comments etc.); currnetly not supported
franta-hg@11
     1
/**
franta-hg@11
     2
 * Alt2XML
franta-hg@11
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@11
     4
 *
franta-hg@11
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@11
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@11
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@11
     8
 * (at your option) any later version.
franta-hg@11
     9
 *
franta-hg@11
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@11
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@11
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@11
    13
 * GNU General Public License for more details.
franta-hg@11
    14
 *
franta-hg@11
    15
 * You should have received a copy of the GNU General Public License
franta-hg@11
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@11
    17
 */
franta-hg@16
    18
package cz.frantovo.alt2xml.cli;
franta-hg@2
    19
franta-hg@21
    20
import java.io.File;
franta-hg@2
    21
import java.io.OutputStream;
franta-hg@30
    22
import java.util.Arrays;
franta-hg@30
    23
import java.util.logging.Level;
franta-hg@30
    24
import java.util.logging.Logger;
franta-hg@2
    25
import javax.xml.parsers.SAXParser;
franta-hg@2
    26
import javax.xml.parsers.SAXParserFactory;
franta-hg@2
    27
import javax.xml.stream.XMLOutputFactory;
franta-hg@2
    28
import javax.xml.stream.XMLStreamWriter;
franta-hg@39
    29
import org.xml.sax.ext.LexicalHandler;
franta-hg@2
    30
import org.xml.sax.helpers.DefaultHandler;
franta-hg@2
    31
franta-hg@2
    32
/**
franta-hg@2
    33
 *
franta-hg@19
    34
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@2
    35
 */
franta-hg@2
    36
public class CLI {
franta-hg@2
    37
franta-hg@39
    38
	public static final String LEXICAL_HANDLER_PROPERTY = "http://xml.org/sax/properties/lexical-handler";
franta-hg@30
    39
	private static final Logger log = Logger.getLogger(CLI.class.getName());
franta-hg@28
    40
franta-hg@30
    41
	public static void main(String[] args) {
franta-hg@16
    42
franta-hg@30
    43
		try {
franta-hg@30
    44
			File vstup = new File(args[0]);
franta-hg@30
    45
			OutputStream výstup = System.out;
franta-hg@16
    46
franta-hg@30
    47
			/**
franta-hg@30
    48
			 * Serializujeme data do XML.
franta-hg@30
    49
			 * To normálně vůbec není potřeba – data se do tvaru proudu obsahujícího ostré závorky
franta-hg@30
    50
			 * vůbec nedostanou – zpracováváme události (volání javovských metod – začátky a konce
franta-hg@30
    51
			 * elementů atd.)
franta-hg@30
    52
			 * a z nich např. deserializujeme nějaké naše objekty, provádíme nějaké akce, nebo třeba
franta-hg@30
    53
			 * stavíme DOM.
franta-hg@30
    54
			 */
franta-hg@30
    55
			XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
franta-hg@30
    56
			XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(výstup);
franta-hg@30
    57
			DefaultHandler h = new EchoContentHandler(w);
franta-hg@30
    58
franta-hg@31
    59
			SAXParserFactory t = SAXParserFactory.newInstance();
franta-hg@30
    60
			SAXParser p = t.newSAXParser();
franta-hg@39
    61
franta-hg@39
    62
			if (h instanceof LexicalHandler
franta-hg@39
    63
					// TODO: add option/feature to disable LexicalHandler registration
franta-hg@39
    64
					&& false) {
franta-hg@39
    65
				try {
franta-hg@39
    66
					p.setProperty(LEXICAL_HANDLER_PROPERTY, h);
franta-hg@39
    67
				} catch (Exception e) {
franta-hg@39
    68
					log.log(Level.WARNING, "LexicalHandler registration exception:", e);
franta-hg@39
    69
					log.log(Level.WARNING,
franta-hg@39
    70
							"Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}",
franta-hg@39
    71
							new Object[]{h, p});
franta-hg@39
    72
				}
franta-hg@39
    73
			}
franta-hg@39
    74
franta-hg@30
    75
			p.parse(vstup, h);
franta-hg@30
    76
		} catch (Exception e) {
franta-hg@30
    77
			log.log(Level.SEVERE, "Error during processing: " + Arrays.toString(args), e);
franta-hg@30
    78
		}
franta-hg@21
    79
franta-hg@2
    80
	}
franta-hg@2
    81
}