franta-hg@11: /** franta-hg@11: * Alt2XML franta-hg@11: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@11: * franta-hg@11: * This program is free software: you can redistribute it and/or modify franta-hg@11: * it under the terms of the GNU General Public License as published by franta-hg@11: * the Free Software Foundation, either version 3 of the License, or franta-hg@11: * (at your option) any later version. franta-hg@11: * franta-hg@11: * This program is distributed in the hope that it will be useful, franta-hg@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@11: * GNU General Public License for more details. franta-hg@11: * franta-hg@11: * You should have received a copy of the GNU General Public License franta-hg@11: * along with this program. If not, see . franta-hg@11: */ franta-hg@16: package cz.frantovo.alt2xml.cli; franta-hg@2: franta-hg@21: import java.io.File; franta-hg@2: import java.io.OutputStream; franta-hg@30: import java.util.Arrays; franta-hg@30: import java.util.logging.Level; franta-hg@30: import java.util.logging.Logger; franta-hg@2: import javax.xml.parsers.SAXParser; franta-hg@2: import javax.xml.parsers.SAXParserFactory; franta-hg@2: import javax.xml.stream.XMLOutputFactory; franta-hg@2: import javax.xml.stream.XMLStreamWriter; franta-hg@39: import org.xml.sax.ext.LexicalHandler; franta-hg@2: import org.xml.sax.helpers.DefaultHandler; franta-hg@2: franta-hg@2: /** franta-hg@2: * franta-hg@19: * @author Ing. František Kučera (frantovo.cz) franta-hg@2: */ franta-hg@2: public class CLI { franta-hg@2: franta-hg@39: public static final String LEXICAL_HANDLER_PROPERTY = "http://xml.org/sax/properties/lexical-handler"; franta-hg@30: private static final Logger log = Logger.getLogger(CLI.class.getName()); franta-hg@28: franta-hg@30: public static void main(String[] args) { franta-hg@16: franta-hg@30: try { franta-hg@30: File vstup = new File(args[0]); franta-hg@30: OutputStream výstup = System.out; franta-hg@16: franta-hg@30: /** franta-hg@30: * Serializujeme data do XML. franta-hg@30: * To normálně vůbec není potřeba – data se do tvaru proudu obsahujícího ostré závorky franta-hg@30: * vůbec nedostanou – zpracováváme události (volání javovských metod – začátky a konce franta-hg@30: * elementů atd.) franta-hg@30: * a z nich např. deserializujeme nějaké naše objekty, provádíme nějaké akce, nebo třeba franta-hg@30: * stavíme DOM. franta-hg@30: */ franta-hg@30: XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory(); franta-hg@30: XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(výstup); franta-hg@30: DefaultHandler h = new EchoContentHandler(w); franta-hg@30: franta-hg@31: SAXParserFactory t = SAXParserFactory.newInstance(); franta-hg@30: SAXParser p = t.newSAXParser(); franta-hg@39: franta-hg@39: if (h instanceof LexicalHandler franta-hg@39: // TODO: add option/feature to disable LexicalHandler registration franta-hg@39: && false) { franta-hg@39: try { franta-hg@39: p.setProperty(LEXICAL_HANDLER_PROPERTY, h); franta-hg@39: } catch (Exception e) { franta-hg@39: log.log(Level.WARNING, "LexicalHandler registration exception:", e); franta-hg@39: log.log(Level.WARNING, franta-hg@39: "Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}", franta-hg@39: new Object[]{h, p}); franta-hg@39: } franta-hg@39: } franta-hg@39: franta-hg@30: p.parse(vstup, h); franta-hg@30: } catch (Exception e) { franta-hg@30: log.log(Level.SEVERE, "Error during processing: " + Arrays.toString(args), e); franta-hg@30: } franta-hg@21: franta-hg@2: } franta-hg@2: }