6 package com.google.appinventor.components.runtime.util;
8 import java.util.ArrayList;
9 import java.util.Deque;
10 import java.util.LinkedList;
11 import java.util.List;
12 import java.util.Map.Entry;
13 import org.xml.sax.Attributes;
14 import org.xml.sax.helpers.DefaultHandler;
16 @SuppressWarnings(
"unchecked")
18 private static final String CONTENT_TAG =
"$content";
21 private Deque<YailDictionary> stack =
new LinkedList<>();
24 public void startElement(String uri, String localName, String qname, Attributes attributes) {
26 el.
put(
"$tag", qname);
27 el.
put(
"$namespaceUri", uri);
28 el.
put(
"$localName", localName.isEmpty() ? qname : localName);
29 if (qname.contains(
":")) {
30 String[] parts = qname.split(
":");
31 el.
put(
"$namespace", parts[0]);
33 el.
put(
"$namespace",
"");
36 for (
int i = 0; i < attributes.getLength(); i++) {
37 attrs.
put(attributes.getQName(i), attributes.getValue(i));
39 el.
put(
"$attributes", attrs);
40 el.
put(CONTENT_TAG,
new ArrayList<>());
41 if (currentElement !=
null) {
42 ((List<Object>) currentElement.
get(CONTENT_TAG)).add(el);
44 currentElement.
put(qname,
new ArrayList<>());
46 ((List<Object>) currentElement.
get(qname)).add(el);
47 stack.push(currentElement);
55 public void characters(
char[] ch,
int start,
int length) {
56 List<Object> items = (List<Object>) currentElement.
get(CONTENT_TAG);
57 if (items instanceof ArrayList) {
58 String content =
new String(ch, start, length);
59 content = content.trim();
60 if (!content.isEmpty()) {
67 public void endElement(String uri, String localName, String qname) {
68 for (Entry<Object, Object> e : currentElement.entrySet()) {
69 if (e.getValue() instanceof ArrayList) {
73 if (!stack.isEmpty()) {
74 currentElement = stack.pop();