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