Search in sources :

Example 91 with Text

use of org.jdom2.Text 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)

Example 92 with Text

use of org.jdom2.Text in project mycore by MyCoRe-Org.

the class MCRRealmFactory method loadRealms.

/**
 */
private static void loadRealms() {
    Element root;
    try {
        root = getRealms().getRootElement();
    } catch (SAXException | JDOMException | TransformerException | IOException e) {
        throw new MCRException("Could not load realms from URI: " + realmsURI);
    }
    String localRealmID = root.getAttributeValue("local");
    HashMap<String, MCRRealm> realmsMap = new HashMap<>();
    HashMap<String, MCRUserAttributeMapper> attributeMapper = new HashMap<>();
    List<MCRRealm> realmsList = new ArrayList<>();
    List<Element> realms = root.getChildren("realm");
    for (Element child : realms) {
        String id = child.getAttributeValue("id");
        MCRRealm realm = new MCRRealm(id);
        List<Element> labels = child.getChildren("label");
        for (Element label : labels) {
            String text = label.getTextTrim();
            String lang = label.getAttributeValue("lang", Namespace.XML_NAMESPACE);
            realm.setLabel(lang, text);
        }
        realm.setPasswordChangeURL(child.getChildTextTrim("passwordChangeURL"));
        Element login = child.getChild("login");
        if (login != null) {
            realm.setLoginURL(login.getAttributeValue("url"));
            realm.setRedirectParameter(login.getAttributeValue("redirectParameter"));
            realm.setRealmParameter(login.getAttributeValue("realmParameter"));
        }
        Element createElement = child.getChild("create");
        if (createElement != null) {
            realm.setCreateURL(createElement.getAttributeValue("url"));
        }
        attributeMapper.put(id, MCRUserAttributeMapper.instance(child));
        realmsMap.put(id, realm);
        realmsList.add(realm);
        if (localRealmID.equals(id)) {
            localRealm = realm;
        }
    }
    MCRRealmFactory.realmsDocument = root.getDocument();
    MCRRealmFactory.realmsMap = realmsMap;
    MCRRealmFactory.realmsList = realmsList;
    MCRRealmFactory.attributeMapper = attributeMapper;
}
Also used : MCRException(org.mycore.common.MCRException) HashMap(java.util.HashMap) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException)

Example 93 with Text

use of org.jdom2.Text in project mycore by MyCoRe-Org.

the class MCROAIDataProvider method addXSLStyle.

/**
 * Add link to XSL stylesheet for displaying OAI response in web browser.
 */
private Document addXSLStyle(Document doc) {
    String styleSheet = MCROAIAdapter.PREFIX + getServletName() + ".ResponseStylesheet";
    String xsl = MCRConfiguration.instance().getString(styleSheet, "oai/oai2.xsl");
    if (!xsl.isEmpty()) {
        Map<String, String> pairs = new HashMap<>();
        pairs.put("type", "text/xsl");
        pairs.put("href", MCRFrontendUtil.getBaseURL() + xsl);
        doc.addContent(0, new ProcessingInstruction("xml-stylesheet", pairs));
    }
    return doc;
}
Also used : HashMap(java.util.HashMap) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Example 94 with Text

use of org.jdom2.Text in project mycore by MyCoRe-Org.

the class MCRXSL2XMLTransformer method getDocument.

private Document getDocument(JDOMResult result) {
    Document resultDoc = result.getDocument();
    if (resultDoc == null) {
        // Sometimes a transformation produces whitespace strings
        // JDOM would produce a empty document if it detects those
        // So we remove them, if they exists.
        List<Content> transformResult = result.getResult();
        int origSize = transformResult.size();
        Iterator<Content> iterator = transformResult.iterator();
        while (iterator.hasNext()) {
            Content content = iterator.next();
            if (content instanceof Text) {
                String trimmedText = ((Text) content).getTextTrim();
                if (trimmedText.length() == 0) {
                    iterator.remove();
                }
            }
        }
        if (transformResult.size() < origSize) {
            JDOMFactory f = result.getFactory();
            if (f == null) {
                f = new DefaultJDOMFactory();
            }
            resultDoc = f.document(null);
            resultDoc.setContent(transformResult);
        }
    }
    return resultDoc;
}
Also used : DefaultJDOMFactory(org.jdom2.DefaultJDOMFactory) MCRContent(org.mycore.common.content.MCRContent) Content(org.jdom2.Content) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) JDOMFactory(org.jdom2.JDOMFactory) DefaultJDOMFactory(org.jdom2.DefaultJDOMFactory) Text(org.jdom2.Text) Document(org.jdom2.Document)

Example 95 with Text

use of org.jdom2.Text in project mycore by MyCoRe-Org.

the class MCRTransferPackageUtil method getMCRObjects.

/**
 * Gets the list of mycore object identifiers from the given directory.
 *
 * @param targetDirectory directory where the *.tar was unpacked
 * @return list of object which lies within the directory
 */
public static List<String> getMCRObjects(Path targetDirectory) throws JDOMException, IOException {
    Path order = targetDirectory.resolve(MCRTransferPackage.IMPORT_CONFIG_FILENAME);
    Document xml = new SAXBuilder().build(order.toFile());
    Element config = xml.getRootElement();
    XPathExpression<Text> exp = MCRConstants.XPATH_FACTORY.compile("order/object/text()", Filters.text());
    return exp.evaluate(config).stream().map(Text::getText).collect(Collectors.toList());
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) Text(org.jdom2.Text) Document(org.jdom2.Document)

Aggregations

Element (org.jdom2.Element)83 Document (org.jdom2.Document)36 File (java.io.File)21 ProcessingInstruction (org.jdom2.ProcessingInstruction)17 IOException (java.io.IOException)14 Text (org.jdom2.Text)12 Attribute (org.jdom2.Attribute)11 XMLOutputter (org.jdom2.output.XMLOutputter)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)9 XmlFile (jmri.jmrit.XmlFile)9 ArrayList (java.util.ArrayList)6 JDOMException (org.jdom2.JDOMException)6 StringWriter (java.io.StringWriter)5 NamedIcon (jmri.jmrit.catalog.NamedIcon)5 FileOutputStream (java.io.FileOutputStream)4 LinkedHashMap (java.util.LinkedHashMap)4 FileNotFoundException (java.io.FileNotFoundException)3 URL (java.net.URL)3 Locale (java.util.Locale)3