Search in sources :

Example 16 with MCRJDOMContent

use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.

the class MCRBibTeX2MODSTransformerTest method testTransformation.

@Test
public void testTransformation() throws IOException, JDOMException, SAXException {
    ClassLoader classLoader = getClass().getClassLoader();
    File baseDir = new File(classLoader.getResource("BibTeX2MODSTransformerTest").getFile());
    int numTests = baseDir.list().length / 2;
    for (int i = 1; i <= numTests; i++) {
        // Read BibTeX file
        File bibTeXFile = new File(baseDir, String.format("test-%s-bibTeX.txt", i));
        MCRFileContent bibTeX = new MCRFileContent(bibTeXFile);
        // Transform BibTeX to MODS
        MCRJDOMContent resultingContent = new MCRBibTeX2MODSTransformer().transform(bibTeX);
        Element resultingMODS = resultingContent.asXML().getRootElement().getChildren().get(0).detach();
        removeSourceExtension(resultingMODS);
        // Read expected MODS
        File modsFile = new File(baseDir, String.format("test-%s-mods.xml", i));
        Element mods = new MCRFileContent(modsFile).asXML().detachRootElement();
        // Compare transformation output with expected MODS
        String expected = new MCRJDOMContent(mods).asString();
        String result = new MCRJDOMContent(resultingMODS).asString();
        String message = "transformation of " + bibTeXFile.getName();
        assertEquals(message, expected, result);
    }
}
Also used : MCRFileContent(org.mycore.common.content.MCRFileContent) Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) File(java.io.File) Test(org.junit.Test)

Example 17 with MCRJDOMContent

use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.

the class MCRCommandLineInterface method getURI.

/**
 * Reads XML content from URIResolver and sends output to a local file.
 */
public static void getURI(String uri, String file) throws Exception {
    Element resolved = MCRURIResolver.instance().resolve(uri);
    Element cloned = resolved.clone();
    new MCRJDOMContent(cloned).sendTo(new File(file));
}
Also used : Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) File(java.io.File)

Example 18 with MCRJDOMContent

use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.

the class MCRBasketServlet method doGetPost.

public void doGetPost(MCRServletJob job) throws Exception {
    HttpServletRequest req = job.getRequest();
    HttpServletResponse res = job.getResponse();
    String type = req.getParameter("type");
    String action = req.getParameter("action");
    String[] uris = req.getParameterValues("uri");
    String[] ids = req.getParameterValues("id");
    String redirect = getProperty(req, "redirect");
    URL referer = getReferer(req);
    boolean resolveContent = "true".equals(req.getParameter("resolve"));
    LOGGER.info("{} {} {}", action, type, ids == null ? "" : ids);
    MCRBasket basket = MCRBasketManager.getOrCreateBasketInSession(type);
    if ("add".equals(action)) {
        if (uris.length != ids.length) {
            throw new MCRException("Amount of URIs must match amount of IDs");
        }
        for (int i = 0; i < uris.length; i++) {
            MCRBasketEntry entry = new MCRBasketEntry(ids[i], uris[i]);
            basket.add(entry);
            if (resolveContent) {
                entry.resolveContent();
            }
        }
    } else if ("remove".equals(action)) {
        for (String id : ids) {
            basket.removeEntry(id);
        }
    } else if ("up".equals(action)) {
        for (String id : ids) {
            basket.up(basket.get(id));
        }
    } else if ("down".equals(action)) {
        for (String id : ids) {
            basket.down(basket.get(id));
        }
    } else if ("clear".equals(action)) {
        basket.clear();
    } else if ("create".equals(action)) {
        String ownerID = req.getParameter("ownerID");
        MCRObjectID ownerOID = MCRObjectID.getInstance(ownerID);
        MCRBasketPersistence.createDerivateWithBasket(basket, ownerOID);
    } else if ("update".equals(action)) {
        MCRBasketPersistence.updateBasket(basket);
    } else if ("retrieve".equals(action)) {
        String derivateID = req.getParameter("derivateID");
        basket = MCRBasketPersistence.retrieveBasket(derivateID);
        type = basket.getType();
        MCRBasketManager.setBasketInSession(basket);
    } else if ("comment".equals(action)) {
        Document sub = (Document) (job.getRequest().getAttribute("MCRXEditorSubmission"));
        String comment = sub.getRootElement().getChildTextTrim("comment");
        for (String id : ids) {
            basket.get(id).setComment(comment);
        }
    } else if ("show".equals(action)) {
        req.setAttribute("XSL.Style", type);
        Document xml = new MCRBasketXMLBuilder(true).buildXML(basket);
        getLayoutService().doLayout(req, res, new MCRJDOMContent(xml));
        return;
    }
    if (referer != null && "referer".equals(redirect)) {
        res.sendRedirect(res.encodeRedirectURL(referer.toExternalForm()));
    } else if (redirect != null) {
        res.sendRedirect(res.encodeRedirectURL(redirect));
    } else {
        res.sendRedirect(res.encodeRedirectURL(getServletBaseURL() + "MCRBasketServlet?action=show&type=" + type));
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) HttpServletResponse(javax.servlet.http.HttpServletResponse) Document(org.jdom2.Document) URL(java.net.URL) HttpServletRequest(javax.servlet.http.HttpServletRequest) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 19 with MCRJDOMContent

use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.

the class MCRJerseyUtil method transform.

/**
 * Transforms a jdom document to a <code>MCRContent</code> via the <code>MCRLayoutService</code>.
 *
 * @param document
 *            the document to transform
 * @param request
 *            the http request
 */
public static MCRContent transform(Document document, HttpServletRequest request) throws Exception {
    MCRParameterCollector parameter = new MCRParameterCollector(request);
    MCRContent result;
    MCRJDOMContent source = new MCRJDOMContent(document);
    MCRContentTransformer transformer = MCRLayoutService.getContentTransformer(source.getDocType(), parameter);
    if (transformer instanceof MCRParameterizedTransformer) {
        result = ((MCRParameterizedTransformer) transformer).transform(source, parameter);
    } else {
        result = transformer.transform(source);
    }
    return result;
}
Also used : MCRParameterCollector(org.mycore.common.xsl.MCRParameterCollector) MCRParameterizedTransformer(org.mycore.common.content.transformer.MCRParameterizedTransformer) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer) MCRContent(org.mycore.common.content.MCRContent)

Example 20 with MCRJDOMContent

use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.

the class MCRGoogleSitemapCommands method buildSitemap.

/**
 * The build and store method.
 */
@MCRCommand(syntax = "build google sitemap", help = "Creates the google sitemap(s) in webapps directory.", order = 10)
public static void buildSitemap() throws Exception {
    // check time
    LOGGER.debug("Build Google sitemap start.");
    final long start = System.currentTimeMillis();
    // init
    File webappBaseDir = new File(MCRConfiguration.instance().getString("MCR.WebApplication.basedir"));
    MCRGoogleSitemapCommon common = new MCRGoogleSitemapCommon(webappBaseDir);
    // remove old files
    common.removeSitemapFiles();
    // compute number of files
    int number = common.checkSitemapFile();
    LOGGER.debug("Build Google number of URL files {}.", Integer.toString(number));
    if (number == 1) {
        String fn = common.getFileName(1, true);
        File xml = new File(fn);
        Document jdom = common.buildSingleSitemap();
        LOGGER.info("Write Google sitemap file {}.", fn);
        new MCRJDOMContent(jdom).sendTo(xml);
    } else {
        String fn = common.getFileName(1, true);
        File xml = new File(fn);
        Document jdom = common.buildSitemapIndex(number);
        LOGGER.info("Write Google sitemap file {}.", fn);
        new MCRJDOMContent(jdom).sendTo(xml);
        for (int i = 0; i < number; i++) {
            fn = common.getFileName(i + 2, true);
            xml = new File(fn);
            jdom = common.buildPartSitemap(i);
            LOGGER.info("Write Google sitemap file {}.", fn);
            new MCRJDOMContent(jdom).sendTo(xml);
        }
    }
    // check time
    LOGGER.debug("Google sitemap request took {}ms.", System.currentTimeMillis() - start);
}
Also used : MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) Document(org.jdom2.Document) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)54 Document (org.jdom2.Document)33 Element (org.jdom2.Element)28 Test (org.junit.Test)21 MCRContent (org.mycore.common.content.MCRContent)20 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)6 IOException (java.io.IOException)5 File (java.io.File)4 MCRParameterCollector (org.mycore.common.xsl.MCRParameterCollector)4 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)4 MCRObject (org.mycore.datamodel.metadata.MCRObject)4 Date (java.util.Date)3 JDOMException (org.jdom2.JDOMException)3 MCRContentTransformer (org.mycore.common.content.transformer.MCRContentTransformer)3 MCRPath (org.mycore.datamodel.niofs.MCRPath)3 FileObject (org.apache.commons.vfs2.FileObject)2 XMLOutputter (org.jdom2.output.XMLOutputter)2 MCRException (org.mycore.common.MCRException)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRFileContent (org.mycore.common.content.MCRFileContent)2