Search in sources :

Example 81 with Text

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;
}
Also used : Element(org.jdom2.Element) MCRISO8601Date(org.mycore.datamodel.common.MCRISO8601Date) Date(java.util.Date)

Example 82 with Text

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());
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) Test(org.junit.Test)

Example 83 with Text

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());
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) JsonObject(com.google.gson.JsonObject) Document(org.jdom2.Document) URL(java.net.URL) Test(org.junit.Test)

Example 84 with Text

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();
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) StringWriter(java.io.StringWriter) Content(org.jdom2.Content) JsonElement(com.google.gson.JsonElement) Element(org.jdom2.Element) Text(org.jdom2.Text)

Example 85 with Text

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);
}
Also used : Element(org.jdom2.Element) Text(org.jdom2.Text) Test(org.junit.Test)

Aggregations

Element (org.jdom2.Element)83 Document (org.jdom2.Document)36 File (java.io.File)21 ProcessingInstruction (org.jdom2.ProcessingInstruction)17 IOException (java.io.IOException)14 Text (org.jdom2.Text)12 Attribute (org.jdom2.Attribute)11 XMLOutputter (org.jdom2.output.XMLOutputter)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)9 XmlFile (jmri.jmrit.XmlFile)9 ArrayList (java.util.ArrayList)6 JDOMException (org.jdom2.JDOMException)6 StringWriter (java.io.StringWriter)5 NamedIcon (jmri.jmrit.catalog.NamedIcon)5 FileOutputStream (java.io.FileOutputStream)4 LinkedHashMap (java.util.LinkedHashMap)4 FileNotFoundException (java.io.FileNotFoundException)3 URL (java.net.URL)3 Locale (java.util.Locale)3