# HG changeset patch # User František Kučera # Date 1410020984 -7200 # Node ID afcb4ccc6594711c8beff63d1017a6e09d1bcca2 # Parent 36ee0aefea273de7875c05824e4d528e5b26a9bd in-ini: first working version diff -r 36ee0aefea27 -r afcb4ccc6594 java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java --- a/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java Sat Sep 06 18:12:15 2014 +0200 +++ b/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java Sat Sep 06 18:29:44 2014 +0200 @@ -43,15 +43,17 @@ outputStart(); try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) { - FileContext c = new FileContext(contentHandler); + FileContext fc = new FileContext(contentHandler); for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) { for (LINE_TYPE lineType : LINE_TYPE.values()) { - boolean lineProcessed = lineType.processLine(currentLine, c); + boolean lineProcessed = lineType.processLine(currentLine, fc); if (lineProcessed) { break; } } } + fc.outputEndSection(fc.lastSection); + } outputEnd(); @@ -73,10 +75,25 @@ private static class FileContext { private final Alt2ContentHandler contentHandler; + private String lastSection; public FileContext(Alt2ContentHandler contentHandler) { this.contentHandler = contentHandler; } + + protected void outputStartSection(String name) throws SAXException { + contentHandler.indentation(1); + contentHandler.startElement(null, null, name, null); + contentHandler.lineBreak(); + } + + protected void outputEndSection(String name) throws SAXException { + if (name != null) { + contentHandler.indentation(1); + contentHandler.endElement(null, null, name); + contentHandler.lineBreak(); + } + } } private static class LineContext { @@ -95,24 +112,33 @@ COMMENT("(;|#)\\s*(.*)") { @Override - public void processLine(LineContext lineContext, FileContext fileContext) { - log.log(Level.FINER, "Comment: {0}", lineContext.matcher.group(2)); + public void processLine(LineContext lc, FileContext fc) throws SAXException { + log.log(Level.FINER, "Comment: {0}", lc.matcher.group(2)); } }, SECTION("\\[(.+)\\]") { @Override - public void processLine(LineContext lineContext, FileContext fileContext) { - log.log(Level.WARNING, "Section: {0}", lineContext.matcher.group(1)); + public void processLine(LineContext lc, FileContext fc) throws SAXException { + String name = lc.matcher.group(1); + fc.outputEndSection(fc.lastSection); + fc.outputStartSection(name); + fc.lastSection = name; } }, ENTRY("\\s*([^=]+)\\s*=\\s*(.+)") { @Override - public void processLine(LineContext lineContext, FileContext fileContext) { - log.log(Level.WARNING, "Entry: {0} = {1}", new Object[]{lineContext.matcher.group(1), lineContext.matcher.group(2)}); + public void processLine(LineContext lc, FileContext fc) throws SAXException { + String key = lc.matcher.group(1); + String value = lc.matcher.group(2); + + fc.contentHandler.indentation(2); + fc.contentHandler.textElement(value, null, null, key, null); + fc.contentHandler.lineBreak(); + } },; @@ -123,7 +149,7 @@ private final Pattern pattern; - protected boolean processLine(String currentLine, FileContext fileContext) { + protected boolean processLine(String currentLine, FileContext fileContext) throws SAXException { Matcher m = pattern.matcher(currentLine); if (m.matches()) { processLine(new LineContext(currentLine, m), fileContext); @@ -133,7 +159,7 @@ } } - public abstract void processLine(LineContext lineContext, FileContext fileContext); + public abstract void processLine(LineContext lc, FileContext fc) throws SAXException; } } diff -r 36ee0aefea27 -r afcb4ccc6594 java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Alt2ContentHandler.java --- a/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Alt2ContentHandler.java Sat Sep 06 18:12:15 2014 +0200 +++ b/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Alt2ContentHandler.java Sat Sep 06 18:29:44 2014 +0200 @@ -55,6 +55,12 @@ handler.ignorableWhitespace(text.toCharArray(), 0, text.length()); } + public void textElement(String text, String uri, String localName, String qName, Attributes atts) throws SAXException { + startElement(uri, localName, qName, atts); + characters(text); + endElement(uri, localName, qName); + } + // --------------------------------------------------------------------------------------------- @Override public void setDocumentLocator(Locator locator) {