Search in sources :

Example 16 with MCRContent

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

the class MCRObjectCommands method exportMCRObject.

/**
 * The method read a MCRObject and use the transformer to write the data to a file. They are any steps to handel
 * errors and save the damaged data.
 * <ul>
 * <li>Read data for object ID in the MCRObject, add ACL's and store it as checked and transformed XML. Return true.
 * </li>
 * <li>If it can't find a transformer instance (no script file found) it store the checked data with ACL's native in
 * the file. Warning and return true.</li>
 * <li>If it get an exception while build the MCRObject, it try to read the XML blob and stor it without check and
 * ACL's to the file. Warning and return true.</li>
 * <li>If it get an exception while store the native data without check, ACĂ–'s and transformation it return a
 * warning and false.</li>
 * </ul>
 *
 * @param dir
 *            the file instance to store
 * @param trans
 *            the XML transformer
 * @param nid
 *            the MCRObjectID
 * @return true if the store was okay (see description), else return false
 * @throws TransformerException
 * @throws IOException
 * @throws MCRException
 * @throws SAXParseException
 */
private static boolean exportMCRObject(File dir, Transformer trans, String nid) throws TransformerException, IOException, MCRException, SAXParseException {
    MCRContent content;
    try {
        // if object do'snt exist - no exception is catched!
        content = MCRXMLMetadataManager.instance().retrieveContent(MCRObjectID.getInstance(nid));
    } catch (MCRException ex) {
        return false;
    }
    File xmlOutput = new File(dir, nid + ".xml");
    if (trans != null) {
        FileOutputStream out = new FileOutputStream(xmlOutput);
        StreamResult sr = new StreamResult(out);
        Document doc = MCRXMLParserFactory.getNonValidatingParser().parseXML(content);
        trans.transform(new org.jdom2.transform.JDOMSource(doc), sr);
    } else {
        content.sendTo(xmlOutput);
    }
    LOGGER.info("Object {} saved to {}.", nid, xmlOutput.getCanonicalPath());
    return true;
}
Also used : JDOMSource(org.jdom2.transform.JDOMSource) MCRException(org.mycore.common.MCRException) StreamResult(javax.xml.transform.stream.StreamResult) FileOutputStream(java.io.FileOutputStream) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) File(java.io.File)

Example 17 with MCRContent

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

the class MCRExportServlet method doGetPost.

@Override
public void doGetPost(MCRServletJob job) throws Exception {
    MCRExportCollection collection = createCollection(job.getRequest());
    fillCollection(job.getRequest(), collection);
    MCRContent content2export = collection.getContent();
    String filename = getProperty(job.getRequest(), "filename");
    if (filename == null) {
        filename = "export-" + System.currentTimeMillis();
    }
    job.getResponse().setHeader("Content-Disposition", "inline;filename=\"" + filename + "\"");
    String transformerID = job.getRequest().getParameter("transformer");
    job.getRequest().setAttribute("XSL.Transformer", transformerID);
    getLayoutService().doLayout(job.getRequest(), job.getResponse(), content2export);
}
Also used : MCRContent(org.mycore.common.content.MCRContent)

Example 18 with MCRContent

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

the class MCRBasicCommands method checkXMLFile.

/**
 * The method parse and check an XML file.
 *
 * @param fileName
 *            the location of the xml file
 */
@MCRCommand(syntax = "check file {0}", help = "Checks the data file {0} against the XML Schema.", order = 160)
public static boolean checkXMLFile(String fileName) throws MCRException, SAXParseException, IOException {
    if (!fileName.endsWith(".xml")) {
        LOGGER.warn("{} ignored, does not end with *.xml", fileName);
        return false;
    }
    File file = new File(fileName);
    if (!file.isFile()) {
        LOGGER.warn("{} ignored, is not a file.", fileName);
        return false;
    }
    LOGGER.info("Reading file {} ...", file);
    MCRContent content = new MCRFileContent(file);
    MCRXMLParserFactory.getParser().parseXML(content);
    LOGGER.info("The file has no XML errors.");
    return true;
}
Also used : MCRFileContent(org.mycore.common.content.MCRFileContent) File(java.io.File) MCRContent(org.mycore.common.content.MCRContent) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 19 with MCRContent

use of org.mycore.common.content.MCRContent 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 MCRContent

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

the class MCRTransferPackagePacker method buildTar.

/**
 * Builds a *.tar archive at the path for the given transfer package.
 *
 * @param pathToTar where to store the *.tar
 * @param transferPackage the package to zip
 * @throws IOException something went wrong while packing
 */
private void buildTar(Path pathToTar, MCRTransferPackage transferPackage) throws IOException {
    FileOutputStream fos = new FileOutputStream(pathToTar.toFile());
    try (TarArchiveOutputStream tarOutStream = new TarArchiveOutputStream(fos)) {
        for (Entry<String, MCRContent> contentEntry : transferPackage.getContent().entrySet()) {
            String filePath = contentEntry.getKey();
            byte[] data = contentEntry.getValue().asByteArray();
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Adding '{}' to {}", filePath, pathToTar.toAbsolutePath());
            }
            writeFile(tarOutStream, filePath, data);
            writeMD5(tarOutStream, filePath + ".md5", data);
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) MCRContent(org.mycore.common.content.MCRContent)

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