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