src/demo/share/jpackager/JNLPConverter/src/jnlp/converter/parser/XMLUtils.java
author herrick
Fri, 12 Oct 2018 19:00:51 -0400
branchJDK-8200758-branch
changeset 56963 eaca4369b068
permissions -rw-r--r--
8198472: Add support for creating bundles from JNLP files Submitten-by: almatvee Reviewed-by: herrick, kcr, prr, asemenyuk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56963
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     1
/*
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     2
 * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     4
 *
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     8
 *
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    13
 * accompanied this code).
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    14
 *
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    18
 *
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    21
 * questions.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    22
 */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    23
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    24
package jnlp.converter.parser;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    25
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    26
import java.net.URL;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    27
import java.net.MalformedURLException;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    28
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    29
import jnlp.converter.parser.exception.BadFieldException;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    30
import jnlp.converter.parser.exception.MissingFieldException;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    31
import jnlp.converter.parser.xml.XMLNode;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    32
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    33
/** Contains handy methods for looking up information
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    34
 *  stored in XMLNodes.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    35
 */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    36
public class XMLUtils {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    37
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    38
    /** Returns the value of an integer attribute */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    39
    public static int getIntAttribute(String source, XMLNode root, String path, String name, int defaultvalue)
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    40
            throws BadFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    41
        String value = getAttribute(root, path, name);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    42
        if (value == null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    43
            return defaultvalue;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    44
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    45
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    46
        try {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    47
            return Integer.parseInt(value);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    48
        } catch (NumberFormatException nfe) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    49
            throw new BadFieldException(source, getPathString(root) + path + name, value);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    50
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    51
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    52
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    53
    /** Returns the value of a given attribute, or null if not set */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    54
    public static String getAttribute(XMLNode root, String path, String name)
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    55
                                                      throws BadFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    56
        return getAttribute(root, path, name, null);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    57
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    58
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    59
    /** Returns the value of a given attribute */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    60
    public static String getRequiredAttributeEmptyOK(String source,
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    61
        XMLNode root, String path, String name) throws MissingFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    62
        String value = null;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    63
        XMLNode elem = findElementPath(root, path);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    64
        if (elem != null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    65
            value = elem.getAttribute(name);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    66
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    67
        if (value == null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    68
            throw new MissingFieldException(source,
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    69
                                            getPathString(root)+ path + name);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    70
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    71
        return value;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    72
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    73
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    74
    /*** Returns the value of an attribute, which must be a valid class name */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    75
    public static String getClassName(String source, XMLNode root,
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    76
        String path, String name, boolean required)
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    77
            throws BadFieldException, MissingFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    78
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    79
        String className;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    80
        if (required) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    81
            className = getRequiredAttribute(source, root, path, name);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    82
        } else {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    83
            className = getAttribute(root, path, name);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    84
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    85
        if (className != null && className.endsWith(".class")) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    86
            int i = className.lastIndexOf(".class");
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    87
            String cname = className.substring(0, i);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    88
            return cname;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    89
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    90
        return className;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    91
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    92
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    93
    /** Returns the value of a given attribute, or null if not set */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    94
    public static String getRequiredAttribute(String source, XMLNode root,
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    95
            String path, String name) throws MissingFieldException, BadFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    96
        String s = getAttribute(root, path, name, null);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    97
        if (s == null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    98
            throw new MissingFieldException(source, getPathString(root) + path + name);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    99
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   100
        s = s.trim();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   101
        return (s.length() == 0) ? null : s;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   102
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   103
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   104
    /** Returns the value of a given attribute, or the default value 'def' if not set */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   105
    public static String getAttribute(XMLNode root, String path, String name,
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   106
            String def) throws BadFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   107
        XMLNode elem = findElementPath(root, path);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   108
        if (elem == null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   109
            return def;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   110
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   111
        String value = elem.getAttribute(name);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   112
        return (value == null || value.length() == 0) ? def : value;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   113
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   114
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   115
    /** Expands a URL into an absolute URL from a relative URL */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   116
    public static URL getAttributeURL(String source, URL base, XMLNode root, String path, String name) throws BadFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   117
        String value = getAttribute(root, path, name);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   118
        if (value == null) return null;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   119
        try {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   120
            if (value.startsWith("jar:")) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   121
                int bang = value.indexOf("!/");
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   122
                if (bang > 0) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   123
                    String entry = value.substring(bang);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   124
                    String urlString = value.substring(4, bang);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   125
                    URL url = (base == null) ?
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   126
                        new URL(urlString) : new URL(base, urlString);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   127
                    return new URL("jar:" + url.toString() + entry);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   128
                }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   129
            }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   130
            return (base == null) ? new URL(value) : new URL(base, value);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   131
        } catch(MalformedURLException mue) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   132
            if (mue.getMessage().contains("https")) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   133
                throw new BadFieldException(source, "<jnlp>", "https");
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   134
            }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   135
            throw new BadFieldException(source, getPathString(root) + path + name, value);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   136
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   137
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   138
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   139
    /** Returns the value of an attribute as a URL or null if not set */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   140
    public static URL getAttributeURL(String source, XMLNode root, String path, String name) throws BadFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   141
        return getAttributeURL(source, null, root, path, name);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   142
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   143
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   144
    public static URL getRequiredURL(String source, URL base, XMLNode root, String path, String name) throws BadFieldException, MissingFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   145
        URL url = getAttributeURL(source, base, root, path, name);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   146
        if (url == null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   147
            throw new MissingFieldException(source, getPathString(root) + path + name);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   148
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   149
        return url;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   150
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   151
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   152
    /** Returns the value of an attribute as a URL. Throws a MissingFieldException if the
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   153
     *  attribute is not defined
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   154
     */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   155
    public static URL getRequiredURL(String source, XMLNode root, String path, String name) throws BadFieldException, MissingFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   156
        return getRequiredURL(source, null, root, path, name);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   157
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   158
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   159
    /** Returns true if the path exists in the document, otherwise false */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   160
    public static boolean isElementPath(XMLNode root, String path) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   161
        return findElementPath(root, path) != null;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   162
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   163
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   164
    public static URL getElementURL(String source, XMLNode root, String path) throws BadFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   165
        String value = getElementContents(root, path);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   166
        try {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   167
            return new URL(value);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   168
        } catch(MalformedURLException mue) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   169
            throw new BadFieldException(source, getPathString(root) + path, value);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   170
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   171
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   172
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   173
    /** Returns a string describing the current location in the DOM */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   174
    public static String getPathString(XMLNode e) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   175
        return (e == null || !(e.isElement())) ? "" : getPathString(e.getParent()) + "<" + e.getName() + ">";
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   176
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   177
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   178
    /** Returns the contents of an element with the given path and an attribute matching a specific value. Returns
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   179
     *  NULL if not found
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   180
     */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   181
    public static String getElementContentsWithAttribute(XMLNode root, String path, String attr, String val, String defaultvalue)
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   182
            throws BadFieldException, MissingFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   183
        XMLNode e = getElementWithAttribute(root, path, attr, val);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   184
        if (e == null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   185
            return defaultvalue;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   186
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   187
        return getElementContents(e, "", defaultvalue);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   188
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   189
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   190
    public static URL getAttributeURLWithAttribute(String source, XMLNode root, String path, String attrcond, String val,
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   191
            String name, URL defaultvalue)
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   192
            throws BadFieldException, MissingFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   193
        XMLNode e = getElementWithAttribute(root, path, attrcond, val);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   194
        if (e == null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   195
            return defaultvalue;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   196
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   197
        URL url = getAttributeURL(source, e, "", name);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   198
        if (url == null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   199
            return defaultvalue;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   200
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   201
        return url;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   202
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   203
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   204
    /** Returns an element with the given path and an attribute matching a specific value. Returns
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   205
     *  NULL if not found
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   206
     */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   207
    public static XMLNode getElementWithAttribute(XMLNode root, String path, final String attr, final String val)
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   208
            throws BadFieldException, MissingFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   209
        final XMLNode[] result = {null};
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   210
        visitElements(root, path, new ElementVisitor() {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   211
            public void visitElement(XMLNode e) throws BadFieldException, MissingFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   212
                if (result[0] == null && e.getAttribute(attr).equals(val)) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   213
                    result[0] = e;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   214
                }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   215
            }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   216
        });
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   217
        return result[0];
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   218
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   219
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   220
    /** Like getElementContents(...) but with a defaultValue of null */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   221
    public static String getElementContents(XMLNode root, String path) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   222
        return getElementContents(root, path, null);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   223
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   224
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   225
    /** Returns the value of the last element tag in the path, e.g.,  <..><tag>value</tag>. The DOM is assumes
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   226
     *  to be normalized. If no value is found, the defaultvalue is returned
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   227
     */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   228
    public static String getElementContents(XMLNode root, String path, String defaultvalue) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   229
        XMLNode e = findElementPath(root, path);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   230
        if (e == null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   231
            return defaultvalue;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   232
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   233
        XMLNode n = e.getNested();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   234
        if (n != null && !n.isElement()) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   235
            return n.getName();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   236
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   237
        return defaultvalue;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   238
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   239
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   240
    /** Parses a path string of the form <tag1><tag2><tag3> and returns the specific Element
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   241
     *  node for that tag, or null if it does not exist. If multiple elements exists with same
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   242
     *  path the first is returned
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   243
     */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   244
    public static XMLNode findElementPath(XMLNode elem, String path) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   245
        // End condition. Root null -> path does not exist
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   246
        if (elem == null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   247
            return null;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   248
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   249
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   250
        // End condition. String empty, return current root
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   251
        if (path == null || path.length() == 0) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   252
            return elem;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   253
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   254
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   255
        // Strip of first tag
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   256
        int idx = path.indexOf('>');
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   257
        if (!(path.charAt(0) == '<')) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   258
            throw new IllegalArgumentException("bad path. Missing begin tag");
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   259
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   260
        if (idx == -1) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   261
            throw new IllegalArgumentException("bad path. Missing end tag");
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   262
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   263
        String head = path.substring(1, idx);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   264
        String tail = path.substring(idx + 1);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   265
        return findElementPath(findChildElement(elem, head), tail);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   266
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   267
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   268
    /** Returns an child element with the current tag name or null. */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   269
    public static XMLNode findChildElement(XMLNode elem, String tag) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   270
        XMLNode n = elem.getNested();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   271
        while (n != null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   272
            if (n.isElement() && n.getName().equals(tag)) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   273
                return n;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   274
            }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   275
            n = n.getNext();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   276
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   277
        return null;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   278
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   279
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   280
    /** Iterator class */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   281
    public abstract static class ElementVisitor {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   282
        abstract public void visitElement(XMLNode e) throws BadFieldException, MissingFieldException;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   283
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   284
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   285
    /** Visits all elements which matches the <path>. The iteration is only
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   286
     *  done on the last element in the path.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   287
     */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   288
    public static void visitElements(XMLNode root, String path, ElementVisitor ev)
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   289
            throws BadFieldException, MissingFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   290
        // Get last element in path
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   291
        int idx = path.lastIndexOf('<');
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   292
        if (idx == -1) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   293
            throw new IllegalArgumentException(
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   294
                    "bad path. Must contain atleast one tag");
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   295
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   296
        if (path.length() == 0 || path.charAt(path.length() - 1) != '>') {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   297
            throw new IllegalArgumentException("bad path. Must end with a >");
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   298
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   299
        String head = path.substring(0, idx);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   300
        String tag = path.substring(idx + 1, path.length() - 1);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   301
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   302
        XMLNode elem = findElementPath(root, head);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   303
        if (elem == null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   304
            return;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   305
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   306
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   307
        // Iterate through all child nodes
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   308
        XMLNode n = elem.getNested();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   309
        while (n != null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   310
            if (n.isElement() && n.getName().equals(tag)) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   311
                ev.visitElement(n);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   312
            }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   313
            n = n.getNext();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   314
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   315
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   316
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   317
    public static void visitChildrenElements(XMLNode elem, ElementVisitor ev)
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   318
            throws BadFieldException, MissingFieldException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   319
        // Iterate through all child nodes
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   320
        XMLNode n = elem.getNested();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   321
        while (n != null) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   322
            if (n.isElement()) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   323
                ev.visitElement(n);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   324
            }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   325
            n = n.getNext();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   326
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   327
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   328
}