use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRURNObjectXPathMetadataManager method getIdentifier.
@Override
public Optional<MCRPersistentIdentifier> getIdentifier(MCRBase obj, String additional) throws MCRPersistentIdentifierException {
String xpath = getProperties().get("Xpath");
Document xml = obj.createXML();
XPathFactory xpfac = XPathFactory.instance();
XPathExpression<Text> xp = xpfac.compile(xpath, Filters.text());
List<Text> evaluate = xp.evaluate(xml);
if (evaluate.size() > 1) {
throw new MCRPersistentIdentifierException("Got " + evaluate.size() + " matches for " + obj.getId() + " with xpath " + xpath + "");
}
if (evaluate.size() == 0) {
return Optional.empty();
}
Text identifierText = evaluate.listIterator().next();
String identifierString = identifierText.getTextNormalize();
Optional<MCRDNBURN> parsedIdentifierOptional = PARSER.parse(identifierString);
return parsedIdentifierOptional.map(MCRPersistentIdentifier.class::cast);
}
use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRRestAPIClassifications method writeChildrenAsJSONCBTree.
/**
* output children in JSON format used as input for Dijit Checkbox Tree
*
* @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 writeChildrenAsJSONCBTree(Element eParent, JsonWriter writer, String lang, boolean checked) 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"));
}
}
writer.name("checked").value(checked);
if (e.getChildren("category").size() > 0) {
writer.name("children");
writeChildrenAsJSONCBTree(e, writer, lang, checked);
}
writer.endObject();
}
writer.endArray();
}
use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRRestAPIClassifications method writeXML.
/**
* Output xml
* @param eRoot - the root element
* @param lang - the language which should be filtered or null for no filter
* @return a string representation of the XML
* @throws IOException
*/
private static String writeXML(Element eRoot, String lang) throws IOException {
StringWriter sw = new StringWriter();
if (lang != null) {
// <label xml:lang="en" text="part" />
XPathExpression<Element> xpE = XPathFactory.instance().compile("//label[@xml:lang!='" + lang + "']", Filters.element(), null, Namespace.XML_NAMESPACE);
for (Element e : xpE.evaluate(eRoot)) {
e.getParentElement().removeContent(e);
}
}
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
Document docOut = new Document(eRoot.detach());
xout.output(docOut, sw);
return sw.toString();
}
use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRRestAPIMessages method getMessage.
/**
* returns a single messages entry.
*
* @param info - the injected Jersey context object for URI
* @param request - the injected HTTPServletRequest object
* @param key - the message key
* @param lang - the language
* @param format
* Possible values are: props (default) | json | xml (required)
* @return a Jersey Response Object
* @throws MCRRestAPIException
*/
@GET
@Path("/{value}")
@Produces({ MediaType.TEXT_XML + ";charset=UTF-8", MediaType.APPLICATION_JSON + ";charset=UTF-8", MediaType.TEXT_PLAIN + ";charset=UTF-8" })
public Response getMessage(@Context UriInfo info, @Context HttpServletRequest request, @PathParam("value") String key, @QueryParam("lang") @DefaultValue("de") String lang, @QueryParam("format") @DefaultValue("text") String format) throws MCRRestAPIException {
MCRRestAPIUtil.checkRestAPIAccess(request, MCRRestAPIACLPermission.READ, "/v1/messages");
Locale locale = Locale.forLanguageTag(lang);
String result = MCRTranslation.translate(key, locale);
String authHeader = MCRJSONWebTokenUtil.createJWTAuthorizationHeader(MCRJSONWebTokenUtil.retrieveAuthenticationToken(request));
try {
if (FORMAT_PROPERTY.equals(format)) {
return Response.ok(key + "=" + result).type("text/plain; charset=ISO-8859-1").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
}
if (FORMAT_XML.equals(format)) {
Document doc = new Document();
Element root = new Element("entry");
root.setAttribute("key", key);
root.setText(result);
doc.addContent(root);
StringWriter sw = new StringWriter();
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(doc, sw);
return Response.ok(sw.toString()).type("application/xml; charset=UTF-8").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
}
if (FORMAT_JSON.equals(format)) {
StringWriter sw = new StringWriter();
JsonWriter writer = new JsonWriter(sw);
writer.setIndent(" ");
writer.beginObject();
writer.name(key);
writer.value(result);
writer.endObject();
writer.close();
return Response.ok(sw.toString()).type("application/json; charset=UTF-8").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
}
// text only
return Response.ok(result).type("text/plain; charset=UTF-8").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
} catch (IOException e) {
// toDo
}
return Response.status(Status.BAD_REQUEST).build();
}
Aggregations