KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > dom > DOMImplementationSourceImpl


1 /*
2  * Copyright 2001, 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.dom;
18
19 import java.util.StringTokenizer JavaDoc;
20 import java.util.Vector JavaDoc;
21 import org.w3c.dom.DOMImplementationList JavaDoc;
22 import org.w3c.dom.DOMImplementationSource JavaDoc;
23 import org.w3c.dom.DOMImplementation JavaDoc;
24 import org.apache.xerces.dom.DOMImplementationListImpl;
25
26 /**
27  * Supply one the right implementation, based upon requested features. Each
28  * implemented <code>DOMImplementationSource</code> object is listed in the
29  * binding-specific list of available sources so that its
30  * <code>DOMImplementation</code> objects are made available.
31  *
32  * <p>See also the <a HREF='http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#DOMImplementationSource'>Document Object Model (DOM) Level 3 Core Specification</a>.
33  *
34  * @xerces.internal
35  *
36  * @version $Id: DOMImplementationSourceImpl.java,v 1.17 2005/05/02 22:02:22 mrglavas Exp $
37  */

38 public class DOMImplementationSourceImpl
39     implements DOMImplementationSource JavaDoc {
40
41     /**
42      * A method to request a DOM implementation.
43      * @param features A string that specifies which features are required.
44      * This is a space separated list in which each feature is specified
45      * by its name optionally followed by a space and a version number.
46      * This is something like: "XML 1.0 Traversal Events 2.0"
47      * @return An implementation that has the desired features, or
48      * <code>null</code> if this source has none.
49      */

50     public DOMImplementation JavaDoc getDOMImplementation(String JavaDoc features) {
51         // first check whether the CoreDOMImplementation would do
52
DOMImplementation JavaDoc impl =
53             CoreDOMImplementationImpl.getDOMImplementation();
54         if (testImpl(impl, features)) {
55             return impl;
56         }
57         // if not try the DOMImplementation
58
impl = DOMImplementationImpl.getDOMImplementation();
59         if (testImpl(impl, features)) {
60             return impl;
61         }
62         
63         return null;
64     }
65     
66     /**
67      * A method to request a list of DOM implementations that support the
68      * specified features and versions, as specified in .
69      * @param features A string that specifies which features and versions
70      * are required. This is a space separated list in which each feature
71      * is specified by its name optionally followed by a space and a
72      * version number. This is something like: "XML 3.0 Traversal +Events
73      * 2.0"
74      * @return A list of DOM implementations that support the desired
75      * features.
76      */

77     public DOMImplementationList JavaDoc getDOMImplementationList(String JavaDoc features) {
78         // first check whether the CoreDOMImplementation would do
79
DOMImplementation JavaDoc impl = CoreDOMImplementationImpl.getDOMImplementation();
80         final Vector JavaDoc implementations = new Vector JavaDoc();
81         if (testImpl(impl, features)) {
82             implementations.addElement(impl);
83         }
84         impl = DOMImplementationImpl.getDOMImplementation();
85         if (testImpl(impl, features)) {
86             implementations.addElement(impl);
87         }
88
89         return new DOMImplementationListImpl(implementations);
90     }
91
92     boolean testImpl(DOMImplementation JavaDoc impl, String JavaDoc features) {
93        
94         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(features);
95         String JavaDoc feature = null;
96         String JavaDoc version = null;
97  
98         if (st.hasMoreTokens()) {
99            feature = st.nextToken();
100         }
101         while (feature != null) {
102            boolean isVersion = false;
103            if (st.hasMoreTokens()) {
104                char c;
105                version = st.nextToken();
106                c = version.charAt(0);
107                switch (c) {
108                case '0': case '1': case '2': case '3': case '4':
109                case '5': case '6': case '7': case '8': case '9':
110                    isVersion = true;
111                }
112            } else {
113                version = null;
114            }
115            if (isVersion) {
116                if (!impl.hasFeature(feature, version)) {
117                    return false;
118                }
119                if (st.hasMoreTokens()) {
120                    feature = st.nextToken();
121                } else {
122                    feature = null;
123                }
124            } else {
125                if (!impl.hasFeature(feature, null)) {
126                    return false;
127                }
128                feature = version;
129            }
130         }
131         return true;
132     }
133 }
134
Popular Tags