CatalogMessages.java revision 808:d6dcb5df3d6d
1/*
2 * Copyright (c) 2015, 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 jdk.xml.internal.SecuritySupport;
28import java.util.Locale;
29import java.util.MissingResourceException;
30import java.util.ResourceBundle;
31
32/**
33 * Catalog Error messages
34 *
35 * @since 9
36 */
37final class CatalogMessages {
38
39    public static final String ERR_INVALID_CATALOG = "InvalidCatalog";
40    public static final String ERR_INVALID_ENTRY_TYPE = "InvalidEntryType";
41    public static final String ERR_INVALID_ARGUMENT = "InvalidArgument";
42    public static final String ERR_NULL_ARGUMENT = "NullArgument";
43    public static final String ERR_CIRCULAR_REFERENCE = "CircularReference";
44    public static final String ERR_INVALID_PATH = "InvalidPath";
45    public static final String ERR_PARSER_CONF = "ParserConf";
46    public static final String ERR_PARSING_FAILED = "ParsingFailed";
47    public static final String ERR_NO_CATALOG = "NoCatalogFound";
48    public static final String ERR_NO_MATCH = "NoMatchFound";
49    public static final String ERR_NO_URI_MATCH = "NoMatchURIFound";
50    public static final String ERR_CREATING_URI = "FailedCreatingURI";
51    public static final String ERR_OTHER = "OtherError";
52
53    static final String bundleName = CatalogMessages.class.getPackage().getName() + ".CatalogMessages";
54    static ResourceBundle resourceBundle;
55
56    /**
57     * Reports an error.
58     * @param key the message key
59     */
60    static void reportError(String key) {
61        reportError(key, null);
62    }
63
64    /**
65     * Reports an error.
66     * @param key the message key
67     * @param arguments the message replacement text arguments. The order of the
68     * arguments must match that of the placeholders in the actual message.
69     */
70    static void reportError(String key, Object[] arguments) {
71        throw new CatalogException(formatMessage(key, arguments));
72    }
73
74    /**
75     * Reports a CatalogException.
76     * @param key the message key
77     * @param arguments the message replacement text arguments. The order of the
78     * arguments must match that of the placeholders in the actual message.
79     */
80    static void reportRunTimeError(String key, Object[] arguments) {
81        throw new CatalogException(formatMessage(key, arguments));
82    }
83
84    /**
85     * Reports a CatalogException.
86     * @param  key the message key
87     * @param cause the cause if any
88     */
89    static void reportRunTimeError(String key, Throwable cause) {
90        throw new CatalogException(formatMessage(key, null), cause);
91    }
92
93    /**
94     * Reports a CatalogException.
95     * @param  key the message key
96     * @param arguments the message replacement text arguments. The order of the
97     * arguments must match that of the placeholders in the actual message.
98     * @param cause the cause if any
99     */
100    static void reportRunTimeError(String key, Object[] arguments, Throwable cause) {
101        throw new CatalogException(formatMessage(key, arguments), cause);
102    }
103
104    /**
105     * Reports IllegalArgumentException if the argument is null.
106     *
107     * @param name the name of the argument
108     * @param value the value of the argument
109     */
110    static void reportIAEOnNull(String name, String value) {
111        if (value == null) {
112            throw new IllegalArgumentException(
113                    formatMessage(ERR_INVALID_ARGUMENT, new Object[]{null, name}));
114        }
115    }
116
117    /**
118     * Reports NullPointerException if the argument is null.
119     *
120     * @param name the name of the argument
121     * @param value the value of the argument
122     */
123    static void reportNPEOnNull(String name, String value) {
124        if (value == null) {
125            throw new NullPointerException(
126                    formatMessage(ERR_NULL_ARGUMENT, new Object[]{name}));
127        }
128    }
129
130    /**
131     * Reports IllegalArgumentException
132     * @param arguments the arguments for formating the error message
133     * @param cause the cause if any
134     */
135    static void reportIAE(Object[] arguments, Throwable cause) {
136        throw new IllegalArgumentException(
137                formatMessage(ERR_INVALID_ARGUMENT, arguments), cause);
138    }
139
140    /**
141     * Format a message with the specified arguments using the default locale
142     * information.
143     *
144     * @param key the message key
145     * @param arguments the message replacement text arguments. The order of the
146     * arguments must match that of the placeholders in the actual message.
147     *
148     * @return the formatted message
149     *
150     * @throws MissingResourceException If the message with the specified key
151     * cannot be found
152     */
153    static String formatMessage(String key, Object[] arguments) {
154        return formatMessage(key, arguments, Locale.getDefault());
155    }
156
157    /**
158     * Format a message with the specified arguments using the given locale
159     * information.
160     *
161     * @param key the message key
162     * @param arguments the message replacement text arguments. The order of the
163     * arguments must match that of the placeholders in the actual message.
164     * @param locale the locale of the message
165     *
166     * @return the formatted message
167     *
168     * @throws MissingResourceException If the message with the specified key
169     * cannot be found
170     */
171    static String formatMessage(String key, Object[] arguments, Locale locale) {
172        return SecuritySupport.getErrorMessage(locale, bundleName, key, arguments);
173    }
174
175    /**
176     * Returns sanitized URI.
177     * @param uri an URI to be sanitized
178     */
179    static String sanitize(String uri) {
180        if (uri == null) {
181            return null;
182        }
183        String temp;
184        int p;
185        p = uri.lastIndexOf("/");
186        if (p > 0 && p < uri.length()) {
187            return uri.substring(p + 1);
188        }
189        return uri;
190    }
191}
192