use of com.thoughtworks.xstream.converters.ConversionException in project camel by apache.
the class StringMultiSelectPicklistConverter method unmarshal.
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
final String listValue = reader.getValue();
final Class<?> requiredArrayType = context.getRequiredType();
try {
// parse the string of the form value1;value2;...
final String[] value = listValue.split(";");
final int length = value.length;
final String[] resultArray = new String[length];
for (int i = 0; i < length; i++) {
// use factory method to create object
resultArray[i] = value[i].trim();
Array.set(resultArray, i, value[i].trim());
}
return resultArray;
} catch (Exception e) {
throw new ConversionException(String.format("Exception reading pick list value %s of type %s: %s", listValue, requiredArrayType.getName(), e.getMessage()), e);
}
}
use of com.thoughtworks.xstream.converters.ConversionException in project camel by apache.
the class StringMultiSelectPicklistConverter method marshal.
@Override
public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext context) {
try {
final int length = Array.getLength(o);
// construct a string of form value1;value2;...
final StringBuilder buffer = new StringBuilder();
for (int i = 0; i < length; i++) {
buffer.append((String) o);
if (i < (length - 1)) {
buffer.append(';');
}
}
writer.setValue(buffer.toString());
} catch (Exception e) {
throw new ConversionException(String.format("Exception writing pick list value %s of type %s: %s", o, o.getClass().getName(), e.getMessage()), e);
}
}
use of com.thoughtworks.xstream.converters.ConversionException in project camel by apache.
the class MultiSelectPicklistConverter method unmarshal.
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
final String listValue = reader.getValue();
// get Picklist enum element class from array class
final Class<?> requiredArrayType = context.getRequiredType();
final Class<?> requiredType = requiredArrayType.getComponentType();
try {
Method factoryMethod = requiredType.getMethod(FACTORY_METHOD, String.class);
// parse the string of the form value1;value2;...
final String[] value = listValue.split(";");
final int length = value.length;
final Object resultArray = Array.newInstance(requiredType, length);
for (int i = 0; i < length; i++) {
// use factory method to create object
Array.set(resultArray, i, factoryMethod.invoke(null, value[i].trim()));
}
return resultArray;
} catch (Exception e) {
throw new ConversionException(String.format("Exception reading pick list value %s of type %s: %s", listValue, requiredArrayType.getName(), e.getMessage()), e);
}
}
use of com.thoughtworks.xstream.converters.ConversionException in project jmeter by apache.
the class Load method loadProjectFile.
/**
* Loads or merges a file into the current GUI, reporting any errors to the user.
* If the file is a complete test plan, sets the GUI test plan file name
*
* @param e the event that triggered the action
* @param f the file to load
* @param merging if true, then try to merge the file into the current GUI.
* @param setDetails if true, then set the file details (if not merging)
*/
static void loadProjectFile(final ActionEvent e, final File f, final boolean merging, final boolean setDetails) {
ActionRouter.getInstance().doActionNow(new ActionEvent(e.getSource(), e.getID(), ActionNames.STOP_THREAD));
final GuiPackage guiPackage = GuiPackage.getInstance();
if (f != null) {
try {
if (merging) {
log.info("Merging file: {}", f);
} else {
log.info("Loading file: {}", f);
// and what if load fails?
if (setDetails) {
FileServer.getFileServer().setBaseForScript(f);
}
}
final HashTree tree = SaveService.loadTree(f);
final boolean isTestPlan = insertLoadedTree(e.getID(), tree, merging);
// don't change name if merging
if (!merging && isTestPlan && setDetails) {
// TODO should setBaseForScript be called here rather than
// above?
guiPackage.setTestPlanFile(f.getAbsolutePath());
}
} catch (NoClassDefFoundError ex) {
// Allow for missing optional jars
reportError("Missing jar file. {}", ex, true);
} catch (ConversionException ex) {
if (log.isWarnEnabled()) {
log.warn("Could not convert file. {}", ex.toString());
}
JMeterUtils.reportErrorToUser(SaveService.CEtoString(ex));
} catch (IOException ex) {
reportError("Error reading file. {}", ex, false);
} catch (Exception ex) {
reportError("Unexpected error. {}", ex, true);
}
FileDialoger.setLastJFCDirectory(f.getParentFile().getAbsolutePath());
guiPackage.updateCurrentGui();
guiPackage.getMainFrame().repaint();
}
}
use of com.thoughtworks.xstream.converters.ConversionException in project jmeter by apache.
the class SaveService method readTree.
/**
*
* @param inputStream {@link InputStream}
* @param file the JMX file used only for debug, can be null
* @return the loaded tree
* @throws IOException if there is a problem reading the file or processing it
*/
private static HashTree readTree(InputStream inputStream, File file) throws IOException {
ScriptWrapper wrapper = null;
try {
// Get the InputReader to use
InputStreamReader inputStreamReader = getInputStreamReader(inputStream);
wrapper = (ScriptWrapper) JMXSAVER.fromXML(inputStreamReader);
inputStreamReader.close();
if (wrapper == null) {
log.error("Problem loading XML: see above.");
return null;
}
return wrapper.testPlan;
} catch (CannotResolveClassException e) {
if (file != null) {
throw new IllegalArgumentException("Problem loading XML from:'" + file.getAbsolutePath() + "', cannot determine class for element: " + e, e);
} else {
throw new IllegalArgumentException("Problem loading XML, cannot determine class for element: " + e, e);
}
} catch (ConversionException | NoClassDefFoundError e) {
if (file != null) {
throw new IllegalArgumentException("Problem loading XML from:'" + file.getAbsolutePath() + "', missing class " + e, e);
} else {
throw new IllegalArgumentException("Problem loading XML, missing class " + e, e);
}
}
}
Aggregations