Example usage for org.w3c.dom Document createCDATASection

List of usage examples for org.w3c.dom Document createCDATASection

Introduction

In this page you can find the example usage for org.w3c.dom Document createCDATASection.

Prototype

public CDATASection createCDATASection(String data) throws DOMException;

Source Link

Document

Creates a CDATASection node whose value is the specified string.

Usage

From source file:org.devgateway.eudevfin.reports.core.utils.ReportTemplate.java

private void appendReportRows(HashMap<String, Node> matchingRows, HashMap<String, Node> matchingColumns,
        RowReport row, Document doc, boolean swapAxis) {
    Node rowNode = matchingRows.get("r_" + row.getName());
    if (rowNode == null)
        return;/*from   www .  j av  a 2  s.  c o m*/
    Integer rowMultiplier = 1;
    //TODO: Remove this terrible hack for the two columns of DAC2a that needs to subtract
    if (row.getName().equals("205") || row.getName().startsWith("205_") || row.getName().equals("215")
            || row.getName().startsWith("215_") || row.getName().equals("219")
            || row.getName().startsWith("219_")) {
        rowMultiplier = -1;
    }
    Integer yCoord, xCoord, width, height;
    xCoord = yCoord = 0;
    //Set default height
    height = 15;
    //Set default width
    width = 55;
    if (swapAxis) {
        xCoord = rowNode.getAttributes().getNamedItem("x") != null
                ? Integer.parseInt(rowNode.getAttributes().getNamedItem("x").getNodeValue())
                : 0;
    } else {
        yCoord = rowNode.getAttributes().getNamedItem("y") != null
                ? Integer.parseInt(rowNode.getAttributes().getNamedItem("y").getNodeValue())
                : 0;
    }

    Set<ColumnReport> columns = row.getColumns();
    //Store multipliers for later use
    HashMap<String, Integer> multipliersByColumn = new HashMap<String, Integer>();
    for (ColumnReport column : columns) {
        multipliersByColumn.put(column.getName(), column.getMultiplier());
    }
    for (ColumnReport column : columns) {
        //         UUID uuid = UUID.randomUUID();
        String cellStyle = (rowNode.getAttributes().getNamedItem("style") != null)
                ? rowNode.getAttributes().getNamedItem("style").getNodeValue()
                : "";

        Node columnNode = matchingColumns.get("c_" + column.getName());
        if (columnNode == null)
            continue;
        Node parentNode;
        if (swapAxis) {
            parentNode = columnNode.getParentNode().getParentNode();
            yCoord = columnNode.getAttributes().getNamedItem("y") != null
                    ? Integer.parseInt(columnNode.getAttributes().getNamedItem("y").getNodeValue())
                    : 0;
        } else {
            parentNode = rowNode.getParentNode().getParentNode();
            xCoord = columnNode.getAttributes().getNamedItem("x") != null
                    ? Integer.parseInt(columnNode.getAttributes().getNamedItem("x").getNodeValue())
                    : 0;
            width = columnNode.getAttributes().getNamedItem("width") != null
                    ? Integer.parseInt(columnNode.getAttributes().getNamedItem("width").getNodeValue())
                    : 0;
            height = rowNode.getAttributes().getNamedItem("height") != null
                    ? Integer.parseInt(rowNode.getAttributes().getNamedItem("height").getNodeValue())
                    : 0;
            if (cellStyle.equals("") && (columnNode.getAttributes().getNamedItem("style") != null)) {
                cellStyle = columnNode.getAttributes().getNamedItem("style").getNodeValue();
            }
        }

        Element textField = doc.createElement("textField");
        textField.setAttribute("pattern", column.getPattern() != null ? column.getPattern() : "#,##0.00");

        Element reportElement = doc.createElement("reportElement");
        reportElement.setAttribute("key", "r_" + row.getName() + "_c_" + column.getName());
        reportElement.setAttribute("x", xCoord.toString());
        reportElement.setAttribute("y", yCoord.toString());
        reportElement.setAttribute("width", width.toString());
        if (row.getVisible() != null && !row.getVisible()) {
            reportElement.setAttribute("height", "0");
        } else {
            reportElement.setAttribute("height", height.toString());
        }
        if (!cellStyle.equals("")) {
            reportElement.setAttribute("style", cellStyle);
        }

        Element textElement = doc.createElement("textElement");
        textElement.setAttribute("textAlignment", "Right");

        Element textFieldExpression = doc.createElement("textFieldExpression");

        if (column.getType() == Constants.CALCULATED) {
            StringBuffer expression = new StringBuffer();
            if (column.getSlicer().equals("All")) {
                String fieldName = row.getName() + "_" + column.getName() + "_All_" + column.getMeasure();
                expression.append("checkNull($F{" + fieldName + "}).doubleValue()");
            } else {
                String[] types = column.getSlicer().split(",");
                for (int i = 0; i < types.length; i++) {
                    if (!types[i].equals("")) {
                        String fieldName = row.getName() + "_" + column.getName() + "_" + shortenType(types[i])
                                + "_" + column.getMeasure();
                        if (nonFlowItems.contains(column.getSlicer())) {
                            expression.append("$F{" + fieldName + "}");
                        } else {
                            expression.append("checkNull($F{" + fieldName + "}).doubleValue()");
                        }
                        if (i != types.length - 1) {
                            expression.append("+");
                        }
                    }
                }
            }
            String finalExpression = "";

            Integer multiplier = rowMultiplier * column.getMultiplier();
            if (expression.length() > 0) {
                finalExpression = "checkZero((" + expression.toString() + "), " + multiplier
                        + ").doubleValue()";
            }

            CDATASection cdata = doc.createCDATASection(finalExpression);
            textFieldExpression.appendChild(cdata);
        } else if (column.getType() == Constants.SUM) {
            StringBuffer expression = new StringBuffer();
            Set<String> subcols = column.getColumnCodes();
            for (Iterator<String> it = subcols.iterator(); it.hasNext();) {
                String code = it.next();
                String[] types = code.split(",");
                for (int i = 0; i < types.length; i++) {
                    String fieldName = row.getName() + "_" + types[i];
                    expression.append("(");
                    expression.append("checkNull($F{" + fieldName + "}).doubleValue()");
                    String extractedColumnName = types[i].split("_")[0];
                    Integer multiplier = multipliersByColumn.get(extractedColumnName);
                    if (multiplier == null) {
                        multiplier = 1;
                    }
                    if (multiplier != null && multiplier < 0) {
                        expression.append("* (" + multiplier + ")");
                    }
                    expression.append(")");
                    if (i != types.length - 1) {
                        expression.append("+");
                    }
                }
                if (it.hasNext()) {
                    expression.append("+");
                }
            }
            String finalExpression = "";
            if (expression.length() > 0)
                finalExpression = "checkZero((" + expression.toString() + "), " + rowMultiplier
                        + ").doubleValue()";

            CDATASection cdata = doc.createCDATASection(finalExpression);
            textFieldExpression.appendChild(cdata);
        } else {
            textField = doc.createElement("staticText");
            textFieldExpression = doc.createElement("text");
            CDATASection cdata = doc.createCDATASection("////////");
            textFieldExpression.appendChild(cdata);
        }

        textField.appendChild(reportElement);
        textField.appendChild(textElement);
        textField.appendChild(textFieldExpression);
        parentNode.appendChild(textField);
    }

}

From source file:org.devgateway.eudevfin.reports.core.utils.ReportTemplate.java

private void generateFields(List<RowReport> rows, Document doc) {

    for (RowReport row : rows) {
        Set<ColumnReport> columns = row.getColumns();
        for (ColumnReport column : columns) {
            if (column.getType() == Constants.CALCULATED) {
                if (column.getSlicer().equals("All")) {
                    String fieldName = row.getName() + "_" + column.getName() + "_All_" + column.getMeasure();
                    Element field = doc.createElement("field");
                    field.setAttribute("name", fieldName);
                    field.setAttribute("class", "java.lang.Number");

                    Element fieldDescription = doc.createElement("fieldDescription");
                    CDATASection cdata = doc.createCDATASection("Data((" + column.getMeasure()
                            + ",[Type of Finance].[TYPE_OF_FINANCE##100]), [Type of Aid].[" + row.getName()
                            + "])\n");
                    fieldDescription.appendChild(cdata);
                    field.appendChild(fieldDescription);
                    Node background = doc.getElementsByTagName("background").item(0);
                    doc.getDocumentElement().insertBefore(field, background);
                } else {
                    for (String type : column.getSlicer().split(",")) {
                        String shortType = shortenType(type);
                        if (!type.equals("")) {
                            String fieldName = row.getName() + "_" + column.getName() + "_" + shortType + "_"
                                    + column.getMeasure();
                            Element field = doc.createElement("field");
                            field.setAttribute("name", fieldName);
                            field.setAttribute("class", "java.lang.Number");

                            Element fieldDescription = doc.createElement("fieldDescription");
                            CDATASection cdata = doc.createCDATASection("Data((" + column.getMeasure() + ", "
                                    + type + "), [Type of Aid].[" + row.getName() + "])\n");
                            fieldDescription.appendChild(cdata);
                            field.appendChild(fieldDescription);
                            Node background = doc.getElementsByTagName("background").item(0);
                            doc.getDocumentElement().insertBefore(field, background);

                        }/*from w w w  .  java2  s  .  c  om*/
                    }
                }
            }
        }
    }
}

From source file:org.devgateway.eudevfin.reports.core.utils.ReportTemplate.java

private void generateMDX(List<RowReport> rows, Document doc, String slicer) {
    if (slicer == null || slicer.equals(""))
        slicer = "[Type of Finance].[Code].Members";
    StringBuffer str = new StringBuffer();
    List<RowReport> calculatedRows = new ArrayList<RowReport>();
    for (RowReport row : rows) {
        if (row.getType() == Constants.CALCULATED) {
            calculatedRows.add(row);//from   www .  ja  v  a 2  s  .  c om
        }
    }

    str.append("WITH\n");
    ArrayList<String> measures = new ArrayList<String>();

    for (RowReport row : calculatedRows) {
        if (row.getType() == Constants.CALCULATED) {
            for (ColumnReport column : row.getColumns()) {
                String measure = column.getMeasure();
                if (measure != null && !measure.equals("") && !measures.contains(measure))
                    measures.add(measure);
            }
            str.append("MEMBER ");
            str.append("[Type of Aid].[" + row.getName() + "]");
            str.append(" as SUM(");
            String formula = row.getFormula();
            if (formula.equals("")) {
                formula = "[BiMultilateral].Members";
            }
            str.append(formula);

            str.append(")\n");
        }
    }

    str.append("\n");
    for (String measure : measures) {
        str.append("MEMBER " + measure + " AS " + measureMap.get(measure) + " \n");
    }
    str.append("SELECT {");
    for (Iterator<RowReport> it = calculatedRows.iterator(); it.hasNext();) {
        RowReport row = it.next();
        str.append("[Type of Aid].[" + row.getName() + "]");
        if (it.hasNext())
            str.append(",");
    }
    str.append("}  ON ROWS, \n");
    str.append(" NON EMPTY {");
    str.append(StringUtils.join(measures.toArray(), ","));
    str.append("}*" + slicer + " ON COLUMNS \n");
    str.append("FROM [Financial] \n");
    str.append(
            "WHERE {[Reporting Year].[$P{REPORTING_YEAR}]} * {[Form Type].[bilateralOda.CRS], [Form Type].[multilateralOda.CRS], [Form Type].[nonOda.nonExport], [Form Type].[nonOda.export], [Form Type].[nonOda.privateGrants], [Form Type].[nonOda.privateMarket], [Form Type].[nonOda.otherFlows], [Form Type].[Unspecified]}\n");
    Node queryString = doc.getElementsByTagName("queryString").item(0);
    CDATASection cdata = doc.createCDATASection(str.toString());
    queryString.appendChild(cdata);
}

From source file:org.etudes.mneme.impl.ExportQtiServiceImpl.java

/**
 * create final message element.//from w w  w.  j a  v  a2s.  co  m
 * 
 * @param assessmentTestDocument
 * @param text
 * @return
 */
private Element createFinalFeedbackElement(Document assessmentTestDocument, String text) {
    Element testFeedbackElement = assessmentTestDocument.createElement("testFeedback");
    testFeedbackElement.setAttribute("access", "atEnd");
    testFeedbackElement.setAttribute("showHide", "hide");
    testFeedbackElement.setAttribute("identifier", "FB_Total");

    Element feedbackContentElement = assessmentTestDocument.createElement("div");
    feedbackContentElement.appendChild(assessmentTestDocument.createCDATASection(text));
    testFeedbackElement.appendChild(feedbackContentElement);
    return testFeedbackElement;
}

From source file:org.etudes.mneme.impl.ExportQtiServiceImpl.java

/**
 * //from  ww  w. j  ava  2 s . c o  m
 * @param poolDocument
 * @param question
 * @param text
 * @return
 */
public Map<String, Element> getallQuestionElements(ZipOutputStream zip, Document questionDocument,
        String testId, Question question, String context, List<String> mediaFiles) throws Exception {
    Map<String, Element> questionParts = new HashMap<String, Element>();

    String text = question.getPresentation().getText();
    Element itemBody = questionDocument.createElement("itemBody");

    // for fill blanks text will be chopped
    if (!"mneme:FillBlanks".equals(question.getType())) {
        if (text == null)
            return questionParts;
        // process embed media and change path as Resources/xxxx.jpg

        // security advisor
        pushAdvisor();
        itemBody = translateEmbedData(zip, testId + "/Resources/", text, itemBody, mediaFiles,
                questionDocument);

        popAdvisor();

        if (mediaFiles.isEmpty()) {
            Element div = questionDocument.createElement("div");
            div.setTextContent(text);
            itemBody.appendChild(div);
        }
    }

    // split the text based on <br/>
    if ("mneme:FillBlanks".equals(question.getType())) {
        FillBlanksQuestionImpl f = (FillBlanksQuestionImpl) (question.getTypeSpecificQuestion());
        text = f.getText();
        itemBody = getFillBlanksResponseChoices(questionDocument, itemBody, f, questionParts);
    } else if ("mneme:TrueFalse".equals(question.getType())) {
        getTFResponseChoices(questionDocument, question, questionParts);
        if (questionParts.containsKey("choiceInteraction"))
            itemBody.appendChild(questionParts.get("choiceInteraction"));
    } else if ("mneme:MultipleChoice".equals(question.getType())) {
        getMCResponseChoices(questionDocument, question, questionParts);
        if (questionParts.containsKey("choiceInteraction"))
            itemBody.appendChild(questionParts.get("choiceInteraction"));
    } else if ("mneme:Match".equals(question.getType())) {
        getMatchResponseChoices(questionDocument, question, questionParts);
        if (questionParts.containsKey("matchInteraction"))
            itemBody.appendChild(questionParts.get("matchInteraction"));
    } else if ("mneme:Essay".equals(question.getType())) {
        getEssayResponseChoices(questionDocument, question, questionParts);
        if (questionParts.containsKey("extendedTextInteraction"))
            itemBody.appendChild(questionParts.get("extendedTextInteraction"));
        if (questionParts.containsKey("uploadInteraction"))
            itemBody.appendChild(questionParts.get("uploadInteraction"));
    } else if ("mneme:Task".equals(question.getType())) {
        // no model answer and no submission type and no basetype
        Element responseDeclaration = questionDocument.createElement("responseDeclaration");
        responseDeclaration.setAttribute("identifier", "RESPONSE");
        responseDeclaration.setAttribute("cardinality", "single");
        questionParts.put("responseDeclaration", responseDeclaration);
    } else if ("mneme:LikertScale".equals(question.getType())) {
        // <itemBody class="likert">
        itemBody.setAttribute("class", "likert");
        getLikertScaleResponseChoices(questionDocument, question, questionParts);
        if (questionParts.containsKey("choiceInteraction"))
            itemBody.appendChild(questionParts.get("choiceInteraction"));
    }

    // Hints are part of item body
    if (question.getHints() != null && question.getHints().length() != 0) {
        Element feedbackInlineElement = questionDocument.createElement("feedbackInline");
        feedbackInlineElement.setAttribute("showHide", "hide");
        feedbackInlineElement.setAttribute("identifier", "FB_Hints");
        feedbackInlineElement.appendChild(questionDocument.createCDATASection(question.getHints()));
        itemBody.appendChild(feedbackInlineElement);
    }

    // question feedback
    if (question.getFeedback() != null && question.getFeedback().length() != 0) {
        Element feedbackElement = questionDocument.createElement("modalFeedback");
        feedbackElement.setAttribute("showHide", "hide");
        feedbackElement.setAttribute("identifier", "FB_Question");
        feedbackElement.appendChild(questionDocument.createCDATASection(question.getFeedback()));
        questionParts.put("modalFeedback", feedbackElement);
    }
    questionParts.put("itemBody", itemBody);
    return questionParts;
}

From source file:org.fireflow.model.io.resource.ResourceSerializer.java

protected static void writeDescription(Element parent, String desc) {
    if (desc == null || desc.trim().equals(""))
        return;//from  w ww.j a  v a  2 s .  c  o  m
    Document doc = parent.getOwnerDocument();
    Element descElem = Util4Serializer.addElement(parent, DESCRIPTION);

    CDATASection cdata = doc.createCDATASection(useJDKTransformerFactory ? (" " + desc) : desc);
    descElem.appendChild(cdata);
}

From source file:org.fireflow.model.io.service.ServiceParser.java

protected void writeExpression(Expression exp, Element parent) {
    if (exp == null)
        return;//w w w . jav a 2 s .  com
    Element expressionElem = Util4Serializer.addElement(parent, EXPRESSION);
    if (exp.getName() != null && !exp.getName().trim().equals("")) {
        expressionElem.setAttribute(NAME, exp.getName());
    }
    if (exp.getDisplayName() != null && !exp.getDisplayName().trim().equals("")) {
        expressionElem.setAttribute(DISPLAY_NAME, exp.getDisplayName());
    }
    expressionElem.setAttribute(LANGUAGE, exp.getLanguage());
    Document doc = parent.getOwnerDocument();
    String body = exp.getBody() == null ? "" : exp.getBody();
    CDATASection cdata = doc.createCDATASection(useJDKTransformerFactory ? (" " + body) : body);
    Element bodyElem = Util4Serializer.addElement(expressionElem, BODY);
    bodyElem.appendChild(cdata);

    if (exp.getNamespaceMap() != null && exp.getNamespaceMap().size() > 0) {

        Element namespaceMapElem = Util4Serializer.addElement(expressionElem, NAMESPACE_PREFIX_URI_MAP);
        Iterator<Map.Entry<String, String>> entrys = exp.getNamespaceMap().entrySet().iterator();
        while (entrys.hasNext()) {
            Map.Entry<String, String> entry = entrys.next();
            Element entryElem = Util4Serializer.addElement(namespaceMapElem, ENTRY);
            entryElem.setAttribute(NAME, entry.getKey());
            entryElem.setAttribute(VALUE, entry.getValue());
        }
    }

}

From source file:org.fireflow.model.io.service.ServiceParser.java

protected void writeDescription(Element parent, String desc) {
    if (desc == null || desc.trim().equals(""))
        return;// w  w  w.  j a v  a2 s. c  om
    Document doc = parent.getOwnerDocument();
    Element descElem = Util4Serializer.addElement(parent, DESCRIPTION);

    CDATASection cdata = doc.createCDATASection(useJDKTransformerFactory ? (" " + desc) : desc);
    descElem.appendChild(cdata);
}

From source file:org.fireflow.pdl.fpdl.io.FPDLSerializer.java

protected void writeLabel(Label lb, Element parentElm) {
    if (lb != null) {
        Element labelElm = Util4Serializer.addElement(parentElm, LABEL);

        if (!StringUtils.isEmpty(lb.getTextDirection())) {
            labelElm.setAttribute(TEXT_DIRECTION, lb.getTextDirection());
        }//from   ww w.  ja v  a 2  s . c  om
        if (!StringUtils.isEmpty(lb.getFontName())) {
            labelElm.setAttribute(FONT_NAME, lb.getFontName());
        }
        labelElm.setAttribute(SIZE, Integer.toString(lb.getFontSize()));

        if (!StringUtils.isEmpty(lb.getFontColor())) {
            labelElm.setAttribute(COLOR, lb.getFontColor());
        }
        if (!StringUtils.isEmpty(lb.getFontStyle())) {

            labelElm.setAttribute(FONT_STYLE, lb.getFontStyle());
        }

        Document doc = parentElm.getOwnerDocument();
        CDATASection cdata = doc
                .createCDATASection(useJDKTransformerFactory ? (" " + lb.getText()) : lb.getText());//?jdkGBK bug 
        labelElm.appendChild(cdata);
    }

}

From source file:org.fireflow.pdl.fpdl.io.FPDLSerializer.java

protected void writeExpression(Expression exp, Element parent) {
    if (exp == null)
        return;/*from   w  w w .  j  a va2  s.  c o m*/
    Element expressionElem = Util4Serializer.addElement(parent, EXPRESSION);
    if (exp.getName() != null && !exp.getName().trim().equals("")) {
        expressionElem.setAttribute(NAME, exp.getName());
    }
    if (exp.getDisplayName() != null && !exp.getDisplayName().trim().equals("")) {
        expressionElem.setAttribute(DISPLAY_NAME, exp.getDisplayName());
    }
    if (exp.getDataType() != null) {
        expressionElem.setAttribute(DATA_TYPE, exp.getDataType().toString());
    }
    expressionElem.setAttribute(LANGUAGE, exp.getLanguage());
    Document doc = parent.getOwnerDocument();

    Element bodyElem = Util4Serializer.addElement(expressionElem, BODY);
    String body = exp.getBody() != null ? exp.getBody() : "";
    CDATASection cdata = doc.createCDATASection(useJDKTransformerFactory ? (" " + body) : body);//?jdk GBK bug
    bodyElem.appendChild(cdata);

    if (exp.getNamespaceMap() != null && exp.getNamespaceMap().size() > 0) {

        Element namespaceMapElem = Util4Serializer.addElement(expressionElem, NAMESPACE_PREFIX_URI_MAP);
        Iterator<Map.Entry<String, String>> entrys = exp.getNamespaceMap().entrySet().iterator();
        while (entrys.hasNext()) {
            Map.Entry<String, String> entry = entrys.next();
            Element entryElem = Util4Serializer.addElement(namespaceMapElem, ENTRY);
            entryElem.setAttribute(NAME, entry.getKey());
            entryElem.setAttribute(VALUE, entry.getValue());
        }
    }
}