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