java/alt2xml-in-fs/src/cz/frantovo/alt2xml/in/fs/Reader.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 28 Oct 2014 16:14:09 +0100
changeset 103 5e22fa13e016
parent 102 java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java@a7f7b9094cc3
child 106 02739f60b1ec
permissions -rw-r--r--
in-fs: basics
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.alt2xml.in.fs;
    19 
    20 import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
    21 import java.io.File;
    22 import java.io.IOException;
    23 import java.net.URI;
    24 import java.net.URISyntaxException;
    25 import java.util.logging.Logger;
    26 import org.xml.sax.Attributes;
    27 import org.xml.sax.InputSource;
    28 import org.xml.sax.SAXException;
    29 import org.xml.sax.helpers.AttributesImpl;
    30 
    31 /**
    32  * Reads filesystem – hierarchical structure of directories and files.
    33  *
    34  * @author Ing. František Kučera (frantovo.cz)
    35  */
    36 public class Reader extends AbstractAlt2XmlReader {
    37 
    38 	public static final String ROOT_ELEMENT = "fs";
    39 	public static final String DIR_ELEMENT = "dir";
    40 	public static final String FILE_ELEMENT = "file";
    41 	public static final String ROOT_ATTRIBUTE = "root";
    42 	public static final String NAME_ATTRIBUTE = "name";
    43 	private static final Logger log = Logger.getLogger(Reader.class.getName());
    44 
    45 	@Override
    46 	public void parse(InputSource input) throws IOException, SAXException {
    47 		File dir = getFile(input.getSystemId());
    48 
    49 		outputStart(dir);
    50 		outputDir(dir);
    51 		outputEnd();
    52 	}
    53 
    54 	private File getFile(String systemId) throws IOException {
    55 		try {
    56 			return new File(new URI(systemId));
    57 		} catch (URISyntaxException e) {
    58 			throw new IOException("Invalid dir URI", e);
    59 		}
    60 	}
    61 
    62 	private Attributes singleAttribute(String name, String value) {
    63 		AttributesImpl attributes = new AttributesImpl();
    64 		attributes.addAttribute(null, name, name, "xs:string", value);
    65 		return attributes;
    66 	}
    67 
    68 	private void outputStart(File root) throws SAXException {
    69 		contentHandler.startDocument();
    70 		contentHandler.lineBreak();
    71 		contentHandler.startElement(null, null, ROOT_ELEMENT, singleAttribute(ROOT_ATTRIBUTE, root.getAbsolutePath()));
    72 		contentHandler.lineBreak();
    73 	}
    74 
    75 	private void outputEnd() throws SAXException {
    76 		contentHandler.endElement(null, null, ROOT_ELEMENT);
    77 		contentHandler.lineBreak();
    78 		contentHandler.endDocument();
    79 	}
    80 
    81 	private void outputFile(File file) throws SAXException {
    82 		contentHandler.startElement(null, null, FILE_ELEMENT, singleAttribute(NAME_ATTRIBUTE, file.getName()));
    83 		contentHandler.lineBreak();
    84 
    85 		//contentHandler.characters(file.getName());
    86 
    87 		contentHandler.endElement(null, null, FILE_ELEMENT);
    88 		contentHandler.lineBreak();
    89 	}
    90 
    91 	private void outputDir(File dir) throws SAXException {
    92 
    93 		contentHandler.startElement(null, null, DIR_ELEMENT, singleAttribute(NAME_ATTRIBUTE, dir.getName()));
    94 		contentHandler.lineBreak();
    95 
    96 		for (File file : dir.listFiles()) {
    97 			if (file.isDirectory()) {
    98 				outputDir(file);
    99 			} else {
   100 				outputFile(file);
   101 			}
   102 		}
   103 
   104 		contentHandler.endElement(null, null, DIR_ELEMENT);
   105 		contentHandler.lineBreak();
   106 	}
   107 }