jaxp/src/java.xml/share/classes/javax/xml/catalog/CatalogImpl.java
changeset 43121 e73af7b6ce47
parent 40753 7fdd41fa7d26
child 44380 0197177795e9
equal deleted inserted replaced
43040:ab2c8b03c328 43121:e73af7b6ce47
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    73     //System Id for this catalog
    73     //System Id for this catalog
    74     String systemId;
    74     String systemId;
    75 
    75 
    76     /*
    76     /*
    77      A list of catalog entry files from the input, excluding the current catalog.
    77      A list of catalog entry files from the input, excluding the current catalog.
    78      Paths in the List are normalized.
    78      URIs in the List are verified during input validation or property retrieval.
    79      */
    79      */
    80     List<String> inputFiles;
    80     List<String> inputFiles;
    81 
    81 
    82     //A list of catalogs specified using the nextCatalog element
    82     //A list of catalogs specified using the nextCatalog element
    83     List<NextCatalog> nextCatalogs;
    83     List<NextCatalog> nextCatalogs;
    84 
    84 
    85     //reuse the parser
    85     //reuse the parser
    86     SAXParser parser;
    86     SAXParser parser;
    87 
    87 
    88     /**
    88     /**
    89      * Construct a Catalog with specified path.
    89      * Construct a Catalog with specified URI.
    90      *
    90      *
    91      * @param file The path to a catalog file.
    91      * @param uris the uri(s) to one or more catalogs
    92      * @throws CatalogException If an error happens while parsing the specified
    92      * @throws CatalogException If an error happens while parsing the specified
    93      * catalog file.
    93      * catalog file.
    94      */
    94      */
    95     public CatalogImpl(CatalogFeatures f, String... file) throws CatalogException {
    95     public CatalogImpl(CatalogFeatures f, URI... uris) throws CatalogException {
    96         this(null, f, file);
    96         this(null, f, uris);
    97     }
    97     }
    98 
    98 
    99     /**
    99     /**
   100      * Construct a Catalog with specified path.
   100      * Construct a Catalog with specified URI.
   101      *
   101      *
   102      * @param parent The parent catalog
   102      * @param parent The parent catalog
   103      * @param file The path to a catalog file.
   103      * @param uris the uri(s) to one or more catalogs
   104      * @throws CatalogException If an error happens while parsing the specified
   104      * @throws CatalogException If an error happens while parsing the specified
   105      * catalog file.
   105      * catalog file.
   106      */
   106      */
   107     public CatalogImpl(CatalogImpl parent, CatalogFeatures f, String... file) throws CatalogException {
   107     public CatalogImpl(CatalogImpl parent, CatalogFeatures f, URI... uris) throws CatalogException {
   108         super(CatalogEntryType.CATALOG, parent);
   108         super(CatalogEntryType.CATALOG, parent);
   109         if (f == null) {
   109         if (f == null) {
   110             throw new NullPointerException(
   110             throw new NullPointerException(
   111                     formatMessage(CatalogMessages.ERR_NULL_ARGUMENT, new Object[]{"CatalogFeatures"}));
   111                     formatMessage(CatalogMessages.ERR_NULL_ARGUMENT, new Object[]{"CatalogFeatures"}));
   112         }
   112         }
   113 
   113 
   114         if (file.length > 0) {
       
   115             CatalogMessages.reportNPEOnNull("The path to the catalog file", file[0]);
       
   116         }
       
   117 
       
   118         init(f);
   114         init(f);
   119 
   115 
   120         //Path of catalog files
   116         //Path of catalog files
   121         String[] catalogFile = file;
   117         String[] catalogFile = null;
   122         if (level == 0 && file.length == 0) {
   118         if (level == 0 && uris.length == 0) {
   123             String files = features.get(Feature.FILES);
   119             String files = features.get(Feature.FILES);
   124             if (files != null) {
   120             if (files != null) {
   125                 catalogFile = files.split(";[ ]*");
   121                 catalogFile = files.split(";");
       
   122             }
       
   123         } else {
       
   124             catalogFile = new String[uris.length];
       
   125             for (int i=0; i<uris.length; i++) {
       
   126                 catalogFile[i] = uris[i].toASCIIString();
   126             }
   127             }
   127         }
   128         }
   128 
   129 
   129         /*
   130         /*
   130          In accordance with 8. Resource Failures of the Catalog spec, missing
   131          In accordance with 8. Resource Failures of the Catalog spec, missing
   132          */
   133          */
   133         if ((catalogFile != null && catalogFile.length > 0)) {
   134         if ((catalogFile != null && catalogFile.length > 0)) {
   134             int start = 0;
   135             int start = 0;
   135             URI uri = null;
   136             URI uri = null;
   136             for (String temp : catalogFile) {
   137             for (String temp : catalogFile) {
   137                 uri = getSystemId(temp);
   138                 uri = URI.create(temp);
   138                 start++;
   139                 start++;
   139                 if (verifyCatalogFile(uri)) {
   140                 if (verifyCatalogFile(uri)) {
   140                     systemId = uri.toASCIIString();
   141                     systemId = temp;
   141                     try {
   142                     try {
   142                         baseURI = new URL(systemId);
   143                         baseURI = new URL(systemId);
   143                     } catch (MalformedURLException e) {
   144                     } catch (MalformedURLException e) {
   144                         CatalogMessages.reportRunTimeError(CatalogMessages.ERR_INVALID_PATH,
   145                         CatalogMessages.reportRunTimeError(CatalogMessages.ERR_INVALID_PATH,
   145                                 new Object[]{temp}, e);
   146                                 new Object[]{temp}, e);
   289             CatalogReader reader = new CatalogReader(this, parser);
   290             CatalogReader reader = new CatalogReader(this, parser);
   290             parser.parse(systemId, reader);
   291             parser.parse(systemId, reader);
   291         } catch (SAXException | IOException ex) {
   292         } catch (SAXException | IOException ex) {
   292             CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSING_FAILED, ex);
   293             CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSING_FAILED, ex);
   293         }
   294         }
   294     }
       
   295 
       
   296     /**
       
   297      * Resolves the specified file path to an absolute systemId. If it is
       
   298      * relative, it shall be resolved using the base or user.dir property if
       
   299      * base is not specified.
       
   300      *
       
   301      * @param file The specified file path
       
   302      * @return The systemId of the file
       
   303      * @throws CatalogException if the specified file path can not be converted
       
   304      * to a system id
       
   305      */
       
   306     private URI getSystemId(String file) {
       
   307         URI temp = null;
       
   308 
       
   309         try {
       
   310             temp = Util.verifyAndGetURI(file, baseURI);
       
   311         } catch (MalformedURLException | URISyntaxException | IllegalArgumentException e) {
       
   312             CatalogMessages.reportRunTimeError(CatalogMessages.ERR_INVALID_PATH,
       
   313                     new Object[]{file}, e);
       
   314         }
       
   315 
       
   316         return temp;
       
   317     }
   295     }
   318 
   296 
   319     /**
   297     /**
   320      * Returns a SAXParser instance
   298      * Returns a SAXParser instance
   321      * @return a SAXParser instance
   299      * @return a SAXParser instance
   392                 }
   370                 }
   393 
   371 
   394                 //Check the input list
   372                 //Check the input list
   395                 if (c == null && inputFiles != null) {
   373                 if (c == null && inputFiles != null) {
   396                     while (c == null && inputFilesIndex < inputFiles.size()) {
   374                     while (c == null && inputFilesIndex < inputFiles.size()) {
   397                         c = getCatalog(getSystemId(inputFiles.get(inputFilesIndex++)));
   375                         c = getCatalog(URI.create(inputFiles.get(inputFilesIndex++)));
   398                     }
   376                     }
   399                 }
   377                 }
   400 
   378 
   401                 return c;
   379                 return c;
   402             }
   380             }
   434             });
   412             });
   435         }
   413         }
   436 
   414 
   437         //loads catalogs from the input list
   415         //loads catalogs from the input list
   438         if (inputFiles != null) {
   416         if (inputFiles != null) {
   439             inputFiles.stream().forEach((file) -> {
   417             inputFiles.stream().forEach((uri) -> {
   440                 getCatalog(getSystemId(file));
   418                 getCatalog(URI.create(uri));
   441             });
   419             });
   442         }
   420         }
   443     }
   421     }
   444 
   422 
   445     /**
   423     /**
   452         if (uri == null) {
   430         if (uri == null) {
   453             return null;
   431             return null;
   454         }
   432         }
   455 
   433 
   456         CatalogImpl c = null;
   434         CatalogImpl c = null;
   457         String path = uri.toASCIIString();
       
   458 
   435 
   459         if (verifyCatalogFile(uri)) {
   436         if (verifyCatalogFile(uri)) {
   460             c = getLoadedCatalog(path);
   437             c = getLoadedCatalog(uri.toASCIIString());
   461             if (c == null) {
   438             if (c == null) {
   462                 c = new CatalogImpl(this, features, path);
   439                 c = new CatalogImpl(this, features, uri);
   463                 c.load();
   440                 c.load();
   464             }
   441             }
   465         }
   442         }
   466         return c;
   443         return c;
   467     }
   444     }