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 short type = pNode.getNodeType(); 43 if (type == Node.ELEMENT_NODE || type == Node.DOCUMENT_NODE) { 44 NamedNodeMap map = pNode.getAttributes(); 45 for (int i = 0; map != null && i < map.getLength(); i++) { 46 Node attr = map.item(i); 47 final String uri = attr.getNamespaceURI(); 48 if (Constants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) { 49 String localName = attr.getLocalName(); 50 String prefix = Constants.XMLNS_ATTRIBUTE.equals(localName) ? Constants.DEFAULT_NS_PREFIX : localName; 51 declare(prefix, attr.getNodeValue()); 52 } 53 } 54 } 55 } 56 57 /** 58 * Searches for namespace prefix declarations in the given node. 59 * For any prefix declaration, it invokes {@link #declare(String, String)}. 60 * This method works recursively: The parent nodes prefix 61 * declarations are collected before the current nodes. 62 */ 63 public void searchAllPrefixDeclarations(Node pNode) { 64 Node parent = pNode.getParentNode(); 65 if (parent != null) { 66 searchAllPrefixDeclarations(parent); 67 } 68 searchLocalPrefixDeclarations(pNode); 69 } 70 }