Search in sources :

Example 11 with ConversionException

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);
    }
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) ConversionException(com.thoughtworks.xstream.converters.ConversionException)

Example 12 with ConversionException

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);
    }
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) ConversionException(com.thoughtworks.xstream.converters.ConversionException)

Example 13 with ConversionException

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);
    }
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) Method(java.lang.reflect.Method) ConversionException(com.thoughtworks.xstream.converters.ConversionException)

Example 14 with ConversionException

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();
    }
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) HashTree(org.apache.jorphan.collections.HashTree) GuiPackage(org.apache.jmeter.gui.GuiPackage) ActionEvent(java.awt.event.ActionEvent) IOException(java.io.IOException) IOException(java.io.IOException) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) ConversionException(com.thoughtworks.xstream.converters.ConversionException)

Example 15 with ConversionException

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);
        }
    }
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) InputStreamReader(java.io.InputStreamReader) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException)

Aggregations

ConversionException (com.thoughtworks.xstream.converters.ConversionException)21 IOException (java.io.IOException)5 InputStreamReader (java.io.InputStreamReader)4 Method (java.lang.reflect.Method)4 XppReader (com.thoughtworks.xstream.io.xml.XppReader)3 Metacard (ddf.catalog.data.Metacard)3 CannotResolveClassException (com.thoughtworks.xstream.mapper.CannotResolveClassException)2 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)2 ActionEvent (java.awt.event.ActionEvent)2 Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2 IllegalUserActionException (org.apache.jmeter.exceptions.IllegalUserActionException)2 HashTree (org.apache.jorphan.collections.HashTree)2 XmlPullParser (org.xmlpull.v1.XmlPullParser)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 XStream (com.thoughtworks.xstream.XStream)1 SingleValueConverter (com.thoughtworks.xstream.converters.SingleValueConverter)1 NonExistentFieldException (com.thoughtworks.xstream.converters.reflection.NonExistentFieldException)1 StreamException (com.thoughtworks.xstream.io.StreamException)1 HierarchicalStreamCopier (com.thoughtworks.xstream.io.copy.HierarchicalStreamCopier)1