AI2 Component  (Version nb184)
XmlParser.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2019 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime.util;
7 
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;
15 
16 @SuppressWarnings("unchecked")
17 public class XmlParser extends DefaultHandler {
18  private static final String CONTENT_TAG = "$content";
19  private YailDictionary root = null;
20  private YailDictionary currentElement = null;
21  private Deque<YailDictionary> stack = new LinkedList<>();
22 
23  @Override
24  public void startElement(String uri, String localName, String qname, Attributes attributes) {
25  YailDictionary el = new YailDictionary();
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]);
32  } else {
33  el.put("$namespace", "");
34  }
35  YailDictionary attrs = new YailDictionary();
36  for (int i = 0; i < attributes.getLength(); i++) {
37  attrs.put(attributes.getQName(i), attributes.getValue(i));
38  }
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);
43  if (!currentElement.containsKey(qname)) {
44  currentElement.put(qname, new ArrayList<>());
45  }
46  ((List<Object>) currentElement.get(qname)).add(el);
47  stack.push(currentElement);
48  } else {
49  root = el;
50  }
51  currentElement = el;
52  }
53 
54  @Override
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()) {
61  items.add(content);
62  }
63  }
64  }
65 
66  @Override
67  public void endElement(String uri, String localName, String qname) {
68  for (Entry<Object, Object> e : currentElement.entrySet()) {
69  if (e.getValue() instanceof ArrayList) {
70  e.setValue(YailList.makeList((List<?>) e.getValue()));
71  }
72  }
73  if (!stack.isEmpty()) {
74  currentElement = stack.pop();
75  }
76  }
77 
79  return root;
80  }
81 }
com.google.appinventor.components.runtime.util.YailDictionary.get
Object get(Object key)
Definition: YailDictionary.java:516
com.google.appinventor.components.runtime.util.YailList
Definition: YailList.java:26
com.google.appinventor.components.runtime.util.XmlParser.getRoot
YailDictionary getRoot()
Definition: XmlParser.java:78
com.google.appinventor.components.runtime.util.YailList.makeList
static YailList makeList(Object[] objects)
Definition: YailList.java:59
com.google.appinventor.components.runtime.util.YailDictionary.containsKey
boolean containsKey(Object key)
Definition: YailDictionary.java:500
com.google.appinventor.components.runtime.util.XmlParser.startElement
void startElement(String uri, String localName, String qname, Attributes attributes)
Definition: XmlParser.java:24
com.google.appinventor.components.runtime.util.XmlParser.characters
void characters(char[] ch, int start, int length)
Definition: XmlParser.java:55
com.google.appinventor.components.runtime.util.XmlParser.endElement
void endElement(String uri, String localName, String qname)
Definition: XmlParser.java:67
com.google.appinventor.components.runtime.util.YailDictionary
Definition: YailDictionary.java:32
com.google.appinventor.components.runtime.util.XmlParser
Definition: XmlParser.java:17
com.google.appinventor.components.runtime.util.YailDictionary.put
Object put(Object key, Object value)
Definition: YailDictionary.java:524