Search in sources :

Example 96 with Text

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);
}
Also used : XPathFactory(org.jdom2.xpath.XPathFactory) Text(org.jdom2.Text) Document(org.jdom2.Document) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier)

Example 97 with Text

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

Example 98 with Text

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();
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) StringWriter(java.io.StringWriter) Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 99 with Text

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();
}
Also used : Locale(java.util.Locale) XMLOutputter(org.jdom2.output.XMLOutputter) StringWriter(java.io.StringWriter) Element(org.jdom2.Element) IOException(java.io.IOException) Document(org.jdom2.Document) JsonWriter(com.google.gson.stream.JsonWriter) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

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