3 * Copyright © 2014 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package cz.frantovo.alt2xml.in.fs;
20 import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
22 import java.io.IOException;
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;
32 * Reads filesystem – hierarchical structure of directories and files.
34 * @author Ing. František Kučera (frantovo.cz)
36 public class Reader extends AbstractAlt2XmlReader {
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());
46 public void parse(InputSource input) throws IOException, SAXException {
47 File dir = getFile(input.getSystemId());
54 private File getFile(String systemId) throws IOException {
56 return new File(new URI(systemId));
57 } catch (URISyntaxException e) {
58 throw new IOException("Invalid dir URI", e);
62 private Attributes singleAttribute(String name, String value) {
63 AttributesImpl attributes = new AttributesImpl();
64 attributes.addAttribute(null, name, name, "xs:string", value);
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();
75 private void outputEnd() throws SAXException {
76 contentHandler.endElement(null, null, ROOT_ELEMENT);
77 contentHandler.lineBreak();
78 contentHandler.endDocument();
81 private void outputFile(File file) throws SAXException {
82 contentHandler.startElement(null, null, FILE_ELEMENT, singleAttribute(NAME_ATTRIBUTE, file.getName()));
83 contentHandler.lineBreak();
85 //contentHandler.characters(file.getName());
87 contentHandler.endElement(null, null, FILE_ELEMENT);
88 contentHandler.lineBreak();
91 private void outputDir(File dir) throws SAXException {
93 contentHandler.startElement(null, null, DIR_ELEMENT, singleAttribute(NAME_ATTRIBUTE, dir.getName()));
94 contentHandler.lineBreak();
96 for (File file : dir.listFiles()) {
97 if (file.isDirectory()) {
104 contentHandler.endElement(null, null, DIR_ELEMENT);
105 contentHandler.lineBreak();