java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java
changeset 78 afcb4ccc6594
parent 77 36ee0aefea27
child 80 c5816dd82cbb
     1.1 --- a/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 18:12:15 2014 +0200
     1.2 +++ b/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 18:29:44 2014 +0200
     1.3 @@ -43,15 +43,17 @@
     1.4  		outputStart();
     1.5  
     1.6  		try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) {
     1.7 -			FileContext c = new FileContext(contentHandler);
     1.8 +			FileContext fc = new FileContext(contentHandler);
     1.9  			for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) {
    1.10  				for (LINE_TYPE lineType : LINE_TYPE.values()) {
    1.11 -					boolean lineProcessed = lineType.processLine(currentLine, c);
    1.12 +					boolean lineProcessed = lineType.processLine(currentLine, fc);
    1.13  					if (lineProcessed) {
    1.14  						break;
    1.15  					}
    1.16  				}
    1.17  			}
    1.18 +			fc.outputEndSection(fc.lastSection);
    1.19 +
    1.20  		}
    1.21  
    1.22  		outputEnd();
    1.23 @@ -73,10 +75,25 @@
    1.24  	private static class FileContext {
    1.25  
    1.26  		private final Alt2ContentHandler contentHandler;
    1.27 +		private String lastSection;
    1.28  
    1.29  		public FileContext(Alt2ContentHandler contentHandler) {
    1.30  			this.contentHandler = contentHandler;
    1.31  		}
    1.32 +
    1.33 +		protected void outputStartSection(String name) throws SAXException {
    1.34 +			contentHandler.indentation(1);
    1.35 +			contentHandler.startElement(null, null, name, null);
    1.36 +			contentHandler.lineBreak();
    1.37 +		}
    1.38 +
    1.39 +		protected void outputEndSection(String name) throws SAXException {
    1.40 +			if (name != null) {
    1.41 +				contentHandler.indentation(1);
    1.42 +				contentHandler.endElement(null, null, name);
    1.43 +				contentHandler.lineBreak();
    1.44 +			}
    1.45 +		}
    1.46  	}
    1.47  
    1.48  	private static class LineContext {
    1.49 @@ -95,24 +112,33 @@
    1.50  		COMMENT("(;|#)\\s*(.*)") {
    1.51  
    1.52  					@Override
    1.53 -					public void processLine(LineContext lineContext, FileContext fileContext) {
    1.54 -						log.log(Level.FINER, "Comment: {0}", lineContext.matcher.group(2));
    1.55 +					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.56 +						log.log(Level.FINER, "Comment: {0}", lc.matcher.group(2));
    1.57  					}
    1.58  
    1.59  				},
    1.60  		SECTION("\\[(.+)\\]") {
    1.61  
    1.62  					@Override
    1.63 -					public void processLine(LineContext lineContext, FileContext fileContext) {
    1.64 -						log.log(Level.WARNING, "Section: {0}", lineContext.matcher.group(1));
    1.65 +					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.66 +						String name = lc.matcher.group(1);
    1.67 +						fc.outputEndSection(fc.lastSection);
    1.68 +						fc.outputStartSection(name);
    1.69 +						fc.lastSection = name;
    1.70  					}
    1.71  
    1.72  				},
    1.73  		ENTRY("\\s*([^=]+)\\s*=\\s*(.+)") {
    1.74  
    1.75  					@Override
    1.76 -					public void processLine(LineContext lineContext, FileContext fileContext) {
    1.77 -						log.log(Level.WARNING, "Entry: {0} = {1}", new Object[]{lineContext.matcher.group(1), lineContext.matcher.group(2)});
    1.78 +					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.79 +						String key = lc.matcher.group(1);
    1.80 +						String value = lc.matcher.group(2);
    1.81 +
    1.82 +						fc.contentHandler.indentation(2);
    1.83 +						fc.contentHandler.textElement(value, null, null, key, null);
    1.84 +						fc.contentHandler.lineBreak();
    1.85 +
    1.86  					}
    1.87  
    1.88  				},;
    1.89 @@ -123,7 +149,7 @@
    1.90  
    1.91  		private final Pattern pattern;
    1.92  
    1.93 -		protected boolean processLine(String currentLine, FileContext fileContext) {
    1.94 +		protected boolean processLine(String currentLine, FileContext fileContext) throws SAXException {
    1.95  			Matcher m = pattern.matcher(currentLine);
    1.96  			if (m.matches()) {
    1.97  				processLine(new LineContext(currentLine, m), fileContext);
    1.98 @@ -133,7 +159,7 @@
    1.99  			}
   1.100  		}
   1.101  
   1.102 -		public abstract void processLine(LineContext lineContext, FileContext fileContext);
   1.103 +		public abstract void processLine(LineContext lc, FileContext fc) throws SAXException;
   1.104  	}
   1.105  
   1.106  }