java/alt2xml-in-fs/src/cz/frantovo/alt2xml/in/fs/Reader.java
changeset 103 5e22fa13e016
parent 102 a7f7b9094cc3
child 106 02739f60b1ec
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/alt2xml-in-fs/src/cz/frantovo/alt2xml/in/fs/Reader.java	Tue Oct 28 16:14:09 2014 +0100
     1.3 @@ -0,0 +1,107 @@
     1.4 +/**
     1.5 + * Alt2XML
     1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package cz.frantovo.alt2xml.in.fs;
    1.22 +
    1.23 +import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
    1.24 +import java.io.File;
    1.25 +import java.io.IOException;
    1.26 +import java.net.URI;
    1.27 +import java.net.URISyntaxException;
    1.28 +import java.util.logging.Logger;
    1.29 +import org.xml.sax.Attributes;
    1.30 +import org.xml.sax.InputSource;
    1.31 +import org.xml.sax.SAXException;
    1.32 +import org.xml.sax.helpers.AttributesImpl;
    1.33 +
    1.34 +/**
    1.35 + * Reads filesystem – hierarchical structure of directories and files.
    1.36 + *
    1.37 + * @author Ing. František Kučera (frantovo.cz)
    1.38 + */
    1.39 +public class Reader extends AbstractAlt2XmlReader {
    1.40 +
    1.41 +	public static final String ROOT_ELEMENT = "fs";
    1.42 +	public static final String DIR_ELEMENT = "dir";
    1.43 +	public static final String FILE_ELEMENT = "file";
    1.44 +	public static final String ROOT_ATTRIBUTE = "root";
    1.45 +	public static final String NAME_ATTRIBUTE = "name";
    1.46 +	private static final Logger log = Logger.getLogger(Reader.class.getName());
    1.47 +
    1.48 +	@Override
    1.49 +	public void parse(InputSource input) throws IOException, SAXException {
    1.50 +		File dir = getFile(input.getSystemId());
    1.51 +
    1.52 +		outputStart(dir);
    1.53 +		outputDir(dir);
    1.54 +		outputEnd();
    1.55 +	}
    1.56 +
    1.57 +	private File getFile(String systemId) throws IOException {
    1.58 +		try {
    1.59 +			return new File(new URI(systemId));
    1.60 +		} catch (URISyntaxException e) {
    1.61 +			throw new IOException("Invalid dir URI", e);
    1.62 +		}
    1.63 +	}
    1.64 +
    1.65 +	private Attributes singleAttribute(String name, String value) {
    1.66 +		AttributesImpl attributes = new AttributesImpl();
    1.67 +		attributes.addAttribute(null, name, name, "xs:string", value);
    1.68 +		return attributes;
    1.69 +	}
    1.70 +
    1.71 +	private void outputStart(File root) throws SAXException {
    1.72 +		contentHandler.startDocument();
    1.73 +		contentHandler.lineBreak();
    1.74 +		contentHandler.startElement(null, null, ROOT_ELEMENT, singleAttribute(ROOT_ATTRIBUTE, root.getAbsolutePath()));
    1.75 +		contentHandler.lineBreak();
    1.76 +	}
    1.77 +
    1.78 +	private void outputEnd() throws SAXException {
    1.79 +		contentHandler.endElement(null, null, ROOT_ELEMENT);
    1.80 +		contentHandler.lineBreak();
    1.81 +		contentHandler.endDocument();
    1.82 +	}
    1.83 +
    1.84 +	private void outputFile(File file) throws SAXException {
    1.85 +		contentHandler.startElement(null, null, FILE_ELEMENT, singleAttribute(NAME_ATTRIBUTE, file.getName()));
    1.86 +		contentHandler.lineBreak();
    1.87 +
    1.88 +		//contentHandler.characters(file.getName());
    1.89 +
    1.90 +		contentHandler.endElement(null, null, FILE_ELEMENT);
    1.91 +		contentHandler.lineBreak();
    1.92 +	}
    1.93 +
    1.94 +	private void outputDir(File dir) throws SAXException {
    1.95 +
    1.96 +		contentHandler.startElement(null, null, DIR_ELEMENT, singleAttribute(NAME_ATTRIBUTE, dir.getName()));
    1.97 +		contentHandler.lineBreak();
    1.98 +
    1.99 +		for (File file : dir.listFiles()) {
   1.100 +			if (file.isDirectory()) {
   1.101 +				outputDir(file);
   1.102 +			} else {
   1.103 +				outputFile(file);
   1.104 +			}
   1.105 +		}
   1.106 +
   1.107 +		contentHandler.endElement(null, null, DIR_ELEMENT);
   1.108 +		contentHandler.lineBreak();
   1.109 +	}
   1.110 +}