All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.holoeverywhere.builder.Document Maven / Gradle / Ivy


package org.holoeverywhere.builder;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.json.JSONArray;
import org.json.JSONObject;

public class Document implements Parseable {
    private static final Comparator COMPARATOR = new Comparator() {
        @Override
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
        }
    };
    public Map blocks = new HashMap();
    public Map data = new HashMap();
    public List include = new ArrayList();
    public String output;

    @SuppressWarnings("unchecked")
    @Override
    public Document parse(JSONObject json) {
        include.clear();
        blocks.clear();
        data.clear();
        output = null;
        if (json.has("include")) {
            JSONArray include = json.optJSONArray("include");
            for (int i = 0; i < include.length(); i++) {
                this.include.add(new IncludeRow().parse(include.optJSONObject(i)));
            }
        }
        if (json.has("blocks")) {
            JSONObject blocks = json.optJSONObject("blocks");
            Iterator keys = blocks.sortedKeys();
            while (keys.hasNext()) {
                String key = keys.next();
                this.blocks.put(key, new Block().parse(blocks.optJSONObject(key)));
            }
        }
        if (json.has("data")) {
            JSONObject data = json.optJSONObject("data");
            Iterator keys = data.sortedKeys();
            while (keys.hasNext()) {
                String key = keys.next();
                this.data.put(key, new Block().parse(data.optJSONObject(key)));
            }
        }
        if (json.has("output")) {
            output = json.optString("output");
        }
        return this;
    }

    public synchronized void process(XMLStreamWriter writer) throws XMLStreamException {
        Map blocks = new HashMap(this.blocks);
        Map dataS = new HashMap(data);
        for (IncludeRow i : include) {
            i.process(blocks, dataS);
        }
        SortedMap data = new TreeMap(COMPARATOR);
        data.putAll(dataS);
        for (Entry entry : data.entrySet()) {
            writer.writeStartElement("style");
            writer.writeAttribute("name", entry.getKey());
            entry.getValue().process(writer, blocks);
            writer.writeEndElement();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy