View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.ws.commons.schema;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.w3c.dom.DocumentFragment;
25  import org.w3c.dom.Node;
26  import org.w3c.dom.NodeList;
27  
28  /**
29   * Class to return a node list without thread-safety issue.
30   *
31   */
32  class DocumentFragmentNodeList implements NodeList {
33  	private List nodes;
34  	private DocumentFragment fragment;
35  	
36  	/**
37  	 * Construct a list of the children of a given node.
38  	 * @param parentNode node from which to copy children.
39  	 */
40  	DocumentFragmentNodeList(Node parentNode) {
41  		fragment = parentNode.getOwnerDocument().createDocumentFragment();
42  		nodes = new ArrayList();
43  		for(Node child = parentNode.getFirstChild(); child != null; child = child.getNextSibling()) {
44  			nodes.add(fragment.appendChild(child.cloneNode(true)));
45  		}
46  	}
47  
48  	/**
49  	 * Create a list of the children of a given node that are elements with a specified qualified name.
50  	 * @param parentNode node from which to copy children.
51  	 * @param filterUri Namespace URI of children to copy.
52  	 * @param filterLocal Local name of children to copy.
53  	 */
54  	DocumentFragmentNodeList(Node parentNode, String filterUri, String filterLocal) {
55  		fragment = parentNode.getOwnerDocument().createDocumentFragment();
56  		nodes = new ArrayList();
57  		for(Node child = parentNode.getFirstChild(); child != null; child = child.getNextSibling()) {
58  			if(child.getNodeType() == Node.ELEMENT_NODE 
59  					&& child.getNamespaceURI().equals(filterUri)
60  					&& child.getLocalName().equals(filterLocal)) {
61  				nodes.add(fragment.appendChild(child.cloneNode(true)));
62  			}
63  		}
64  	}
65  
66  	public int getLength() {
67  		return nodes.size(); 
68  	}
69  
70  	public Node item(int index) {
71  		if(nodes == null) {
72  			return null;
73  		} else {
74  			return (Node) nodes.get(index);
75  		}
76  	}
77  
78  }