Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
*
*/
package org.javabuilders;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.javabuilders.handler.ITypeChildrenHandler;
import org.javabuilders.handler.ITypeHandler;
import org.jvyaml.YAML;
/**
* Pre-processor for YAML file to handle advanced functionality specific to Java Builders.
* Namely, it handles virtual constructors embedded in the types, as well as exploding shortcut types (i.e. those entered as a single value or a list) into a proper map
* @author Jacek Furmankiewicz
*/
public class BuilderPreProcessor {
private static final Logger logger = Logger.getLogger(BuilderPreProcessor.class.getSimpleName());
static {
logger.setLevel(Level.ALL);
}
/**
* Pre-processes the YAML object to implement advanced/enhanced JB-YAML features
* @param config Config
* @param current Current
* @throws BuildException If anything goes wrong...
*/
public static Object preprocess(BuilderConfig config, BuildProcess process, Object current, Object parent) throws BuildException {
try {
if (current instanceof Map) {
current = handleMap(config, process, current, parent);
} else if (current instanceof List) {
current = handleList(config, process, current, parent);
} else if (current instanceof String && parent == null) {
//only handle String if it is the root in a file
current = handleString(config, process, current, parent);
}
return current;
} catch (BuildException be) {
logger.severe(be.getMessage());
throw be;
} catch (Exception e) {
logger.severe(e.getMessage());
throw new BuildException(e);
}
}
@SuppressWarnings("unchecked")
private static Object handleMap(BuilderConfig config, BuildProcess process, Object current, Object parent) throws BuildException {
Map map = (Map)current;
List keysToRemove = new ArrayList();
Map propertiesToAdd = new HashMap();
for(String key : map.keySet()) {
Object value = map.get(key);
String realKey = getRealKey(key);
Class> typeClass = BuilderUtils.getClassFromAlias(process, realKey, null);
if (typeClass != null) {
//we're in business...dealing with a type here...
ITypeHandler handler = config.getTypeHandler(typeClass);
//handle types that are expressed as a list or single value
explodeShortcutTypeToMap(config, process, handler, key, map);
//if exploded, we are now sure it is a proper type Map
Map typeMap = (Map) map.get(key);
if (typeMap == null) {
//handle case where is root line in compact syntax with no children underneath
typeMap = new HashMap();
}
if (!key.equals(realKey)) {
//enhanced JB-YAML : embedded virtual constructor in the type
//explode the embedded property values into stand-alone entries in the document
explodeVirtualConstructorProperties(handler, key, realKey, typeMap);
propertiesToAdd.put(realKey, typeMap);
keysToRemove.add(key);
}
//handle aliased properties
handlePropertyAlias(config,typeClass,typeMap);
handleMappedProperties(config, typeClass, typeMap);
//handle mapped properties
/*
for(String typeMapKey : typeMap.keySet()) {
Object typeMapValue = typeMap.get(typeMapKey);
//mapped value?
Object mappedValue = TypeDefinition.getPropertyValue(config, typeClass, typeMapKey, typeMapValue);
if (!mappedValue.equals(typeMapValue)) {
typeMap.put(typeMapKey, mappedValue);
}
}
*/
//keep goin' down...
if(!(handler instanceof ITypeChildrenHandler)) {
preprocess(config, process, typeMap, current);
}
} else {
//keep looking further down the rabbit hole...
preprocess(config, process, value, current);
}
}
//remove all the keys with virtual constructors
for(String key : keysToRemove) {
map.remove(key);
}
//add their properly exploded versions
for(String key : propertiesToAdd.keySet()) {
map.put(key, propertiesToAdd.get(key));
}
return current;
}
@SuppressWarnings("unchecked")
private static Object handleList(BuilderConfig config, BuildProcess process, Object current, Object parent) throws BuildException {
List