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  
20  package org.apache.ws.commons.schema.utils;
21  
22  import org.apache.ws.commons.schema.constants.Constants;
23  import org.w3c.dom.NamedNodeMap;
24  import org.w3c.dom.Node;
25  
26  /**
27   * Searches for namespace prefix declarations.
28   */
29  public abstract class PrefixCollector {
30      /**
31       * Records a single namespace prefix declaration.
32       */
33      protected abstract void declare(String pPrefix, String pNamespaceURI);
34  
35      /**
36       * Searches for namespace prefix declarations in the given node.
37       * For any prefix declaration, it invokes {@link #declare(String, String)}.
38       * This method doesn't work recursively: The parent nodes prefix
39       * declarations are ignored.
40       */
41      public void searchLocalPrefixDeclarations(Node pNode) {
42          if (pNode.getNodeType() == Node.ELEMENT_NODE) {
43              NamedNodeMap map = pNode.getAttributes();
44              for (int i = 0; i < map.getLength(); i++) {
45                  Node attr = map.item(i);
46                  final String uri = attr.getNamespaceURI();
47                  if (Constants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) {
48                      String localName = attr.getLocalName();
49                      String prefix = Constants.XMLNS_ATTRIBUTE.equals(localName) ? Constants.DEFAULT_NS_PREFIX : localName;
50                      declare(prefix, attr.getNodeValue());
51                  }
52              }
53          }
54      }
55  
56      /**
57       * Searches for namespace prefix declarations in the given node.
58       * For any prefix declaration, it invokes {@link #declare(String, String)}.
59       * This method works recursively: The parent nodes prefix
60       * declarations are collected before the current nodes.
61       */
62      public void searchAllPrefixDeclarations(Node pNode) {
63          Node parent = pNode.getParentNode();
64          if (parent != null) {
65              searchAllPrefixDeclarations(parent);
66          }
67          searchLocalPrefixDeclarations(pNode);
68      }
69  }