org.mapfish.print.processor.ProcessorUtils Maven / Gradle / Ivy
package org.mapfish.print.processor;
import com.google.common.base.Strings;
import com.google.common.collect.BiMap;
import org.mapfish.print.ExceptionUtils;
import org.mapfish.print.output.Values;
import org.mapfish.print.parser.HasDefaultValue;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static org.mapfish.print.parser.ParserUtils.getAllAttributes;
/**
* Shared methods for working with processor.
*
* @author Jesse on 6/26/2014.
*/
public final class ProcessorUtils {
private ProcessorUtils() {
// do nothing
}
/**
* Create the input object required by the processor and populate all the fields from the values object.
*
* If {@link Processor#createInputParameter()} returns an instance of values then the values object will be returned.
*
* @param processor the processor that the input object will be for.
* @param values the object containing the values to put into the input object
* @param type of the processor input object
* @param type of the processor output object
*/
public static In populateInputParameter(final Processor processor, final Values values) {
In inputObject = processor.createInputParameter();
if (inputObject instanceof Values) {
@SuppressWarnings("unchecked")
final In castValues = (In) values;
return castValues;
}
if (inputObject != null) {
Collection fields = getAllAttributes(inputObject.getClass());
for (Field field : fields) {
String name = getInputValueName(processor.getOutputPrefix(),
processor.getInputMapperBiMap(), field.getName());
Object value;
if (field.getType() == Values.class) {
value = values;
} else {
value = values.getObject(name, Object.class);
}
if (value != null) {
try {
field.set(inputObject, value);
} catch (IllegalAccessException e) {
throw ExceptionUtils.getRuntimeException(e);
}
} else {
if (field.getAnnotation(HasDefaultValue.class) == null) {
throw new NoSuchElementException(name + " or " + field.getName() + " is a required property for " + processor +
" and therefore must be defined in the Request Data or be an output of one" +
" of the other processors. Available values: " + values.asMap().keySet() + ".");
}
}
}
}
return inputObject;
}
/**
* Read the values from the output object and write them to the values object.
* @param output the output object from a processor
* @param processor the processor the output if from
* @param values the object for sharing values between processors
*/
public static void writeProcessorOutputToValues(final Object output,
final Processor, ?> processor,
final Values values) {
Map mapper = processor.getOutputMapperBiMap();
if (mapper == null) {
mapper = Collections.emptyMap();
}
final Collection fields = getAllAttributes(output.getClass());
for (Field field : fields) {
String name = getOutputValueName(processor.getOutputPrefix(), mapper, field);
try {
final Object value = field.get(output);
if (value != null) {
values.put(name, value);
} else {
values.remove(name);
}
} catch (IllegalAccessException e) {
throw ExceptionUtils.getRuntimeException(e);
}
}
}
/**
* Calculate the name of the input value.
*
* @param inputPrefix a nullable prefix to prepend to the name if non-null and non-empty
* @param inputMapper the name mapper
* @param field the field containing the value
*/
public static String getInputValueName(@Nullable final String inputPrefix,
@Nonnull final BiMap inputMapper,
@Nonnull final String field) {
String name = inputMapper == null ? null : inputMapper.inverse().get(field);
if (name == null) {
if (inputMapper.containsKey(field)) {
throw new RuntimeException("field in keys");
}
final String[] defaultValues = {
Values.TASK_DIRECTORY_KEY, Values.CLIENT_HTTP_REQUEST_FACTORY_KEY,
Values.TEMPLATE_KEY, Values.PDF_CONFIG, Values.SUBREPORT_DIR
};
if (inputPrefix == null || Arrays.asList(defaultValues).contains(field)) {
name = field;
} else {
name = inputPrefix.trim() +
Character.toUpperCase(field.charAt(0)) +
field.substring(1);
}
}
return name;
}
/**
* Calculate the name of the output value.
*
* @param outputPrefix a nullable prefix to prepend to the name if non-null and non-empty
* @param outputMapper the name mapper
* @param field the field containing the value
*/
public static String getOutputValueName(@Nullable final String outputPrefix,
@Nonnull final Map outputMapper,
@Nonnull final Field field) {
String name = outputMapper.get(field.getName());
if (name == null) {
name = field.getName();
if (!Strings.isNullOrEmpty(outputPrefix) && !outputPrefix.trim().isEmpty()) {
name = outputPrefix.trim() + Character.toUpperCase(name.charAt(0)) + name.substring(1);
}
}
return name;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy