src/java.xml/share/classes/javax/xml/catalog/CatalogImpl.java

Print this page




  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 com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.net.MalformedURLException;
  31 import java.net.URI;
  32 import java.net.URISyntaxException;
  33 import java.net.URL;
  34 import java.util.ArrayList;
  35 import java.util.Collections;
  36 import java.util.Iterator;
  37 import java.util.List;
  38 import java.util.NoSuchElementException;
  39 import java.util.Spliterator;
  40 import java.util.Spliterators;
  41 import java.util.stream.Stream;
  42 import java.util.stream.StreamSupport;
  43 import static javax.xml.catalog.BaseEntry.CatalogEntryType;
  44 import static javax.xml.catalog.CatalogFeatures.DEFER_TRUE;
  45 import javax.xml.catalog.CatalogFeatures.Feature;

  46 import javax.xml.parsers.ParserConfigurationException;
  47 import javax.xml.parsers.SAXParser;
  48 import javax.xml.parsers.SAXParserFactory;
  49 import org.xml.sax.SAXException;
  50 
  51 /**
  52  * Implementation of the Catalog.
  53  *
  54  * @since 9
  55  */
  56 class CatalogImpl extends GroupEntry implements Catalog {
  57 
  58     //Catalog level, 0 means the top catalog
  59     int level = 0;
  60 
  61     //Value of the defer attribute to determine if alternative catalogs are read
  62     boolean isDeferred = true;
  63 
  64     //Value of the resolve attribute
  65     ResolveType resolveType = ResolveType.STRICT;


  92      * Construct a Catalog with specified path.
  93      *
  94      * @param file The path to a catalog file.
  95      * @throws CatalogException If an error happens while parsing the specified
  96      * catalog file.
  97      */
  98     public CatalogImpl(CatalogFeatures f, String... file) throws CatalogException {
  99         this(null, f, file);
 100     }
 101 
 102     /**
 103      * Construct a Catalog with specified path.
 104      *
 105      * @param parent The parent catalog
 106      * @param file The path to a catalog file.
 107      * @throws CatalogException If an error happens while parsing the specified
 108      * catalog file.
 109      */
 110     public CatalogImpl(CatalogImpl parent, CatalogFeatures f, String... file) throws CatalogException {
 111         super(CatalogEntryType.CATALOG);
 112         this.parent = parent;
 113         if (parent == null) {
 114             level = 0;
 115         } else {
 116             level = parent.level + 1;
 117         }
 118         if (f == null) {
 119             this.features = CatalogFeatures.defaults();
 120         } else {
 121             this.features = f;
 122         }
 123         setPrefer(features.get(Feature.PREFER));
 124         setDeferred(features.get(Feature.DEFER));
 125         setResolve(features.get(Feature.RESOLVE));



 126 
 127         //Path of catalog files
 128         String[] catalogFile = file;
 129         if (level == 0
 130                 && (file == null || (file.length == 0 || file[0] == null))) {
 131             String files = features.get(Feature.FILES);
 132             if (files != null) {
 133                 catalogFile = files.split(";[ ]*");
 134             }
 135         }
 136 
 137         /*
 138          In accordance with 8. Resource Failures of the Catalog spec, missing
 139          Catalog entry files are to be ignored.
 140          */
 141         if ((catalogFile != null && catalogFile.length > 0)) {
 142             int start = 0;
 143             URI uri = null;
 144             for (String temp : catalogFile) {
 145                 uri = getSystemId(temp);
 146                 start++;
 147                 if (verifyCatalogFile(uri)) {
 148                     systemId = uri.toASCIIString();
 149                     break;
 150                 }
 151             }
 152 
 153             //Save the rest of input files as alternative catalogs
 154             if (level == 0 && catalogFile.length > start) {
 155                 inputFiles = new ArrayList<>();
 156                 for (int i = start; i < catalogFile.length; i++) {
 157                     if (catalogFile[i] != null) {
 158                         inputFiles.add(catalogFile[i]);
 159                     }
 160                 }
 161             }
 162 
 163             if (systemId != null) {
 164                 parse(systemId);
 165             }
 166         }

















 167     }
 168 
 169     /**
 170      * Resets the Catalog instance to its initial state.
 171      */
 172     @Override
 173     public void reset() {
 174         super.reset();
 175         current = 0;
 176         if (level == 0) {
 177             catalogsSearched.clear();
 178         }
 179         entries.stream().filter((entry) -> (entry.type == CatalogEntryType.GROUP)).forEach((entry) -> {
 180             ((GroupEntry) entry).reset();
 181         });
 182 
 183         if (parent != null) {
 184             this.loadedCatalogs = parent.loadedCatalogs;
 185             this.catalogsSearched = parent.catalogsSearched;
 186         }




  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 com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.net.MalformedURLException;
  31 import java.net.URI;
  32 import java.net.URISyntaxException;
  33 import java.net.URL;
  34 import java.util.ArrayList;

  35 import java.util.Iterator;
  36 import java.util.List;
  37 import java.util.NoSuchElementException;
  38 import java.util.Spliterator;
  39 import java.util.Spliterators;
  40 import java.util.stream.Stream;
  41 import java.util.stream.StreamSupport;
  42 import static javax.xml.catalog.BaseEntry.CatalogEntryType;
  43 import static javax.xml.catalog.CatalogFeatures.DEFER_TRUE;
  44 import javax.xml.catalog.CatalogFeatures.Feature;
  45 import static javax.xml.catalog.CatalogMessages.formatMessage;
  46 import javax.xml.parsers.ParserConfigurationException;
  47 import javax.xml.parsers.SAXParser;
  48 import javax.xml.parsers.SAXParserFactory;
  49 import org.xml.sax.SAXException;
  50 
  51 /**
  52  * Implementation of the Catalog.
  53  *
  54  * @since 9
  55  */
  56 class CatalogImpl extends GroupEntry implements Catalog {
  57 
  58     //Catalog level, 0 means the top catalog
  59     int level = 0;
  60 
  61     //Value of the defer attribute to determine if alternative catalogs are read
  62     boolean isDeferred = true;
  63 
  64     //Value of the resolve attribute
  65     ResolveType resolveType = ResolveType.STRICT;


  92      * Construct a Catalog with specified path.
  93      *
  94      * @param file The path to a catalog file.
  95      * @throws CatalogException If an error happens while parsing the specified
  96      * catalog file.
  97      */
  98     public CatalogImpl(CatalogFeatures f, String... file) throws CatalogException {
  99         this(null, f, file);
 100     }
 101 
 102     /**
 103      * Construct a Catalog with specified path.
 104      *
 105      * @param parent The parent catalog
 106      * @param file The path to a catalog file.
 107      * @throws CatalogException If an error happens while parsing the specified
 108      * catalog file.
 109      */
 110     public CatalogImpl(CatalogImpl parent, CatalogFeatures f, String... file) throws CatalogException {
 111         super(CatalogEntryType.CATALOG);






 112         if (f == null) {
 113             throw new NullPointerException(
 114                     formatMessage(CatalogMessages.ERR_NULL_ARGUMENT, new Object[]{"CatalogFeatures"}));

 115         }
 116 
 117         if (file.length > 0) {
 118             CatalogMessages.reportNPEOnNull("The path to the catalog file", file[0]);
 119         }
 120 
 121         init(parent, f);
 122 
 123         //Path of catalog files
 124         String[] catalogFile = file;
 125         if (level == 0 && file.length == 0) {

 126             String files = features.get(Feature.FILES);
 127             if (files != null) {
 128                 catalogFile = files.split(";[ ]*");
 129             }
 130         }
 131 
 132         /*
 133          In accordance with 8. Resource Failures of the Catalog spec, missing
 134          Catalog entry files are to be ignored.
 135          */
 136         if ((catalogFile != null && catalogFile.length > 0)) {
 137             int start = 0;
 138             URI uri = null;
 139             for (String temp : catalogFile) {
 140                 uri = getSystemId(temp);
 141                 start++;
 142                 if (verifyCatalogFile(uri)) {
 143                     systemId = uri.toASCIIString();
 144                     break;
 145                 }
 146             }
 147 
 148             //Save the rest of input files as alternative catalogs
 149             if (level == 0 && catalogFile.length > start) {
 150                 inputFiles = new ArrayList<>();
 151                 for (int i = start; i < catalogFile.length; i++) {
 152                     if (catalogFile[i] != null) {
 153                         inputFiles.add(catalogFile[i]);
 154                     }
 155                 }
 156             }
 157 
 158             if (systemId != null) {
 159                 parse(systemId);
 160             }
 161         }
 162     }
 163 
 164     private void init(CatalogImpl parent, CatalogFeatures f) {
 165         this.parent = parent;
 166         if (parent == null) {
 167             level = 0;
 168         } else {
 169             level = parent.level + 1;
 170         }
 171         if (f == null) {
 172             this.features = CatalogFeatures.defaults();
 173         } else {
 174             this.features = f;
 175         }
 176         setPrefer(features.get(Feature.PREFER));
 177         setDeferred(features.get(Feature.DEFER));
 178         setResolve(features.get(Feature.RESOLVE));
 179     }
 180 
 181     /**
 182      * Resets the Catalog instance to its initial state.
 183      */
 184     @Override
 185     public void reset() {
 186         super.reset();
 187         current = 0;
 188         if (level == 0) {
 189             catalogsSearched.clear();
 190         }
 191         entries.stream().filter((entry) -> (entry.type == CatalogEntryType.GROUP)).forEach((entry) -> {
 192             ((GroupEntry) entry).reset();
 193         });
 194 
 195         if (parent != null) {
 196             this.loadedCatalogs = parent.loadedCatalogs;
 197             this.catalogsSearched = parent.catalogsSearched;
 198         }