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

Print this page


   1 /*
   2  * Copyright (c) 2015, 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 jdk.xml.internal.SecuritySupport;
  28 
  29 /**
  30  * The CatalogFeatures holds a collection of features and properties.
  31  * <p>
  32  *
  33  * <center><h2><a name="CatalogFeatures">Catalog Features</a></h2></center></p>
  34  *
  35  * <table border="1">
  36  * <thead>
  37  * <tr>
  38  * <th rowspan="2">Feature</th>
  39  * <th rowspan="2">Description</th>
  40  * <th rowspan="2">Property Name</th>
  41  * <th rowspan="2">System Property [1]</th>
  42  * <th rowspan="2">jaxp.properties [1]</th>
  43  * <th colspan="2" align="center">Value [2]</th>
  44  * <th rowspan="2">Action</th>
  45  * </tr>
  46  * <tr>


 363      * Private class constructor
 364      */
 365     private CatalogFeatures() {
 366     }
 367 
 368     /**
 369      * Returns a CatalogFeatures instance with default settings.
 370      * @return a default CatalogFeatures instance
 371      */
 372     public static CatalogFeatures defaults() {
 373         return CatalogFeatures.builder().build();
 374     }
 375 
 376     /**
 377      * Constructs a new CatalogFeatures instance with the builder.
 378      *
 379      * @param builder the builder to build the CatalogFeatures
 380      */
 381     CatalogFeatures(Builder builder) {
 382         init();
 383         setProperty(Feature.FILES.ordinal(), State.APIPROPERTY, builder.files);
 384         setProperty(Feature.PREFER.ordinal(), State.APIPROPERTY, builder.prefer);
 385         setProperty(Feature.DEFER.ordinal(), State.APIPROPERTY, builder.defer);
 386         setProperty(Feature.RESOLVE.ordinal(), State.APIPROPERTY, builder.resolve);
 387     }
 388 
 389     /**
 390      * Returns the value of the specified feature.
 391      *
 392      * @param cf the type of the Catalog feature
 393      * @return the value of the feature
 394      */
 395     public String get(Feature cf) {
 396         return values[cf.ordinal()];
 397     }
 398 
 399     /**
 400      * Initializes the supported properties
 401      */
 402     private void init() {
 403         values = new String[Feature.values().length];
 404         states = new State[Feature.values().length];
 405         for (Feature cf : Feature.values()) {
 406             setProperty(cf.ordinal(), State.DEFAULT, cf.defaultValue());
 407         }
 408         //read system properties or jaxp.properties
 409         readSystemProperties();
 410     }
 411 
 412     /**









 413      * Sets the value of a property by its index, updates only if it shall override.
 414      *
 415      * @param index the index of the property
 416      * @param state the state of the property
 417      * @param value the value of the property
 418      * @throws IllegalArgumentException if the value is invalid
 419      */
 420     private void setProperty(int index, State state, String value) {
 421         if (value != null && value.length() != 0) {
 422             if (index == Feature.PREFER.ordinal()) {
 423                 if (!value.equals(PREFER_SYSTEM) && !value.equals(PREFER_PUBLIC)) {
 424                     CatalogMessages.reportIAE(new Object[]{value, Feature.PREFER.name()}, null);
 425                 }
 426             } else if (index == Feature.DEFER.ordinal()) {
 427                 if (!value.equals(DEFER_TRUE) && !value.equals(DEFER_FALSE)) {
 428                     CatalogMessages.reportIAE(new Object[]{value, Feature.DEFER.name()}, null);
 429                 }
 430             } else if (index == Feature.RESOLVE.ordinal()) {
 431                 if (!value.equals(RESOLVE_STRICT) && !value.equals(RESOLVE_CONTINUE)
 432                          && !value.equals(RESOLVE_IGNORE)) {
 433                     CatalogMessages.reportIAE(new Object[]{value, Feature.RESOLVE.name()}, null);
 434                 }









 435             }
 436             if (states[index] == null || state.compareTo(states[index]) >= 0) {
 437                 values[index] = value;
 438                 states[index] = state;
 439             }




 440         }
 441     }
 442 
 443     /**
 444      * Reads from system properties, or those in jaxp.properties
 445      */
 446     private void readSystemProperties() {
 447         for (Feature cf : Feature.values()) {
 448             getSystemProperty(cf, cf.getPropertyName());
 449         }
 450     }
 451 
 452     /**
 453      * Reads from system properties, or those in jaxp.properties
 454      *
 455      * @param cf the type of the property
 456      * @param sysPropertyName the name of system property
 457      */
 458     private boolean getSystemProperty(Feature cf, String sysPropertyName) {
 459         if (cf.hasSystemProperty()) {


 469                 return true;
 470             }
 471         }
 472         return false;
 473     }
 474 
 475     /**
 476      * Returns an instance of the builder for creating the CatalogFeatures object.
 477      *
 478      * @return an instance of the builder
 479      */
 480     public static Builder builder() {
 481         return new CatalogFeatures.Builder();
 482     }
 483 
 484     /**
 485      * The Builder class for building the CatalogFeatures object.
 486      */
 487     public static class Builder {
 488         /**
 489          * Variables for the features supported by CatalogFeatures.
 490          */
 491         String files, prefer, defer, resolve;
 492 
 493         /**
 494          * Instantiation of Builder is not allowed.
 495          */
 496         private Builder() {}
 497 
 498         /**
 499          * Sets the value to a specified Feature.
 500          * @param feature the Feature to be set
 501          * @param value the value to be set for the Feature
 502          * @return this Builder instance
 503          * @throws IllegalArgumentException if the value is not valid for the
 504          * Feature or has the wrong syntax for the {@code javax.xml.catalog.files}
 505          * property
 506          */
 507         public Builder with(Feature feature, String value) {
 508             switch (feature) {
 509                 case FILES :
 510                     files = value;
 511                     break;
 512                 case PREFER :
 513                     prefer = value;
 514                     break;
 515                 case DEFER :
 516                     defer = value;
 517                     break;
 518                 case RESOLVE :
 519                     resolve = value;
 520                     break;
 521             }

 522             return this;
 523         }
 524 
 525         /**
 526          * Returns a CatalogFeatures object built by this builder.
 527          *
 528          * @return an instance of CatalogFeatures
 529          */
 530         public CatalogFeatures build() {
 531             return new CatalogFeatures(this);
 532         }
 533     }
 534 }
   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.URISyntaxException;
  29 import java.util.HashMap;
  30 import java.util.Map;
  31 import jdk.xml.internal.SecuritySupport;
  32 
  33 /**
  34  * The CatalogFeatures holds a collection of features and properties.
  35  * <p>
  36  *
  37  * <center><h2><a name="CatalogFeatures">Catalog Features</a></h2></center></p>
  38  *
  39  * <table border="1">
  40  * <thead>
  41  * <tr>
  42  * <th rowspan="2">Feature</th>
  43  * <th rowspan="2">Description</th>
  44  * <th rowspan="2">Property Name</th>
  45  * <th rowspan="2">System Property [1]</th>
  46  * <th rowspan="2">jaxp.properties [1]</th>
  47  * <th colspan="2" align="center">Value [2]</th>
  48  * <th rowspan="2">Action</th>
  49  * </tr>
  50  * <tr>


 367      * Private class constructor
 368      */
 369     private CatalogFeatures() {
 370     }
 371 
 372     /**
 373      * Returns a CatalogFeatures instance with default settings.
 374      * @return a default CatalogFeatures instance
 375      */
 376     public static CatalogFeatures defaults() {
 377         return CatalogFeatures.builder().build();
 378     }
 379 
 380     /**
 381      * Constructs a new CatalogFeatures instance with the builder.
 382      *
 383      * @param builder the builder to build the CatalogFeatures
 384      */
 385     CatalogFeatures(Builder builder) {
 386         init();
 387         setProperties(builder);



 388     }
 389 
 390     /**
 391      * Returns the value of the specified feature.
 392      *
 393      * @param cf the type of the Catalog feature
 394      * @return the value of the feature
 395      */
 396     public String get(Feature cf) {
 397         return values[cf.ordinal()];
 398     }
 399 
 400     /**
 401      * Initializes the supported properties
 402      */
 403     private void init() {
 404         values = new String[Feature.values().length];
 405         states = new State[Feature.values().length];
 406         for (Feature cf : Feature.values()) {
 407             setProperty(cf.ordinal(), State.DEFAULT, cf.defaultValue());
 408         }
 409         //read system properties or jaxp.properties
 410         readSystemProperties();
 411     }
 412 
 413     /**
 414      * Sets properties by the Builder.
 415      * @param builder the CatalogFeatures builder
 416      */
 417     private void setProperties(Builder builder) {
 418         builder.values.entrySet().stream().forEach((entry) -> {
 419             setProperty(entry.getKey().ordinal(), State.APIPROPERTY, entry.getValue());
 420         });
 421     }
 422     /**
 423      * Sets the value of a property by its index, updates only if it shall override.
 424      *
 425      * @param index the index of the property
 426      * @param state the state of the property
 427      * @param value the value of the property
 428      * @throws IllegalArgumentException if the value is invalid
 429      */
 430     private void setProperty(int index, State state, String value) {
 431         if (value != null && value.length() != 0) {
 432             if (index == Feature.PREFER.ordinal()) {
 433                 if (!value.equals(PREFER_SYSTEM) && !value.equals(PREFER_PUBLIC)) {
 434                     CatalogMessages.reportIAE(new Object[]{value, Feature.PREFER.name()}, null);
 435                 }
 436             } else if (index == Feature.DEFER.ordinal()) {
 437                 if (!value.equals(DEFER_TRUE) && !value.equals(DEFER_FALSE)) {
 438                     CatalogMessages.reportIAE(new Object[]{value, Feature.DEFER.name()}, null);
 439                 }
 440             } else if (index == Feature.RESOLVE.ordinal()) {
 441                 if (!value.equals(RESOLVE_STRICT) && !value.equals(RESOLVE_CONTINUE)
 442                          && !value.equals(RESOLVE_IGNORE)) {
 443                     CatalogMessages.reportIAE(new Object[]{value, Feature.RESOLVE.name()}, null);
 444                 }
 445             } else if (index == Feature.FILES.ordinal()) {
 446                 try {
 447                     if (Util.verifyAndGetURI(value, null) == null) {
 448                         CatalogMessages.reportIAE(new Object[]{value, Feature.FILES.name()}, null);
 449                     }
 450                 }catch (MalformedURLException | URISyntaxException | IllegalArgumentException ex) {
 451                     CatalogMessages.reportIAE(new Object[]{value, Feature.FILES.name()}, ex);
 452                 }
 453 
 454             }
 455             if (states[index] == null || state.compareTo(states[index]) >= 0) {
 456                 values[index] = value;
 457                 states[index] = state;
 458             }
 459         } else {
 460             if (state == State.SYSTEMPROPERTY || state == State.JAXPDOTPROPERTIES) {
 461                 CatalogMessages.reportIAE(new Object[]{value, Feature.values()[index].name()}, null);
 462             }
 463         }
 464     }
 465 
 466     /**
 467      * Reads from system properties, or those in jaxp.properties
 468      */
 469     private void readSystemProperties() {
 470         for (Feature cf : Feature.values()) {
 471             getSystemProperty(cf, cf.getPropertyName());
 472         }
 473     }
 474 
 475     /**
 476      * Reads from system properties, or those in jaxp.properties
 477      *
 478      * @param cf the type of the property
 479      * @param sysPropertyName the name of system property
 480      */
 481     private boolean getSystemProperty(Feature cf, String sysPropertyName) {
 482         if (cf.hasSystemProperty()) {


 492                 return true;
 493             }
 494         }
 495         return false;
 496     }
 497 
 498     /**
 499      * Returns an instance of the builder for creating the CatalogFeatures object.
 500      *
 501      * @return an instance of the builder
 502      */
 503     public static Builder builder() {
 504         return new CatalogFeatures.Builder();
 505     }
 506 
 507     /**
 508      * The Builder class for building the CatalogFeatures object.
 509      */
 510     public static class Builder {
 511         /**
 512          * Values of the features supported by CatalogFeatures.
 513          */
 514         Map<Feature, String> values = new HashMap<>();
 515 
 516         /**
 517          * Instantiation of Builder is not allowed.
 518          */
 519         private Builder() {}
 520 
 521         /**
 522          * Sets the value to a specified Feature.
 523          * @param feature the Feature to be set
 524          * @param value the value to be set for the Feature
 525          * @return this Builder instance
 526          * @throws IllegalArgumentException if the value is not valid for the
 527          * Feature or has the wrong syntax for the {@code javax.xml.catalog.files}
 528          * property
 529          */
 530         public Builder with(Feature feature, String value) {
 531             if (value == null || value.length() == 0) {
 532                 CatalogMessages.reportIAE(new Object[]{value, feature.name()}, null);











 533             }
 534             values.put(feature, value);
 535             return this;
 536         }
 537 
 538         /**
 539          * Returns a CatalogFeatures object built by this builder.
 540          *
 541          * @return an instance of CatalogFeatures
 542          */
 543         public CatalogFeatures build() {
 544             return new CatalogFeatures(this);
 545         }
 546     }
 547 }