1/*
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package javax.xml.catalog;
26
27import java.net.URI;
28import java.util.Locale;
29import java.util.MissingResourceException;
30import java.util.ResourceBundle;
31import jdk.xml.internal.SecuritySupport;
32
33/**
34 * Catalog Error messages
35 *
36 * @since 9
37 */
38final class CatalogMessages {
39
40    public static final String ERR_INVALID_CATALOG = "InvalidCatalog";
41    public static final String ERR_INVALID_ENTRY_TYPE = "InvalidEntryType";
42    public static final String ERR_URI_NOTABSOLUTE = "UriNotAbsolute";
43    public static final String ERR_URI_NOTVALIDURL = "UriNotValidUrl";
44    public static final String ERR_INVALID_ARGUMENT = "InvalidArgument";
45    public static final String ERR_NULL_ARGUMENT = "NullArgument";
46    public static final String ERR_CIRCULAR_REFERENCE = "CircularReference";
47    public static final String ERR_INVALID_PATH = "InvalidPath";
48    public static final String ERR_PARSER_CONF = "ParserConf";
49    public static final String ERR_PARSING_FAILED = "ParsingFailed";
50    public static final String ERR_NO_CATALOG = "NoCatalogFound";
51    public static final String ERR_NO_MATCH = "NoMatchFound";
52    public static final String ERR_NO_URI_MATCH = "NoMatchURIFound";
53    public static final String ERR_CREATING_URI = "FailedCreatingURI";
54    public static final String ERR_OTHER = "OtherError";
55
56    static final String bundleName = CatalogMessages.class.getPackage().getName() + ".CatalogMessages";
57    static ResourceBundle resourceBundle;
58
59    /**
60     * Reports an error.
61     * @param key the message key
62     */
63    static void reportError(String key) {
64        reportError(key, null);
65    }
66
67    /**
68     * Reports an error.
69     * @param key the message key
70     * @param arguments the message replacement text arguments. The order of the
71     * arguments must match that of the placeholders in the actual message.
72     */
73    static void reportError(String key, Object[] arguments) {
74        throw new CatalogException(formatMessage(key, arguments));
75    }
76
77    /**
78     * Reports a CatalogException.
79     * @param key the message key
80     * @param arguments the message replacement text arguments. The order of the
81     * arguments must match that of the placeholders in the actual message.
82     */
83    static void reportRunTimeError(String key, Object[] arguments) {
84        throw new CatalogException(formatMessage(key, arguments));
85    }
86
87    /**
88     * Reports a CatalogException.
89     * @param  key the message key
90     * @param cause the cause if any
91     */
92    static void reportRunTimeError(String key, Throwable cause) {
93        throw new CatalogException(formatMessage(key, null), cause);
94    }
95
96    /**
97     * Reports a CatalogException.
98     * @param  key the message key
99     * @param arguments the message replacement text arguments. The order of the
100     * arguments must match that of the placeholders in the actual message.
101     * @param cause the cause if any
102     */
103    static void reportRunTimeError(String key, Object[] arguments, Throwable cause) {
104        throw new CatalogException(formatMessage(key, arguments), cause);
105    }
106
107    /**
108     * Reports IllegalArgumentException if the argument is null.
109     *
110     * @param name the name of the argument
111     * @param value the value of the argument
112     */
113    static void reportIAEOnNull(String name, String value) {
114        if (value == null) {
115            throw new IllegalArgumentException(
116                    formatMessage(ERR_INVALID_ARGUMENT, new Object[]{null, name}));
117        }
118    }
119
120    /**
121     * Reports NullPointerException if the argument is null.
122     *
123     * @param name the name of the argument
124     * @param value the value of the argument
125     */
126    static void reportNPEOnNull(String name, Object value) {
127        if (value == null) {
128            throw new NullPointerException(
129                    formatMessage(ERR_NULL_ARGUMENT, new Object[]{name}));
130        }
131    }
132
133    /**
134     * Reports IllegalArgumentException
135     * @param arguments the arguments for formating the error message
136     * @param cause the cause if any
137     */
138    static void reportIAE(String key, Object[] arguments, Throwable cause) {
139        throw new IllegalArgumentException(
140                formatMessage(key, arguments), cause);
141    }
142
143    /**
144     * Format a message with the specified arguments using the default locale
145     * information.
146     *
147     * @param key the message key
148     * @param arguments the message replacement text arguments. The order of the
149     * arguments must match that of the placeholders in the actual message.
150     *
151     * @return the formatted message
152     *
153     * @throws MissingResourceException If the message with the specified key
154     * cannot be found
155     */
156    static String formatMessage(String key, Object[] arguments) {
157        return formatMessage(key, arguments, Locale.getDefault());
158    }
159
160    /**
161     * Format a message with the specified arguments using the given locale
162     * information.
163     *
164     * @param key the message key
165     * @param arguments the message replacement text arguments. The order of the
166     * arguments must match that of the placeholders in the actual message.
167     * @param locale the locale of the message
168     *
169     * @return the formatted message
170     *
171     * @throws MissingResourceException If the message with the specified key
172     * cannot be found
173     */
174    static String formatMessage(String key, Object[] arguments, Locale locale) {
175        return SecuritySupport.getErrorMessage(locale, bundleName, key, arguments);
176    }
177
178    /**
179     * Returns sanitized URI.
180     * @param uri a URI to be sanitized
181     */
182    static String sanitize(String uri) {
183        if (uri == null) {
184            return null;
185        }
186        String temp;
187        int p;
188        p = uri.lastIndexOf("/");
189        if (p > 0 && p < uri.length()) {
190            return uri.substring(p + 1);
191        }
192        return uri;
193    }
194}
195