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