Code example for Hashtable

Methods: clearput

0
    { 
  
 
        // Define the default namespace (initially maps to "" uri) 
        Stack stack;
        m_namespaces.put(EMPTYSTRING, stack = new Stack());
        stack.push(new MappingRecord(EMPTYSTRING,EMPTYSTRING,0));
 
        m_namespaces.put(XML_PREFIX, stack = new Stack());
        stack.push(new MappingRecord( XML_PREFIX,
            "http://www.w3.org/XML/1998/namespace",0)); 
 
        m_nodeStack.push(new MappingRecord(null,null,-1));
 
    } 
 
    /** 
     * Use a namespace prefix to lookup a namespace URI. 
     *  
     * @param prefix String the prefix of the namespace 
     * @return the URI corresponding to the prefix 
     */ 
    public String lookupNamespace(String prefix)
    { 
        final Stack stack = (Stack) m_namespaces.get(prefix);
        return stack != null && !stack.isEmpty() ? 
            ((MappingRecord) stack.peek()).m_uri : null;
    } 
     
    MappingRecord getMappingFromPrefix(String prefix) {
        final Stack stack = (Stack) m_namespaces.get(prefix);
        return stack != null && !stack.isEmpty() ? 
            ((MappingRecord) stack.peek()) : null;
    } 
 
    /** 
     * Given a namespace uri, and the namespaces mappings for the  
     * current element, return the current prefix for that uri. 
     *  
     * @param uri the namespace URI to be search for 
     * @return an existing prefix that maps to the given URI, null if no prefix 
     * maps to the given namespace URI. 
     */ 
    public String lookupPrefix(String uri)
    { 
        String foundPrefix = null;
        Enumeration prefixes = m_namespaces.keys();
        while (prefixes.hasMoreElements())
        { 
            String prefix = (String) prefixes.nextElement();
            String uri2 = lookupNamespace(prefix);
            if (uri2 != null && uri2.equals(uri))
            { 
                foundPrefix = prefix;
                break; 
            } 
        } 
        return foundPrefix;
    } 
     
    MappingRecord getMappingFromURI(String uri)
    { 
        MappingRecord foundMap = null;
        Enumeration prefixes = m_namespaces.keys();
        while (prefixes.hasMoreElements())
        { 
            String prefix = (String) prefixes.nextElement();
            MappingRecord map2 = getMappingFromPrefix(prefix);
            if (map2 != null && (map2.m_uri).equals(uri))
            { 
                foundMap = map2;
                break; 
            } 
        } 
        return foundMap;
    } 
 
    /** 
     * Undeclare the namespace that is currently pointed to by a given prefix 
     */ 
    boolean popNamespace(String prefix)
    { 
        // Prefixes "xml" and "xmlns" cannot be redefined 
        if (prefix.startsWith(XML_PREFIX))
        { 
            return false; 
        } 
 
        Stack stack;
        if ((stack = (Stack) m_namespaces.get(prefix)) != null)
        { 
            stack.pop();
            return true; 
        } 
        return false; 
    } 
 
    /** 
     * Declare a mapping of a prefix to namespace URI at the given element depth. 
     * @param prefix a String with the prefix for a qualified name 
     * @param uri a String with the uri to which the prefix is to map 
     * @param elemDepth the depth of current declaration 
     */ 
    boolean pushNamespace(String prefix, String uri, int elemDepth)
    { 
        // Prefixes "xml" and "xmlns" cannot be redefined 
        if (prefix.startsWith(XML_PREFIX))
        { 
            return false; 
        } 
 
        Stack stack;
        // Get the stack that contains URIs for the specified prefix 
        if ((stack = (Stack) m_namespaces.get(prefix)) == null)
        { 
            m_namespaces.put(prefix, stack = new Stack());
        } 
 
        if (!stack.empty() && uri.equals(((MappingRecord)stack.peek()).m_uri))
        { 
            return false; 
        } 
        MappingRecord map = new MappingRecord(prefix,uri,elemDepth);
        stack.push(map);
        m_nodeStack.push(map);
        return true; 
    } 
 
    /** 
     * Pop, or undeclare all namespace definitions that are currently 
     * declared at the given element depth, or deepter. 
     * @param elemDepth the element depth for which mappings declared at this 
     * depth or deeper will no longer be valid 
     * @param saxHandler The ContentHandler to notify of any endPrefixMapping() 
     * calls.  This parameter can be null. 
     */ 
    void popNamespaces(int elemDepth, ContentHandler saxHandler)
    { 
        while (true) 
        { 
            if (m_nodeStack.isEmpty())
                return; 
            MappingRecord map = (MappingRecord)(m_nodeStack.peek());
            int depth = map.m_declarationDepth;
            if (depth < elemDepth)
                return; 
            /* the depth of the declared mapping is elemDepth or deeper 
             * so get rid of it 
             */ 
 
            map = (MappingRecord) m_nodeStack.pop();
            final String prefix = map.m_prefix; 
            popNamespace(prefix);
            if (saxHandler != null)
            { 
                try 
                { 
                    saxHandler.endPrefixMapping(prefix);
                } 
                catch (SAXException e)
                { 
                    // not much we can do if they aren't willing to listen 
                } 
            } 
                
        } 
    } 
 
    /** 
     * Generate a new namespace prefix ( ns0, ns1 ...) not used before 
     * @return String a new namespace prefix ( ns0, ns1, ns2 ...) 
     */ 
    public String generateNextPrefix()
    { 
        return "ns" + (count++);
    } 
 
  
    /** 
     * This method makes a clone of this object. 
     * 
     */ 
    public Object clone() throws CloneNotSupportedException {
        NamespaceMappings clone = new NamespaceMappings();
        clone.m_nodeStack = (Stack) m_nodeStack.clone();
        clone.m_namespaces = (Hashtable) m_namespaces.clone();
         
        clone.count = count;
        return clone;
         
    } 
     
    final void reset() 
    { 
        this.count = 0;
        this.m_namespaces.clear();
        this.m_nodeStack.clear();        
        initNamespaces(); 
    } 
     
    class MappingRecord { 
Stop searching for code, let great code find you!  Add Codota to your java IDE