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  package org.apache.portals.bridges.util;
    18
    19  import java.util.Enumeration;
    20  import java.util.NoSuchElementException;
    21
    22  /***
    23   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
    24   * @version $Id: NamespacedNamesEnumeration.java 549659 2007-06-22 01:21:29Z ate $
    25   */
    26  public class NamespacedNamesEnumeration implements Enumeration
    27  {
    28      private Enumeration namesEnumeration;
    29      private String      namespace;
    30
    31      private String nextName;
    32      private boolean done;
    33
    34      public NamespacedNamesEnumeration(Enumeration namesEnumeration, String namespace)
    35      {
    36          this.namesEnumeration = namesEnumeration;
    37          this.namespace = namespace;
    38          hasMoreElements();
    39      }
    40
    41      public boolean hasMoreElements()
    42      {
    43          if (!done)
    44          {
    45              if (nextName == null)
    46              {
    47                  while (namesEnumeration.hasMoreElements())
    48                  {
    49                      String name = (String)namesEnumeration.nextElement();
    50                      if ( name.startsWith(namespace))
    51                      {
    52                          nextName = name.substring(namespace.length());
    53                          break;
    54                      }
    55                  }
    56                  done = nextName == null;
    57              }
    58          }
    59          return !done;
    60      }
    61
    62      public Object nextElement()
    63      {
    64          if (done)
    65          {
    66              throw new NoSuchElementException();
    67          }
    68          String name = nextName;
    69          nextName = null;
    70          return name;
    71      }
    72  }