Search in sources :

Example 6 with DomDriver

use of com.thoughtworks.xstream.io.xml.DomDriver in project universa by UniversaBlockchain.

the class CLIMain method importContract.

/**
 * Import contract from specified yaml, xml or json file.
 *
 * @param sourceName
 *
 * @return loaded and from loaded data created Contract.
 */
private static Contract importContract(String sourceName) throws IOException {
    ContractFileTypes fileType = getFileType(sourceName);
    Contract contract = null;
    File pathFile = new File(sourceName);
    if (pathFile.exists()) {
        try {
            Binder binder;
            FileReader reader = new FileReader(sourceName);
            if (fileType == ContractFileTypes.YAML) {
                Yaml yaml = new Yaml();
                binder = Binder.convertAllMapsToBinders(yaml.load(reader));
            } else if (fileType == ContractFileTypes.JSON) {
                Gson gson = new GsonBuilder().create();
                binder = Binder.convertAllMapsToBinders(gson.fromJson(reader, Binder.class));
            } else {
                XStream xstream = new XStream(new DomDriver());
                xstream.registerConverter(new MapEntryConverter());
                xstream.alias("contract", Binder.class);
                binder = Binder.convertAllMapsToBinders(xstream.fromXML(reader));
            }
            BiDeserializer bm = DefaultBiMapper.getInstance().newDeserializer();
            contract = new Contract();
            contract.deserialize(binder, bm);
            report(">>> imported contract: " + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(contract.getCreatedAt()));
            report("import from " + fileType.toString().toLowerCase() + " ok");
        } catch (Exception e) {
            addError(Errors.FAILURE.name(), sourceName, e.getMessage());
        }
    } else {
        addError(Errors.NOT_FOUND.name(), sourceName, "Path " + sourceName + " does not exist");
    // usage("Path " + sourceName + " does not exist");
    }
    return contract;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) XStream(com.thoughtworks.xstream.XStream) Gson(com.google.gson.Gson) BiDeserializer(net.sergeych.biserializer.BiDeserializer) Yaml(org.yaml.snakeyaml.Yaml) BackingStoreException(java.util.prefs.BackingStoreException) OptionException(joptsimple.OptionException) Binder(net.sergeych.tools.Binder) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) Contract(com.icodici.universa.contract.Contract)

Example 7 with DomDriver

use of com.thoughtworks.xstream.io.xml.DomDriver in project intellij-plugins by StepicOrg.

the class StepikProjectManager method getXStream.

@NotNull
public static XStream getXStream() {
    if (xStream == null) {
        xStream = new XStream(new DomDriver());
        xStream.alias("StepikProjectManager", StepikProjectManager.class);
        xStream.alias("CourseNode", CourseNode.class);
        xStream.alias("SectionNode", SectionNode.class);
        xStream.alias("LessonNode", LessonNode.class);
        xStream.alias("StepNode", StepNode.class);
        xStream.alias("Limit", Limit.class);
        xStream.alias("SupportedLanguages", SupportedLanguages.class);
        xStream.alias("VideoUrl", VideoUrl.class);
        xStream.alias("LinkedTreeMap", LinkedTreeMap.class);
        xStream.alias("Sample", Sample.class);
        xStream.alias("Course", Course.class);
        xStream.alias("Section", Section.class);
        xStream.alias("CompoundUnitLesson", CompoundUnitLesson.class);
        xStream.alias("Step", Step.class);
        xStream.alias("User", User.class);
        xStream.autodetectAnnotations(true);
        xStream.setClassLoader(StepikProjectManager.class.getClassLoader());
        xStream.registerConverter(new SupportedLanguagesConverter());
        xStream.registerConverter(new SampleConverter());
        xStream.ignoreUnknownElements();
        xStream.setMode(XStream.ID_REFERENCES);
    }
    return xStream;
}
Also used : DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) SupportedLanguagesConverter(org.stepik.core.serialization.SupportedLanguagesConverter) XStream(com.thoughtworks.xstream.XStream) SampleConverter(org.stepik.core.serialization.SampleConverter) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with DomDriver

use of com.thoughtworks.xstream.io.xml.DomDriver in project KJFrameForAndroid by kymjs.

the class Parser method xmlToBean.

public static <T> T xmlToBean(Class<T> type, String xml) {
    T data = null;
    try {
        XStream xStream = new XStream(new DomDriver("UTF-8"));
        xStream.processAnnotations(type);
        data = (T) xStream.fromXML(xml);
    } catch (Exception e) {
        try {
            data = type.newInstance();
        } catch (Exception ee) {
        } finally {
            Log.e("kymjs", "xml解析异常");
        }
    }
    return data;
}
Also used : DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) XStream(com.thoughtworks.xstream.XStream) JSONException(org.json.JSONException)

Example 9 with DomDriver

use of com.thoughtworks.xstream.io.xml.DomDriver in project cloudstack by apache.

the class ApiXmlDocReader method main.

public static void main(String[] args) {
    String newFile = null;
    String oldFile = null;
    String dirName = "";
    LinkedHashMap<String, Command> commands = new LinkedHashMap<String, Command>();
    LinkedHashMap<String, Command> oldCommands = new LinkedHashMap<String, Command>();
    ArrayList<Command> addedCommands = new ArrayList<Command>();
    ArrayList<Command> removedCommands = new ArrayList<Command>();
    HashMap<String, Command> stableCommands = new HashMap<String, Command>();
    HashMap<String, Object> jsonOut = new HashMap<String, Object>();
    Gson gson = new Gson();
    XStream xs = new XStream(new DomDriver());
    xs.alias("command", Command.class);
    xs.alias("arg", Argument.class);
    List<String> argsList = Arrays.asList(args);
    Iterator<String> iter = argsList.iterator();
    while (iter.hasNext()) {
        String arg = iter.next();
        // populate the file names
        if (arg.equals("-new")) {
            newFile = iter.next();
        }
        if (arg.equals("-old")) {
            oldFile = iter.next();
        }
        if (arg.equals("-d")) {
            dirName = iter.next();
        }
    }
    try {
        try (ObjectInputStream inOld = xs.createObjectInputStream(new FileReader(oldFile))) {
            while (true) {
                Command c1 = (Command) inOld.readObject();
                oldCommands.put(c1.getName(), c1);
            }
        } catch (EOFException ex) {
        // EOF exception shows that there is no more objects in ObjectInputStream, so do nothing here
        }
        try (ObjectInputStream inNew = xs.createObjectInputStream(new FileReader(newFile))) {
            while (true) {
                Command c = (Command) inNew.readObject();
                commands.put(c.getName(), c);
            }
        } catch (EOFException ex) {
        // EOF exception shows that there is no more objects in ObjectInputStream, so do nothing here
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    // Check if any commands got added in new version
    for (Map.Entry<String, Command> entry : commands.entrySet()) {
        if (!oldCommands.containsKey(entry.getKey())) {
            addedCommands.add(entry.getValue());
        } else {
            stableCommands.put(entry.getValue().getName(), entry.getValue());
        }
    }
    // Check if any commands were removed in new version
    for (Map.Entry<String, Command> entry : oldCommands.entrySet()) {
        if (!commands.containsKey(entry.getKey())) {
            removedCommands.add(entry.getValue());
            if (stableCommands.get(entry.getKey()) != null) {
                stableCommands.remove(entry.getKey());
            }
        }
    }
    try (FileWriter fstream = new FileWriter(dirName + "/diff.txt");
        BufferedWriter out = new BufferedWriter(fstream);
        FileWriter jfstream = new FileWriter(dirName + "/diff.json");
        BufferedWriter json = new BufferedWriter(jfstream)) {
        // Print added commands
        out.write("Added commands:\n");
        ArrayList<HashMap<String, Object>> addedCmds = new ArrayList<HashMap<String, Object>>();
        for (Command c : addedCommands) {
            HashMap<String, Object> addedCmd = new HashMap<String, Object>();
            if (c.getDescription() != null && !c.getDescription().isEmpty()) {
                out.write("\n    " + c.getName() + " (" + c.getDescription() + ")\n");
                addedCmd.put("description", c.getDescription());
            } else {
                out.write("\n    " + c.getName() + "\n");
            }
            addedCmd.put("name", c.getName());
            addedCmds.add(addedCmd);
        }
        jsonOut.put("commands_added", addedCmds);
        // Print removed commands
        out.write("\nRemoved commands:\n");
        ArrayList<HashMap<String, Object>> removedCmds = new ArrayList<HashMap<String, Object>>();
        for (Command c : removedCommands) {
            HashMap<String, Object> removedCmd = new HashMap<String, Object>();
            if (c.getDescription() != null && !c.getDescription().isEmpty()) {
                out.write("\n\t" + c.getName() + " (" + c.getDescription() + ")\n");
                removedCmd.put("description", c.getDescription());
            } else {
                out.write("\n\t" + c.getName() + "\n");
            }
            removedCmd.put("name", c.getName());
            removedCmds.add(removedCmd);
        }
        jsonOut.put("commands_removed", removedCmds);
        out.write("\nChanges in command type (sync versus async)\n");
        ArrayList<HashMap<String, Object>> syncChangeCmds = new ArrayList<HashMap<String, Object>>();
        // Verify if the command was sync and became async and vice versa
        for (Map.Entry<String, Command> entry : stableCommands.entrySet()) {
            if (commands.get(entry.getKey()).isAsync() != oldCommands.get(entry.getKey()).isAsync()) {
                HashMap<String, Object> syncChangeCmd = new HashMap<String, Object>();
                String type = "Sync";
                if (commands.get(entry.getKey()).isAsync()) {
                    type = "Async";
                }
                syncChangeCmd.put("name", entry.getValue().getName());
                syncChangeCmd.put("sync_type", type);
                syncChangeCmds.add(syncChangeCmd);
                out.write("\n\t" + entry.getValue().getName() + " became " + type);
            }
        }
        jsonOut.put("commands_sync_changed", syncChangeCmds);
        // Print differences between commands arguments
        out.write("\n\nChanges in commands arguments:\n");
        ArrayList<HashMap<String, Object>> argsChangeCmds = new ArrayList<HashMap<String, Object>>();
        for (String key : stableCommands.keySet()) {
            ArrayList<Argument> newReqArgs = new ArrayList<Argument>();
            ArrayList<Argument> removedReqArgs = new ArrayList<Argument>();
            HashMap<String, Argument> stableReqArgs = new HashMap<String, Argument>();
            ArrayList<Argument> newRespArgs = new ArrayList<Argument>();
            ArrayList<Argument> removedRespArgs = new ArrayList<Argument>();
            HashMap<String, Object> argsChangeCmd = new HashMap<String, Object>();
            Command newCommand = commands.get(key);
            Command oldCommand = oldCommands.get(key);
            // Check if any request arguments were added in new version
            for (Argument arg : newCommand.getRequest()) {
                if (oldCommand.getReqArgByName(arg.getName()) == null) {
                    if (!(arg.getName().equals("page") || arg.getName().equals("pagesize") || arg.getName().equals("keyword"))) {
                        newReqArgs.add(arg);
                    }
                } else {
                    stableReqArgs.put(arg.getName(), arg);
                }
            }
            // Check if any request arguments were removed in new version
            for (Argument arg : oldCommand.getRequest()) {
                if (newCommand.getReqArgByName(arg.getName()) == null) {
                    removedReqArgs.add(arg);
                    if (stableReqArgs.get(arg.getName()) != null) {
                        stableReqArgs.remove(arg.getName());
                    }
                }
            }
            // Compare stable request arguments of old and new version
            for (Iterator<String> i = stableReqArgs.keySet().iterator(); i.hasNext(); ) {
                String argName = i.next();
                if ((oldCommand.getReqArgByName(argName) != null) && (newCommand.getReqArgByName(argName) != null)) {
                    if (oldCommand.getReqArgByName(argName).isRequired().equals(newCommand.getReqArgByName(argName).isRequired())) {
                        i.remove();
                    }
                }
            }
            // Check if any response arguments were added in new version
            if (newCommand.getResponse() != null && oldCommand.getResponse() != null) {
                for (Argument arg : newCommand.getResponse()) {
                    if (oldCommand.getResArgByName(arg.getName()) == null) {
                        newRespArgs.add(arg);
                    }
                }
                // Check if any response arguments were removed in new version
                for (Argument arg : oldCommand.getResponse()) {
                    if (newCommand.getResArgByName(arg.getName()) == null) {
                        removedRespArgs.add(arg);
                    }
                }
            }
            if (newReqArgs.size() != 0 || newRespArgs.size() != 0 || removedReqArgs.size() != 0 || removedRespArgs.size() != 0 || stableReqArgs.size() != 0) {
                StringBuffer commandInfo = new StringBuffer();
                commandInfo.append("\n\t" + key);
                out.write(commandInfo.toString());
                out.write("\n");
                argsChangeCmd.put("name", key);
                // Request
                if (newReqArgs.size() != 0 || removedReqArgs.size() != 0 || stableReqArgs.size() != 0) {
                    HashMap<String, Object> requestChanges = new HashMap<String, Object>();
                    StringBuffer request = new StringBuffer();
                    request.append("\n\t\tRequest:\n");
                    out.write(request.toString());
                    if (newReqArgs.size() != 0) {
                        StringBuffer newParameters = new StringBuffer();
                        newParameters.append("\n\t\t\tNew parameters: ");
                        ArrayList<HashMap<String, Object>> newRequestParams = new ArrayList<HashMap<String, Object>>();
                        for (Argument newArg : newReqArgs) {
                            HashMap<String, Object> newRequestParam = new HashMap<String, Object>();
                            String isRequiredParam = "optional";
                            if (newArg.isRequired()) {
                                isRequiredParam = "required";
                            }
                            newRequestParam.put("name", newArg.getName());
                            newRequestParam.put("required", newArg.isRequired());
                            newRequestParams.add(newRequestParam);
                            newParameters.append(newArg.getName() + " (" + isRequiredParam + "), ");
                        }
                        requestChanges.put("params_new", newRequestParams);
                        newParameters.delete(newParameters.length() - 2, newParameters.length() - 1);
                        out.write(newParameters.toString());
                        out.write("\n");
                    }
                    if (removedReqArgs.size() != 0) {
                        StringBuffer removedParameters = new StringBuffer();
                        removedParameters.append("\n\t\t\tRemoved parameters: ");
                        ArrayList<HashMap<String, Object>> removedRequestParams = new ArrayList<HashMap<String, Object>>();
                        for (Argument removedArg : removedReqArgs) {
                            HashMap<String, Object> removedRequestParam = new HashMap<String, Object>();
                            removedRequestParam.put("name", removedArg.getName());
                            removedRequestParams.add(removedRequestParam);
                            removedParameters.append(removedArg.getName() + ", ");
                        }
                        requestChanges.put("params_removed", removedRequestParams);
                        removedParameters.delete(removedParameters.length() - 2, removedParameters.length() - 1);
                        out.write(removedParameters.toString());
                        out.write("\n");
                    }
                    if (stableReqArgs.size() != 0) {
                        StringBuffer changedParameters = new StringBuffer();
                        changedParameters.append("\n\t\t\tChanged parameters: ");
                        ArrayList<HashMap<String, Object>> changedRequestParams = new ArrayList<HashMap<String, Object>>();
                        for (Argument stableArg : stableReqArgs.values()) {
                            HashMap<String, Object> changedRequestParam = new HashMap<String, Object>();
                            String newRequired = "optional";
                            String oldRequired = "optional";
                            changedRequestParam.put("required_old", false);
                            changedRequestParam.put("required_new", false);
                            if ((oldCommand.getReqArgByName(stableArg.getName()) != null) && (oldCommand.getReqArgByName(stableArg.getName()).isRequired() == true)) {
                                oldRequired = "required";
                                changedRequestParam.put("required_old", true);
                            }
                            if ((newCommand.getReqArgByName(stableArg.getName()) != null) && (newCommand.getReqArgByName(stableArg.getName()).isRequired() == true)) {
                                newRequired = "required";
                                changedRequestParam.put("required_new", true);
                            }
                            changedRequestParam.put("name", stableArg.getName());
                            changedRequestParams.add(changedRequestParam);
                            changedParameters.append(stableArg.getName() + " (old version - " + oldRequired + ", new version - " + newRequired + "), ");
                        }
                        requestChanges.put("params_changed", changedRequestParams);
                        changedParameters.delete(changedParameters.length() - 2, changedParameters.length() - 1);
                        out.write(changedParameters.toString());
                        out.write("\n");
                    }
                    argsChangeCmd.put("request", requestChanges);
                }
                // Response
                if (newRespArgs.size() != 0 || removedRespArgs.size() != 0) {
                    HashMap<String, Object> responseChanges = new HashMap<String, Object>();
                    StringBuffer changedResponseParams = new StringBuffer();
                    changedResponseParams.append("\n\t\tResponse:\n");
                    out.write(changedResponseParams.toString());
                    if (newRespArgs.size() != 0) {
                        ArrayList<HashMap<String, Object>> newResponseParams = new ArrayList<HashMap<String, Object>>();
                        StringBuffer newRespParams = new StringBuffer();
                        newRespParams.append("\n\t\t\tNew parameters: ");
                        for (Argument newArg : newRespArgs) {
                            HashMap<String, Object> newResponseParam = new HashMap<String, Object>();
                            newResponseParam.put("name", newArg.getName());
                            newResponseParams.add(newResponseParam);
                            newRespParams.append(newArg.getName() + ", ");
                        }
                        responseChanges.put("params_new", newResponseParams);
                        newRespParams.delete(newRespParams.length() - 2, newRespParams.length() - 1);
                        out.write(newRespParams.toString());
                        out.write("\n");
                    }
                    if (removedRespArgs.size() != 0) {
                        ArrayList<HashMap<String, Object>> removedResponseParams = new ArrayList<HashMap<String, Object>>();
                        StringBuffer removedRespParams = new StringBuffer();
                        removedRespParams.append("\n\t\t\tRemoved parameters: ");
                        for (Argument removedArg : removedRespArgs) {
                            HashMap<String, Object> removedResponseParam = new HashMap<String, Object>();
                            removedResponseParam.put("name", removedArg.getName());
                            removedResponseParams.add(removedResponseParam);
                            removedRespParams.append(removedArg.getName() + ", ");
                        }
                        responseChanges.put("params_removed", removedResponseParams);
                        removedRespParams.delete(removedRespParams.length() - 2, removedRespParams.length() - 1);
                        out.write(removedRespParams.toString());
                        out.write("\n");
                    }
                    argsChangeCmd.put("response", responseChanges);
                }
                argsChangeCmds.add(argsChangeCmd);
            }
        }
        jsonOut.put("commands_args_changed", argsChangeCmds);
        json.write(gson.toJson(jsonOut));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) LinkedHashMap(java.util.LinkedHashMap) BufferedWriter(java.io.BufferedWriter) EOFException(java.io.EOFException) FileReader(java.io.FileReader) XStream(com.thoughtworks.xstream.XStream) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ObjectInputStream(java.io.ObjectInputStream)

Example 10 with DomDriver

use of com.thoughtworks.xstream.io.xml.DomDriver in project cloudstack by apache.

the class RegionsApiUtil method makeUserAccountAPICall.

/**
 * Makes an api call using region service end_point, api command and params
 * Returns UserAccount object on success
 * @param region
 * @param command
 * @param params
 * @return
 */
protected static UserAccount makeUserAccountAPICall(Region region, String command, List<NameValuePair> params) {
    try {
        String url = buildUrl(buildParams(command, params), region);
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod(url);
        if (client.executeMethod(method) == 200) {
            InputStream is = method.getResponseBodyAsStream();
            XStream xstream = new XStream(new DomDriver());
            xstream.alias("useraccount", UserAccountVO.class);
            xstream.aliasField("id", UserAccountVO.class, "uuid");
            try (ObjectInputStream in = xstream.createObjectInputStream(is)) {
                return (UserAccountVO) in.readObject();
            } catch (IOException e) {
                s_logger.error(e.getMessage());
                return null;
            }
        } else {
            return null;
        }
    } catch (HttpException e) {
        s_logger.error(e.getMessage());
        return null;
    } catch (IOException e) {
        s_logger.error(e.getMessage());
        return null;
    } catch (ClassNotFoundException e) {
        s_logger.error(e.getMessage());
        return null;
    }
}
Also used : DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) UserAccountVO(com.cloud.user.UserAccountVO) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) XStream(com.thoughtworks.xstream.XStream) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

XStream (com.thoughtworks.xstream.XStream)32 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)32 IOException (java.io.IOException)8 Gson (com.google.gson.Gson)5 File (java.io.File)5 InputStream (java.io.InputStream)5 ObjectInputStream (java.io.ObjectInputStream)5 GsonBuilder (com.google.gson.GsonBuilder)4 Binder (net.sergeych.tools.Binder)4 Yaml (org.yaml.snakeyaml.Yaml)4 HttpClient (org.apache.commons.httpclient.HttpClient)3 HttpException (org.apache.commons.httpclient.HttpException)3 HttpMethod (org.apache.commons.httpclient.HttpMethod)3 GetMethod (org.apache.commons.httpclient.methods.GetMethod)3 XStreamException (com.thoughtworks.xstream.XStreamException)2 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 BackingStoreException (java.util.prefs.BackingStoreException)2 OptionException (joptsimple.OptionException)2 BiDeserializer (net.sergeych.biserializer.BiDeserializer)2