src/java.xml/share/classes/com/sun/org/apache/xerces/internal/dom/DOMImplementationSourceImpl.java
author joehw
Wed, 18 Oct 2017 13:25:49 -0700
changeset 47359 e1a6c0168741
parent 47216 71c04702a3d5
child 48409 5ab69533994b
permissions -rw-r--r--
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked Reviewed-by: lancea, rriggs, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     3
 * @LastModified: Oct 2017
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xerces.internal.dom;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    24
import java.util.ArrayList;
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    25
import java.util.List;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.util.StringTokenizer;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    27
import org.w3c.dom.DOMImplementation;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import org.w3c.dom.DOMImplementationList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import org.w3c.dom.DOMImplementationSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * Supply one the right implementation, based upon requested features. Each
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * implemented <code>DOMImplementationSource</code> object is listed in the
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * binding-specific list of available sources so that its
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * <code>DOMImplementation</code> objects are made available.
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 *
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    37
 * <p>See also the
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    38
 * <a href='http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#DOMImplementationSource'>
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    39
 * Document Object Model (DOM) Level 3 Core Specification</a>.
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * @xerces.internal
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
public class DOMImplementationSourceImpl
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
    implements DOMImplementationSource {
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
     * A method to request a DOM implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
     * @param features A string that specifies which features are required.
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
     *   This is a space separated list in which each feature is specified
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
     *   by its name optionally followed by a space and a version number.
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
     *   This is something like: "XML 1.0 Traversal Events 2.0"
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
     * @return An implementation that has the desired features, or
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
     *   <code>null</code> if this source has none.
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
    public DOMImplementation getDOMImplementation(String features) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
        // first check whether the CoreDOMImplementation would do
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
        DOMImplementation impl =
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
            CoreDOMImplementationImpl.getDOMImplementation();
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
        if (testImpl(impl, features)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
            return impl;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
        // if not try the DOMImplementation
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
        impl = DOMImplementationImpl.getDOMImplementation();
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
        if (testImpl(impl, features)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
            return impl;
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
     * A method to request a list of DOM implementations that support the
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
     * specified features and versions, as specified in .
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
     * @param features A string that specifies which features and versions
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
     *   are required. This is a space separated list in which each feature
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
     *   is specified by its name optionally followed by a space and a
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
     *   version number. This is something like: "XML 3.0 Traversal +Events
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
     *   2.0"
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
     * @return A list of DOM implementations that support the desired
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
     *   features.
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    public DOMImplementationList getDOMImplementationList(String features) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
        // first check whether the CoreDOMImplementation would do
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
        DOMImplementation impl = CoreDOMImplementationImpl.getDOMImplementation();
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    86
        final List<DOMImplementation> implementations = new ArrayList<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
        if (testImpl(impl, features)) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    88
            implementations.add(impl);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
        impl = DOMImplementationImpl.getDOMImplementation();
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
        if (testImpl(impl, features)) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    92
            implementations.add(impl);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
        return new DOMImplementationListImpl(implementations);
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    boolean testImpl(DOMImplementation impl, String features) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
        StringTokenizer st = new StringTokenizer(features);
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        String feature = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
        String version = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
        if (st.hasMoreTokens()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
           feature = st.nextToken();
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        while (feature != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
           boolean isVersion = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
           if (st.hasMoreTokens()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
               char c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
               version = st.nextToken();
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
               c = version.charAt(0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
               switch (c) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
               case '0': case '1': case '2': case '3': case '4':
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
               case '5': case '6': case '7': case '8': case '9':
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
                   isVersion = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
               }
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
           } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
               version = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
           }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
           if (isVersion) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
               if (!impl.hasFeature(feature, version)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
                   return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
               }
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
               if (st.hasMoreTokens()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
                   feature = st.nextToken();
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
               } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
                   feature = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
               }
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
           } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
               if (!impl.hasFeature(feature, null)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
                   return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
               }
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
               feature = version;
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
           }
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
}