Back to project page sodf.
The source code is released under:
Copyright (c) 2013 Lorenz Lehmann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Sof...
If you think the Android project sodf listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package lal.sodf.framework.parser; //from w ww .ja v a 2 s. c o m import java.util.Iterator; import java.util.LinkedList; import lal.sodf.framework.ontology.FunctionsNode; import lal.sodf.framework.ontology.KeyNode; import lal.sodf.framework.ontology.KeyValueNode; import lal.sodf.framework.ontology.MetadataNode; import lal.sodf.framework.ontology.PropertiesNode; import lal.sodf.framework.ontology.SodfTree; import lal.sodf.framework.ontology.SodfVocabulary; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * A parser, which creates a SODF tree from the serialized version or serializes a SODF tree * @author Lorenz Lehmann * */ public class SodfParser { /** * Create a SodfTree from its JSON representation * @param serialization The JSON representation of the SodfTree * @return The deserialized SodfTree */ public static SodfTree deserializeTree(String serialization){ SodfTree st = new SodfTree(); KeyNode keyNode = SodfParser.deserializeNode(serialization); if (keyNode == null) return st; st.setRoot(keyNode); return st; } /** * Serialize a SodfTree. This function will create a valid JSON object with the data of the SodfTree * @param node The SodfTree to be serialized * @return The JSON representation as String */ public static String serializeTree(SodfTree tree){ //simply serialize the root return SodfParser.serializeNode(tree.getRoot()); } /** * Create a Node from its JSON representation * @param serialization The JSON representation of the Node * @return The deserialized Node */ public static KeyNode deserializeNode(String serialization){ try { JSONObject json = new JSONObject(serialization); //create an empty KeyNode as root KeyNode root = new KeyNode(""); SodfParser.deserializeNode(root, json); //return the final tree return root; } catch(JSONException e){ e.printStackTrace(); return null; } } /** Deserialize a node. Used by {@link deserializeNode()} for recursive calls*/ private static void deserializeNode(KeyNode root, JSONObject json) throws JSONException{ Iterator<?> it = json.keys(); String key; //iterate over all the elements of the JSON object while (it.hasNext()){ key = (String) it.next(); //if the key leads to an object it most likely belongs to a KeyNode if (json.get(key) instanceof JSONObject) { KeyNode kn; //check if a special node should be created for this key if (key.equals(SodfVocabulary.SODF_PROPERTIES)) kn = new PropertiesNode(); else if (key.equals(SodfVocabulary.SODF_FUNCTIONS)) kn = new FunctionsNode(); else if (key.equals(SodfVocabulary.SODF_METADATA)) kn = new MetadataNode(); else kn = new KeyNode(key); //the prefixes subtree has to be added in a special way, all other children are added directly if (key.equals(SodfVocabulary.SODF_PREFIXES)) ((MetadataNode) root).setPrefixesSubtree(kn); else root.addChild(kn); //continue traversing the tree SodfParser.deserializeNode(kn, (JSONObject) json.get(key)); } //arrays are only allowed in KeyValueNodes so it has to be a KeyValueNode else if (json.get(key) instanceof JSONArray) { JSONArray arr = (JSONArray) json.get(key); LinkedList<String> values = new LinkedList<String>(); for (int i = 0; i < arr.length(); i++){ values.add(arr.get(i).toString()); } root.addChild(new KeyValueNode(key, values.toArray(new String[values.size()]))); } else {//the attached child is a value root.addChild(new KeyValueNode(key, json.get(key).toString())); } } } /** * Serialize a KeyNode. This function will create a valid JSON object with the data of the node. * If you want to serialize a KeyValueNode use a KeyNode with an empty key as root. * @param node The node to be serialized * @return The JSON representation as String or null if an error occured when parsing the object */ public static String serializeNode(KeyNode node){ if (node == null) return null; try { //if the root is empty ignore it if (node.getKey().equals("")) return node.toJSON().toString(); else { //otherwise use its key to for all of its contents JSONObject root = new JSONObject(); root.put(node.getKey(), node.toJSON()); return root.toString(); } } catch (JSONException e) { e.printStackTrace(); return null; } } }