Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.8 kB
1
Indexable
Never
/**
     * for comparision purpose
     * @param originalMessage is an input
     * @return last tag
     */
    private String getLastTag(String originalMessage) {
        String lastTag = "";
        if (!StringUtils.isEmpty(originalMessage)) {
            int lastTagStart = originalMessage.lastIndexOf("<");
            int lastTagEnd = originalMessage.lastIndexOf(">") + 1;
            if(lastTagEnd > lastTagStart) {
                lastTag = originalMessage.substring(lastTagStart, lastTagEnd);
            }
        }
        return lastTag;
    }

    /**
     * logic to format msg_content
     *
     * @param inputContent as  an input
     * @return formatted output
     */
    private String formatMessageContent(String inputContent) {
        String output = inputContent;
        int outLength = output.length();
        if (outLength > 16) {
            String startString = output.substring(0, 15);
            Pattern pattern = Pattern.compile("CB[0-9][0-9][0-9]T[0-9][0-9]");
            Matcher matcher = pattern.matcher(startString);
            if (matcher.find()) {
                int startPoint = startString.contains("{") ? startString.indexOf("{"): matcher.end(0);
                output = output.substring(startPoint, outLength);
                Pattern pt = Pattern.compile("[^a-zA-Z0-9\\-_<>$&+.,!~`$/%^*\\\\():;=?@#|{}\\[\\]\\s\\n\\r]");
                Matcher match = pt.matcher(output);
                while (match.find()) {
                    String s = match.group();
//                    output = output.replaceAll("\\" + s, "");
                }
            }
        }
        return output;
    }

    /**
     * Converting the xml message into proper format
     *
     * @param input  xml message
     * @param indent indent spaces
     * @return XML message with indents
     * @throws CMMApplicationException - exception thrown in case of error
     */
    private String prettyFormat(String input, int indent) throws CMMApplicationException {
        final String logMethod = "::prettyFormat()";
        try {
            Source xmlInput = new StreamSource(new StringReader(input));
            StringWriter stringWriter = new StringWriter();
            StreamResult xmlOutput = new StreamResult(stringWriter);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
            transformerFactory.setAttribute("indent-number", indent);
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(xmlInput, xmlOutput);
            return xmlOutput.getWriter().toString();
        } catch (TransformerException | TransformerFactoryConfigurationError | IllegalArgumentException e) {
            logHelper.error(logMethod, ConsultationLogKeys.XML_FORMAT_ERR_CODE, e);
            return input;
        }
    }

    /**
     * To make the XML string readable
     *
     * @param input XML message
     * @return XML message in indented format
     * @throws CMMApplicationException - thrown in case of error
     */
    private String prettyFormat(String input) throws CMMApplicationException {
        return prettyFormat(input, 2);
    }

    /**
     * To remove extra lines in the message
     *
     * @param input XML message
     * @return XML message in formatted text
     * @throws CMMApplicationException - thrown in case of error
     */
    private String removeExtraLines(String input) throws CMMApplicationException {
        return input.replaceAll("(?m)^[ \t]*\r?\n","");
    }