Search in sources :

Example 31 with OutputFormat

use of org.dom4j.io.OutputFormat in project atlas by alibaba.

the class ManifestFileUtils method removeProvider.

public static void removeProvider(File androidManifestFile) throws IOException, DocumentException {
    File backupFile = new File(androidManifestFile.getParentFile(), "AndroidManifest-backup.xml");
    FileUtils.deleteQuietly(backupFile);
    FileUtils.moveFile(androidManifestFile, backupFile);
    // Declares the object that writes XML
    XMLWriter writer = null;
    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    // Sets the encoding format for the XML file
    format.setEncoding("UTF-8");
    FileOutputStream fos = new FileOutputStream(androidManifestFile);
    if (androidManifestFile.exists()) {
        try {
            // Read the XML file
            Document document = reader.read(backupFile);
            // Get the root node
            Element root = document.getRootElement();
            List<? extends Node> nodes = root.selectNodes("//provider");
            for (Node node : nodes) {
                Element element = (Element) node;
                String name = element.attributeValue("name");
                logger.info("[Remove Provider]" + name);
                element.getParent().remove(element);
            }
            writer = new XMLWriter(fos, format);
            writer.write(document);
        } finally {
            if (null != writer) {
                writer.close();
            }
            IOUtils.closeQuietly(fos);
        }
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) FileOutputStream(java.io.FileOutputStream) Element(org.dom4j.Element) Node(org.dom4j.Node) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document) File(java.io.File) XMLWriter(org.dom4j.io.XMLWriter)

Example 32 with OutputFormat

use of org.dom4j.io.OutputFormat in project Openfire by igniterealtime.

the class LdapUserProfile method saveProperties.

/**
 * Saves current configuration as XML/DB properties.
 */
public void saveProperties() {
    Element vCard = DocumentHelper.createElement(QName.get("vCard", "vcard-temp"));
    Element subelement;
    // Add name
    if (name != null && name.trim().length() > 0) {
        subelement = vCard.addElement("N");
        subelement.addElement("GIVEN").setText(name.trim());
    }
    // Add email
    if (email != null && email.trim().length() > 0) {
        subelement = vCard.addElement("EMAIL");
        subelement.addElement("INTERNET");
        subelement.addElement("USERID").setText(email.trim());
    }
    // Add Full Name
    vCard.addElement("FN").setText(fullName.trim());
    // Add nickname
    if (nickname != null && nickname.trim().length() > 0) {
        vCard.addElement("NICKNAME").setText(nickname.trim());
    }
    // Add birthday
    if (birthday != null && birthday.trim().length() > 0) {
        vCard.addElement("BDAY").setText(birthday.trim());
    }
    // Add photo/avatar
    if (photo != null && photo.trim().length() > 0) {
        Element element = vCard.addElement("PHOTO");
        element.addElement("TYPE").setText("image/jpeg");
        element.addElement("BINVAL").setText(photo.trim());
    }
    // Add home address
    subelement = vCard.addElement("ADR");
    subelement.addElement("HOME");
    if (homeStreet != null && homeStreet.trim().length() > 0) {
        subelement.addElement("STREET").setText(homeStreet.trim());
    }
    if (homeCity != null && homeCity.trim().length() > 0) {
        subelement.addElement("LOCALITY").setText(homeCity.trim());
    }
    if (homeState != null && homeState.trim().length() > 0) {
        subelement.addElement("REGION").setText(homeState.trim());
    }
    if (homeZip != null && homeZip.trim().length() > 0) {
        subelement.addElement("PCODE").setText(homeZip.trim());
    }
    if (homeCountry != null && homeCountry.trim().length() > 0) {
        subelement.addElement("CTRY").setText(homeCountry.trim());
    }
    // Add business address
    subelement = vCard.addElement("ADR");
    subelement.addElement("WORK");
    if (businessStreet != null && businessStreet.trim().length() > 0) {
        subelement.addElement("STREET").setText(businessStreet.trim());
    }
    if (businessCity != null && businessCity.trim().length() > 0) {
        subelement.addElement("LOCALITY").setText(businessCity.trim());
    }
    if (businessState != null && businessState.trim().length() > 0) {
        subelement.addElement("REGION").setText(businessState.trim());
    }
    if (businessZip != null && businessZip.trim().length() > 0) {
        subelement.addElement("PCODE").setText(businessZip.trim());
    }
    if (businessCountry != null && businessCountry.trim().length() > 0) {
        subelement.addElement("CTRY").setText(businessCountry.trim());
    }
    // Add home phone
    if (homePhone != null && homePhone.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("HOME");
        subelement.addElement("VOICE");
        subelement.addElement("NUMBER").setText(homePhone.trim());
    }
    // Add home mobile
    if (homeMobile != null && homeMobile.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("HOME");
        subelement.addElement("CELL");
        subelement.addElement("NUMBER").setText(homeMobile.trim());
    }
    // Add home fax
    if (homeFax != null && homeFax.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("HOME");
        subelement.addElement("FAX");
        subelement.addElement("NUMBER").setText(homeFax.trim());
    }
    // Add home pager
    if (homePager != null && homePager.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("HOME");
        subelement.addElement("PAGER");
        subelement.addElement("NUMBER").setText(homePager.trim());
    }
    // Add business phone
    if (businessPhone != null && businessPhone.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("WORK");
        subelement.addElement("VOICE");
        subelement.addElement("NUMBER").setText(businessPhone.trim());
    }
    // Add business mobile
    if (businessMobile != null && businessMobile.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("WORK");
        subelement.addElement("CELL");
        subelement.addElement("NUMBER").setText(businessMobile.trim());
    }
    // Add business fax
    if (businessFax != null && businessFax.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("WORK");
        subelement.addElement("FAX");
        subelement.addElement("NUMBER").setText(businessFax.trim());
    }
    // Add business pager
    if (businessPager != null && businessPager.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("WORK");
        subelement.addElement("PAGER");
        subelement.addElement("NUMBER").setText(businessPager.trim());
    }
    // Add job title
    if (businessJobTitle != null && businessJobTitle.trim().length() > 0) {
        vCard.addElement("TITLE").setText(businessJobTitle.trim());
    }
    // Add job department
    if (businessDepartment != null && businessDepartment.trim().length() > 0) {
        vCard.addElement("ORG").addElement("ORGUNIT").setText(businessDepartment.trim());
    }
    // Generate content to store in property
    String vcardXML;
    StringWriter writer = new StringWriter();
    OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
    XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
    try {
        xmlWriter.write(vCard);
        vcardXML = writer.toString();
    } catch (IOException e) {
        Log.error("Error pretty formating XML", e);
        vcardXML = vCard.asXML();
    }
    StringBuilder sb = new StringBuilder(vcardXML.length());
    sb.append("<![CDATA[").append(vcardXML).append("]]>");
    // Save mapping as an XML property
    JiveGlobals.setProperty("ldap.vcard-mapping", sb.toString());
    // Set that the vcard provider is LdapVCardProvider
    VCardManager.VCARD_PROVIDER.setValue(LdapVCardProvider.class);
    // Save duplicated fields in LdapManager (should be removed in the future)
    LdapManager.getInstance().setNameField(new LdapUserTester.PropertyMapping(name));
    LdapManager.getInstance().setEmailField(email.replaceAll("(\\{)([\\d\\D&&[^}]]+)(})", "$2"));
    // Store the DB storage variable in the actual database.
    LdapVCardProvider.STORE_AVATAR_IN_DB.setValue(avatarStoredInDB);
}
Also used : StringWriter(java.io.StringWriter) OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException) XMLWriter(org.jivesoftware.util.XMLWriter)

Example 33 with OutputFormat

use of org.dom4j.io.OutputFormat in project Openfire by igniterealtime.

the class XMLProperties method saveProperties.

/**
 * Saves the properties to disk as an XML document. A temporary file is
 * used during the writing process for maximum safety.
 *
 * Callers <em>MUST</em> hold the write-lock from {@link #readWriteLock}.
 *
 * @return false if the file could not be saved, otherwise true
 */
private boolean saveProperties() {
    if (file == null) {
        Log.error("Unable to save XML properties; no file specified");
        return false;
    }
    // Write data out to a temporary file first.
    final Path tempFile = file.getParent().resolve(file.getFileName() + ".tmp");
    try (final Writer writer = Files.newBufferedWriter(tempFile, StandardCharsets.UTF_8)) {
        OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
        xmlWriter.write(document);
    } catch (final Exception e) {
        Log.error("Unable to write properties to tmpFile {}", tempFile, e);
        // There were errors so abort replacing the old property file.
        return false;
    }
    // Delete the old file so we can replace it.
    try {
        Files.deleteIfExists(file);
    } catch (final IOException e) {
        Log.error("Error deleting existing property file {}: ", tempFile, e);
        return false;
    }
    // Copy new contents to the file.
    try {
        Files.copy(tempFile, file, StandardCopyOption.REPLACE_EXISTING);
    } catch (final Exception e) {
        Log.error("Error copying new property file from {} to {}:", tempFile, file, e);
        // There were errors so abort replacing the old property file.
        return false;
    }
    // If no errors, delete the temp file.
    try {
        Files.deleteIfExists(tempFile);
    } catch (IOException e) {
        Log.error("Error deleting temp file {}", tempFile, e);
    }
    return true;
}
Also used : OutputFormat(org.dom4j.io.OutputFormat)

Example 34 with OutputFormat

use of org.dom4j.io.OutputFormat in project openolat by klemens.

the class ContentPackage method writeToFile.

/**
 * writes the manifest.xml
 */
void writeToFile() {
    String filename = "imsmanifest.xml";
    OutputFormat format = OutputFormat.createPrettyPrint();
    try {
        VFSLeaf outFile;
        // file may exist
        outFile = (VFSLeaf) cpcore.getRootDir().resolve("/" + filename);
        if (outFile == null) {
            // if not, create it
            outFile = cpcore.getRootDir().createChildLeaf("/" + filename);
        }
        DefaultDocument manifestDocument = cpcore.buildDocument();
        XMLWriter writer = new XMLWriter(outFile.getOutputStream(false), format);
        writer.write(manifestDocument);
    } catch (Exception e) {
        log.error("imsmanifest for ores " + ores.getResourceableId() + "couldn't be written to file.", e);
        throw new OLATRuntimeException(CPOrganizations.class, "Error writing imsmanifest-file", new IOException());
    }
}
Also used : CPOrganizations(org.olat.ims.cp.objects.CPOrganizations) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) OutputFormat(org.dom4j.io.OutputFormat) DefaultDocument(org.dom4j.tree.DefaultDocument) IOException(java.io.IOException) XMLWriter(org.dom4j.io.XMLWriter) IOException(java.io.IOException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException)

Example 35 with OutputFormat

use of org.dom4j.io.OutputFormat in project openolat by klemens.

the class QTIImportProcessor method processAssessmentFiles.

protected void processAssessmentFiles(QuestionItemImpl item, ItemInfos itemInfos) {
    // a package with an item
    String dir = item.getDirectory();
    String rootFilename = item.getRootFilename();
    VFSContainer container = qpoolFileStorage.getContainer(dir);
    VFSLeaf endFile = container.createChildLeaf(rootFilename);
    // embed in <questestinterop>
    DocumentFactory df = DocumentFactory.getInstance();
    Document itemDoc = df.createDocument();
    Element questestinteropEl = df.createElement(QTIDocument.DOCUMENT_ROOT);
    itemDoc.setRootElement(questestinteropEl);
    Element deepClone = (Element) itemInfos.getItemEl().clone();
    questestinteropEl.add(deepClone);
    // write
    try {
        OutputStream os = endFile.getOutputStream(false);
        ;
        XMLWriter xw = new XMLWriter(os, new OutputFormat("  ", true));
        xw.write(itemDoc.getRootElement());
        xw.close();
        os.close();
    } catch (IOException e) {
        log.error("", e);
    }
    // there perhaps some other materials
    if (importedFilename.toLowerCase().endsWith(".zip")) {
        processAssessmentMaterials(deepClone, container);
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) DocumentFactory(org.dom4j.DocumentFactory) VFSContainer(org.olat.core.util.vfs.VFSContainer) Element(org.dom4j.Element) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException) Document(org.dom4j.Document) QTIDocument(org.olat.ims.qti.editor.beecom.objects.QTIDocument) XMLWriter(org.dom4j.io.XMLWriter)

Aggregations

OutputFormat (org.dom4j.io.OutputFormat)89 XMLWriter (org.dom4j.io.XMLWriter)75 IOException (java.io.IOException)39 Document (org.dom4j.Document)31 Element (org.dom4j.Element)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)18 StringWriter (java.io.StringWriter)18 SAXReader (org.dom4j.io.SAXReader)16 File (java.io.File)15 FileOutputStream (java.io.FileOutputStream)14 FileWriter (java.io.FileWriter)10 OutputStreamWriter (java.io.OutputStreamWriter)8 StringReader (java.io.StringReader)6 DocumentException (org.dom4j.DocumentException)6 OutputStream (java.io.OutputStream)5 Test (org.junit.Test)5 BufferedOutputStream (java.io.BufferedOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 MalformedURLException (java.net.MalformedURLException)4 ArrayList (java.util.ArrayList)4