java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/JsonSimpleContentHandler.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:56:03 +0200
changeset 111 e4900596abdb
parent 74 6d1fc2895273
permissions -rw-r--r--
fix license version: GNU GPLv3
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@111
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@11
     8
 *
franta-hg@11
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@11
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@11
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@11
    12
 * GNU General Public License for more details.
franta-hg@11
    13
 *
franta-hg@11
    14
 * You should have received a copy of the GNU General Public License
franta-hg@11
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@11
    16
 */
franta-hg@14
    17
package cz.frantovo.alt2xml.in.json;
franta-hg@2
    18
franta-hg@2
    19
import java.io.IOException;
franta-hg@2
    20
import java.util.Stack;
franta-hg@2
    21
import org.json.simple.parser.ParseException;
franta-hg@2
    22
import org.xml.sax.ContentHandler;
franta-hg@2
    23
import org.xml.sax.SAXException;
franta-hg@2
    24
franta-hg@2
    25
/**
franta-hg@4
    26
 *
franta-hg@19
    27
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@2
    28
 */
franta-hg@2
    29
public class JsonSimpleContentHandler implements org.json.simple.parser.ContentHandler {
franta-hg@2
    30
franta-hg@2
    31
	/** Sem vypisujeme XML události */
franta-hg@20
    32
	private final ContentHandler saxVýstup;
franta-hg@2
    33
	/** Musíme si pamatovat polohu v XML stromu, abychom věděli, kterou značku kdy uzavřít */
franta-hg@20
    34
	private final Stack<String> poloha = new Stack<>();
franta-hg@3
    35
	/**
franta-hg@3
    36
	 * Po textových uzlech vkládáme konce elementů rovnou,
franta-hg@74
    37
	 * ale pokud jeden element končí hned po jiném,
franta-hg@3
    38
	 * vložíme mezi ně ještě konec řádku a odsazení.
franta-hg@3
    39
	 */
franta-hg@3
    40
	private boolean zalomitŘádek = false;
franta-hg@2
    41
franta-hg@2
    42
	public JsonSimpleContentHandler(ContentHandler saxVýstup) {
franta-hg@2
    43
		this.saxVýstup = saxVýstup;
franta-hg@2
    44
	}
franta-hg@2
    45
franta-hg@3
    46
	private void začniElement(String název) throws IOException {
franta-hg@3
    47
		try {
franta-hg@3
    48
			vložOdsazení();
franta-hg@3
    49
			saxVýstup.startElement(null, null, název, null);
franta-hg@3
    50
			poloha.push(název);
franta-hg@3
    51
		} catch (SAXException e) {
franta-hg@3
    52
			throw new IOException("Chyba při začátku elementu.", e);
franta-hg@3
    53
		}
franta-hg@3
    54
	}
franta-hg@3
    55
franta-hg@3
    56
	private void ukončiElement() throws IOException {
franta-hg@3
    57
		try {
franta-hg@3
    58
			String značka = poloha.pop();
franta-hg@3
    59
			if (zalomitŘádek) {
franta-hg@3
    60
				vložOdsazení();
franta-hg@3
    61
			}
franta-hg@3
    62
			zalomitŘádek = true;
franta-hg@3
    63
			saxVýstup.endElement(null, null, značka);
franta-hg@3
    64
		} catch (SAXException e) {
franta-hg@3
    65
			throw new IOException("Chyba při ukončování elementu.", e);
franta-hg@3
    66
		}
franta-hg@3
    67
	}
franta-hg@3
    68
franta-hg@3
    69
	private void vložOdsazení() throws IOException {
franta-hg@3
    70
		/**
franta-hg@3
    71
		 * TODO: ignorableWhitespace() ?
franta-hg@3
    72
		 */
franta-hg@3
    73
		vložText("\n");
franta-hg@3
    74
		for (int i = 0; i < poloha.size(); i++) {
franta-hg@3
    75
			vložText("\t");
franta-hg@3
    76
		}
franta-hg@3
    77
	}
franta-hg@3
    78
franta-hg@3
    79
	private void vložText(String text) throws IOException {
franta-hg@3
    80
		try {
franta-hg@3
    81
			saxVýstup.characters(text.toCharArray(), 0, text.length());
franta-hg@3
    82
		} catch (SAXException e) {
franta-hg@3
    83
			throw new IOException("Chyba při vkládání textu.", e);
franta-hg@3
    84
		}
franta-hg@3
    85
	}
franta-hg@3
    86
franta-hg@2
    87
	@Override
franta-hg@2
    88
	public void startJSON() throws ParseException, IOException {
franta-hg@2
    89
		try {
franta-hg@2
    90
			saxVýstup.startDocument();
franta-hg@3
    91
			začniElement("objekt");
franta-hg@2
    92
		} catch (SAXException e) {
franta-hg@3
    93
			throw new IOException("Chyba při začátku dokumentu.", e);
franta-hg@2
    94
		}
franta-hg@2
    95
	}
franta-hg@2
    96
franta-hg@2
    97
	@Override
franta-hg@2
    98
	public void endJSON() throws ParseException, IOException {
franta-hg@2
    99
		try {
franta-hg@3
   100
			ukončiElement();
franta-hg@3
   101
			vložText("\n");
franta-hg@2
   102
			saxVýstup.endDocument();
franta-hg@2
   103
		} catch (SAXException e) {
franta-hg@2
   104
			throw new IOException(e);
franta-hg@2
   105
		}
franta-hg@2
   106
	}
franta-hg@2
   107
franta-hg@2
   108
	@Override
franta-hg@2
   109
	public boolean startObject() throws ParseException, IOException {
franta-hg@74
   110
		/**
franta-hg@74
   111
		 * TODO: zachovat hranice mezi objekty, které jsou prvkem pole
franta-hg@74
   112
		 * "phoneNumber": [
franta-hg@74
   113
		 * { "type": "home", "number": "212 555-1234" },
franta-hg@74
   114
		 * { "type": "fax", "number": "646 555-4567" }
franta-hg@74
   115
		 * ]
franta-hg@74
   116
		 *
franta-hg@74
   117
		 */
franta-hg@74
   118
franta-hg@2
   119
		// System.err.println("startObject");
franta-hg@2
   120
		return true;
franta-hg@2
   121
	}
franta-hg@2
   122
franta-hg@2
   123
	@Override
franta-hg@2
   124
	public boolean endObject() throws ParseException, IOException {
franta-hg@2
   125
		// System.err.println("endObject");
franta-hg@2
   126
		return true;
franta-hg@2
   127
	}
franta-hg@2
   128
franta-hg@2
   129
	@Override
franta-hg@2
   130
	public boolean startObjectEntry(String key) throws ParseException, IOException {
franta-hg@3
   131
		začniElement(key);
franta-hg@2
   132
		return true;
franta-hg@2
   133
	}
franta-hg@2
   134
franta-hg@2
   135
	@Override
franta-hg@2
   136
	public boolean endObjectEntry() throws ParseException, IOException {
franta-hg@3
   137
		ukončiElement();
franta-hg@2
   138
		// System.err.println("endObjectEntry");
franta-hg@2
   139
		return true;
franta-hg@2
   140
	}
franta-hg@2
   141
franta-hg@2
   142
	@Override
franta-hg@2
   143
	public boolean startArray() throws ParseException, IOException {
franta-hg@2
   144
		// System.err.println("startArray");
franta-hg@2
   145
		return true;
franta-hg@2
   146
	}
franta-hg@2
   147
franta-hg@2
   148
	@Override
franta-hg@2
   149
	public boolean endArray() throws ParseException, IOException {
franta-hg@2
   150
		// System.err.println("endArray");
franta-hg@2
   151
		return true;
franta-hg@2
   152
	}
franta-hg@2
   153
franta-hg@2
   154
	@Override
franta-hg@2
   155
	public boolean primitive(Object value) throws ParseException, IOException {
franta-hg@3
   156
		vložText(String.valueOf(value));
franta-hg@3
   157
		zalomitŘádek = false;
franta-hg@2
   158
		return true;
franta-hg@2
   159
	}
franta-hg@2
   160
}