lib-input: Alt2ContentHandler - extended wrapper
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 06 Sep 2014 17:17:37 +0200
changeset 75ed9ec17aa67f
parent 74 6d1fc2895273
child 76 56b731d14aec
lib-input: Alt2ContentHandler - extended wrapper
java/alt2xml-in-properties/src/cz/frantovo/alt2xml/in/properties/Reader.java
java/alt2xml-lib-input/src/cz/frantovo/alt2xml/AbstractAlt2XmlReader.java
java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Alt2ContentHandler.java
     1.1 --- a/java/alt2xml-in-properties/src/cz/frantovo/alt2xml/in/properties/Reader.java	Sat Sep 06 15:06:41 2014 +0200
     1.2 +++ b/java/alt2xml-in-properties/src/cz/frantovo/alt2xml/in/properties/Reader.java	Sat Sep 06 17:17:37 2014 +0200
     1.3 @@ -109,11 +109,11 @@
     1.4  		contentHandler.startDocument();
     1.5  		contentHandler.startElement(null, null, "properties", null);
     1.6  
     1.7 -		outputLineBreak();
     1.8 +		contentHandler.lineBreak();
     1.9  	}
    1.10  
    1.11  	private void outputProperty(String key, String value) throws SAXException {
    1.12 -		outputIndentation();
    1.13 +		contentHandler.indentation(1);
    1.14  
    1.15  		AttributesImpl attributes = new AttributesImpl();
    1.16  		attributes.addAttribute(null, "name", "name", "xs:string", key);
    1.17 @@ -122,7 +122,7 @@
    1.18  		contentHandler.characters(value.toCharArray(), 0, value.length());
    1.19  		contentHandler.endElement(null, null, "property");
    1.20  
    1.21 -		outputLineBreak();
    1.22 +		contentHandler.lineBreak();
    1.23  	}
    1.24  
    1.25  	private void outputProperties(Properties loadedData) throws SAXException {
    1.26 @@ -133,21 +133,9 @@
    1.27  		}
    1.28  	}
    1.29  
    1.30 -	private void outputIndentation() throws SAXException {
    1.31 -		outputText("\t");
    1.32 -	}
    1.33 -
    1.34 -	private void outputLineBreak() throws SAXException {
    1.35 -		outputText("\n");
    1.36 -	}
    1.37 -
    1.38 -	private void outputText(String text) throws SAXException {
    1.39 -		contentHandler.characters(text.toCharArray(), 0, text.length());
    1.40 -	}
    1.41 -
    1.42  	private void outputEnd() throws SAXException {
    1.43  		contentHandler.endElement(null, null, "properties");
    1.44 -		outputLineBreak();
    1.45 +		contentHandler.lineBreak();
    1.46  		contentHandler.endDocument();
    1.47  	}
    1.48  }
     2.1 --- a/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/AbstractAlt2XmlReader.java	Sat Sep 06 15:06:41 2014 +0200
     2.2 +++ b/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/AbstractAlt2XmlReader.java	Sat Sep 06 17:17:37 2014 +0200
     2.3 @@ -17,6 +17,7 @@
     2.4   */
     2.5  package cz.frantovo.alt2xml;
     2.6  
     2.7 +import cz.frantovo.alt2xml.in.Alt2ContentHandler;
     2.8  import java.io.IOException;
     2.9  import java.util.HashMap;
    2.10  import java.util.Map;
    2.11 @@ -38,7 +39,7 @@
    2.12  public abstract class AbstractAlt2XmlReader implements XMLReader {
    2.13  
    2.14  	private static final String PROPERTY_BASE_URL = "https://alt2xml.globalcode.info/sax-property/";
    2.15 -	protected ContentHandler contentHandler;
    2.16 +	protected Alt2ContentHandler contentHandler;
    2.17  	protected ErrorHandler errorHandler;
    2.18  	protected DTDHandler dtdHandler;
    2.19  	protected EntityResolver entityResolver;
    2.20 @@ -113,11 +114,11 @@
    2.21  
    2.22  	@Override
    2.23  	public void setContentHandler(ContentHandler contentHandler) {
    2.24 -		this.contentHandler = contentHandler;
    2.25 +		this.contentHandler = new Alt2ContentHandler(contentHandler);
    2.26  	}
    2.27  
    2.28  	@Override
    2.29 -	public ContentHandler getContentHandler() {
    2.30 +	public Alt2ContentHandler getContentHandler() {
    2.31  		return contentHandler;
    2.32  	}
    2.33  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Alt2ContentHandler.java	Sat Sep 06 17:17:37 2014 +0200
     3.3 @@ -0,0 +1,113 @@
     3.4 +/**
     3.5 + * Alt2XML
     3.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, either version 3 of the License, or
    3.11 + * (at your option) any later version.
    3.12 + *
    3.13 + * This program is distributed in the hope that it will be useful,
    3.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    3.16 + * GNU General Public License for more details.
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License
    3.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    3.20 + */
    3.21 +package cz.frantovo.alt2xml.in;
    3.22 +
    3.23 +import org.xml.sax.Attributes;
    3.24 +import org.xml.sax.ContentHandler;
    3.25 +import org.xml.sax.Locator;
    3.26 +import org.xml.sax.SAXException;
    3.27 +
    3.28 +/**
    3.29 + * Improved wrapper for SAX ContentHandler.
    3.30 + *
    3.31 + * @author Ing. František Kučera (frantovo.cz)
    3.32 + */
    3.33 +public class Alt2ContentHandler implements ContentHandler {
    3.34 +
    3.35 +	private final ContentHandler handler;
    3.36 +
    3.37 +	public Alt2ContentHandler(ContentHandler handler) {
    3.38 +		this.handler = handler;
    3.39 +	}
    3.40 +
    3.41 +	public void lineBreak() throws SAXException {
    3.42 +		// TODO: ignorableWhitespace()
    3.43 +		characters("\n");
    3.44 +	}
    3.45 +
    3.46 +	public void indentation(int level) throws SAXException {
    3.47 +		for (int i = 0; i < level; i++) {
    3.48 +			// TODO: ignorableWhitespace()
    3.49 +			characters("\t");
    3.50 +		}
    3.51 +	}
    3.52 +
    3.53 +	public void characters(String text) throws SAXException {
    3.54 +		handler.characters(text.toCharArray(), 0, text.length());
    3.55 +	}
    3.56 +
    3.57 +	public void ignorableWhitespace(String text) throws SAXException {
    3.58 +		handler.ignorableWhitespace(text.toCharArray(), 0, text.length());
    3.59 +	}
    3.60 +
    3.61 +	// ---------------------------------------------------------------------------------------------
    3.62 +	@Override
    3.63 +	public void setDocumentLocator(Locator locator) {
    3.64 +		handler.setDocumentLocator(locator);
    3.65 +	}
    3.66 +
    3.67 +	@Override
    3.68 +	public void startDocument() throws SAXException {
    3.69 +		handler.startDocument();
    3.70 +	}
    3.71 +
    3.72 +	@Override
    3.73 +	public void endDocument() throws SAXException {
    3.74 +		handler.endDocument();
    3.75 +	}
    3.76 +
    3.77 +	@Override
    3.78 +	public void startPrefixMapping(String prefix, String uri) throws SAXException {
    3.79 +		handler.startPrefixMapping(prefix, uri);
    3.80 +	}
    3.81 +
    3.82 +	@Override
    3.83 +	public void endPrefixMapping(String prefix) throws SAXException {
    3.84 +		handler.endPrefixMapping(prefix);
    3.85 +	}
    3.86 +
    3.87 +	@Override
    3.88 +	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    3.89 +		handler.startElement(uri, localName, qName, atts);
    3.90 +	}
    3.91 +
    3.92 +	@Override
    3.93 +	public void endElement(String uri, String localName, String qName) throws SAXException {
    3.94 +		handler.endElement(uri, localName, qName);
    3.95 +	}
    3.96 +
    3.97 +	@Override
    3.98 +	public void characters(char[] ch, int start, int length) throws SAXException {
    3.99 +		handler.characters(ch, start, length);
   3.100 +	}
   3.101 +
   3.102 +	@Override
   3.103 +	public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
   3.104 +		handler.ignorableWhitespace(ch, start, length);
   3.105 +	}
   3.106 +
   3.107 +	@Override
   3.108 +	public void processingInstruction(String target, String data) throws SAXException {
   3.109 +		handler.processingInstruction(target, data);
   3.110 +	}
   3.111 +
   3.112 +	@Override
   3.113 +	public void skippedEntity(String name) throws SAXException {
   3.114 +		handler.skippedEntity(name);
   3.115 +	}
   3.116 +}