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.io.StringReader;
  28 import java.util.Iterator;
  29 import org.xml.sax.InputSource;
  30 
  31 /**
  32  * A SAX EntityResolver/JAXP URIResolver that uses catalogs.
  33  *
  34  * <p>
  35  * This class implements both a SAX EntityResolver and a JAXP URIResolver.
  36  *
  37  *
  38  * @since 9
  39  */
  40 final class CatalogResolverImpl implements CatalogResolver {
  41     Catalog catalog;
  42 
  43     /**
  44      * Construct an instance of the CatalogResolver from a Catalog.
  45      *
  46      * @param catalog A Catalog.
  47      */
  48     public CatalogResolverImpl(Catalog catalog) {
  49         this.catalog = catalog;
  50     }
  51 
  52     @Override
  53     public InputSource resolveEntity(String publicId, String systemId) {
  54         CatalogMessages.reportNPEOnNull("systemId", systemId);
  55         //Normalize publicId and systemId
  56         systemId = Normalizer.normalizeURI(Util.getNotNullOrEmpty(systemId));
  57         publicId = Normalizer.normalizePublicId(Normalizer.decodeURN(Util.getNotNullOrEmpty(publicId)));
  58 
  59         //check whether systemId is an urn
  60         if (systemId != null && systemId.startsWith(Util.URN)) {
  61             systemId = Normalizer.decodeURN(systemId);
  62             if (publicId != null && !publicId.equals(systemId)) {
  63                 systemId = null;
  64             } else {
  65                 publicId = systemId;
  66                 systemId = null;
  67             }
  68         }
  69 
  70         CatalogImpl c = (CatalogImpl)catalog;
  71         String resolvedSystemId = Util.resolve(c, publicId, systemId);
  72 
  73         if (resolvedSystemId != null) {
  74             return new InputSource(resolvedSystemId);
  75         }
  76 
  77         GroupEntry.ResolveType resolveType = ((CatalogImpl) catalog).getResolve();
  78         switch (resolveType) {
  79             case IGNORE:
  80                 return new InputSource(new StringReader(""));
  81             case STRICT:
  82                 CatalogMessages.reportError(CatalogMessages.ERR_NO_MATCH,
  83                         new Object[]{publicId, systemId});
  84         }
  85 
  86         //no action, allow the parser to continue
  87         return null;
  88     }
  89 
  90 }