Example usage for org.apache.commons.lang StringUtils deleteWhitespace

List of usage examples for org.apache.commons.lang StringUtils deleteWhitespace

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils deleteWhitespace.

Prototype

public static String deleteWhitespace(String str) 

Source Link

Document

Deletes all whitespaces from a String as defined by Character#isWhitespace(char) .

Usage

From source file:org.openmrs.module.reporting.report.renderer.TextTemplateRendererTest.java

private void shouldRenderTemplate(String templateName, String templateType) throws Exception {

    ReportDefinition reportDefinition = new ReportDefinition();
    reportDefinition.setName("Test Report");

    SimplePatientDataSetDefinition allPatients = new SimplePatientDataSetDefinition("allPatients", "");
    allPatients.addPatientProperty("patientId");
    allPatients.addPatientProperty("gender");
    allPatients.addPatientProperty("birthdate");
    reportDefinition.addDataSetDefinition("allPatients", allPatients, null);

    GenderCohortDefinition males = new GenderCohortDefinition();
    males.setName("Males");
    males.setMaleIncluded(true);//from w  ww.j  av  a2s  .  c  o  m

    GenderCohortDefinition females = new GenderCohortDefinition();
    females.setName("Females");
    females.setFemaleIncluded(true);

    CohortCrossTabDataSetDefinition genderDsd = new CohortCrossTabDataSetDefinition();
    genderDsd.addColumn("males", males, null);
    genderDsd.addColumn("females", females, null);
    reportDefinition.addDataSetDefinition("genders", genderDsd, null);

    final ReportDesign reportDesign = new ReportDesign();
    reportDesign.setName("TestDesign");
    reportDesign.setReportDefinition(reportDefinition);
    reportDesign.setRendererType(TextTemplateRenderer.class);

    if (templateType != null) {
        reportDesign.addPropertyValue(TextTemplateRenderer.TEMPLATE_TYPE, templateType);
    }

    EvaluationContext context = new EvaluationContext();
    ReportDefinitionService rs = Context.getService(ReportDefinitionService.class);
    ReportData reportData = rs.evaluate(reportDefinition, context);

    ReportDesignResource resource = new ReportDesignResource();
    resource.setName(templateName);
    InputStream is = OpenmrsClassLoader.getInstance()
            .getResourceAsStream("org/openmrs/module/reporting/report/renderer/" + templateName);
    resource.setContents(IOUtils.toByteArray(is));
    IOUtils.closeQuietly(is);
    reportDesign.addResource(resource);

    TextTemplateRenderer renderer = new TextTemplateRenderer() {
        public ReportDesign getDesign(String argument) {
            return reportDesign;
        }
    };

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    renderer.render(reportData, "ReportData", baos);
    String renderedOutput = StringUtils.deleteWhitespace(baos.toString());

    String xml = "<?xml version=\"1.0\"?>" + "<dataset>" + "   <rows>"
            + "      <row><patientId>2</patientId><gender>M</gender><birthdate>08/Apr/1975</birthdate></row>"
            + "      <row><patientId>6</patientId><gender>M</gender><birthdate>27/May/2007</birthdate></row>"
            + "      <row><patientId>7</patientId><gender>F</gender><birthdate>25/Aug/1976</birthdate></row>"
            + "      <row><patientId>8</patientId><gender>F</gender><birthdate></birthdate></row>"
            + "   </rows>" + "</dataset>";

    xml = templateType != null ? StringUtils.deleteWhitespace(xml) : "Males=2Females=2";
    Assert.assertEquals(xml, renderedOutput);
}

From source file:org.openmrs.module.reporting.web.reports.renderers.TextTemplateFormController.java

@SuppressWarnings("unchecked")
@RequestMapping(value = "/module/reporting/reports/renderers/previewTextTemplateReportRenderer", method = RequestMethod.POST)
public void previewScript(ModelMap model, HttpServletRequest request,
        @RequestParam(required = true, value = "reportDefinition") String reportDefinitionUuid,
        @RequestParam(required = true, value = "uuid") String uuid,
        @RequestParam(required = false, value = "iframe") String iframe,
        @RequestParam(required = true, value = "script") String script,
        @RequestParam(required = true, value = "scriptType") String scriptType,
        @RequestParam(required = true, value = "rendererType") Class<? extends TextTemplateRenderer> rendererType,
        @ModelAttribute("userParams") UserParams userParams, BindingResult bindingResult)
        throws EvaluationException, UnsupportedEncodingException, ClassNotFoundException,
        InstantiationException, IllegalAccessException {

    ReportDefinitionService rds = Context.getService(ReportDefinitionService.class);
    ReportDefinition reportDefinition = rds.getDefinitionByUuid(reportDefinitionUuid);

    // validate parameters
    if (!reportDefinition.getParameters().isEmpty()) {

        Set<String> requiredParams = new HashSet<String>();
        for (Parameter parameter : reportDefinition.getParameters()) {
            requiredParams.add(parameter.getName());
        }/*from w w  w.j a  v  a 2s  .c  om*/
        for (Map.Entry<String, Object> e : userParams.getUserEnteredParams().entrySet()) {
            if (e.getValue() instanceof Iterable || e.getValue() instanceof Object[]) {
                Object iterable = e.getValue();
                if (e.getValue() instanceof Object[]) {
                    iterable = Arrays.asList((Object[]) e.getValue());
                }

                boolean hasNull = true;

                for (Object value : (Iterable<Object>) iterable) {
                    hasNull = !ObjectUtil.notNull(value);
                }

                if (!hasNull) {
                    requiredParams.remove(e.getKey());
                }
            } else if (ObjectUtil.notNull(e.getValue())) {
                requiredParams.remove(e.getKey());
            }
        }

        if (requiredParams.size() > 0 && !userParams.getExpressions().isEmpty()
                && !userParams.getUserEnteredParams().isEmpty()) {
            for (Iterator<String> iterator = requiredParams.iterator(); iterator.hasNext();) {
                String parameterName = (String) iterator.next();
                if (StringUtils.isNotEmpty(userParams.getExpressions().get(parameterName))) {
                    String expression = userParams.getExpressions().get(parameterName);
                    if (!EvaluationUtil.isExpression(expression)) {
                        bindingResult.rejectValue("expressions[" + parameterName + "]",
                                "reporting.Report.run.error.invalidParamExpression");
                    }
                } else {
                    bindingResult.rejectValue("userEnteredParams[" + parameterName + "]", "error.required",
                            new Object[] { "This parameter" }, "{0} is required");
                }
            }
        }
    }

    // Try to parse the required parameters into appropriate objects if they are available
    if (!bindingResult.hasErrors()) {

        Map<String, Object> params = new LinkedHashMap<String, Object>();
        if (reportDefinition.getParameters() != null
                && (userParams.getUserEnteredParams() != null || userParams.getExpressions() != null)) {
            for (Parameter parameter : reportDefinition.getParameters()) {
                Object value = null;
                String expression = null;
                if (userParams.getExpressions() != null
                        && ObjectUtil.notNull(userParams.getExpressions().get(parameter.getName())))
                    expression = userParams.getExpressions().get(parameter.getName());
                else
                    value = userParams.getUserEnteredParams().get(parameter.getName());

                if (ObjectUtil.notNull(value) || ObjectUtil.notNull(expression)) {
                    try {
                        if (StringUtils.isNotEmpty(expression))
                            value = expression;
                        else
                            value = WidgetUtil.parseInput(value, parameter.getType(),
                                    parameter.getCollectionType());

                        params.put(parameter.getName(), value);
                    } catch (Exception ex) {
                        bindingResult.rejectValue("userEnteredParams[" + parameter.getName() + "]",
                                ex.getMessage());
                    }
                }
            }
        }

        // if no errors were found while parsing, retrieve a report design object and renderer the data
        if (!bindingResult.hasErrors()) {
            String previewResult = "";
            ReportDesign design = null;
            ReportDesignResource designResource = new ReportDesignResource();
            ReportData result = null;
            EvaluationContext ec = new EvaluationContext();
            ReportService rs = Context.getService(ReportService.class);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            if (StringUtils.isNotEmpty(uuid)) {
                design = rs.getReportDesignByUuid(uuid);
                design.setRendererType(rendererType);
                design.setReportDefinition(reportDefinition);
                design.getProperties().clear();
                design.getResources().clear();

                designResource.setReportDesign(design);
                designResource.setName("template");
                designResource.setContentType("text/html");
                designResource.setContents(script.getBytes("UTF-8"));

                design.addResource(designResource);
                design.addPropertyValue(TextTemplateRenderer.TEMPLATE_TYPE, scriptType);

                if (userParams.getBaseCohort() != null) {
                    try {
                        Cohort baseCohort = Context.getService(CohortDefinitionService.class)
                                .evaluate(userParams.getBaseCohort(), ec);
                        ec.setBaseCohort(baseCohort);

                    } catch (Exception ex) {
                        throw new EvaluationException("baseCohort", ex);
                    }
                }

                if (params != null) {
                    ec.setParameterValues(params);
                }

                Class<?> rt = Context.loadClass(design.getRendererType().getName());
                ReportTemplateRenderer reportRenderer = (ReportTemplateRenderer) rt.newInstance();
                Throwable errorDetails = null;
                result = rds.evaluate(reportDefinition, ec);
                try {
                    reportRenderer.render(result, design.getUuid(), out);
                } catch (Throwable e) {
                    errorDetails = e;
                }
                previewResult = (out.toByteArray() != null ? new String(out.toByteArray(), "UTF-8") : "");
                StringUtils.deleteWhitespace(previewResult);
                model.addAttribute("previewResult", previewResult);
                model.addAttribute("design", design);
                model.addAttribute("errorDetails", errorDetails);

            }

        }

    }

    model.addAttribute("iframe", iframe);
    model.addAttribute("script", script);
    model.addAttribute("scriptType", scriptType);
    model.addAttribute("reportDefinition", reportDefinition);
    model.addAttribute("rendererType", rendererType);
    model.addAttribute("errors", bindingResult);

}

From source file:org.openmrs.serialization.SimpleXStreamSerializerTest.java

/**
 * @throws SerializationException//from   w ww.  j  ava  2 s. co  m
 * @see org.openmrs.serialization.SimpleXStreamSerializer#serialize(Object)
 */
@Test
@Verifies(value = "should get serialized", method = "serialize(Object)")
public void serialize_shouldSerializeObject() throws SerializationException {

    OpenmrsSerializer serializer = new SimpleXStreamSerializer();

    Foo foo = new Foo("test", 1);
    List<String> list = new ArrayList<String>();
    list.add("foo");
    list.add("bar");
    Map<Integer, String> map = new HashMap<Integer, String>();
    map.put(1, "foo");
    map.put(2, "fooBar");
    map.put(3, "bar");
    foo.setAttributeList(list);
    foo.setAttributeMap(map);

    String serializedFoo = serializer.serialize(foo);

    Assert.assertTrue(StringUtils.deleteWhitespace(serializedFoo)
            .equals(StringUtils.deleteWhitespace("<org.openmrs.serialization.Foo>\n"
                    + "  <attributeString>test</attributeString>\n" + "  <attributeInt>1</attributeInt>\n"
                    + "  <attributeList>\n" + "    <string>foo</string>\n" + "    <string>bar</string>\n"
                    + "  </attributeList>\n" + "  <attributeMap>\n" + "    <entry>\n" + "      <int>1</int>\n"
                    + "      <string>foo</string>\n" + "    </entry>\n" + "    <entry>\n"
                    + "      <int>2</int>\n" + "      <string>fooBar</string>\n" + "    </entry>\n"
                    + "    <entry>\n" + "      <int>3</int>\n" + "      <string>bar</string>\n"
                    + "    </entry>\n" + "  </attributeMap>\n" + "  </org.openmrs.serialization.Foo>")));

}

From source file:org.opennms.features.jmxconfiggenerator.graphs.JmxConfigReader.java

private Collection<Report> generateAttributeReportsByMBean(Mbean mbean) {
    Collection<Report> reports = new ArrayList<Report>();
    for (Attrib attrib : mbean.getAttrib()) {
        final String title = String.format("%s[%s]", mbean.getObjectname().toString(), attrib.getName());
        final String reportId = StringUtils.deleteWhitespace(mbean.getName()) + "." + attrib.getAlias() + "."
                + ATTRIBUTEREPORT;// ww  w . j  a  v  a  2  s .co  m
        Report report = new Report(reportId, title, title, "verticalLabel");
        report.addGraph(new Graph(attrib.getAlias(), attrib.getName(), attrib.getAlias(), Colors.getNextColor(),
                Colors.getNextColor(), Colors.getNextColor()));
        reports.add(report);
        Colors.resetColor();
    }
    return reports;
}

From source file:org.opennms.features.jmxconfiggenerator.graphs.JmxConfigReader.java

private Collection<Report> generateMbeanReportsByMBean(Mbean mbean) {
    Collection<Report> reports = new ArrayList<Report>();
    if (!mbean.getAttrib().isEmpty()) {

        String reportId = StringUtils.deleteWhitespace(mbean.getName()) + "." + MBEANREPORT;
        Report report = new Report(reportId, mbean.getName(), mbean.getName(), "verticalLabel");
        for (Attrib attrib : mbean.getAttrib()) {
            report.addGraph(new Graph(attrib.getAlias(), attrib.getName(), attrib.getAlias(),
                    Colors.getNextColor(), Colors.getNextColor(), Colors.getNextColor()));
        }// w  w w .ja  v  a2s.co m
        reports.add(report);
        Colors.resetColor();
    }
    return reports;
}

From source file:org.opennms.features.jmxconfiggenerator.graphs.JmxConfigReader.java

private Collection<Report> generateCompositeReportsByMBean(Mbean mbean) {
    Collection<Report> reports = new ArrayList<Report>();

    for (CompAttrib compAttrib : mbean.getCompAttrib()) {

        String reportId = StringUtils.deleteWhitespace(mbean.getName()) + "." + compAttrib.getName() + "."
                + COMPOSITEREPORT;//from  w  w  w  . j a v a  2  s . c  o  m

        Report report = new Report(reportId, reportId, reportId, "verticalLabel");
        for (CompMember compMember : compAttrib.getCompMember()) {
            report.addGraph(new Graph(compMember.getAlias(), compMember.getName(), compMember.getAlias(),
                    Colors.getNextColor(), Colors.getNextColor(), Colors.getNextColor()));
        }
        reports.add(report);
        Colors.resetColor();
    }
    return reports;
}

From source file:org.opennms.features.jmxconfiggenerator.graphs.JmxConfigReader.java

private Collection<Report> generateCompositeMemberReportsByMBean(Mbean mbean) {
    Collection<Report> reports = new ArrayList<Report>();

    for (CompAttrib compAttrib : mbean.getCompAttrib()) {
        for (CompMember compMember : compAttrib.getCompMember()) {

            String reportId = StringUtils.deleteWhitespace(mbean.getName()) + "." + compAttrib.getName() + "."
                    + compMember.getName() + "." + COMPOSITATTRIBEREPORT;

            Report report = new Report(reportId, reportId, reportId, "verticalLabel");
            report.addGraph(new Graph(compMember.getAlias(), compMember.getName(), compMember.getAlias(),
                    Colors.getNextColor(), Colors.getNextColor(), Colors.getNextColor()));
            reports.add(report);/*from  ww w.  j  a  v a2s. c  o m*/
            Colors.resetColor();
        }
    }
    return reports;
}

From source file:org.opennms.jmxconfiggenerator.graphs.JmxConfigReader.java

private Collection<Report> generateAttributeReporsByMBean(Mbean mbean) {
    Collection<Report> reports = new ArrayList<Report>();
    for (Attrib attrib : mbean.getAttrib()) {

        String reportId = StringUtils.deleteWhitespace(mbean.getName()) + "." + attrib.getAlias() + "."
                + ATTRIBUTEREPORT;//from  w  ww .j a va  2  s.  c om
        Report report = new Report(reportId, attrib.getName(), attrib.getName(), "verticalLabel");
        report.addGraph(new Graph(attrib.getAlias(), attrib.getName(), attrib.getAlias(), Colors.getNextColor(),
                Colors.getNextColor(), Colors.getNextColor()));
        reports.add(report);
        Colors.restetColor();
    }
    return reports;
}

From source file:org.opennms.jmxconfiggenerator.graphs.JmxConfigReader.java

private Collection<Report> generateMbeanReportsByMBean(Mbean mbean) {
    Collection<Report> reports = new ArrayList<Report>();
    if (!mbean.getAttrib().isEmpty()) {

        String reportId = StringUtils.deleteWhitespace(mbean.getName()) + "." + MBEANREPORT;
        Report report = new Report(reportId, mbean.getName(), mbean.getName(), "verticalLabel");
        for (Attrib attrib : mbean.getAttrib()) {
            report.addGraph(new Graph(attrib.getAlias(), attrib.getName(), attrib.getAlias(),
                    Colors.getNextColor(), Colors.getNextColor(), Colors.getNextColor()));
        }/*from ww  w . ja v a2  s  .c o  m*/
        reports.add(report);
        Colors.restetColor();
    }
    return reports;
}

From source file:org.opennms.jmxconfiggenerator.graphs.JmxConfigReader.java

private Collection<Report> generateCompositeReportsByMBean(Mbean mbean) {
    Collection<Report> reports = new ArrayList<Report>();

    for (CompAttrib compAttrib : mbean.getCompAttrib()) {

        String reportId = StringUtils.deleteWhitespace(mbean.getName()) + "." + compAttrib.getName() + "."
                + COMPOSITEREPORT;//from www. ja  va 2s.  c om

        Report report = new Report(reportId, reportId, reportId, "verticalLabel");
        for (CompMember compMember : compAttrib.getCompMember()) {
            report.addGraph(new Graph(compMember.getAlias(), compMember.getName(), compMember.getAlias(),
                    Colors.getNextColor(), Colors.getNextColor(), Colors.getNextColor()));
        }
        reports.add(report);
        Colors.restetColor();
    }
    return reports;
}