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  package org.apache.myfaces.view.facelets.compiler;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.myfaces.view.facelets.tag.TagLibrary;
25  
26  /**
27   * @author Jacob Hookom
28   * @version $Id$
29   */
30  final class NamespaceManager
31  {
32  
33      private final static class NS
34      {
35          public final String prefix;
36  
37          public final String namespace;
38  
39          public NS(String prefix, String ns)
40          {
41              this.prefix = prefix;
42              this.namespace = ns;
43          }
44      }
45  
46      private final List<NS> namespaces;
47  
48      /**
49       * 
50       */
51      public NamespaceManager()
52      {
53          this.namespaces = new ArrayList<NS>();
54      }
55  
56      public void reset()
57      {
58          this.namespaces.clear();
59      }
60  
61      public void pushNamespace(String prefix, String namespace)
62      {
63          NS ns = new NS(prefix, namespace);
64          this.namespaces.add(0, ns);
65      }
66  
67      public String getNamespace(String prefix)
68      {
69          NS ns = null;
70          for (int i = 0; i < this.namespaces.size(); i++)
71          {
72              ns = (NS) this.namespaces.get(i);
73              if (ns.prefix.equals(prefix))
74              {
75                  return ns.namespace;
76              }
77          }
78          return null;
79      }
80  
81      public void popNamespace(String prefix)
82      {
83          NS ns = null;
84          for (int i = 0; i < this.namespaces.size(); i++)
85          {
86              ns = (NS) this.namespaces.get(i);
87              if (ns.prefix.equals(prefix))
88              {
89                  this.namespaces.remove(i);
90                  return;
91              }
92          }
93      }
94  
95      public NamespaceUnit toNamespaceUnit(TagLibrary library)
96      {
97          NamespaceUnit unit = new NamespaceUnit(library);
98          if (this.namespaces.size() > 0)
99          {
100             NS ns = null;
101             for (int i = this.namespaces.size() - 1; i >= 0; i--)
102             {
103                 ns = this.namespaces.get(i);
104                 unit.setNamespace(ns.prefix, ns.namespace);
105             }
106         }
107         return unit;
108     }
109 
110 }