use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRObjectFactory method createServiceElement.
/**
*/
private static Element createServiceElement() {
Element service = new Element("service");
Element servDates = new Element("servdates");
service.addContent(servDates);
Element createDate = new Element("servdate");
createDate.setAttribute("type", "createdate");
createDate.setAttribute("inherited", "0");
Element modifyDate = new Element("servdate");
modifyDate.setAttribute("type", "modifydate");
modifyDate.setAttribute("inherited", "0");
servDates.addContent(createDate);
servDates.addContent(modifyDate);
String text = getDateString(new Date());
createDate.setText(text);
modifyDate.setText(text);
return service;
}
use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRNodeBuilderTest method testBuildingValues.
@Test
public void testBuildingValues() throws JaxenException, JDOMException {
Attribute built = new MCRNodeBuilder().buildAttribute("@attribute='A \"test\"'", "ignore", null);
assertNotNull(built);
assertEquals("attribute", built.getName());
assertEquals("A \"test\"", built.getValue());
built = new MCRNodeBuilder().buildAttribute("@attribute=\"O'Brian\"", null, null);
assertNotNull(built);
assertEquals("attribute", built.getName());
assertEquals("O'Brian", built.getValue());
built = new MCRNodeBuilder().buildAttribute("@mime=\"text/plain\"", null, null);
assertNotNull(built);
assertEquals("mime", built.getName());
assertEquals("text/plain", built.getValue());
Element element = new MCRNodeBuilder().buildElement("name=\"O'Brian\"", null, null);
assertNotNull(element);
assertEquals("name", element.getName());
assertEquals("O'Brian", element.getText());
}
use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRXMLHelperTest method jsonSerialize.
@Test
public void jsonSerialize() throws Exception {
// simple text
Element e = new Element("hallo").setText("Hallo Welt");
JsonObject json = MCRXMLHelper.jsonSerialize(e);
assertEquals("Hallo Welt", json.getAsJsonPrimitive("$text").getAsString());
// attribute
e = new Element("hallo").setAttribute("hallo", "welt");
json = MCRXMLHelper.jsonSerialize(e);
assertEquals("welt", json.getAsJsonPrimitive("_hallo").getAsString());
// complex world class test
URL world = MCRXMLHelperTest.class.getResource("/worldclass.xml");
SAXBuilder builder = new SAXBuilder();
Document worldDocument = builder.build(world.openStream());
json = MCRXMLHelper.jsonSerialize(worldDocument.getRootElement());
assertNotNull(json);
assertEquals("World", json.getAsJsonPrimitive("_ID").getAsString());
assertEquals("http://www.w3.org/2001/XMLSchema-instance", json.getAsJsonPrimitive("_xmlns:xsi").getAsString());
JsonObject deLabel = json.getAsJsonArray("label").get(0).getAsJsonObject();
assertEquals("de", deLabel.getAsJsonPrimitive("_xml:lang").getAsString());
assertEquals("Staaten", deLabel.getAsJsonPrimitive("_text").getAsString());
assertEquals(2, json.getAsJsonObject("categories").getAsJsonArray("category").size());
}
use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRWCMSDefaultSectionProvider method getContent.
/**
* Returns the content of an element as string. The element itself
* is ignored.
*
* @param e the element to get the content from
* @return the content as string
*/
protected String getContent(Element e) throws IOException {
XMLOutputter out = new XMLOutputter();
StringWriter writer = new StringWriter();
for (Content child : e.getContent()) {
if (child instanceof Element) {
out.output((Element) child, writer);
} else if (child instanceof Text) {
Text t = (Text) child;
String trimmedText = t.getTextTrim();
if (!trimmedText.equals("")) {
Text newText = new Text(trimmedText);
out.output(newText, writer);
}
}
}
return writer.toString();
}
use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRMetaNumberTest method xmlRoundrip.
@Test
public void xmlRoundrip() throws IOException {
// test 0.100
MCRMetaNumber meta_number = new MCRMetaNumber();
Element imported = new Element("number");
imported.setAttribute("inherited", "0");
imported.setAttribute("dimension", "width");
imported.setAttribute("measurement", "cm");
imported.addContent(new Text("0.100"));
meta_number.setFromDOM(imported);
Element exported = meta_number.createXML();
print_data(imported, exported);
check_data(imported, exported);
}
Aggregations