Search in sources :

Example 1 with MCRFileContent

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

the class MCRDefaultConfigurationLoader method loadFromFile.

/**
 * Loads configuration properties from a specified properties file and adds
 * them to the properties currently set. This method scans the <CODE>
 * CLASSPATH</CODE> for the properties file, it may be a plain file, but
 * may also be located in a zip or jar file. If the properties file contains
 * a property called <CODE>MCR.Configuration.Include</CODE>, the files
 * specified in that property will also be read. Multiple include files have
 * to be separated by spaces or colons.
 *
 * @param filename
 *            the properties file to be loaded
 * @throws MCRConfigurationException
 *             if the file can not be loaded
 */
private void loadFromFile(String filename) {
    File mycoreProperties = new File(filename);
    MCRContent input = null;
    try {
        if (mycoreProperties.canRead()) {
            input = new MCRFileContent(mycoreProperties);
        } else {
            URL url = this.getClass().getResource("/" + filename);
            if (url == null) {
                throw new MCRConfigurationException("Could not find file or resource:" + filename);
            }
            input = new MCRURLContent(url);
        }
        loadFromContent(input);
    } catch (IOException e) {
        String name = input == null ? filename : input.getSystemId();
        throw new MCRConfigurationException("Could not load configuration from: " + name, e);
    }
}
Also used : MCRFileContent(org.mycore.common.content.MCRFileContent) MCRURLContent(org.mycore.common.content.MCRURLContent) IOException(java.io.IOException) File(java.io.File) MCRContent(org.mycore.common.content.MCRContent) URL(java.net.URL)

Example 2 with MCRFileContent

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

the class MCRFileTest method contentFile.

@Test
public void contentFile() throws Exception {
    MCRFile file = col.createFile("foo.txt");
    File src = File.createTempFile("foo", "txt");
    src.deleteOnExit();
    byte[] content = "Hello World".getBytes("UTF-8");
    try (FileOutputStream fo = new FileOutputStream(src)) {
        IOUtils.copy(new ByteArrayInputStream(content), fo);
    }
    file.setContent(new MCRFileContent(src));
    assertFalse(MCRFile.MD5_OF_EMPTY_FILE.equals(file.getMD5()));
    assertEquals(11, file.getSize());
    file.getContent().sendTo(src);
    assertEquals(11, src.length());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MCRFileContent(org.mycore.common.content.MCRFileContent) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test)

Example 3 with MCRFileContent

use of org.mycore.common.content.MCRFileContent 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 4 with MCRFileContent

use of org.mycore.common.content.MCRFileContent 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 5 with MCRFileContent

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

the class MCRAccessCommands method createPermissionsFromFile.

/**
 * This method sets the new permissions given in a certain file
 *
 * @param filename
 *            the filename of the file that contains the mcrpermissions
 */
public static void createPermissionsFromFile(String filename) throws Exception {
    MCRAccessInterface AI = MCRAccessManager.getAccessImpl();
    if (!checkFilename(filename)) {
        return;
    }
    LOGGER.info("Reading file {} ...", filename);
    org.jdom2.Document doc = MCRXMLParserFactory.getValidatingParser().parseXML(new MCRFileContent(filename));
    org.jdom2.Element rootelm = doc.getRootElement();
    if (!rootelm.getName().equals("mcrpermissions")) {
        throw new MCRException("The data are not for mcrpermissions.");
    }
    List<Element> listelm = rootelm.getChildren("mcrpermission");
    for (Element mcrpermission : listelm) {
        String permissionName = mcrpermission.getAttributeValue("name").trim();
        String ruleDescription = mcrpermission.getAttributeValue("ruledescription");
        if (ruleDescription == null) {
            ruleDescription = "";
        }
        Element rule = mcrpermission.getChild("condition").clone();
        String objectid = mcrpermission.getAttributeValue("objectid");
        if (objectid == null) {
            AI.addRule(permissionName, rule, ruleDescription);
        } else {
            AI.addRule(objectid, permissionName, rule, ruleDescription);
        }
    }
}
Also used : Document(org.jdom2.Document) MCRException(org.mycore.common.MCRException) MCRAccessInterface(org.mycore.access.MCRAccessInterface) MCRFileContent(org.mycore.common.content.MCRFileContent) Element(org.jdom2.Element) Element(org.jdom2.Element)

Aggregations

MCRFileContent (org.mycore.common.content.MCRFileContent)12 File (java.io.File)8 Document (org.jdom2.Document)6 Element (org.jdom2.Element)4 MCRContent (org.mycore.common.content.MCRContent)3 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)3 FileOutputStream (java.io.FileOutputStream)2 URL (java.net.URL)2 Test (org.junit.Test)2 MCRException (org.mycore.common.MCRException)2 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)2 MCRURLContent (org.mycore.common.content.MCRURLContent)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 MCRAccessInterface (org.mycore.access.MCRAccessInterface)1 MCRSourceContent (org.mycore.common.content.MCRSourceContent)1