diff -r a7f7b9094cc3 -r 5e22fa13e016 java/alt2xml-in-fs/src/cz/frantovo/alt2xml/in/fs/Reader.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/alt2xml-in-fs/src/cz/frantovo/alt2xml/in/fs/Reader.java Tue Oct 28 16:14:09 2014 +0100 @@ -0,0 +1,107 @@ +/** + * Alt2XML + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.alt2xml.in.fs; + +import cz.frantovo.alt2xml.AbstractAlt2XmlReader; +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.logging.Logger; +import org.xml.sax.Attributes; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +/** + * Reads filesystem – hierarchical structure of directories and files. + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class Reader extends AbstractAlt2XmlReader { + + public static final String ROOT_ELEMENT = "fs"; + public static final String DIR_ELEMENT = "dir"; + public static final String FILE_ELEMENT = "file"; + public static final String ROOT_ATTRIBUTE = "root"; + public static final String NAME_ATTRIBUTE = "name"; + private static final Logger log = Logger.getLogger(Reader.class.getName()); + + @Override + public void parse(InputSource input) throws IOException, SAXException { + File dir = getFile(input.getSystemId()); + + outputStart(dir); + outputDir(dir); + outputEnd(); + } + + private File getFile(String systemId) throws IOException { + try { + return new File(new URI(systemId)); + } catch (URISyntaxException e) { + throw new IOException("Invalid dir URI", e); + } + } + + private Attributes singleAttribute(String name, String value) { + AttributesImpl attributes = new AttributesImpl(); + attributes.addAttribute(null, name, name, "xs:string", value); + return attributes; + } + + private void outputStart(File root) throws SAXException { + contentHandler.startDocument(); + contentHandler.lineBreak(); + contentHandler.startElement(null, null, ROOT_ELEMENT, singleAttribute(ROOT_ATTRIBUTE, root.getAbsolutePath())); + contentHandler.lineBreak(); + } + + private void outputEnd() throws SAXException { + contentHandler.endElement(null, null, ROOT_ELEMENT); + contentHandler.lineBreak(); + contentHandler.endDocument(); + } + + private void outputFile(File file) throws SAXException { + contentHandler.startElement(null, null, FILE_ELEMENT, singleAttribute(NAME_ATTRIBUTE, file.getName())); + contentHandler.lineBreak(); + + //contentHandler.characters(file.getName()); + + contentHandler.endElement(null, null, FILE_ELEMENT); + contentHandler.lineBreak(); + } + + private void outputDir(File dir) throws SAXException { + + contentHandler.startElement(null, null, DIR_ELEMENT, singleAttribute(NAME_ATTRIBUTE, dir.getName())); + contentHandler.lineBreak(); + + for (File file : dir.listFiles()) { + if (file.isDirectory()) { + outputDir(file); + } else { + outputFile(file); + } + } + + contentHandler.endElement(null, null, DIR_ELEMENT); + contentHandler.lineBreak(); + } +}