View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  
18  
19  package org.apache.commons.betwixt.strategy;
20  
21  import java.util.HashMap;
22  
23  /**
24   * <p>Maps namespace <code>URI</code>'s to prefixes.
25   * </p><p>
26   * When validating xml documents including namespaces,
27   * the issue of prefixes (the short expression before the colon in a universal name)
28   * becomes important.
29   * DTDs are not namespace aware and so a fixed prefixed must be chosen 
30   * and used consistently.
31   * This class is used to supply consistent, user specified prefixes.
32   * </p>
33   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
34   * @version $Revision: 561314 $
35   */
36  public class NamespacePrefixMapper {
37      
38      private int count = 0;
39      private HashMap prefixesByUri = new HashMap();
40      
41      /**
42       * Gets the prefix to be used with the given namespace URI
43       * @param namespaceUri
44       * @return prefix, not null
45       */
46      public String getPrefix(String namespaceUri) {
47          String prefix = (String) prefixesByUri.get(namespaceUri);    
48          if (prefix == null) {
49              prefix = generatePrefix(namespaceUri);
50              setPrefix(namespaceUri, prefix);
51          }
52          return prefix;
53      }
54      
55      /**
56       * Sets the prefix to be used for the given namespace URI.
57       * This method does not check for clashes amongst the namespaces.
58       * Possibly it should.
59       * @param namespaceUri
60       * @param prefix
61       */
62      public void setPrefix(String namespaceUri, String prefix) {
63          prefixesByUri.put(namespaceUri, prefix);
64      }
65      
66      /**
67       * Generates a prefix for the given namespace Uri.
68       * Used to assign prefixes for unassigned namespaces.
69       * Subclass may wish to override this method to provide more
70       * sophisticated implementations. 
71       * @param namespaceUri URI, not null
72       * @return prefix, not null
73       */
74      protected String generatePrefix(String namespaceUri) {
75          String prefix = "bt" + ++count;
76          if (prefixesByUri.values().contains(prefix)) {
77              prefix = generatePrefix(namespaceUri);
78          }
79          return prefix;
80      }
81      
82  }