BaseEntry.java revision 1060:8c9a2a24752b
112048Speter/*
212048Speter * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
312048Speter * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
412048Speter *
512048Speter * This code is free software; you can redistribute it and/or modify it
612048Speter * under the terms of the GNU General Public License version 2 only, as
712048Speter * published by the Free Software Foundation.  Oracle designates this
812048Speter * particular file as subject to the "Classpath" exception as provided
912048Speter * by Oracle in the LICENSE file that accompanied this code.
1012048Speter *
1112048Speter * This code is distributed in the hope that it will be useful, but WITHOUT
1212048Speter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1312048Speter * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1412048Speter * version 2 for more details (a copy is included in the LICENSE file that
1512048Speter * accompanied this code).
1612048Speter *
1712048Speter * You should have received a copy of the GNU General Public License version
1812048Speter * 2 along with this work; if not, write to the Free Software Foundation,
1912048Speter * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2012048Speter *
2112048Speter * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2212048Speter * or visit www.oracle.com if you need additional information or have any
2312048Speter * questions.
2412048Speter */
2512048Speterpackage javax.xml.catalog;
2612048Speter
2712048Speterimport java.net.MalformedURLException;
2812048Speterimport java.net.URL;
2912048Speterimport java.util.Objects;
3012048Speterimport static javax.xml.catalog.CatalogMessages.ERR_INVALID_ARGUMENT;
3112048Speter
3237001Scharnier/**
3350476Speter * Represents a general Catalog entry.
3412048Speter *
3512048Speter * @since 9
3674594Salfred */
3712048Speterabstract class BaseEntry {
3837001Scharnier    final String SLASH = "/";
3912048Speter
4012048Speter    CatalogEntryType type;
4112048Speter
4237001Scharnier    //The id attribute
4312048Speter    String id;
4412048Speter
4574594Salfred    //The attribute to be matched, e.g. systemId
4612048Speter    String matchId;
4789827Sjoerg
4889827Sjoerg    //The baseURI attribute
4912048Speter    URL baseURI;
5012048Speter
5112048Speter    //Indicates whether the base attribute is specified
5292881Simp    boolean baseSpecified = false;
5398542Smckusick
5498542Smckusick    /**
5589827Sjoerg     * CatalogEntryType represents catalog entry types.
5612048Speter     */
5792881Simp    static enum CatalogEntryType {
5812048Speter
5912048Speter        CATALOG("catalogfile"),
6012048Speter        CATALOGENTRY("catalog"),
6112048Speter        GROUP("group"),
6212048Speter        PUBLIC("public"),
6312048Speter        SYSTEM("system"),
6412048Speter        REWRITESYSTEM("rewriteSystem"),
6512048Speter        SYSTEMSUFFIX("systemSuffix"),
6612048Speter        DELEGATEPUBLIC("delegatePublic"),
6712048Speter        DELEGATESYSTEM("delegateSystem"),
6812048Speter        URI("uri"),
6912048Speter        REWRITEURI("rewriteURI"),
7012048Speter        URISUFFIX("uriSuffix"),
7112048Speter        DELEGATEURI("delegateURI"),
7212048Speter        NEXTCATALOG("nextCatalog");
7312048Speter
7489791Sgreen        final String literal;
7592881Simp
7689791Sgreen        CatalogEntryType(String literal) {
7789791Sgreen            this.literal = literal;
7889791Sgreen        }
7989791Sgreen
8089791Sgreen        public boolean isType(String type) {
8189791Sgreen            return literal.equals(type);
8289791Sgreen        }
8389791Sgreen
8489791Sgreen        static public CatalogEntryType getType(String entryType) {
8589791Sgreen            for (CatalogEntryType type : CatalogEntryType.values()) {
8689791Sgreen                if (type.isType(entryType)) {
8789791Sgreen                    return type;
8889791Sgreen                }
8989791Sgreen            }
9089791Sgreen            return null;
9189791Sgreen        }
9289791Sgreen    }
9389791Sgreen
9412048Speter    /**
9592881Simp     * Constructs a CatalogEntry
9612048Speter     *
9712048Speter     * @param type The type of the entry
9889791Sgreen     */
9989791Sgreen    public BaseEntry(CatalogEntryType type) {
10012048Speter        this.type = Objects.requireNonNull(type);
10112048Speter    }
10212048Speter
10312048Speter    /**
10412048Speter     * Constructs a CatalogEntry
10512048Speter     *
10612048Speter     * @param type The type of the entry
10712048Speter     * @param base The base URI
10812048Speter     */
10998542Smckusick    public BaseEntry(CatalogEntryType type, String base) {
11012048Speter        this.type = Objects.requireNonNull(type);
11112048Speter        setBaseURI(base);
11212048Speter    }
11398542Smckusick
11498542Smckusick    /**
11512048Speter     * Returns the type of the entry
11623854Sbde     *
11712048Speter     * @return The type of the entry
11812048Speter     */
11998542Smckusick    public CatalogEntryType getType() {
12012048Speter        return type;
12112048Speter    }
12212048Speter
12312048Speter    /**
12412048Speter     * Sets the entry type
12512048Speter     *
12612048Speter     * @param type The entry type
12712048Speter     */
12898542Smckusick    public void setType(CatalogEntryType type) {
12912048Speter        this.type = type;
13012048Speter    }
13112048Speter
13298542Smckusick    /**
13312048Speter     * Returns the id of the entry
13412048Speter     *
13512048Speter     * @return The id of the entry
13698542Smckusick     */
13798542Smckusick    public String getId() {
13898542Smckusick        return id;
13998542Smckusick    }
14098542Smckusick
14198542Smckusick    /**
14298542Smckusick     * Set the entry Id
14398542Smckusick     *
14498542Smckusick     * @param id The Id attribute
14598542Smckusick     */
14698542Smckusick    public void setId(String id) {
14712048Speter        this.id = id;
14812048Speter    }
14912048Speter
15012048Speter    /**
15112048Speter     * Sets the base URI for the entry
15212048Speter     *
15312048Speter     * @param base The base URI
15412048Speter     */
15598542Smckusick    public final void setBaseURI(String base) {
15698542Smckusick        baseURI = verifyURI("base", null, base);
15798542Smckusick    }
15898542Smckusick
15998542Smckusick    /**
16098542Smckusick     * Gets the base URI for the entry
16123854Sbde     *
16212048Speter     * @return The base URI as a string.
16398542Smckusick     */
16498542Smckusick    public URL getBaseURI() {
16598542Smckusick        return baseURI;
16698542Smckusick    }
16798542Smckusick
16823854Sbde    /**
16912048Speter     * Gets the attribute used for matching
17098542Smckusick     *
17198542Smckusick     * @return The value of the field
17298542Smckusick     */
17398542Smckusick    public String getMatchId() {
17498542Smckusick        return matchId;
17523854Sbde    }
17612048Speter
17798542Smckusick    /**
17812048Speter     * Sets the matchId field
17998542Smckusick     * @param matchId The value of the Id
18012048Speter     */
18112048Speter    public void setMatchId(String matchId) {
18298542Smckusick        this.matchId = matchId;
18398542Smckusick    }
18412048Speter
18512048Speter    /**
18698542Smckusick     * Matches the specified string with the identifier attribute of the entry.
18712048Speter     *
18898542Smckusick     * @param match The identifier attribute to be matched
18998542Smckusick     * @return The replacement URI if a matching entry is found, null if not.
19098542Smckusick     */
19198542Smckusick    public String match(String match) {
19212048Speter        return null;
19312048Speter    }
19489827Sjoerg
19589827Sjoerg    /**
19689827Sjoerg     * Try to match the specified id with the entry. Return the match if it
19789827Sjoerg     * is successful and the length of the start String is longer than the
19889827Sjoerg     * longest of any previous match.
19989827Sjoerg     *
20089827Sjoerg     * @param id The id to be matched.
20192881Simp     * @param currentMatch The length of start String of previous match if any.
20289827Sjoerg     * @return The replacement URI if the match is successful, null if not.
20389827Sjoerg     */
20489827Sjoerg    public String match(String id, int currentMatch) {
20589827Sjoerg        return null;
20689827Sjoerg    }
20789827Sjoerg
20889827Sjoerg    /**
20989827Sjoerg     * Verifies the specified URI.
21089827Sjoerg     *
21189827Sjoerg     * @param arg The name of the argument
21289827Sjoerg     * @param uri The URI to be verified
21389827Sjoerg     * @return The URI created from the specified uri
21489827Sjoerg     * @throws NullPointerException if the specified uri is null
21589827Sjoerg     * @throws IllegalArgumentException if a URL can not be created based on
21689827Sjoerg     * the specified base and uri
21789827Sjoerg     */
21889827Sjoerg    URL verifyURI(String arg, URL base, String uri) {
21989827Sjoerg        CatalogMessages.reportNPEOnNull(arg, uri);
22089827Sjoerg
22189827Sjoerg        URL url = null;
22298542Smckusick        uri = Normalizer.normalizeURI(uri);
22389827Sjoerg
22489827Sjoerg        try {
22598542Smckusick            if (base != null) {
22689827Sjoerg                url = new URL(base, uri);
22798542Smckusick            } else {
22889827Sjoerg                url = new URL(uri);
22989827Sjoerg            }
23089827Sjoerg        } catch (MalformedURLException e) {
23189827Sjoerg            CatalogMessages.reportIAE(ERR_INVALID_ARGUMENT,
23289827Sjoerg                    new Object[]{uri, arg}, e);
23389827Sjoerg        }
23489827Sjoerg        return url;
23589827Sjoerg    }
23689827Sjoerg}
23789827Sjoerg