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.MalformedURLException;
28import java.net.URL;
29import java.util.Objects;
30import static javax.xml.catalog.CatalogMessages.ERR_INVALID_ARGUMENT;
31
32/**
33 * Represents a general Catalog entry.
34 *
35 * @since 9
36 */
37abstract class BaseEntry {
38    final String SLASH = "/";
39
40    CatalogEntryType type;
41
42    //The id attribute
43    String id;
44
45    //The attribute to be matched, e.g. systemId
46    String matchId;
47
48    //The baseURI attribute
49    URL baseURI;
50
51    //Indicates whether the base attribute is specified
52    boolean baseSpecified = false;
53
54    /**
55     * CatalogEntryType represents catalog entry types.
56     */
57    static enum CatalogEntryType {
58
59        CATALOG("catalogfile"),
60        CATALOGENTRY("catalog"),
61        GROUP("group"),
62        PUBLIC("public"),
63        SYSTEM("system"),
64        REWRITESYSTEM("rewriteSystem"),
65        SYSTEMSUFFIX("systemSuffix"),
66        DELEGATEPUBLIC("delegatePublic"),
67        DELEGATESYSTEM("delegateSystem"),
68        URI("uri"),
69        REWRITEURI("rewriteURI"),
70        URISUFFIX("uriSuffix"),
71        DELEGATEURI("delegateURI"),
72        NEXTCATALOG("nextCatalog");
73
74        final String literal;
75
76        CatalogEntryType(String literal) {
77            this.literal = literal;
78        }
79
80        public boolean isType(String type) {
81            return literal.equals(type);
82        }
83
84        static public CatalogEntryType getType(String entryType) {
85            for (CatalogEntryType type : CatalogEntryType.values()) {
86                if (type.isType(entryType)) {
87                    return type;
88                }
89            }
90            return null;
91        }
92    }
93
94    /**
95     * Constructs a CatalogEntry
96     *
97     * @param type The type of the entry
98     */
99    public BaseEntry(CatalogEntryType type) {
100        this.type = Objects.requireNonNull(type);
101    }
102
103    /**
104     * Constructs a CatalogEntry
105     *
106     * @param type The type of the entry
107     * @param base The base URI
108     */
109    public BaseEntry(CatalogEntryType type, String base) {
110        this.type = Objects.requireNonNull(type);
111        setBaseURI(base);
112    }
113
114    /**
115     * Returns the type of the entry
116     *
117     * @return The type of the entry
118     */
119    public CatalogEntryType getType() {
120        return type;
121    }
122
123    /**
124     * Sets the entry type
125     *
126     * @param type The entry type
127     */
128    public void setType(CatalogEntryType type) {
129        this.type = type;
130    }
131
132    /**
133     * Returns the id of the entry
134     *
135     * @return The id of the entry
136     */
137    public String getId() {
138        return id;
139    }
140
141    /**
142     * Set the entry Id
143     *
144     * @param id The Id attribute
145     */
146    public void setId(String id) {
147        this.id = id;
148    }
149
150    /**
151     * Sets the base URI for the entry
152     *
153     * @param base The base URI
154     */
155    public final void setBaseURI(String base) {
156        baseURI = verifyURI("base", null, base);
157    }
158
159    /**
160     * Gets the base URI for the entry
161     *
162     * @return The base URI as a string.
163     */
164    public URL getBaseURI() {
165        return baseURI;
166    }
167
168    /**
169     * Gets the attribute used for matching
170     *
171     * @return The value of the field
172     */
173    public String getMatchId() {
174        return matchId;
175    }
176
177    /**
178     * Sets the matchId field
179     * @param matchId The value of the Id
180     */
181    public void setMatchId(String matchId) {
182        this.matchId = matchId;
183    }
184
185    /**
186     * Matches the specified string with the identifier attribute of the entry.
187     *
188     * @param match The identifier attribute to be matched
189     * @return The replacement URI if a matching entry is found, null if not.
190     */
191    public String match(String match) {
192        return null;
193    }
194
195    /**
196     * Try to match the specified id with the entry. Return the match if it
197     * is successful and the length of the start String is longer than the
198     * longest of any previous match.
199     *
200     * @param id The id to be matched.
201     * @param currentMatch The length of start String of previous match if any.
202     * @return The replacement URI if the match is successful, null if not.
203     */
204    public String match(String id, int currentMatch) {
205        return null;
206    }
207
208    /**
209     * Verifies the specified URI.
210     *
211     * @param arg The name of the argument
212     * @param uri The URI to be verified
213     * @return The URI created from the specified uri
214     * @throws NullPointerException if the specified uri is null
215     * @throws IllegalArgumentException if a URL can not be created based on
216     * the specified base and uri
217     */
218    URL verifyURI(String arg, URL base, String uri) {
219        CatalogMessages.reportNPEOnNull(arg, uri);
220
221        URL url = null;
222        uri = Normalizer.normalizeURI(uri);
223
224        try {
225            if (base != null) {
226                url = new URL(base, uri);
227            } else {
228                url = new URL(uri);
229            }
230        } catch (MalformedURLException e) {
231            CatalogMessages.reportIAE(ERR_INVALID_ARGUMENT,
232                    new Object[]{uri, arg}, e);
233        }
234        return url;
235    }
236}
237