Search in sources :

Example 81 with MCRException

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

the class MCRMetaElement method setFromDOM.

/**
 * This methode read the XML input stream part from a DOM part for the
 * metadata of the document.
 *
 * @param element
 *            a relevant JDOM element for the metadata
 * @exception MCRException
 *                if the class can't loaded
 */
@SuppressWarnings("unchecked")
public final void setFromDOM(org.jdom2.Element element) throws MCRException {
    String fullname;
    Class<? extends MCRMetaInterface> forName;
    try {
        String classname = element.getAttributeValue("class");
        if (classname == null) {
            throw new MCRException("Missing required class attribute in element " + element.getName());
        }
        fullname = META_PACKAGE_NAME + classname;
        forName = (Class<? extends MCRMetaInterface>) Class.forName(fullname);
        setClass(forName);
    } catch (ClassNotFoundException e) {
        throw new MCRException(e);
    }
    tag = element.getName();
    String heritable = element.getAttributeValue("heritable");
    if (heritable != null)
        setHeritable(Boolean.valueOf(heritable));
    String notInherit = element.getAttributeValue("notinherit");
    if (notInherit != null)
        setNotInherit(Boolean.valueOf(notInherit));
    List<Element> element_list = element.getChildren();
    for (Element subtag : element_list) {
        MCRMetaInterface obj;
        try {
            obj = forName.getDeclaredConstructor().newInstance();
            obj.setFromDOM(subtag);
        } catch (ReflectiveOperationException e) {
            throw new MCRException(fullname + " ReflectiveOperationException", e);
        }
        list.add(obj);
    }
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element)

Example 82 with MCRException

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

the class MCRMetaElement method createXML.

/**
 * This methode create a XML stream for all data in this class, defined by
 * the MyCoRe XML MCRLangText definition for the given subtag.
 *
 * @param flag
 *            true if all inherited data should be include, else false
 * @exception MCRException
 *                if the content of this class is not valid
 * @return a JDOM Element with the XML Element part
 */
public final Element createXML(boolean flag) throws MCRException {
    try {
        validate();
    } catch (MCRException exc) {
        debug();
        throw new MCRException("MCRMetaElement : The content is not valid: Tag=" + this.tag, exc);
    }
    Element elm = new Element(tag);
    elm.setAttribute("class", getClassName());
    elm.setAttribute("heritable", String.valueOf(heritable));
    elm.setAttribute("notinherit", String.valueOf(notinherit));
    list.stream().filter(metaInterface -> (flag || metaInterface.getInherited() == 0)).map(MCRMetaInterface::createXML).forEachOrdered(elm::addContent);
    return elm;
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element)

Example 83 with MCRException

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

the class MCRMetaHistoryDate method validate.

/**
 * Validates this MCRMetaHistoryDate. This method throws an exception if:
 * <ul>
 * <li>the subtag is not null or empty</li>
 * <li>the lang value was supported</li>
 * <li>the inherited value is lower than zero</li>
 * <li>the number of texts is 0 (empty texts are delete)</li>
 * <li>von is null or bis is null or calendar is null</li>
 * </ul>
 *
 * @throws MCRException the MCRMetaHistoryDate is invalid
 */
@Override
public void validate() throws MCRException {
    super.validate();
    for (int i = 0; i < texts.size(); i++) {
        MCRMetaHistoryDateText textitem = texts.get(i);
        if (!textitem.isValid()) {
            texts.remove(i);
            i--;
        }
    }
    if (texts.size() == 0) {
        throw new MCRException(getSubTag() + ": no texts defined");
    }
    if (von == null || bis == null || calendar == null) {
        throw new MCRException(getSubTag() + ": von,bis or calendar are null");
    }
    if (ibis < ivon) {
        Calendar swp = (Calendar) von.clone();
        setVonDate((Calendar) bis.clone());
        setBisDate(swp);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRCalendar(org.mycore.common.MCRCalendar) Calendar(com.ibm.icu.util.Calendar) GregorianCalendar(com.ibm.icu.util.GregorianCalendar)

Example 84 with MCRException

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

the class MCRPath method relativize.

/* (non-Javadoc)
     * @see java.nio.file.Path#relativize(java.nio.file.Path)
     */
@Override
public MCRPath relativize(final Path other) {
    if (equals(Objects.requireNonNull(other, "Cannot relativize against 'null'."))) {
        return getFileSystem().emptyPath();
    }
    if (isAbsolute() != other.isAbsolute()) {
        throw new IllegalArgumentException("'other' must be absolute if and only if this is absolute, too.");
    }
    final MCRPath that = toMCRPath(other);
    if (!isAbsolute() && isEmpty()) {
        return that;
    }
    URI thisURI;
    URI thatURI;
    try {
        thisURI = new URI(null, null, path, null);
        thatURI = new URI(null, null, that.path, null);
    } catch (URISyntaxException e) {
        throw new MCRException(e);
    }
    final URI relativizedURI = thisURI.relativize(thatURI);
    if (thatURI.equals(relativizedURI)) {
        return that;
    }
    return MCRAbstractFileSystem.getPath(null, relativizedURI.getPath(), getFileSystem());
}
Also used : MCRException(org.mycore.common.MCRException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 85 with MCRException

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

the class MCRUserCommands method encryptPasswordsInXMLFile.

/**
 * A given XML file containing user data with cleartext passwords must be converted prior to loading the user data
 * into the system. This method reads all user objects in the given XML file, encrypts the passwords and writes them
 * back to a file with name original-file-name_encrypted.xml.
 *
 * @param oldFile
 *            the filename of the user data input
 * @param newFile
 *            the filename of the user data output (encrypted passwords)
 */
@MCRCommand(syntax = "encrypt passwords in user xml file {0} to file {1}", help = "A migration tool to change old plain text password entries to encrpted entries.", order = 40)
public static void encryptPasswordsInXMLFile(String oldFile, String newFile) throws SAXParseException, IOException {
    File inputFile = getCheckedFile(oldFile);
    if (inputFile == null) {
        return;
    }
    LOGGER.info("Reading file {} ...", inputFile.getAbsolutePath());
    Document doc = MCRXMLParserFactory.getNonValidatingParser().parseXML(new MCRFileContent(inputFile));
    Element rootelm = doc.getRootElement();
    MCRUser mcrUser = MCRUserTransformer.buildMCRUser(rootelm);
    if (mcrUser == null) {
        throw new MCRException("These data do not correspond to a user.");
    }
    MCRUserManager.updatePasswordHashToSHA256(mcrUser, mcrUser.getPassword());
    FileOutputStream outFile = new FileOutputStream(newFile);
    saveToXMLFile(mcrUser, outFile);
}
Also used : MCRException(org.mycore.common.MCRException) MCRFileContent(org.mycore.common.content.MCRFileContent) Element(org.jdom2.Element) FileOutputStream(java.io.FileOutputStream) Document(org.jdom2.Document) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRException (org.mycore.common.MCRException)131 IOException (java.io.IOException)39 Element (org.jdom2.Element)26 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)19 Document (org.jdom2.Document)18 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)18 File (java.io.File)15 MCRConfigurationException (org.mycore.common.config.MCRConfigurationException)12 MCRObject (org.mycore.datamodel.metadata.MCRObject)12 ArrayList (java.util.ArrayList)11 JDOMException (org.jdom2.JDOMException)11 MCRAccessException (org.mycore.access.MCRAccessException)11 MCRPath (org.mycore.datamodel.niofs.MCRPath)10 SAXException (org.xml.sax.SAXException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 List (java.util.List)7 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)7 SAXParseException (org.xml.sax.SAXParseException)7 URI (java.net.URI)6 Path (java.nio.file.Path)6