1 /*
   2  * Copyright (c) 2015, 2016, 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  */
  25 package javax.xml.catalog;
  26 

  27 import java.net.MalformedURLException;


  28 import java.net.URL;
  29 import java.util.Objects;

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












 237      * Construct an absolute URI from a relative one, using the current base
 238      * URI.
 239      *
 240      * @param sysid The (possibly relative) system identifier
 241      * @return The system identifier made absolute with respect to the current
 242      * {@link #base}.
 243      */
 244     protected String makeAbsolute(String sysid) {
 245         URL local = null;
 246 
 247         sysid = Util.fixSlashes(sysid);
 248         /**
 249          * try { local = new URL(base, sysid); } catch (MalformedURLException e)
 250          * { catalogManager.debug.message(1, "Malformed URL on system
 251          * identifier", sysid); }
 252          */
 253         if (local != null) {
 254             return local.toString();
 255         } else {
 256             return sysid;
 257         }
 258     }
 259 }
--- EOF ---