Java XML String Transform convertPlist(File info_plist_file, String script_url, Map script_parameters)

Here you can find the source of convertPlist(File info_plist_file, String script_url, Map script_parameters)

Description

convert Plist

License

Open Source License

Declaration

private static void convertPlist(File info_plist_file, String script_url, Map script_parameters) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import java.util.Iterator;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.xml.sax.InputSource;

public class Main {
    private static void convertPlist(File info_plist_file, String script_url, Map script_parameters) {
        try {/*w  ww  . j  a  va 2s.  com*/
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder document_builder = factory.newDocumentBuilder();
            // Hack to avoid lookup of the DTD
            document_builder.setEntityResolver((String publicId, String systemId) -> {
                if (publicId.equals("-//Apple Computer//DTD PLIST 1.0//EN"))
                    return new InputSource(
                            new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
                else
                    return null;
            });

            URL xslt_url = Thread.currentThread().getContextClassLoader().getResource(script_url);
            Source xsltSource = new StreamSource(xslt_url.toString());
            Source source = new StreamSource(info_plist_file);
            File tmp_file = File.createTempFile("Info", "plist");
            tmp_file.deleteOnExit();
            Result result = new StreamResult(tmp_file);

            TransformerFactory tf = TransformerFactory.newInstance();
            Templates transformation = tf.newTemplates(xsltSource);
            Transformer transformer = transformation.newTransformer();
            Iterator it = script_parameters.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                transformer.setParameter((String) entry.getKey(), entry.getValue());
            }
            transformer.transform(source, result);
            tmp_file.renameTo(info_plist_file);
        } catch (IOException | TransformerException | ParserConfigurationException e) {
            throw new RuntimeException(e);
        }
    }

    private static void convertPlist(String gamename, String script_url, Map script_parameters) {
        String app_dir_name = gamename + ".app";
        File app_dir = locateDir(app_dir_name);
        if (app_dir == null) {
            System.out.println("Failed to locate " + app_dir_name);
            return;
        }
        File info_plist_file = new File(new File(app_dir, "Contents"), "Info.plist");
        if (!info_plist_file.exists()) {
            System.out.println("Failed to locate " + info_plist_file);
            return;
        }
        convertPlist(info_plist_file, script_url, script_parameters);
    }

    private static File locateDir(String app_dir_name) {
        File current_dir = new File(".").getAbsoluteFile();
        while (current_dir != null && !current_dir.getName().equals(app_dir_name))
            current_dir = current_dir.getParentFile();
        return current_dir;
    }
}

Related

  1. addElementToXml(String xmlFile, String nodeToAdd, String nodeContent)
  2. addNode(String nodeType, String idField, String nodeID, File destFile, ArrayList attributes)
  3. applyXSL(File xmlFile, File xslFile, String outputFilename)
  4. base64encode(String decodedString)
  5. convertResult(StreamResult result, StringWriter writer)
  6. convertValidatorResult(Result result, StringWriter writer)
  7. createElement(Node node, String name, String value, Map attributes)
  8. createFullReportLog(String sessionLogDir)