Java Code Examples for org.dom4j.Element#addCDATA()

The following examples show how to use org.dom4j.Element#addCDATA() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomCondition.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
public void toXml(Element element, Param.ValueProperty valueProperty) {
    super.toXml(element, valueProperty);

    element.addAttribute("type", ConditionType.CUSTOM.name());

    if (isBlank(caption)) {
        element.addAttribute("locCaption", locCaption);
    }

    element.addAttribute("entityAlias", entityAlias);

    if (!isBlank(join)) {
        Element joinElement = element.addElement("join");
        joinElement.addCDATA(join);
    }
    if (operator != null) {
        element.addAttribute("operatorType", operator.name());
    }
}
 
Example 2
Source File: PropertyCondition.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
public void toXml(Element element, Param.ValueProperty valueProperty) {
    super.toXml(element, valueProperty);
    element.addAttribute("type", ConditionType.PROPERTY.name());
    element.addAttribute("operatorType", getOperatorType());

    if (metaClass instanceof KeyValueMetaClass){
        element.addAttribute("entityAlias", entityAlias);
        element.addAttribute("propertiesPath", propertiesPath);
    }

    if (!Strings.isNullOrEmpty(join)) {
        if (element.attribute("entityAlias") == null) {
            element.addAttribute("entityAlias", entityAlias);
        }
        Element joinElement = element.addElement("join");
        joinElement.addCDATA(join);
    }
}
 
Example 3
Source File: PublishUtil.java    From boubei-tss with Apache License 2.0 5 votes vote down vote up
private static void addValue2XmlNode(Element articleElement, Map<String, Object> attributes) {
    Element eleKey = null;
    for( Entry<String, Object> entry : attributes.entrySet() ) {
        String key = entry.getKey();
        Object value = entry.getValue();
        if(value == null) continue;
                   
        eleKey = articleElement.addElement(key);
        eleKey.addCDATA(value.toString()); 
    }
}
 
Example 4
Source File: XMLProperties.java    From Whack with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the value of the specified property. If the property doesn't
 * currently exist, it will be automatically created.
 *
 * @param name  the name of the property to set.
 * @param value the new value for the property.
 */
public synchronized void setProperty(String name, String value) {
    if (name == null) {
        return;
    }
    if (value == null) {
        value = "";
    }

    // Set cache correctly with prop name and value.
    propertyCache.put(name, value);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length; i++) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }
        element = element.element(propName[i]);
    }
    // Set the value of the property in this node.
    if (value.startsWith("<![CDATA[")) {
        element.addCDATA(value.substring(9, value.length()-3));
    }
    else {
        element.setText(value);
    }
    // Write the XML properties to disk
    saveProperties();

    // Generate event.
    Map<String, String> params = new HashMap<String,String>();
    params.put("value", value);
    PropertyEventDispatcher.dispatchEvent(name,
            PropertyEventDispatcher.EventType.xml_property_set, params);
}
 
Example 5
Source File: QTIEditHelperEBL.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * Add objectives.
 * 
 * @param root
 * @param objectives
 */
public static void addObjectives(final Element root, final String objectives) {
    if (objectives != null && objectives.length() > 0) {
        final Element mattext = root.addElement("objectives").addElement("material").addElement("mattext");
        mattext.addCDATA(objectives);
    }
}
 
Example 6
Source File: FIBQuestion.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * Build mastery respcondition for FIB in all-banks-must-be-correct mode. Adds one respcondition in which all blanks must be answered correctly. This respcondition
 * uses the mastery feedback.
 * 
 * @param resprocessingXML
 */
private void buildRespconditionFIBSingle(final Element resprocessingXML) {
    final Element correct = resprocessingXML.addElement("respcondition");
    correct.addAttribute("title", "Mastery");
    correct.addAttribute("continue", "Yes");

    final Element conditionvar = correct.addElement("conditionvar");
    final Element and = conditionvar.addElement("and");
    for (final Iterator i = getResponses().iterator(); i.hasNext();) {
        final FIBResponse fib = (FIBResponse) i.next();
        if (fib.getType().equals(FIBResponse.TYPE_BLANK)) {
            final String[] correctFIBs = fib.getCorrectBlank().split(";");
            final Element or = and.addElement("or");
            for (int j = 0; j < correctFIBs.length; j++) {
                final Element varequal = or.addElement("varequal");
                varequal.addAttribute("respident", fib.getIdent());
                varequal.addAttribute("case", fib.getCaseSensitive());
                if (correctFIBs[j].length() > 0) {
                    varequal.addCDATA(correctFIBs[j]);
                }
            }
        }
    }

    final Element setvar = correct.addElement("setvar");
    setvar.addAttribute("varname", "SCORE");
    setvar.addAttribute("action", "Set");
    setvar.setText("" + getSingleCorrectScore());

    // Use mastery feedback
    QTIEditHelperEBL.addFeedbackMastery(correct);

    // remove whole respcondition if empty
    if (and.element("or") == null) {
        resprocessingXML.remove(correct);
    }
}
 
Example 7
Source File: Mattext.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
*/
  @Override
  public void addToElement(final Element root) {
      if (content != null) {
          final Element mattext = root.addElement("mattext");
          // Since we use rich text (html) as content, the text type is set to
          // "text/html". This way the document conforms to the qti standard.
          mattext.addAttribute("texttype", "text/html");
          mattext.addCDATA(content);
      }
  }
 
Example 8
Source File: QTIEditHelperEBL.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * Add objectives.
 * 
 * @param root
 * @param objectives
 */
public static void addObjectives(final Element root, final String objectives) {
    if (objectives != null && objectives.length() > 0) {
        final Element mattext = root.addElement("objectives").addElement("material").addElement("mattext");
        mattext.addCDATA(objectives);
    }
}
 
Example 9
Source File: FIBQuestion.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * Build mastery respcondition for FIB in all-banks-must-be-correct mode. Adds one respcondition in which all blanks must be answered correctly. This respcondition
 * uses the mastery feedback.
 * 
 * @param resprocessingXML
 */
private void buildRespconditionFIBSingle(final Element resprocessingXML) {
    final Element correct = resprocessingXML.addElement("respcondition");
    correct.addAttribute("title", "Mastery");
    correct.addAttribute("continue", "Yes");

    final Element conditionvar = correct.addElement("conditionvar");
    final Element and = conditionvar.addElement("and");
    for (final Iterator i = getResponses().iterator(); i.hasNext();) {
        final FIBResponse fib = (FIBResponse) i.next();
        if (fib.getType().equals(FIBResponse.TYPE_BLANK)) {
            final String[] correctFIBs = fib.getCorrectBlank().split(";");
            final Element or = and.addElement("or");
            for (int j = 0; j < correctFIBs.length; j++) {
                final Element varequal = or.addElement("varequal");
                varequal.addAttribute("respident", fib.getIdent());
                varequal.addAttribute("case", fib.getCaseSensitive());
                if (correctFIBs[j].length() > 0) {
                    varequal.addCDATA(correctFIBs[j]);
                }
            }
        }
    }

    final Element setvar = correct.addElement("setvar");
    setvar.addAttribute("varname", "SCORE");
    setvar.addAttribute("action", "Set");
    setvar.setText("" + getSingleCorrectScore());

    // Use mastery feedback
    QTIEditHelperEBL.addFeedbackMastery(correct);

    // remove whole respcondition if empty
    if (and.element("or") == null) {
        resprocessingXML.remove(correct);
    }
}
 
Example 10
Source File: Mattext.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
*/
  @Override
  public void addToElement(final Element root) {
      if (content != null) {
          final Element mattext = root.addElement("mattext");
          // Since we use rich text (html) as content, the text type is set to
          // "text/html". This way the document conforms to the qti standard.
          mattext.addAttribute("texttype", "text/html");
          mattext.addCDATA(content);
      }
  }
 
Example 11
Source File: JUnitFormatter.java    From validatar with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * Writes out the report for the given testSuites in the JUnit XML format.
 */
@Override
public void writeReport(List<TestSuite> testSuites) throws IOException {
    Document document = DocumentHelper.createDocument();

    Element testSuitesRoot = document.addElement(TESTSUITES_TAG);

    // Output for each test suite
    for (TestSuite testSuite : testSuites) {
        Element testSuiteRoot = testSuitesRoot.addElement(TESTSUITE_TAG);
        testSuiteRoot.addAttribute(TESTS_ATTRIBUTE, Integer.toString(testSuite.queries.size() + testSuite.tests.size()));
        testSuiteRoot.addAttribute(NAME_ATTRIBUTE, testSuite.name);

        for (Query query : testSuite.queries) {
            Element queryNode = testSuiteRoot.addElement(TESTCASE_TAG).addAttribute(NAME_ATTRIBUTE, query.name);
            if (query.failed()) {
                String failureMessage = StringUtils.join(query.getMessages(), NEWLINE);
                queryNode.addElement(FAILED_TAG).addCDATA(failureMessage);
            }
        }
        for (Test test : testSuite.tests) {
            Element testNode = testSuiteRoot.addElement(TESTCASE_TAG).addAttribute(NAME_ATTRIBUTE, test.name);
            if (test.failed()) {
                Element target = testNode;
                if (test.isWarnOnly()) {
                    testNode.addElement(SKIPPED_TAG);
                } else {
                    target = testNode.addElement(FAILED_TAG);
                }
                target.addCDATA(NEWLINE + test.description + NEWLINE + StringUtils.join(test.getMessages(), NEWLINE));
            }
        }
    }

    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(new FileWriter(outputFile), format);
    writer.write(document);
    writer.close();
}
 
Example 12
Source File: PublishUtil.java    From boubei-tss with Apache License 2.0 5 votes vote down vote up
/**
 * 生成单个文章发布文件
 * @param article 
 * @param publishPath
 * @return
 */
public static String publishOneArticle(Article article, String publishPath) {
       // 删除已发布的文章,如果有的话
       String pubUrl = article.getPubUrl();
       if(pubUrl != null) {
           new File(pubUrl).delete();
       }
       
	// 生成发布路径
	File publishDir = new File(publishPath);
	if (!publishDir.exists()) {
		publishDir.mkdirs();
	}
	
	Document doc = DocumentHelper.createDocument();
	doc.setXMLEncoding(ArticleHelper.getSystemEncoding()); //一般:windows “GBK” linux “UTF-8”
	Element articleNode = doc.addElement("Article");
	
       Map<String, Object> articleAttributes = article.getAttributes4XForm(); // 包含文章的所有属性
       articleAttributes.remove("content");
	addValue2XmlNode(articleNode, articleAttributes);
	
	Element contentNode = articleNode.addElement("content");
	contentNode.addCDATA(article.getContent());
       
	// 发布文章对文章附件的处理
	Element eleAtts = articleNode.addElement("Attachments");
       ArticleHelper.addPicListInfo(eleAtts, article.getAttachments());
       
       // 以 “栏目ID_文章ID.xml” 格式命名文章发布的xml文件
       String fileName = article.getChannel().getId() + "_" + article.getId() + ".xml";
	String filePathAndName = publishPath + "/" + fileName;
       FileHelper.writeXMLDoc(doc, filePathAndName);
	return filePathAndName;
}
 
Example 13
Source File: Dom4JXMLOutput.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void writeCDATA(String cdata) {
    Element top = (Element) stack.getLast();
    top.addCDATA(cdata);
}
 
Example 14
Source File: AbstractCondition.java    From cuba with Apache License 2.0 4 votes vote down vote up
public void toXml(Element element, Param.ValueProperty valueProperty) {
    String text = getText();
    if (StringUtils.isNotBlank(text))
        element.addCDATA(text);

    element.addAttribute("name", name);

    if (javaClass != null)
        element.addAttribute("class", javaClass.getName());

    if (caption != null)
        element.addAttribute("caption", caption);

    if (unary)
        element.addAttribute("unary", "true");

    if (inExpr)
        element.addAttribute("inExpr", "true");

    if (hidden)
        element.addAttribute("hidden", "true");

    if (required)
        element.addAttribute("required", "true");

    if (Boolean.TRUE.equals(useUserTimeZone))
        element.addAttribute("useUserTimeZone", "true");

    if (operator != null) {
        element.addAttribute("operatorType", operator.name());
    }

    if (param != null) {
        param.toXml(element, valueProperty);
        if (entityParamWhere != null)
            element.addAttribute("paramWhere", entityParamWhere);
        if (entityParamView != null)
            element.addAttribute("paramView", entityParamView);
    }

    if (width != null) {
        element.addAttribute("width", width.toString());
    }
}
 
Example 15
Source File: XMLProperties.java    From Whack with Apache License 2.0 4 votes vote down vote up
/**
 * Sets a property to an array of values. Multiple values matching the same property
 * is mapped to an XML file as multiple elements containing each value.
 * For example, using the name "foo.bar.prop", and the value string array containing
 * {"some value", "other value", "last value"} would produce the following XML:
 * <pre>
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 *
 * @param name the name of the property.
 * @param values the values for the property (can be empty but not null).
 */
public void setProperties(String name, List<String> values) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }
        element = element.element(propName[i]);
    }
    String childName = propName[propName.length - 1];
    // We found matching property, clear all children.
    List toRemove = new ArrayList();
    Iterator iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add(iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove((Element)iter.next());
    }
    // Add the new children.
    for (String value : values) {
        Element childElement = element.addElement(childName);
        if (value.startsWith("<![CDATA[")) {
            childElement.addCDATA(value.substring(9, value.length()-3));
        }
        else {
            childElement.setText(value);
        }
    }
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", values);
    PropertyEventDispatcher.dispatchEvent(name,
            PropertyEventDispatcher.EventType.xml_property_set, params);
}
 
Example 16
Source File: XMLProperties.java    From Openfire with Apache License 2.0 4 votes vote down vote up
/**
 * Sets a property to an array of values. Multiple values matching the same property
 * is mapped to an XML file as multiple elements containing each value.
 * For example, using the name "foo.bar.prop", and the value string array containing
 * {"some value", "other value", "last value"} would produce the following XML:
 * <pre>
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 *
 * @param name the name of the property.
 * @param values the values for the property (can be empty but not null).
 */
public void setProperties(String name, List<String> values) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML hierarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        // If we don't find this part of the property in the XML hierarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }
        element = element.element(propName[i]);
    }
    String childName = propName[propName.length - 1];
    // We found matching property, clear all children.
    List<Element> toRemove = new ArrayList<>();
    Iterator<Element> iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add(iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove(iter.next());
    }
    // Add the new children.
    for (String value : values) {
        Element childElement = element.addElement(childName);
        if (value.startsWith("<![CDATA[")) {
            Iterator<Node> it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length()-3));
        }
        else {
            String propValue = value;
            // check to see if the property is marked as encrypted
            if (JiveGlobals.isPropertyEncrypted(name)) {
                propValue = JiveGlobals.getPropertyEncryptor().encrypt(value);
                childElement.addAttribute(ENCRYPTED_ATTRIBUTE, "true");
            }
            childElement.setText(propValue);
        }
    }
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<>();
    params.put("value", values);
    PropertyEventDispatcher.dispatchEvent(name,
            PropertyEventDispatcher.EventType.xml_property_set, params);
}
 
Example 17
Source File: XMLProperties.java    From Openfire with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the value of the specified property. If the property doesn't
 * currently exist, it will be automatically created.
 *
 * @param name  the name of the property to set.
 * @param value the new value for the property.
 * @return {@code true} if the property was correctly saved to file, otherwise {@code false}
 */
public synchronized boolean setProperty(String name, String value) {
    if (name == null) {
        return false;
    }
    if (!StringEscapeUtils.escapeXml10(name).equals(name)) {
        throw new IllegalArgumentException("Property name cannot contain XML entities.");
    }
    if (value == null) {
        value = "";
    }

    // Set cache correctly with prop name and value.
    propertyCache.put(name, value);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML hierarchy.
    Element element = document.getRootElement();
    for (String aPropName : propName) {
        // If we don't find this part of the property in the XML hierarchy
        // we add it as a new node
        if (element.element(aPropName) == null) {
            element.addElement(aPropName);
        }
        element = element.element(aPropName);
    }
    // Set the value of the property in this node.
    if (value.startsWith("<![CDATA[")) {
        Iterator it = element.nodeIterator();
        while (it.hasNext()) {
            Node node = (Node) it.next();
            if (node instanceof CDATA) {
                element.remove(node);
                break;
            }
        }
        element.addCDATA(value.substring(9, value.length() - 3));
    } else {
        String propValue = value;
        // check to see if the property is marked as encrypted
        if (JiveGlobals.isXMLPropertyEncrypted(name)) {
            propValue = JiveGlobals.getPropertyEncryptor(true).encrypt(value);
            element.addAttribute(ENCRYPTED_ATTRIBUTE, "true");
        }
        element.setText(propValue);
    }
    // Write the XML properties to disk
    final boolean saved = saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<>();
    params.put("value", value);
    PropertyEventDispatcher.dispatchEvent(name, PropertyEventDispatcher.EventType.xml_property_set, params);
    return saved;
}