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", " &äHallo", "de");
// custom tags
wp.addSection("title6", "<p><printlatestobjects /></p>", "de");
}
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());
}
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();
}
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();
}
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();
}
Aggregations