jaxp/src/java.xml/share/classes/javax/xml/catalog/CatalogImpl.java
author joehw
Thu, 29 Oct 2015 21:53:21 -0700
changeset 33542 9f0eef87e8c1
child 34984 48a409c654e9
permissions -rw-r--r--
8081248: Implement JEP 268: XML Catalog API Reviewed-by: lancea, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     1
/*
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     4
 *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    10
 *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    15
 * accompanied this code).
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    16
 *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    20
 *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    23
 * questions.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    24
 */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    25
package javax.xml.catalog;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    26
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    27
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    28
import java.io.File;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    29
import java.io.IOException;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    30
import java.net.MalformedURLException;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    31
import java.net.URI;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    32
import java.net.URISyntaxException;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    33
import java.net.URL;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    34
import java.util.ArrayList;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    35
import java.util.Collections;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    36
import java.util.Iterator;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    37
import java.util.List;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    38
import java.util.NoSuchElementException;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    39
import java.util.Spliterator;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    40
import java.util.Spliterators;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    41
import java.util.stream.Stream;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    42
import java.util.stream.StreamSupport;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    43
import static javax.xml.catalog.BaseEntry.CatalogEntryType;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    44
import static javax.xml.catalog.CatalogFeatures.DEFER_TRUE;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    45
import javax.xml.catalog.CatalogFeatures.Feature;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    46
import javax.xml.parsers.ParserConfigurationException;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    47
import javax.xml.parsers.SAXParser;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    48
import javax.xml.parsers.SAXParserFactory;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    49
import org.xml.sax.SAXException;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    50
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    51
/**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    52
 * Implementation of the Catalog.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    53
 *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    54
 * @since 9
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    55
 */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    56
class CatalogImpl extends GroupEntry implements Catalog {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    57
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    58
    //Catalog level, 0 means the top catalog
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    59
    int level = 0;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    60
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    61
    //Value of the defer attribute to determine if alternative catalogs are read
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    62
    boolean isDeferred = true;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    63
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    64
    //Value of the resolve attribute
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    65
    ResolveType resolveType = ResolveType.STRICT;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    66
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    67
    //indicate whether the Catalog is empty
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    68
    boolean isEmpty;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    69
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    70
    //Current parsed Catalog
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    71
    int current = 0;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    72
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    73
    //System Id for this catalog
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    74
    String systemId;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    75
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    76
    //The parent
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    77
    CatalogImpl parent = null;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    78
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    79
    /*
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    80
     A list of catalog entry files from the input, excluding the current catalog.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    81
     Paths in the List are normalized.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    82
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    83
    List<String> inputFiles;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    84
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    85
    //A list of catalogs specified using the nextCatalog element
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    86
    List<NextCatalog> nextCatalogs;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    87
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    88
    //reuse the parser
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    89
    SAXParser parser;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    90
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    91
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    92
     * Construct a Catalog with specified path.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    93
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    94
     * @param file The path to a catalog file.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    95
     * @throws CatalogException If an error happens while parsing the specified
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    96
     * catalog file.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    97
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    98
    public CatalogImpl(CatalogFeatures f, String... file) throws CatalogException {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    99
        this(null, f, file);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   100
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   101
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   102
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   103
     * Construct a Catalog with specified path.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   104
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   105
     * @param parent The parent catalog
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   106
     * @param file The path to a catalog file.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   107
     * @throws CatalogException If an error happens while parsing the specified
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   108
     * catalog file.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   109
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   110
    public CatalogImpl(CatalogImpl parent, CatalogFeatures f, String... file) throws CatalogException {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   111
        super(CatalogEntryType.CATALOG);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   112
        this.parent = parent;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   113
        if (parent == null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   114
            level = 0;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   115
        } else {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   116
            level = parent.level + 1;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   117
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   118
        if (f == null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   119
            this.features = CatalogFeatures.defaults();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   120
        } else {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   121
            this.features = f;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   122
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   123
        setPrefer(features.get(Feature.PREFER));
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   124
        setDeferred(features.get(Feature.DEFER));
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   125
        setResolve(features.get(Feature.RESOLVE));
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   126
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   127
        //Path of catalog files
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   128
        String[] catalogFile = file;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   129
        if (level == 0
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   130
                && (file == null || (file.length == 0 || file[0] == null))) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   131
            String files = features.get(Feature.FILES);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   132
            if (files != null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   133
                catalogFile = files.split(";[ ]*");
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   134
            }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   135
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   136
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   137
        /*
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   138
         In accordance with 8. Resource Failures of the Catalog spec, missing
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   139
         Catalog entry files are to be ignored.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   140
         */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   141
        if ((catalogFile != null && catalogFile.length > 0)) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   142
            int start = 0;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   143
            URI uri = null;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   144
            for (String temp : catalogFile) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   145
                uri = getSystemId(temp);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   146
                start++;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   147
                if (verifyCatalogFile(uri)) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   148
                    systemId = uri.toASCIIString();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   149
                    break;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   150
                }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   151
            }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   152
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   153
            //Save the rest of input files as alternative catalogs
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   154
            if (level == 0 && catalogFile.length > start) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   155
                inputFiles = new ArrayList<>();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   156
                for (int i = start; i < catalogFile.length; i++) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   157
                    if (catalogFile[i] != null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   158
                        inputFiles.add(catalogFile[i]);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   159
                    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   160
                }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   161
            }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   162
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   163
            if (systemId != null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   164
                parse(systemId);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   165
            }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   166
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   167
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   168
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   169
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   170
     * Resets the Catalog instance to its initial state.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   171
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   172
    @Override
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   173
    public void reset() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   174
        super.reset();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   175
        current = 0;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   176
        if (level == 0) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   177
            catalogsSearched.clear();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   178
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   179
        entries.stream().filter((entry) -> (entry.type == CatalogEntryType.GROUP)).forEach((entry) -> {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   180
            ((GroupEntry) entry).reset();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   181
        });
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   182
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   183
        if (parent != null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   184
            this.loadedCatalogs = parent.loadedCatalogs;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   185
            this.catalogsSearched = parent.catalogsSearched;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   186
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   187
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   188
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   189
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   190
     * Returns whether this Catalog instance is the top (main) Catalog.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   191
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   192
     * @return true if the instance is the top Catalog, false otherwise
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   193
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   194
    boolean isTop() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   195
        return level == 0;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   196
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   197
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   198
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   199
     * Gets the parent of this catalog.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   200
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   201
     * @returns The parent catalog
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   202
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   203
    public Catalog getParent() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   204
        return this.parent;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   205
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   206
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   207
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   208
     * Sets the defer property. If the value is null or empty, or any String
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   209
     * other than the defined, it will be assumed as the default value.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   210
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   211
     * @param value The value of the defer attribute
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   212
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   213
    public final void setDeferred(String value) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   214
        isDeferred = DEFER_TRUE.equals(value);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   215
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   216
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   217
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   218
     * Queries the defer attribute
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   219
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   220
     * @return true if the prefer attribute is set to system, false if not.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   221
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   222
    public boolean isDeferred() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   223
        return isDeferred;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   224
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   225
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   226
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   227
     * Sets the resolve property. If the value is null or empty, or any String
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   228
     * other than the defined, it will be assumed as the default value.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   229
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   230
     * @param value The value of the resolve attribute
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   231
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   232
    public final void setResolve(String value) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   233
        resolveType = ResolveType.getType(value);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   234
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   235
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   236
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   237
     * Gets the value of the resolve attribute
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   238
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   239
     * @return The value of the resolve attribute
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   240
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   241
    public final ResolveType getResolve() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   242
        return resolveType;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   243
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   244
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   245
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   246
     * Marks the Catalog as being searched already.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   247
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   248
    void markAsSearched() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   249
        catalogsSearched.add(systemId);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   250
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   251
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   252
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   253
     * Parses the catalog.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   254
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   255
     * @param systemId The systemId of the catalog
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   256
     * @throws CatalogException if parsing the catalog failed
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   257
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   258
    private void parse(String systemId) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   259
        if (parser == null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   260
            parser = getParser();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   261
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   262
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   263
        try {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   264
            CatalogReader reader = new CatalogReader(this, parser);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   265
            parser.parse(systemId, reader);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   266
        } catch (SAXException | IOException ex) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   267
            CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSING_FAILED, ex);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   268
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   269
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   270
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   271
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   272
     * Resolves the specified file path to an absolute systemId. If it is
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   273
     * relative, it shall be resolved using the base or user.dir property if
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   274
     * base is not specified.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   275
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   276
     * @param file The specified file path
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   277
     * @return The systemId of the file
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   278
     * @throws CatalogException if the specified file path can not be converted
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   279
     * to a system id
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   280
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   281
    private URI getSystemId(String file) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   282
        URL filepath;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   283
        if (file != null && file.length() > 0) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   284
            try {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   285
                File f = new File(file);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   286
                if (baseURI != null && !f.isAbsolute()) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   287
                    filepath = new URL(baseURI, fixSlashes(file));
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   288
                    return filepath.toURI();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   289
                } else {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   290
                    return resolveURI(file);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   291
                }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   292
            } catch (MalformedURLException | URISyntaxException e) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   293
                CatalogMessages.reportRunTimeError(CatalogMessages.ERR_INVALID_PATH,
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   294
                        new Object[]{file}, e);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   295
            }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   296
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   297
        return null;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   298
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   299
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   300
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   301
     * Resolves the specified uri. If the uri is relative, makes it absolute by
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   302
     * the user.dir directory.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   303
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   304
     * @param uri The specified URI.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   305
     * @return The resolved URI
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   306
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   307
    private URI resolveURI(String uri) throws MalformedURLException {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   308
        if (uri == null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   309
            uri = "";
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   310
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   311
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   312
        URI temp = toURI(uri);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   313
        String str = temp.toASCIIString();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   314
        String base = str.substring(0, str.lastIndexOf('/') + 1);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   315
        baseURI = new URL(str);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   316
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   317
        return temp;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   318
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   319
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   320
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   321
     * Converts an URI string or file path to URI.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   322
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   323
     * @param uri an URI string or file path
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   324
     * @return an URI
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   325
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   326
    private URI toURI(String uri) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   327
        URI temp = null;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   328
        try {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   329
            URL url = new URL(uri);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   330
            temp = url.toURI();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   331
        } catch (MalformedURLException | URISyntaxException mue) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   332
            File file = new File(uri);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   333
            temp = file.toURI();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   334
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   335
        return temp;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   336
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   337
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   338
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   339
     * Returns a SAXParser instance
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   340
     * @return a SAXParser instance
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   341
     * @throws CatalogException if constructing a SAXParser failed
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   342
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   343
    private SAXParser getParser() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   344
        SAXParser p = null;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   345
        try {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   346
            SAXParserFactory spf = new SAXParserFactoryImpl();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   347
            spf.setNamespaceAware(true);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   348
            spf.setValidating(false);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   349
            spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   350
            p = spf.newSAXParser();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   351
        } catch (ParserConfigurationException | SAXException e) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   352
            CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSING_FAILED, e);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   353
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   354
        return p;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   355
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   356
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   357
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   358
     * Indicate that the catalog is empty
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   359
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   360
     * @return True if the catalog is empty; False otherwise.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   361
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   362
    public boolean isEmpty() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   363
        return isEmpty;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   364
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   365
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   366
    @Override
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   367
    public Stream<Catalog> catalogs() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   368
        Iterator<Catalog> iter = new Iterator<Catalog>() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   369
            Catalog nextCatalog = null;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   370
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   371
            //Current index of the input files
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   372
            int inputFilesIndex = 0;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   373
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   374
            //Next catalog
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   375
            int nextCatalogIndex = 0;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   376
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   377
            @Override
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   378
            public boolean hasNext() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   379
                if (nextCatalog != null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   380
                    return true;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   381
                } else {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   382
                    nextCatalog = nextCatalog();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   383
                    return (nextCatalog != null);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   384
                }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   385
            }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   386
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   387
            @Override
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   388
            public Catalog next() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   389
                if (nextCatalog != null || hasNext()) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   390
                    Catalog catalog = nextCatalog;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   391
                    nextCatalog = null;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   392
                    return catalog;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   393
                } else {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   394
                    throw new NoSuchElementException();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   395
                }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   396
            }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   397
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   398
            /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   399
             * Returns the next alternative catalog.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   400
             *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   401
             * @return the next catalog if any
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   402
             */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   403
            private Catalog nextCatalog() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   404
                Catalog c = null;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   405
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   406
                //Check those specified in nextCatalogs
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   407
                if (nextCatalogs != null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   408
                    while (c == null && nextCatalogIndex < nextCatalogs.size()) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   409
                        c = getCatalog(nextCatalogs.get(nextCatalogIndex++).getCatalogURI());
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   410
                    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   411
                }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   412
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   413
                //Check the input list
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   414
                if (c == null && inputFiles != null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   415
                    while (c == null && inputFilesIndex < inputFiles.size()) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   416
                        c = getCatalog(getSystemId(inputFiles.get(inputFilesIndex++)));
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   417
                    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   418
                }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   419
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   420
                return c;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   421
            }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   422
        };
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   423
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   424
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   425
                iter, Spliterator.ORDERED | Spliterator.NONNULL), false);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   426
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   427
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   428
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   429
     * Adds the catalog to the nextCatalog list
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   430
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   431
     * @param catalog a catalog specified in a nextCatalog entry
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   432
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   433
    void addNextCatalog(NextCatalog catalog) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   434
        if (catalog == null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   435
            return;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   436
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   437
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   438
        if (nextCatalogs == null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   439
            nextCatalogs = new ArrayList<>();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   440
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   441
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   442
        nextCatalogs.add(catalog);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   443
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   444
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   445
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   446
     * Loads all alternative catalogs.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   447
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   448
    void loadNextCatalogs() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   449
        //loads catalogs specified in nextCatalogs
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   450
        if (nextCatalogs != null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   451
            for (NextCatalog next : nextCatalogs) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   452
                getCatalog(next.getCatalogURI());
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   453
            }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   454
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   455
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   456
        //loads catalogs from the input list
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   457
        if (inputFiles != null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   458
            for (String file : inputFiles) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   459
                getCatalog(getSystemId(file));
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   460
            }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   461
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   462
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   463
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   464
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   465
     * Returns a Catalog object by the specified path.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   466
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   467
     * @param path the path to a catalog
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   468
     * @return a Catalog object
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   469
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   470
    Catalog getCatalog(URI uri) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   471
        if (uri == null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   472
            return null;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   473
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   474
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   475
        Catalog c = null;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   476
        String path = uri.toASCIIString();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   477
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   478
        if (verifyCatalogFile(uri)) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   479
            c = getLoadedCatalog(path);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   480
            if (c == null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   481
                c = new CatalogImpl(this, features, path);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   482
                saveLoadedCatalog(path, c);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   483
            }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   484
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   485
        return c;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   486
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   487
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   488
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   489
     * Saves a loaded Catalog.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   490
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   491
     * @param catalogId the catalogId associated with the Catalog object
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   492
     * @param c the Catalog to be saved
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   493
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   494
    void saveLoadedCatalog(String catalogId, Catalog c) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   495
        loadedCatalogs.put(catalogId, c);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   496
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   497
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   498
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   499
     * Returns a count of all loaded catalogs, including delegate catalogs.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   500
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   501
     * @return a count of all loaded catalogs
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   502
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   503
    int loadedCatalogCount() {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   504
        return loadedCatalogs.size() + delegateCatalogs.size();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   505
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   506
}