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);
}
}
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));
}
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));
}
}
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;
}
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);
}
Aggregations