Search in sources :

Example 36 with Text

use of org.jdom2.Text in project mycore by MyCoRe-Org.

the class MyCoReWebPageProviderTest method addSection.

@Test
public void addSection() throws Exception {
    MyCoReWebPageProvider wp = new MyCoReWebPageProvider();
    // simple test
    Element content = new Element("content");
    content.setText("sample text");
    Element section = wp.addSection("title1", content, "de");
    assertNotNull(section);
    assertEquals("title1", section.getAttributeValue("title"));
    assertEquals("de", section.getAttributeValue("lang", Namespace.XML_NAMESPACE));
    assertEquals("sample text", section.getChild("content").getText());
    // xml text surrounded by p-tag
    Element section2 = wp.addSection("title2", "<p>text test</p>", "en");
    assertEquals("title2", section2.getAttributeValue("title"));
    assertEquals("en", section2.getAttributeValue("lang", Namespace.XML_NAMESPACE));
    assertEquals("text test", section2.getChild("p").getText());
    // only text
    Element section3 = wp.addSection("title3", "simple text", "uk");
    assertEquals("simple text", section3.getText());
    // multi tags
    Element section4 = wp.addSection("title4", "<b>bold</b> <i>italic</i>", "at");
    assertEquals(2, section4.getChildren().size());
    // check section count
    assertEquals(4, wp.getXML().getRootElement().getContentSize());
    // entities
    wp.addSection("title5", "&nbsp;&amp;&auml;Hallo", "de");
    // custom tags
    wp.addSection("title6", "<p><printlatestobjects /></p>", "de");
}
Also used : Element(org.jdom2.Element) Test(org.junit.Test)

Example 37 with Text

use of org.jdom2.Text in project mycore by MyCoRe-Org.

the class MCROAIDataProvider method doGetPost.

@Override
protected void doGetPost(MCRServletJob job) throws Exception {
    HttpServletRequest request = job.getRequest();
    // get base url
    if (this.myBaseURL == null) {
        this.myBaseURL = MCRFrontendUtil.getBaseURL() + request.getServletPath().substring(1);
    }
    logRequest(request);
    // create new oai request
    OAIRequest oaiRequest = new OAIRequest(fixParameterMap(request.getParameterMap()));
    // create new oai provider
    OAIXMLProvider oaiProvider = new JAXBOAIProvider(getOAIAdapter());
    // handle request
    OAIResponse oaiResponse = oaiProvider.handleRequest(oaiRequest);
    // build response
    Element xmlRespone = oaiResponse.toXML();
    // fire
    job.getResponse().setContentType("text/xml; charset=UTF-8");
    XMLOutputter xout = new XMLOutputter();
    xout.setFormat(Format.getPrettyFormat().setEncoding("UTF-8"));
    xout.output(addXSLStyle(new Document(xmlRespone)), job.getResponse().getOutputStream());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) OAIXMLProvider(org.mycore.oai.pmh.dataprovider.OAIXMLProvider) XMLOutputter(org.jdom2.output.XMLOutputter) OAIResponse(org.mycore.oai.pmh.dataprovider.OAIResponse) Element(org.jdom2.Element) JAXBOAIProvider(org.mycore.oai.pmh.dataprovider.jaxb.JAXBOAIProvider) Document(org.jdom2.Document) OAIRequest(org.mycore.oai.pmh.dataprovider.OAIRequest)

Example 38 with Text

use of org.jdom2.Text in project mycore by MyCoRe-Org.

the class MCRRestAPIClassifications method writeJSON.

/**
 * Output JSON
 * @param eRoot - the category element
 * @param lang - the language to be filtered for or null if all languages should be displayed
 * @param style - the style
 * @return a string representation of a JSON object
 * @throws IOException
 */
private String writeJSON(Element eRoot, String lang, String style) throws IOException {
    StringWriter sw = new StringWriter();
    JsonWriter writer = new JsonWriter(sw);
    writer.setIndent("  ");
    if (style.contains("checkboxtree")) {
        if (lang == null) {
            lang = "de";
        }
        writer.beginObject();
        writer.name("identifier").value(eRoot.getAttributeValue("ID"));
        for (Element eLabel : eRoot.getChildren("label")) {
            if (lang.equals(eLabel.getAttributeValue("lang", Namespace.XML_NAMESPACE))) {
                writer.name("label").value(eLabel.getAttributeValue("text"));
            }
        }
        writer.name("items");
        writeChildrenAsJSONCBTree(eRoot = eRoot.getChild("categories"), writer, lang, style.contains("checked"));
        writer.endObject();
    } else if (style.contains("jstree")) {
        if (lang == null) {
            lang = "de";
        }
        writeChildrenAsJSONJSTree(eRoot = eRoot.getChild("categories"), writer, lang, style.contains("opened"), style.contains("disabled"), style.contains("selected"));
    } else {
        // {
        writer.beginObject();
        writer.name("ID").value(eRoot.getAttributeValue("ID"));
        writer.name("label");
        writer.beginArray();
        for (Element eLabel : eRoot.getChildren("label")) {
            if (lang == null || lang.equals(eLabel.getAttributeValue("lang", Namespace.XML_NAMESPACE))) {
                writer.beginObject();
                writer.name("lang").value(eLabel.getAttributeValue("lang", Namespace.XML_NAMESPACE));
                writer.name("text").value(eLabel.getAttributeValue("text"));
                if (eLabel.getAttributeValue("description") != null) {
                    writer.name("description").value(eLabel.getAttributeValue("description"));
                }
                writer.endObject();
            }
        }
        writer.endArray();
        if (eRoot.equals(eRoot.getDocument().getRootElement())) {
            writeChildrenAsJSON(eRoot.getChild("categories"), writer, lang);
        } else {
            writeChildrenAsJSON(eRoot, writer, lang);
        }
        writer.endObject();
    }
    writer.close();
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) Element(org.jdom2.Element) JsonWriter(com.google.gson.stream.JsonWriter)

Example 39 with Text

use of org.jdom2.Text in project mycore by MyCoRe-Org.

the class MCRRestAPIClassifications method writeChildrenAsJSON.

/**
 * output categories in JSON format
 * @param eParent - the parent xml element
 * @param writer - the JSON writer
 * @param lang - the language to be filtered or null if all languages should be displayed
 *
 * @throws IOException
 */
private static void writeChildrenAsJSON(Element eParent, JsonWriter writer, String lang) throws IOException {
    if (eParent.getChildren("category").size() == 0)
        return;
    writer.name("categories");
    writer.beginArray();
    for (Element e : eParent.getChildren("category")) {
        writer.beginObject();
        writer.name("ID").value(e.getAttributeValue("ID"));
        writer.name("labels").beginArray();
        for (Element eLabel : e.getChildren("label")) {
            if (lang == null || lang.equals(eLabel.getAttributeValue("lang", Namespace.XML_NAMESPACE))) {
                writer.beginObject();
                writer.name("lang").value(eLabel.getAttributeValue("lang", Namespace.XML_NAMESPACE));
                writer.name("text").value(eLabel.getAttributeValue("text"));
                if (eLabel.getAttributeValue("description") != null) {
                    writer.name("description").value(eLabel.getAttributeValue("description"));
                }
                writer.endObject();
            }
        }
        writer.endArray();
        if (e.getChildren("category").size() > 0) {
            writeChildrenAsJSON(e, writer, lang);
        }
        writer.endObject();
    }
    writer.endArray();
}
Also used : Element(org.jdom2.Element)

Example 40 with Text

use of org.jdom2.Text in project mycore by MyCoRe-Org.

the class MCRRestAPIClassifications method writeChildrenAsJSONJSTree.

/**
 * output children in JSON format used as input for a jsTree
 *
 * @param eParent - the parent xml element
 * @param writer - the JSON writer
 * @param lang - the language to be filtered or null if all languages should be displayed
 * @param opened - true, if all leaf nodes should be displayed
 * @param disabled - true, if all nodes should be disabled
 * @param selected - true, if all node should be selected
 *
 * @throws IOException
 */
private static void writeChildrenAsJSONJSTree(Element eParent, JsonWriter writer, String lang, boolean opened, boolean disabled, boolean selected) throws IOException {
    writer.beginArray();
    for (Element e : eParent.getChildren("category")) {
        writer.beginObject();
        writer.name("id").value(e.getAttributeValue("ID"));
        for (Element eLabel : e.getChildren("label")) {
            if (lang == null || lang.equals(eLabel.getAttributeValue("lang", Namespace.XML_NAMESPACE))) {
                writer.name("text").value(eLabel.getAttributeValue("text"));
            }
        }
        if (opened || disabled || selected) {
            writer.name("state");
            writer.beginObject();
            if (opened) {
                writer.name("opened").value(true);
            }
            if (disabled) {
                writer.name("disabled").value(true);
            }
            if (selected) {
                writer.name("selected").value(true);
            }
            writer.endObject();
        }
        if (e.getChildren("category").size() > 0) {
            writer.name("children");
            writeChildrenAsJSONJSTree(e, writer, lang, opened, disabled, selected);
        }
        writer.endObject();
    }
    writer.endArray();
}
Also used : Element(org.jdom2.Element)

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