Search in sources :

Example 46 with MCRContent

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

the class MCRMetadataStoreTest method createDocument.

@Test
public void createDocument() throws Exception {
    Document xml1 = new Document(new Element("root"));
    MCRStoredMetadata sm = getMetaDataStore().create(new MCRJDOMContent(xml1));
    assertNotNull(sm);
    int id1 = sm.getID();
    assertTrue(id1 > 0);
    MCRStoredMetadata sm2 = getMetaDataStore().retrieve(id1);
    MCRContent xml2 = sm2.getMetadata();
    assertEquals(new MCRJDOMContent(xml1).asString(), xml2.asString());
    int id2 = getMetaDataStore().create(new MCRJDOMContent(xml1)).getID();
    assertTrue(id2 > id1);
}
Also used : Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) Test(org.junit.Test)

Example 47 with MCRContent

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

the class MCRVersioningMetadataStoreTest method update.

@Test
public void update() throws Exception {
    Document xml1 = new Document(new Element("root"));
    MCRVersionedMetadata vm = getVersStore().create(new MCRJDOMContent(xml1));
    Document xml3 = new Document(new Element("update"));
    long rev = vm.getRevision();
    vm.update(new MCRJDOMContent(xml3));
    assertTrue(vm.getRevision() > rev);
    MCRContent xml4 = getVersStore().retrieve(vm.getID()).getMetadata();
    assertEquals(new MCRJDOMContent(xml3).asString(), xml4.asString());
}
Also used : Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) Test(org.junit.Test)

Example 48 with MCRContent

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

the class MCRLayoutServiceTarget method handleSubmission.

@Override
public void handleSubmission(ServletContext context, MCRServletJob job, MCREditorSession session, String style) throws Exception {
    session.getSubmission().setSubmittedValues(job.getRequest().getParameterMap());
    Document result = session.getEditedXML();
    if (session.getValidator().isValid()) {
        result = MCRChangeTracker.removeChangeTracking(result);
        result = session.getXMLCleaner().clean(result);
        result = session.getPostProcessor().process(result);
        if ((style != null) && (!style.isEmpty()))
            job.getRequest().setAttribute("XSL.Style", style);
        MCRContent editedXML = new MCRJDOMContent(result);
        MCRLayoutService.instance().doLayout(job.getRequest(), job.getResponse(), editedXML);
        session.setBreakpoint("After handling target layout " + style);
    } else {
        session.setBreakpoint("After validation failed, target layout " + style);
        job.getResponse().sendRedirect(job.getResponse().encodeRedirectURL(session.getRedirectURL(null)));
    }
}
Also used : MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent)

Example 49 with MCRContent

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

the class MCRViewerResource method show.

@GET
@Path("{derivate}{path: (/[^?#]*)?}")
public Response show(@Context HttpServletRequest request, @Context Request jaxReq, @Context ServletContext context, @Context ServletConfig config) throws Exception {
    MCRContent content = getContent(request);
    String contentETag = content.getETag();
    Response.ResponseBuilder responseBuilder = null;
    EntityTag eTag = contentETag == null ? null : new EntityTag(contentETag);
    if (eTag != null) {
        responseBuilder = jaxReq.evaluatePreconditions(eTag);
    }
    if (responseBuilder == null) {
        responseBuilder = Response.ok(content.asByteArray(), MediaType.valueOf(content.getMimeType()));
    }
    if (eTag != null) {
        responseBuilder.tag(eTag);
    }
    if (content.isUsingSession()) {
        CacheControl cc = new CacheControl();
        cc.setPrivate(true);
        cc.setMaxAge(0);
        cc.setMustRevalidate(true);
        responseBuilder.cacheControl(cc);
    }
    return responseBuilder.build();
}
Also used : Response(javax.ws.rs.core.Response) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) MCRContent(org.mycore.common.content.MCRContent) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 50 with MCRContent

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

the class MCRDerivateCommands method transformXMLMatchingPatternWithStylesheet.

@MCRCommand(syntax = "transform xml matching file name pattern {0} in derivate {1} with stylesheet {2}", help = "Finds all files in Derivate {1} which match the pattern {0} (the complete path with regex: or glob:*.xml syntax) and transforms them with stylesheet {2}")
public static void transformXMLMatchingPatternWithStylesheet(String pattern, String derivate, String stylesheet) throws IOException {
    MCRXSLTransformer transformer = new MCRXSLTransformer(stylesheet);
    MCRPath derivateRoot = MCRPath.getPath(derivate, "/");
    PathMatcher matcher = derivateRoot.getFileSystem().getPathMatcher(pattern);
    Files.walkFileTree(derivateRoot, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (matcher.matches(file)) {
                LOGGER.info("The file {} matches the pattern {}", file, pattern);
                MCRContent sourceContent = new MCRPathContent(file);
                MCRContent resultContent = transformer.transform(sourceContent);
                try {
                    Document source = sourceContent.asXML();
                    Document result = resultContent.asXML();
                    LOGGER.info("Transforming complete!");
                    if (!MCRXMLHelper.deepEqual(source, result)) {
                        LOGGER.info("Writing result..");
                        resultContent.sendTo(file, StandardCopyOption.REPLACE_EXISTING);
                    } else {
                        LOGGER.info("Result and Source is the same..");
                    }
                } catch (JDOMException | SAXException e) {
                    throw new IOException("Error while processing file : " + file, e);
                }
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) PathMatcher(java.nio.file.PathMatcher) MCRPathContent(org.mycore.common.content.MCRPathContent) MCRXSLTransformer(org.mycore.common.content.transformer.MCRXSLTransformer) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRContent (org.mycore.common.content.MCRContent)63 Document (org.jdom2.Document)21 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)20 IOException (java.io.IOException)16 Element (org.jdom2.Element)13 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)11 MCRPath (org.mycore.datamodel.niofs.MCRPath)10 Test (org.junit.Test)8 MCRPathContent (org.mycore.common.content.MCRPathContent)7 MCRParameterCollector (org.mycore.common.xsl.MCRParameterCollector)6 File (java.io.File)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 JDOMException (org.jdom2.JDOMException)5 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 MCRException (org.mycore.common.MCRException)4 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)4 URL (java.net.URL)3 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)3