src/jdk.jpackage/share/classes/jdk/jpackage/internal/AppImageFile.java
author herrick
Mon, 30 Sep 2019 15:13:14 -0400
branchJDK-8200758-branch
changeset 58414 a5f66aa04f68
child 58696 61c44899b4eb
permissions -rw-r--r--
8230920 : jpackage problems when -input dir contains any files with "cfg" extension. Reviewed-by: asemenyuk, almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58414
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
     1
/*
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
     4
 *
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    10
 *
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    15
 * accompanied this code).
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    16
 *
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    20
 *
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    23
 * questions.
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    24
 */
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    25
package jdk.jpackage.internal;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    26
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    27
import java.io.BufferedWriter;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    28
import java.io.FileInputStream;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    29
import java.io.FileWriter;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    30
import java.io.IOException;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    31
import java.io.Writer;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    32
import java.nio.file.Path;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    33
import java.util.ArrayList;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    34
import java.util.Collections;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    35
import java.util.List;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    36
import java.util.ArrayList;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    37
import java.util.Map;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    38
import java.util.regex.Matcher;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    39
import java.util.regex.Pattern;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    40
import java.util.stream.Collectors;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    41
import javax.xml.parsers.DocumentBuilder;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    42
import javax.xml.parsers.DocumentBuilderFactory;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    43
import javax.xml.parsers.ParserConfigurationException;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    44
import javax.xml.stream.XMLOutputFactory;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    45
import javax.xml.stream.XMLStreamException;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    46
import javax.xml.stream.XMLStreamWriter;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    47
import javax.xml.xpath.XPath;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    48
import javax.xml.xpath.XPathConstants;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    49
import javax.xml.xpath.XPathExpressionException;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    50
import javax.xml.xpath.XPathFactory;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    51
import org.w3c.dom.Document;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    52
import org.w3c.dom.NodeList;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    53
import org.xml.sax.SAXException;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    54
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    55
import static jdk.jpackage.internal.StandardBundlerParam.*;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    56
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    57
class AppImageFile {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    58
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    59
    // These values will be loaded from AppImage xml file.
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    60
    private final String creatorVersion;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    61
    private final String creatorPlatform;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    62
    private final String launcherName;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    63
    private final List<String> addLauncherNames;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    64
    
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    65
    final static String XML_FILENAME = ".jpackage.xml";
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    66
    
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    67
    private final static Map<Platform, String> PLATFORM_LABELS = Map.of(
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    68
            Platform.LINUX, "linux", Platform.WINDOWS, "windows", Platform.MAC,
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    69
            "macOS");
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    70
    
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    71
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    72
    private AppImageFile() {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    73
        this(null, null, null, null);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    74
    }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    75
    
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    76
    private AppImageFile(String launcherName, List<String> addLauncherNames,
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    77
            String creatorVersion, String creatorPlatform) {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    78
        this.launcherName = launcherName;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    79
        this.addLauncherNames = addLauncherNames;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    80
        this.creatorVersion = creatorVersion;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    81
        this.creatorPlatform = creatorPlatform;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    82
    }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    83
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    84
    /**
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    85
     * Would return null to indicate stored command line is invalid.
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    86
     */
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    87
    List<String> getAddLauncherNames() {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    88
        return addLauncherNames;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    89
    }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    90
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    91
    String getLauncherName() {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    92
        return launcherName;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    93
    }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    94
    
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    95
    void verifyCompatible() throws ConfigException {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    96
        // Just do nohing for now.
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    97
    }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    98
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
    99
    static void save(Path appImage, Map<String, Object> params)
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   100
            throws IOException {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   101
        Path xmlFile = appImage.resolve(XML_FILENAME);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   102
        XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   103
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   104
        try (Writer w = new BufferedWriter(new FileWriter(xmlFile.toFile()))) {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   105
            XMLStreamWriter xml = xmlFactory.createXMLStreamWriter(w);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   106
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   107
            xml.writeStartDocument();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   108
            xml.writeStartElement("jpackage-state");
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   109
            xml.writeAttribute("version", getVersion());
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   110
            xml.writeAttribute("platform", getPlatform());
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   111
            
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   112
            xml.writeStartElement("main-launcher");
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   113
            xml.writeCharacters(APP_NAME.fetchFrom(params));
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   114
            xml.writeEndElement();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   115
            
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   116
            List<Map<String, ? super Object>> addLaunchers =
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   117
                ADD_LAUNCHERS.fetchFrom(params);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   118
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   119
            for (int i = 0; i < addLaunchers.size(); i++) {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   120
                Map<String, ? super Object> sl = addLaunchers.get(i);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   121
                xml.writeStartElement("add-launcher");
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   122
                xml.writeCharacters(APP_NAME.fetchFrom(sl));
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   123
                xml.writeEndElement();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   124
            }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   125
            
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   126
            xml.writeEndElement();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   127
            xml.writeEndDocument();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   128
            xml.flush();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   129
            xml.close();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   130
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   131
        } catch (XMLStreamException ex) {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   132
            Log.verbose(ex);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   133
            throw new IOException(ex);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   134
        }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   135
    }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   136
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   137
    static AppImageFile load(Path appImageDir) throws IOException {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   138
        try {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   139
            Path path = appImageDir.resolve(XML_FILENAME);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   140
            DocumentBuilderFactory dbf =
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   141
                    DocumentBuilderFactory.newDefaultInstance();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   142
            dbf.setFeature(
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   143
                   "http://apache.org/xml/features/nonvalidating/load-external-dtd",
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   144
                    false);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   145
            DocumentBuilder b = dbf.newDocumentBuilder();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   146
            Document doc = b.parse(new FileInputStream(path.toFile()));
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   147
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   148
            XPath xPath = XPathFactory.newInstance().newXPath();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   149
            
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   150
            String mainLauncher = xpathQueryNullable(xPath,
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   151
                    "/jpackage-state/main-launcher/text()", doc);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   152
            if (mainLauncher == null) {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   153
                // No main launcher, this is fatal.
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   154
                return new AppImageFile();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   155
            }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   156
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   157
            List<String> addLaunchers = new ArrayList<String>();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   158
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   159
            String platform = xpathQueryNullable(xPath,
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   160
                    "/jpackage-state/@platform", doc);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   161
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   162
            String version = xpathQueryNullable(xPath,
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   163
                    "/jpackage-state/@version", doc);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   164
            
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   165
            NodeList launcherNameNodes = (NodeList) xPath.evaluate(
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   166
                    "/jpackage-state/add-launcher/text()", doc,
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   167
                    XPathConstants.NODESET);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   168
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   169
            for (int i = 0; i != launcherNameNodes.getLength(); i++) {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   170
                addLaunchers.add(launcherNameNodes.item(i).getNodeValue());
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   171
            }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   172
            
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   173
            AppImageFile file = new AppImageFile(
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   174
                    mainLauncher, addLaunchers, version, platform);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   175
            if (!file.isValid()) {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   176
                file = new AppImageFile();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   177
            }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   178
            return file;       
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   179
        } catch (ParserConfigurationException | SAXException ex) {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   180
            // Let caller sort this out
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   181
            throw new IOException(ex);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   182
        } catch (XPathExpressionException ex) {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   183
            // This should never happen as XPath expressions should be correct
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   184
            throw new RuntimeException(ex);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   185
        }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   186
    }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   187
    
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   188
    private static String xpathQueryNullable(XPath xPath, String xpathExpr,
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   189
            Document xml) throws XPathExpressionException {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   190
        NodeList nodes = (NodeList) xPath.evaluate(xpathExpr, xml,
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   191
                XPathConstants.NODESET);
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   192
        if (nodes != null && nodes.getLength() > 0) {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   193
            return nodes.item(0).getNodeValue();
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   194
        }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   195
        return null;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   196
    }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   197
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   198
    private static String getVersion() {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   199
        return System.getProperty("java.version");
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   200
    }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   201
    
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   202
    private static String getPlatform() {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   203
        return PLATFORM_LABELS.get(Platform.getPlatform());
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   204
    }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   205
    
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   206
    private boolean isValid() {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   207
        if (launcherName == null || launcherName.length() == 0 ||
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   208
            addLauncherNames.indexOf("") != -1) {
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   209
            // Some launchers have empty names. This is invalid.
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   210
            return false;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   211
        }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   212
        
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   213
        // Add more validation.
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   214
        
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   215
        return true;
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   216
    }
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   217
    
a5f66aa04f68 8230920 : jpackage problems when -input dir contains any files with "cfg" extension.
herrick
parents:
diff changeset
   218
}