Search in sources :

Example 1 with XMLWriter

use of org.dom4j.io.XMLWriter 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);
    // 声明写XML的对象
    XMLWriter writer = null;
    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    // 设置XML文件的编码格式
    format.setEncoding("UTF-8");
    FileOutputStream fos = new FileOutputStream(androidManifestFile);
    if (androidManifestFile.exists()) {
        try {
            // 读取XML文件
            Document document = reader.read(backupFile);
            // 得到根节点
            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 2 with XMLWriter

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

the class ManifestFileUtils method postProcessManifests.

/**
     * 对manifest做后续处理
     *
     * @param mainManifest
     * @param libManifestMap
     * @param baseBunfleInfoFile
     * @param manifestOptions
     */
public static void postProcessManifests(File mainManifest, Map<String, File> libManifestMap, Multimap<String, File> libDependenciesMaps, File baseBunfleInfoFile, ManifestOptions manifestOptions, boolean addMultiDex, Set<String> remoteBundles) throws IOException, DocumentException {
    File backupFile = new File(mainManifest.getParentFile(), "AndroidManifest-backup.xml");
    FileUtils.deleteQuietly(backupFile);
    FileUtils.moveFile(mainManifest, backupFile);
    // 声明写XML的对象
    XMLWriter writer = null;
    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    // 设置XML文件的编码格式
    format.setEncoding("UTF-8");
    FileOutputStream fos = new FileOutputStream(mainManifest);
    if (mainManifest.exists()) {
        try {
            // 读取XML文件
            Document document = reader.read(backupFile);
            if (null != baseBunfleInfoFile && baseBunfleInfoFile.exists()) {
                addApplicationMetaData(document, libManifestMap, baseBunfleInfoFile, manifestOptions, remoteBundles);
            }
            if (null != manifestOptions && manifestOptions.isAddBundleLocation()) {
                addBundleLocationToDestManifest(document, libManifestMap, libDependenciesMaps, manifestOptions);
            }
            if (null != manifestOptions && manifestOptions.isReplaceApplication()) {
                replaceManifestApplicationName(document);
            }
            if ((null != manifestOptions && manifestOptions.isAddMultiDexMetaData()) || addMultiDex) {
                addMultiDexMetaData(document);
            }
            if (null != manifestOptions && manifestOptions.isRemoveProvider()) {
                removeProvider(document);
            }
            removeCustomLaunches(document, manifestOptions);
            updatePermission(document, manifestOptions);
            removeComments(document);
            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) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document) File(java.io.File) XMLWriter(org.dom4j.io.XMLWriter)

Example 3 with XMLWriter

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

the class Xep227ExporterTest method testExportUsers.

/**
   * Test method for {@link org.jivesoftware.openfire.plugin.OpenfireExporter#exportUsers(org.jivesoftware.openfire.user.UserManager)}.
   * @throws UserAlreadyExistsException 
   * @throws IOException 
   */
@Test
public void testExportUsers() throws UserAlreadyExistsException, IOException {
    InExporter testobject = new Xep227Exporter("serverName", offlineMessagesStore, vCardManager, privateStorage, userManager, null);
    for (int i = 0; i < 10; i++) {
        userManager.createUser("username" + i, "pw", "name" + i, "email" + i);
    }
    Document result = testobject.exportUsers();
    assertNotNull(result);
    assertEquals(1, result.nodeCount());
    assertNotNull(result.node(0));
    Element elem = ((Element) result.node(0));
    assertEquals(1, elem.nodeCount());
    assertNotNull(elem.node(0));
    elem = ((Element) elem.node(0));
    assertEquals(10, elem.nodeCount());
    assertNotNull(elem.node(0));
    elem = ((Element) elem.node(0));
    assertEquals(3, elem.nodeCount());
    assertEquals(2, elem.attributeCount());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
    writer.write(result);
    logger.fine(out.toString());
    assertTrue("Invalid input", testobject.validate(new ByteArrayInputStream(out.toByteArray())));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DOMElement(org.dom4j.dom.DOMElement) Element(org.dom4j.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) Test(org.junit.Test)

Example 4 with XMLWriter

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

the class ImportExportPlugin method exportUsersToByteArray.

/**
     * Converts the user data that is to be exported to a byte[]. If a read-only
     * user store is being used a user's password will be the same as their username.
     *
     * @return a byte[] of the user data.
     * @throws IOException if there's a problem writing to the XMLWriter.
     */
public byte[] exportUsersToByteArray(boolean xep227Support) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
    writer.write(exportUsers(xep227Support));
    return out.toByteArray();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLWriter(org.dom4j.io.XMLWriter)

Example 5 with XMLWriter

use of org.dom4j.io.XMLWriter in project hibernate-orm by hibernate.

the class AdditionalJaxbMappingProducerImpl method produceAdditionalMappings.

@Override
public Collection<MappingDocument> produceAdditionalMappings(final MetadataImplementor metadata, IndexView jandexIndex, final MappingBinder mappingBinder, final MetadataBuildingContext buildingContext) {
    final ServiceRegistry serviceRegistry = metadata.getMetadataBuildingOptions().getServiceRegistry();
    final EnversService enversService = serviceRegistry.getService(EnversService.class);
    if (!enversService.isEnabled()) {
        // short-circuit if envers integration has been disabled.
        return Collections.emptyList();
    }
    final ArrayList<MappingDocument> additionalMappingDocuments = new ArrayList<>();
    // atm we do not have distinct origin info for envers
    final Origin origin = new Origin(SourceType.OTHER, "envers");
    //		final DOMWriter writer = new DOMWriter();
    final MappingCollector mappingCollector = new MappingCollector() {

        @Override
        public void addDocument(Document document) throws DocumentException {
            dump(document);
            // while the commented-out code here is more efficient (well, understanding that
            // this whole process is un-efficient)  it leads to un-decipherable messages when
            // we get mapping mapping errors from envers output.
            //				final DOMSource domSource = new DOMSource( writer.write( document ) );
            //				domSource.setSystemId( "envers" );
            //				final Binding jaxbBinding = mappingBinder.bind( domSource, origin );
            // this form at least allows us to get better error messages
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                final Writer w = new BufferedWriter(new OutputStreamWriter(baos, "UTF-8"));
                final XMLWriter xw = new XMLWriter(w, new OutputFormat(" ", true));
                xw.write(document);
                w.flush();
            } catch (IOException e) {
                throw new HibernateException("Unable to bind Envers-generated XML", e);
            }
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            BufferedInputStream bis = new BufferedInputStream(bais);
            final Binding jaxbBinding = mappingBinder.bind(bis, origin);
            final JaxbHbmHibernateMapping jaxbRoot = (JaxbHbmHibernateMapping) jaxbBinding.getRoot();
            additionalMappingDocuments.add(new MappingDocument(jaxbRoot, origin, buildingContext));
        }
    };
    enversService.initialize(metadata, mappingCollector);
    return additionalMappingDocuments;
}
Also used : Origin(org.hibernate.boot.jaxb.Origin) Binding(org.hibernate.boot.jaxb.spi.Binding) HibernateException(org.hibernate.HibernateException) MappingDocument(org.hibernate.boot.model.source.internal.hbm.MappingDocument) ArrayList(java.util.ArrayList) OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Document(org.dom4j.Document) MappingDocument(org.hibernate.boot.model.source.internal.hbm.MappingDocument) XMLWriter(org.dom4j.io.XMLWriter) BufferedWriter(java.io.BufferedWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) OutputStreamWriter(java.io.OutputStreamWriter) ServiceRegistry(org.hibernate.service.ServiceRegistry) MappingCollector(org.hibernate.envers.configuration.internal.MappingCollector) XMLWriter(org.dom4j.io.XMLWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) JaxbHbmHibernateMapping(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping)

Aggregations

XMLWriter (org.dom4j.io.XMLWriter)42 Document (org.dom4j.Document)21 OutputFormat (org.dom4j.io.OutputFormat)17 IOException (java.io.IOException)15 Element (org.dom4j.Element)15 FileOutputStream (java.io.FileOutputStream)14 File (java.io.File)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 SAXReader (org.dom4j.io.SAXReader)9 FileWriter (java.io.FileWriter)6 StringWriter (java.io.StringWriter)5 BufferedWriter (java.io.BufferedWriter)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)3 PrintWriter (java.io.PrintWriter)3 Writer (java.io.Writer)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 TreeSet (java.util.TreeSet)3