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@103: package cz.frantovo.alt2xml.in.fs; franta-hg@2: franta-hg@17: import cz.frantovo.alt2xml.AbstractAlt2XmlReader; franta-hg@103: import java.io.File; franta-hg@2: import java.io.IOException; franta-hg@103: import java.net.URI; franta-hg@103: import java.net.URISyntaxException; franta-hg@77: import java.util.logging.Logger; franta-hg@103: import org.xml.sax.Attributes; franta-hg@2: import org.xml.sax.InputSource; franta-hg@2: import org.xml.sax.SAXException; franta-hg@92: import org.xml.sax.helpers.AttributesImpl; franta-hg@2: franta-hg@2: /** franta-hg@103: * Reads filesystem – hierarchical structure of directories and files. franta-hg@2: * franta-hg@17: * @author Ing. František Kučera (frantovo.cz) franta-hg@2: */ franta-hg@17: public class Reader extends AbstractAlt2XmlReader { franta-hg@13: franta-hg@103: public static final String ROOT_ELEMENT = "fs"; franta-hg@103: public static final String DIR_ELEMENT = "dir"; franta-hg@103: public static final String FILE_ELEMENT = "file"; franta-hg@103: public static final String ROOT_ATTRIBUTE = "root"; franta-hg@103: public static final String NAME_ATTRIBUTE = "name"; franta-hg@77: private static final Logger log = Logger.getLogger(Reader.class.getName()); franta-hg@77: franta-hg@2: @Override franta-hg@6: public void parse(InputSource input) throws IOException, SAXException { franta-hg@103: File dir = getFile(input.getSystemId()); franta-hg@77: franta-hg@103: outputStart(dir); franta-hg@103: outputDir(dir); franta-hg@59: outputEnd(); franta-hg@59: } franta-hg@59: franta-hg@103: private File getFile(String systemId) throws IOException { franta-hg@103: try { franta-hg@103: return new File(new URI(systemId)); franta-hg@103: } catch (URISyntaxException e) { franta-hg@103: throw new IOException("Invalid dir URI", e); franta-hg@103: } franta-hg@103: } franta-hg@103: franta-hg@103: private Attributes singleAttribute(String name, String value) { franta-hg@103: AttributesImpl attributes = new AttributesImpl(); franta-hg@103: attributes.addAttribute(null, name, name, "xs:string", value); franta-hg@103: return attributes; franta-hg@103: } franta-hg@103: franta-hg@103: private void outputStart(File root) throws SAXException { franta-hg@21: contentHandler.startDocument(); franta-hg@76: contentHandler.lineBreak(); franta-hg@103: contentHandler.startElement(null, null, ROOT_ELEMENT, singleAttribute(ROOT_ATTRIBUTE, root.getAbsolutePath())); franta-hg@75: contentHandler.lineBreak(); franta-hg@59: } franta-hg@59: franta-hg@59: private void outputEnd() throws SAXException { franta-hg@102: contentHandler.endElement(null, null, ROOT_ELEMENT); franta-hg@75: contentHandler.lineBreak(); franta-hg@21: contentHandler.endDocument(); franta-hg@6: } franta-hg@76: franta-hg@103: private void outputFile(File file) throws SAXException { franta-hg@103: contentHandler.startElement(null, null, FILE_ELEMENT, singleAttribute(NAME_ATTRIBUTE, file.getName())); franta-hg@103: contentHandler.lineBreak(); franta-hg@77: franta-hg@103: //contentHandler.characters(file.getName()); franta-hg@77: franta-hg@103: contentHandler.endElement(null, null, FILE_ELEMENT); franta-hg@103: contentHandler.lineBreak(); franta-hg@77: } franta-hg@77: franta-hg@103: private void outputDir(File dir) throws SAXException { franta-hg@89: franta-hg@103: contentHandler.startElement(null, null, DIR_ELEMENT, singleAttribute(NAME_ATTRIBUTE, dir.getName())); franta-hg@103: contentHandler.lineBreak(); franta-hg@77: franta-hg@103: for (File file : dir.listFiles()) { franta-hg@103: if (file.isDirectory()) { franta-hg@103: outputDir(file); franta-hg@103: } else { franta-hg@103: outputFile(file); franta-hg@81: } franta-hg@77: } franta-hg@77: franta-hg@103: contentHandler.endElement(null, null, DIR_ELEMENT); franta-hg@103: contentHandler.lineBreak(); franta-hg@77: } franta-hg@2: }