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

ca.carleton.gcrc.couch.export.SchemaExportProperty Maven / Gradle / Ivy

There is a newer version: 2.2.7
Show newest version
package ca.carleton.gcrc.couch.export;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

public class SchemaExportProperty {

	static public SchemaExportProperty parseJson(JSONObject jsonExportProperty) throws Exception {

		String type = jsonExportProperty.optString("type");
		String label = jsonExportProperty.optString("label");
		String selectString = jsonExportProperty.optString("select");
		if( null == selectString ){
			throw new Exception("Attribute 'select' must be provided in an export property: "+label);
		}
		
		if( null == label ) {
			label = selectString;
		}
		
		String[] selectors = selectString.split("\\.");
		ArrayList selector = new ArrayList(selectors.length);
		for(int i=0; i selector;
	private String type;
	
	public String getLabel() {
		return label;
	}
	
	public void setLabel(String label) {
		this.label = label;
	}
	
	public List getSelector() {
		return selector;
	}
	
	public void setSelector(List selector) {
		this.selector = selector;
	}
	
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	
	public Object select(JSONObject doc){
		return select(doc,0);
	}

	private Object select(Object doc, int index){
		if( index >= selector.size() ){
			return null;
		}

		String propertyName = selector.get(index);
		Object value = null;
		
		if( doc instanceof JSONObject ) {
			JSONObject jsonObj = (JSONObject)doc;
			value = jsonObj.opt(propertyName);
			if( null != value 
			 && index < (selector.size()-1) ) {
				value = select(value, index+1);
			}
		}
		
		return value;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy