Skip to content

Commit 048f7ab

Browse files
committed
Adopt commons-jxpath classes to the eclipse codebase
1 parent 323f82d commit 048f7ab

File tree

164 files changed

+30416
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+30416
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.jxpath;
18+
19+
/**
20+
* The {@link JXPathContext#createPath JXPathContext.createPath()} method of
21+
* JXPathContext can create missing objects as it traverses an XPath; it
22+
* utilizes an AbstractFactory for that purpose. Install a factory on
23+
* JXPathContext by calling {@link JXPathContext#setFactory JXPathContext.
24+
* setFactory()}.
25+
* <p>
26+
* All methods of this class return false. Override any of them to return true
27+
* to indicate that the factory has successfully created the described object.
28+
*/
29+
public abstract class AbstractFactory {
30+
31+
/**
32+
* The parameters may describe a collection element or an individual
33+
* object. It is up to the factory to infer which one it is. If it is a
34+
* collection, the factory should check if the collection exists. If not,
35+
* it should create the collection. Then it should create the index'th
36+
* element of the collection and return true.
37+
* <p>
38+
*
39+
* @param context can be used to evaluate other XPaths, get to variables
40+
* etc.
41+
* @param pointer describes the location of the node to be created
42+
* @param parent is the object that will serve as a parent of the new
43+
* object
44+
* @param name is the name of the child of the parent that needs to be
45+
* created. In the case of DOM may be qualified.
46+
* @param index is used if the pointer represents a collection element. You
47+
* may need to expand or even create the collection to accommodate the new
48+
* element.
49+
*
50+
* @return true if the object was successfully created
51+
*/
52+
public boolean createObject(final JXPathContext context, final Pointer pointer,
53+
final Object parent, final String name, final int index) {
54+
return false;
55+
}
56+
57+
/**
58+
* Declare the specified variable
59+
*
60+
* @param context hosts variable pools. See
61+
* {@link JXPathContext#getVariables() JXPathContext.getVariables()}
62+
* @param name is the name of the variable without the "$" sign
63+
* @return true if the variable was successfully defined
64+
*/
65+
public boolean declareVariable(final JXPathContext context, final String name) {
66+
return false;
67+
}
68+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.jxpath;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
/**
24+
* A simple implementation of {@link NodeSet} that behaves as a collection
25+
* of pointers.
26+
*/
27+
public class BasicNodeSet implements NodeSet {
28+
private final List pointers = new ArrayList();
29+
private List readOnlyPointers;
30+
private List nodes;
31+
private List values;
32+
33+
/**
34+
* Add a pointer to this NodeSet.
35+
* @param pointer to add
36+
*/
37+
public void add(final Pointer pointer) {
38+
if (pointers.add(pointer)) {
39+
clearCacheLists();
40+
}
41+
}
42+
43+
/**
44+
* Add the specified NodeSet to this NodeSet.
45+
* @param nodeSet to add
46+
*/
47+
public void add(final NodeSet nodeSet) {
48+
if (pointers.addAll(nodeSet.getPointers())) {
49+
clearCacheLists();
50+
}
51+
}
52+
53+
/**
54+
* Remove a pointer from this NodeSet.
55+
* @param pointer to remove
56+
*/
57+
public void remove(final Pointer pointer) {
58+
if (pointers.remove(pointer)) {
59+
clearCacheLists();
60+
}
61+
}
62+
63+
@Override
64+
public synchronized List getPointers() {
65+
if (readOnlyPointers == null) {
66+
readOnlyPointers = Collections.unmodifiableList(pointers);
67+
}
68+
return readOnlyPointers;
69+
}
70+
71+
@Override
72+
public synchronized List getNodes() {
73+
if (nodes == null) {
74+
nodes = new ArrayList();
75+
for (int i = 0; i < pointers.size(); i++) {
76+
final Pointer pointer = (Pointer) pointers.get(i);
77+
nodes.add(pointer.getNode());
78+
}
79+
nodes = Collections.unmodifiableList(nodes);
80+
}
81+
return nodes;
82+
}
83+
84+
@Override
85+
public synchronized List getValues() {
86+
if (values == null) {
87+
values = new ArrayList();
88+
for (int i = 0; i < pointers.size(); i++) {
89+
final Pointer pointer = (Pointer) pointers.get(i);
90+
values.add(pointer.getValue());
91+
}
92+
values = Collections.unmodifiableList(values);
93+
}
94+
return values;
95+
}
96+
97+
@Override
98+
public String toString() {
99+
return pointers.toString();
100+
}
101+
102+
/**
103+
* Clear cache list members.
104+
*/
105+
private synchronized void clearCacheLists() {
106+
readOnlyPointers = null;
107+
nodes = null;
108+
values = null;
109+
}
110+
111+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.jxpath;
18+
19+
import java.util.HashMap;
20+
21+
/**
22+
* A basic implementation of the Variables interface that uses a HashMap.
23+
*/
24+
public class BasicVariables implements Variables {
25+
private static final long serialVersionUID = 2708263960832062725L;
26+
27+
/**
28+
* Contains the values of declared variables
29+
*/
30+
private final HashMap vars = new HashMap();
31+
32+
/**
33+
* Returns true if the variable has been defined, even if the
34+
* value of the variable is null.
35+
*
36+
* @param varName is a variable name without the "$" sign
37+
* @return true if the variable is declared
38+
*/
39+
@Override
40+
public boolean isDeclaredVariable(final String varName) {
41+
return vars.containsKey(varName);
42+
}
43+
44+
/**
45+
* Returns the value of the variable if it is defined,
46+
* otherwise, throws IllegalArgumentException
47+
*
48+
* @param varName is a variable name without the "$" sign
49+
* @return the value of the variable
50+
*/
51+
@Override
52+
public Object getVariable(final String varName) {
53+
// Note that a variable may be defined with a null value
54+
55+
if (vars.containsKey(varName)) {
56+
return vars.get(varName);
57+
}
58+
59+
throw new IllegalArgumentException(
60+
"No such variable: '" + varName + "'");
61+
}
62+
63+
/**
64+
* Defines a new variable with the specified value or modifies
65+
* the value of an existing variable.
66+
*
67+
* @param varName is a variable name without the "$" sign
68+
* @param value is the new value for the variable, which can be null
69+
*/
70+
@Override
71+
public void declareVariable(final String varName, final Object value) {
72+
vars.put(varName, value);
73+
}
74+
75+
/**
76+
* Removes an existing variable. May throw UnsupportedOperationException.
77+
*
78+
* @param varName is a variable name without the "$" sign
79+
*/
80+
@Override
81+
public void undeclareVariable(final String varName) {
82+
vars.remove(varName);
83+
}
84+
85+
@Override
86+
public String toString() {
87+
return vars.toString();
88+
}
89+
}

0 commit comments

Comments
 (0)