Android Open Source - sodf Key Node






From Project

Back to project page sodf.

License

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.

Java Source Code

package lal.sodf.framework.ontology;
//from   w  ww.j  ava2 s. c  o  m
import java.util.LinkedList;
import java.util.List;

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

/**
 * A node that has one or multiple children nodes, which are also KeyNodes or KeyValueNodes
 * @author Lorenz Lehmann
 *
 */
public class KeyNode extends Node{
  /** All children of this node, which can be either keys or values */
  private LinkedList<Node> children = new LinkedList<Node>();
  
  /** Create a new node */
  public KeyNode(KeyNode parent, String key){
    super(parent, key);
  }
  
  /** Create a new node */
  public KeyNode(String key){
    super(key);
  }
  
  /**
   * Add a new child to this node
   * @param child The child, which is added
   * @return True if the child was added, false if the key already existed and the node was not added 
   */
  public boolean addChild(Node child){
    if (child == null) return false;
    if (child.getKey() == null) return false;//don't add nodes without a key
    //check if any other node already has the same content, but not for nodes with a blank key
    if (!child.getKey().equals("") && getChild(child.getKey()) != null) return false;
    child.setParent(this);
    children.add(child);
    return true;
  }
  
  /**
   * Add a list of children to this node
   * @param _children The children, which are added
   * @return True if the children were added, false if one of the keys already existed and one of the nodes was not added 
   */
  public boolean addChildren(List<Node> _children){
    boolean result = true;
    for (Node n : _children){
      if (this.addChild(n) == false) result = false;
    }
    return result;
  }
  
  /**
   * Get a specific child node of this KeyNode
   * @param _child The key of the child node
   * @return The node, or null if it was not found
   */
  public Node getChild(String key){
    for (Node n : children){
      if (n.getKey().equals(key)) return n;
    }
    return null;
  }
  
  /**
   * Remove a node from the list of children
   * @param key The key of the node to be removed
   * @return The node, which has been removed or null if it did not exist
   */
  public Node removeChild(String key){
    for (Node n : children){
      if (n.getKey().equals(key)) {
        n.setParent(null);
        children.remove(n);
        return n;
      }
    }
    return null;
  }
  
  /** Get all children of this node */
  public LinkedList<Node> getChildren(){
    return children;
  }

  /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
    return "KeyNode [key=" + key + "]";
  }

  @Override
  public KeyNode getKeyNodeChild(String key) {
    //check if the child exists
    Node selectedChild = this.getChild(key);
    if (selectedChild == null) return null;
    //make sure it is a KeyNode
    if (!(selectedChild instanceof KeyNode)) return null;
    //return the child
    return (KeyNode) selectedChild;
  }
  
  @Override
  public KeyValueNode getKeyValueNodeChild(String key) {
    //check if the child exists
    Node selectedChild = this.getChild(key);
    if (selectedChild == null) return null;
    //make sure it is a KeyNode
    if (!(selectedChild instanceof KeyValueNode)) return null;
    //return the child
    return (KeyValueNode) selectedChild;
  }
  

  @Override
  public List<KeyNode> getKeyNodeChildren() {
    List<KeyNode> results = new LinkedList<KeyNode>();
    for (Node n : children){
      if (n instanceof KeyNode) results.add((KeyNode) n);
    }
    return results;
  }

  /** 
   * Get this object and all its children as JSON representation 
   * @throws JSONException 
   */
  public JSONObject toJSON() throws JSONException{
    JSONObject result = new JSONObject();    
    //add all the children
    for (Node n : children){
      //for KeyNodes keep traversing the tree
      if (n instanceof KeyNode) result.put(n.getKey(), ((KeyNode) n).toJSON());
      else {//for KeyValue nodes stop traversing the tree but add them directly
        KeyValueNode tmp = (KeyValueNode) n;
        if (tmp.getValues().length == 1){
          //KeyValueNodes wiht only one value get added directly
          result.put(tmp.getKey(), tmp.getValues()[0]);
        }
        else if (tmp.getValues().length > 1){
          //create an array of values
          JSONArray jarr = new JSONArray();
          for (int i = 0; i < tmp.getValues().length; i++){
            jarr.put(tmp.getValues()[i]);
          }
          result.put(tmp.getKey(), jarr);
        }
        //the case of an empty KeyValueNode must not exist (unless somebody messes with the class)
      }
    }    
    return result;
  }
}




Java Source Code List

lal.apps.ontap.MainActivity.java
lal.apps.ontap.WifiVocabulary.java
lal.apps.smartfoodenvironment.activities.ExpiredActivity.java
lal.apps.smartfoodenvironment.activities.FactoryActivity.java
lal.apps.smartfoodenvironment.activities.MicrowaveActivity.java
lal.apps.smartfoodenvironment.activities.StartActivity.java
lal.apps.smartfoodenvironment.model.MicrowaveVocabulary.java
lal.apps.smartfoodenvironment.model.ProductFactory.java
lal.apps.smartfoodenvironment.model.ProductVocabulary.java
lal.sodf.example.MainActivity.java
lal.sodf.framework.SodfCallback.java
lal.sodf.framework.SodfCallback.java
lal.sodf.framework.SodfCallback.java
lal.sodf.framework.SodfFramework.java
lal.sodf.framework.SodfFramework.java
lal.sodf.framework.SodfFramework.java
lal.sodf.framework.SodfWrapper.java
lal.sodf.framework.SodfWrapper.java
lal.sodf.framework.SodfWrapper.java
lal.sodf.framework.compressor.CompressionAlgorithm.java
lal.sodf.framework.compressor.CompressionAlgorithm.java
lal.sodf.framework.compressor.CompressionAlgorithm.java
lal.sodf.framework.compressor.Compressor.java
lal.sodf.framework.compressor.Compressor.java
lal.sodf.framework.compressor.Compressor.java
lal.sodf.framework.compressor.Gzip.java
lal.sodf.framework.compressor.Gzip.java
lal.sodf.framework.compressor.Gzip.java
lal.sodf.framework.exceptions.CompressionAlgorithmNotFoundException.java
lal.sodf.framework.exceptions.CompressionAlgorithmNotFoundException.java
lal.sodf.framework.exceptions.CompressionAlgorithmNotFoundException.java
lal.sodf.framework.exceptions.DuplicateKeyException.java
lal.sodf.framework.exceptions.DuplicateKeyException.java
lal.sodf.framework.exceptions.DuplicateKeyException.java
lal.sodf.framework.exceptions.MalformedTypeException.java
lal.sodf.framework.exceptions.MalformedTypeException.java
lal.sodf.framework.exceptions.MalformedTypeException.java
lal.sodf.framework.exceptions.TagEmptyException.java
lal.sodf.framework.exceptions.TagEmptyException.java
lal.sodf.framework.exceptions.TagEmptyException.java
lal.sodf.framework.exceptions.UnformattedTagException.java
lal.sodf.framework.exceptions.UnformattedTagException.java
lal.sodf.framework.exceptions.UnformattedTagException.java
lal.sodf.framework.nfc.NfcContentWrapper.java
lal.sodf.framework.nfc.NfcContentWrapper.java
lal.sodf.framework.nfc.NfcContentWrapper.java
lal.sodf.framework.nfc.NfcHandler.java
lal.sodf.framework.nfc.NfcHandler.java
lal.sodf.framework.nfc.NfcHandler.java
lal.sodf.framework.ontology.FunctionsNode.java
lal.sodf.framework.ontology.FunctionsNode.java
lal.sodf.framework.ontology.FunctionsNode.java
lal.sodf.framework.ontology.KeyNode.java
lal.sodf.framework.ontology.KeyNode.java
lal.sodf.framework.ontology.KeyNode.java
lal.sodf.framework.ontology.KeyValueNode.java
lal.sodf.framework.ontology.KeyValueNode.java
lal.sodf.framework.ontology.KeyValueNode.java
lal.sodf.framework.ontology.MetadataNode.java
lal.sodf.framework.ontology.MetadataNode.java
lal.sodf.framework.ontology.MetadataNode.java
lal.sodf.framework.ontology.Node.java
lal.sodf.framework.ontology.Node.java
lal.sodf.framework.ontology.Node.java
lal.sodf.framework.ontology.PropertiesNode.java
lal.sodf.framework.ontology.PropertiesNode.java
lal.sodf.framework.ontology.PropertiesNode.java
lal.sodf.framework.ontology.RestInteractionNode.java
lal.sodf.framework.ontology.RestInteractionNode.java
lal.sodf.framework.ontology.RestInteractionNode.java
lal.sodf.framework.ontology.RestParameters.java
lal.sodf.framework.ontology.RestParameters.java
lal.sodf.framework.ontology.RestParameters.java
lal.sodf.framework.ontology.SodfTree.java
lal.sodf.framework.ontology.SodfTree.java
lal.sodf.framework.ontology.SodfTree.java
lal.sodf.framework.ontology.SodfVocabulary.java
lal.sodf.framework.ontology.SodfVocabulary.java
lal.sodf.framework.ontology.SodfVocabulary.java
lal.sodf.framework.parser.SodfParser.java
lal.sodf.framework.parser.SodfParser.java
lal.sodf.framework.parser.SodfParser.java