12009
|
1 |
/*
|
|
2 |
* Copyright (c) 1997, 2010, 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 |
|
|
26 |
package com.sun.xml.internal.xsom;
|
|
27 |
|
|
28 |
import com.sun.xml.internal.xsom.impl.scd.Iterators;
|
|
29 |
import com.sun.xml.internal.xsom.impl.scd.ParseException;
|
|
30 |
import com.sun.xml.internal.xsom.impl.scd.SCDImpl;
|
|
31 |
import com.sun.xml.internal.xsom.impl.scd.SCDParser;
|
|
32 |
import com.sun.xml.internal.xsom.impl.scd.Step;
|
|
33 |
import com.sun.xml.internal.xsom.impl.scd.TokenMgrError;
|
|
34 |
import com.sun.xml.internal.xsom.util.DeferedCollection;
|
|
35 |
|
|
36 |
import javax.xml.namespace.NamespaceContext;
|
|
37 |
import java.util.Collection;
|
|
38 |
import java.util.Iterator;
|
|
39 |
import java.util.List;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* Schema Component Designator (SCD).
|
|
43 |
*
|
|
44 |
* <p>
|
|
45 |
* SCD for schema is what XPath is for XML. SCD allows you to select a schema component(s)
|
|
46 |
* from a schema component(s).
|
|
47 |
*
|
|
48 |
* <p>
|
|
49 |
* See <a href="http://www.w3.org/TR/2005/WD-xmlschema-ref-20050329/">XML Schema: Component Designators</a>.
|
|
50 |
* This implementation is based on 03/29/2005 working draft.
|
|
51 |
*
|
|
52 |
* @author Kohsuke Kawaguchi
|
|
53 |
*/
|
|
54 |
public abstract class SCD {
|
|
55 |
|
|
56 |
/**
|
|
57 |
* Parses the string representation of SCD.
|
|
58 |
*
|
|
59 |
* <p>
|
|
60 |
* This method involves parsing the path expression and preparing the in-memory
|
|
61 |
* structure, so this is useful when you plan to use the same SCD against
|
|
62 |
* different context node multiple times.
|
|
63 |
*
|
|
64 |
* <p>
|
|
65 |
* If you want to evaluate SCD just once, use {@link XSComponent#select} methods.
|
|
66 |
*
|
|
67 |
* @param path
|
|
68 |
* the string representation of SCD, such as "/foo/bar".
|
|
69 |
* @param nsContext
|
|
70 |
* Its {@link NamespaceContext#getNamespaceURI(String)} is used
|
|
71 |
* to resolve prefixes in the SCD to the namespace URI.
|
|
72 |
*/
|
|
73 |
public static SCD create(String path, NamespaceContext nsContext) throws java.text.ParseException {
|
|
74 |
try {
|
|
75 |
SCDParser p = new SCDParser(path,nsContext);
|
|
76 |
List<?> list = p.RelativeSchemaComponentPath();
|
|
77 |
return new SCDImpl(path,list.toArray(new Step[list.size()]));
|
|
78 |
} catch (TokenMgrError e) {
|
|
79 |
throw setCause(new java.text.ParseException(e.getMessage(), -1 ),e);
|
|
80 |
} catch (ParseException e) {
|
|
81 |
throw setCause(new java.text.ParseException(e.getMessage(), e.currentToken.beginColumn ),e);
|
|
82 |
}
|
|
83 |
}
|
|
84 |
|
|
85 |
private static java.text.ParseException setCause(java.text.ParseException e, Throwable x) {
|
|
86 |
e.initCause(x);
|
|
87 |
return e;
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Evaluates the SCD against the given context node and
|
|
92 |
* returns the matched nodes.
|
|
93 |
*
|
|
94 |
* @return
|
|
95 |
* could be empty but never be null.
|
|
96 |
*/
|
|
97 |
public final Collection<XSComponent> select(XSComponent contextNode) {
|
|
98 |
return new DeferedCollection<XSComponent>(select(Iterators.singleton(contextNode)));
|
|
99 |
}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* Evaluates the SCD against the whole schema and
|
|
103 |
* returns the matched nodes.
|
|
104 |
*
|
|
105 |
* <p>
|
|
106 |
* This method is here because {@link XSSchemaSet}
|
|
107 |
* doesn't implement {@link XSComponent}.
|
|
108 |
*
|
|
109 |
* @return
|
|
110 |
* could be empty but never be null.
|
|
111 |
*/
|
|
112 |
public final Collection<XSComponent> select(XSSchemaSet contextNode) {
|
|
113 |
return select(contextNode.getSchemas());
|
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Evaluates the SCD against the given context node and
|
|
118 |
* returns the matched node.
|
|
119 |
*
|
|
120 |
* @return
|
|
121 |
* null if the SCD didn't match anything. If the SCD matched more than one node,
|
|
122 |
* the first one will be returned.
|
|
123 |
*/
|
|
124 |
public final XSComponent selectSingle(XSComponent contextNode) {
|
|
125 |
Iterator<XSComponent> r = select(Iterators.singleton(contextNode));
|
|
126 |
if(r.hasNext()) return r.next();
|
|
127 |
return null;
|
|
128 |
}
|
|
129 |
|
|
130 |
/**
|
|
131 |
* Evaluates the SCD against the whole schema set and
|
|
132 |
* returns the matched node.
|
|
133 |
*
|
|
134 |
* @return
|
|
135 |
* null if the SCD didn't match anything. If the SCD matched more than one node,
|
|
136 |
* the first one will be returned.
|
|
137 |
*/
|
|
138 |
public final XSComponent selectSingle(XSSchemaSet contextNode) {
|
|
139 |
Iterator<XSComponent> r = select(contextNode.iterateSchema());
|
|
140 |
if(r.hasNext()) return r.next();
|
|
141 |
return null;
|
|
142 |
}
|
|
143 |
|
|
144 |
/**
|
|
145 |
* Evaluates the SCD against the given set of context nodes and
|
|
146 |
* returns the matched nodes.
|
|
147 |
*
|
|
148 |
* @param contextNodes
|
|
149 |
* {@link XSComponent}s that represent the context node against
|
|
150 |
* which {@link SCD} is evaluated.
|
|
151 |
*
|
|
152 |
* @return
|
|
153 |
* could be empty but never be null.
|
|
154 |
*/
|
|
155 |
public abstract Iterator<XSComponent> select(Iterator<? extends XSComponent> contextNodes);
|
|
156 |
|
|
157 |
/**
|
|
158 |
* Evaluates the SCD against the given set of context nodes and
|
|
159 |
* returns the matched nodes.
|
|
160 |
*
|
|
161 |
* @param contextNodes
|
|
162 |
* {@link XSComponent}s that represent the context node against
|
|
163 |
* which {@link SCD} is evaluated.
|
|
164 |
*
|
|
165 |
* @return
|
|
166 |
* could be empty but never be null.
|
|
167 |
*/
|
|
168 |
public final Collection<XSComponent> select(Collection<? extends XSComponent> contextNodes) {
|
|
169 |
return new DeferedCollection<XSComponent>(select(contextNodes.iterator()));
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* Returns the textual SCD representation as given to {@link SCD#create(String, NamespaceContext)}.
|
|
174 |
*/
|
|
175 |
public abstract String toString();
|
|
176 |
}
|