Search in sources :

Example 11 with Text

use of com.adobe.cq.wcm.core.components.it.seljup.util.components.text.v2.Text in project mycore by MyCoRe-Org.

the class MCRMetaXMLTest method xmlRoundrip.

@Test
public void xmlRoundrip() throws IOException {
    MCRMetaXML mXml = new MCRMetaXML("def.heading", "complete", 0);
    Element imported = new Element("heading");
    imported.setAttribute("lang", MCRMetaDefault.DEFAULT_LANGUAGE, Namespace.XML_NAMESPACE);
    imported.setAttribute("inherited", "0");
    imported.setAttribute("type", "complete");
    imported.addContent(new Text("This is a "));
    imported.addContent(new Element("span").setText("JUnit"));
    imported.addContent(new Text("test"));
    mXml.setFromDOM(imported);
    Element exported = mXml.createXML();
    if (LOGGER.isDebugEnabled()) {
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        StringWriter sw = new StringWriter();
        StringWriter sw2 = new StringWriter();
        try {
            xout.output(imported, sw);
            LOGGER.info(sw.toString());
            xout.output(exported, sw2);
            LOGGER.info(sw2.toString());
        } catch (IOException e) {
            LOGGER.warn("Failure printing xml result", e);
        }
    }
    try {
        assertTrue(MCRXMLHelper.deepEqual(new Document(imported), new Document(exported)));
    } catch (AssertionError e) {
        XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
        out.output(imported, System.err);
        out.output(exported, System.err);
        throw e;
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) StringWriter(java.io.StringWriter) Element(org.jdom2.Element) Text(org.jdom2.Text) IOException(java.io.IOException) Document(org.jdom2.Document) Test(org.junit.Test)

Example 12 with Text

use of com.adobe.cq.wcm.core.components.it.seljup.util.components.text.v2.Text in project mycore by MyCoRe-Org.

the class MCRXSL2XMLTransformer method getDocument.

private Document getDocument(JDOMResult result) {
    Document resultDoc = result.getDocument();
    if (resultDoc == null) {
        // Sometimes a transformation produces whitespace strings
        // JDOM would produce a empty document if it detects those
        // So we remove them, if they exists.
        List<Content> transformResult = result.getResult();
        int origSize = transformResult.size();
        Iterator<Content> iterator = transformResult.iterator();
        while (iterator.hasNext()) {
            Content content = iterator.next();
            if (content instanceof Text) {
                String trimmedText = ((Text) content).getTextTrim();
                if (trimmedText.length() == 0) {
                    iterator.remove();
                }
            }
        }
        if (transformResult.size() < origSize) {
            JDOMFactory f = result.getFactory();
            if (f == null) {
                f = new DefaultJDOMFactory();
            }
            resultDoc = f.document(null);
            resultDoc.setContent(transformResult);
        }
    }
    return resultDoc;
}
Also used : DefaultJDOMFactory(org.jdom2.DefaultJDOMFactory) MCRContent(org.mycore.common.content.MCRContent) Content(org.jdom2.Content) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) JDOMFactory(org.jdom2.JDOMFactory) DefaultJDOMFactory(org.jdom2.DefaultJDOMFactory) Text(org.jdom2.Text) Document(org.jdom2.Document)

Example 13 with Text

use of com.adobe.cq.wcm.core.components.it.seljup.util.components.text.v2.Text in project mycore by MyCoRe-Org.

the class MCRTransferPackageUtil method getMCRObjects.

/**
 * Gets the list of mycore object identifiers from the given directory.
 *
 * @param targetDirectory directory where the *.tar was unpacked
 * @return list of object which lies within the directory
 */
public static List<String> getMCRObjects(Path targetDirectory) throws JDOMException, IOException {
    Path order = targetDirectory.resolve(MCRTransferPackage.IMPORT_CONFIG_FILENAME);
    Document xml = new SAXBuilder().build(order.toFile());
    Element config = xml.getRootElement();
    XPathExpression<Text> exp = MCRConstants.XPATH_FACTORY.compile("order/object/text()", Filters.text());
    return exp.evaluate(config).stream().map(Text::getText).collect(Collectors.toList());
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) Text(org.jdom2.Text) Document(org.jdom2.Document)

Example 14 with Text

use of com.adobe.cq.wcm.core.components.it.seljup.util.components.text.v2.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)

Aggregations

Text (org.jdom2.Text)11 Element (org.jdom2.Element)7 Document (org.jdom2.Document)4 Content (org.jdom2.Content)3 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 XMLOutputter (org.jdom2.output.XMLOutputter)2 Test (org.junit.Test)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 BaseText (com.adobe.cq.wcm.core.components.it.seljup.util.components.text.BaseText)1 Text (com.adobe.cq.wcm.core.components.it.seljup.util.components.text.v1.Text)1 Text (com.adobe.cq.wcm.core.components.it.seljup.util.components.text.v2.Text)1 JsonElement (com.google.gson.JsonElement)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 PluginContent (org.apache.wiki.parser.PluginContent)1