001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.builder.xml; 018 019 import java.util.HashMap; 020 import java.util.Map; 021 import java.util.Set; 022 023 /** 024 * A helper class for creating namespaces which can then be used to create XPath expressions 025 * 026 * @version $Revision: 1.1 $ 027 */ 028 public class NamespaceBuilder { 029 private Map<String, String> namespaces = new HashMap<String, String>(); 030 031 public static NamespaceBuilder namespaceContext() { 032 return new NamespaceBuilder(); 033 } 034 035 public static NamespaceBuilder namespaceContext(String prefix, String uri) { 036 return new NamespaceBuilder().namespace(prefix, uri); 037 } 038 039 public NamespaceBuilder namespace(String prefix, String uri) { 040 namespaces.put(prefix, uri); 041 return this; 042 } 043 044 /** 045 * Creates a new XPath expression using the current namespaces 046 * 047 * @param xpath the XPath expression 048 * @return a new XPath expression 049 */ 050 public XPathBuilder xpath(String xpath) { 051 XPathBuilder answer = XPathBuilder.xpath(xpath); 052 Set<Map.Entry<String, String>> entries = namespaces.entrySet(); 053 for (Map.Entry<String, String> entry : entries) { 054 answer.namespace(entry.getKey(), entry.getValue()); 055 } 056 return answer; 057 } 058 }