src/java.xml/share/classes/com/sun/org/apache/xerces/internal/dom/NamedNodeMapImpl.java
changeset 47359 e1a6c0168741
parent 47216 71c04702a3d5
child 48409 5ab69533994b
equal deleted inserted replaced
47358:d07d5f7cab35 47359:e1a6c0168741
     1 /*
     1 /*
     2  * reserved comment block
     2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT REMOVE OR ALTER!
     3  * @LastModified: Oct 2017
     4  */
     4  */
     5 /*
     5 /*
     6  * Licensed to the Apache Software Foundation (ASF) under one or more
     6  * Licensed to the Apache Software Foundation (ASF) under one or more
     7  * contributor license agreements.  See the NOTICE file distributed with
     7  * contributor license agreements.  See the NOTICE file distributed with
     8  * this work for additional information regarding copyright ownership.
     8  * this work for additional information regarding copyright ownership.
    26 import java.io.ObjectOutputStream;
    26 import java.io.ObjectOutputStream;
    27 import java.io.Serializable;
    27 import java.io.Serializable;
    28 import java.util.ArrayList;
    28 import java.util.ArrayList;
    29 import java.util.List;
    29 import java.util.List;
    30 import java.util.Vector;
    30 import java.util.Vector;
    31 
       
    32 import org.w3c.dom.DOMException;
    31 import org.w3c.dom.DOMException;
    33 import org.w3c.dom.NamedNodeMap;
    32 import org.w3c.dom.NamedNodeMap;
    34 import org.w3c.dom.Node;
    33 import org.w3c.dom.Node;
    35 
    34 
    36 /**
    35 /**
    78     protected final static short READONLY     = 0x1<<0;
    77     protected final static short READONLY     = 0x1<<0;
    79     protected final static short CHANGED      = 0x1<<1;
    78     protected final static short CHANGED      = 0x1<<1;
    80     protected final static short HASDEFAULTS  = 0x1<<2;
    79     protected final static short HASDEFAULTS  = 0x1<<2;
    81 
    80 
    82     /** Nodes. */
    81     /** Nodes. */
    83     protected List nodes;
    82     protected List<Node> nodes;
    84 
    83 
    85     protected NodeImpl ownerNode; // the node this map belongs to
    84     protected NodeImpl ownerNode; // the node this map belongs to
    86 
    85 
    87     //
    86     //
    88     // Constructors
    87     // Constructors
   120      *
   119      *
   121      * @return the node which currenly has the specified index, or null if index
   120      * @return the node which currenly has the specified index, or null if index
   122      * is greater than or equal to getLength().
   121      * is greater than or equal to getLength().
   123      */
   122      */
   124     public Node item(int index) {
   123     public Node item(int index) {
   125         return (nodes != null && index < nodes.size()) ?
   124         return (nodes != null && index < nodes.size()) ? (nodes.get(index)) : null;
   126                     (Node)(nodes.get(index)) : null;
       
   127     }
   125     }
   128 
   126 
   129     /**
   127     /**
   130      * Retrieve a node by name.
   128      * Retrieve a node by name.
   131      *
   129      *
   134      * null if no value has been assigned to that name.
   132      * null if no value has been assigned to that name.
   135      */
   133      */
   136     public Node getNamedItem(String name) {
   134     public Node getNamedItem(String name) {
   137 
   135 
   138         int i = findNamePoint(name,0);
   136         int i = findNamePoint(name,0);
   139         return (i < 0) ? null : (Node)(nodes.get(i));
   137         return (i < 0) ? null : (nodes.get(i));
   140 
   138 
   141     } // getNamedItem(String):Node
   139     } // getNamedItem(String):Node
   142 
   140 
   143     /**
   141     /**
   144      * Introduced in DOM Level 2. <p>
   142      * Introduced in DOM Level 2. <p>
   152      *                      name did not identify any node in the map.
   150      *                      name did not identify any node in the map.
   153      */
   151      */
   154     public Node getNamedItemNS(String namespaceURI, String localName) {
   152     public Node getNamedItemNS(String namespaceURI, String localName) {
   155 
   153 
   156         int i = findNamePoint(namespaceURI, localName);
   154         int i = findNamePoint(namespaceURI, localName);
   157         return (i < 0) ? null : (Node)(nodes.get(i));
   155         return (i < 0) ? null : (nodes.get(i));
   158 
   156 
   159     } // getNamedItemNS(String,String):Node
   157     } // getNamedItemNS(String,String):Node
   160 
   158 
   161     /**
   159     /**
   162      * Adds a node using its nodeName attribute.
   160      * Adds a node using its nodeName attribute.
   196             previous = (NodeImpl) nodes.get(i);
   194             previous = (NodeImpl) nodes.get(i);
   197             nodes.set(i, arg);
   195             nodes.set(i, arg);
   198         } else {
   196         } else {
   199             i = -1 - i; // Insert point (may be end of list)
   197             i = -1 - i; // Insert point (may be end of list)
   200             if (null == nodes) {
   198             if (null == nodes) {
   201                 nodes = new ArrayList(5);
   199                 nodes = new ArrayList<>(5);
   202             }
   200             }
   203             nodes.add(i, arg);
   201             nodes.add(i, arg);
   204         }
   202         }
   205         return previous;
   203         return previous;
   206 
   204 
   246                 previous = (NodeImpl) nodes.get(i);
   244                 previous = (NodeImpl) nodes.get(i);
   247                 nodes.add(i, arg);
   245                 nodes.add(i, arg);
   248             } else {
   246             } else {
   249                 i = -1 - i; // Insert point (may be end of list)
   247                 i = -1 - i; // Insert point (may be end of list)
   250                 if (null == nodes) {
   248                 if (null == nodes) {
   251                     nodes = new ArrayList(5);
   249                     nodes = new ArrayList<>(5);
   252                 }
   250                 }
   253                 nodes.add(i, arg);
   251                 nodes.add(i, arg);
   254             }
   252             }
   255         }
   253         }
   256         return previous;
   254         return previous;
   335         newmap.cloneContent(this);
   333         newmap.cloneContent(this);
   336         return newmap;
   334         return newmap;
   337     }
   335     }
   338 
   336 
   339     protected void cloneContent(NamedNodeMapImpl srcmap) {
   337     protected void cloneContent(NamedNodeMapImpl srcmap) {
   340         List srcnodes = srcmap.nodes;
   338         List<Node> srcnodes = srcmap.nodes;
   341         if (srcnodes != null) {
   339         if (srcnodes != null) {
   342             int size = srcnodes.size();
   340             int size = srcnodes.size();
   343             if (size != 0) {
   341             if (size != 0) {
   344                 if (nodes == null) {
   342                 if (nodes == null) {
   345                     nodes = new ArrayList(size);
   343                     nodes = new ArrayList<>(size);
   346                 }
   344                 }
   347                 else {
   345                 else {
   348                     nodes.clear();
   346                     nodes.clear();
   349                 }
   347                 }
   350                 for (int i = 0; i < size; ++i) {
   348                 for (int i = 0; i < size; ++i) {
   455             int first = start;
   453             int first = start;
   456             int last  = nodes.size() - 1;
   454             int last  = nodes.size() - 1;
   457 
   455 
   458             while (first <= last) {
   456             while (first <= last) {
   459                 i = (first + last) / 2;
   457                 i = (first + last) / 2;
   460                 int test = name.compareTo(((Node)(nodes.get(i))).getNodeName());
   458                 int test = name.compareTo(((nodes.get(i))).getNodeName());
   461                 if (test == 0) {
   459                 if (test == 0) {
   462                     return i; // Name found
   460                     return i; // Name found
   463                 }
   461                 }
   464                 else if (test < 0) {
   462                 else if (test < 0) {
   465                     last = i - 1;
   463                     last = i - 1;
   520     protected boolean precedes(Node a, Node b) {
   518     protected boolean precedes(Node a, Node b) {
   521 
   519 
   522         if (nodes != null) {
   520         if (nodes != null) {
   523             final int size = nodes.size();
   521             final int size = nodes.size();
   524             for (int i = 0; i < size; ++i) {
   522             for (int i = 0; i < size; ++i) {
   525                 Node n = (Node)nodes.get(i);
   523                 Node n = nodes.get(i);
   526                 if (n==a) return true;
   524                 if (n==a) return true;
   527                 if (n==b) return false;
   525                 if (n==b) return false;
   528             }
   526             }
   529         }
   527         }
   530         return false;
   528         return false;
   561                 nodes.add(i, arg);
   559                 nodes.add(i, arg);
   562             }
   560             }
   563             else {
   561             else {
   564                 i = -1 - i; // Insert point (may be end of list)
   562                 i = -1 - i; // Insert point (may be end of list)
   565                 if (null == nodes) {
   563                 if (null == nodes) {
   566                     nodes = new ArrayList(5);
   564                     nodes = new ArrayList<>(5);
   567                 }
   565                 }
   568                 nodes.add(i, arg);
   566                 nodes.add(i, arg);
   569             }
   567             }
   570         }
   568         }
   571         return i;
   569         return i;
   575      * NON-DOM: copy content of this map into the specified ArrayList
   573      * NON-DOM: copy content of this map into the specified ArrayList
   576      *
   574      *
   577      * @param list   ArrayList to copy information into.
   575      * @param list   ArrayList to copy information into.
   578      * @return A copy of this node named map
   576      * @return A copy of this node named map
   579      */
   577      */
   580     protected ArrayList cloneMap(ArrayList list) {
   578     protected List<Node> cloneMap(List<Node> list) {
   581         if (list == null) {
       
   582             list = new ArrayList(5);
       
   583         }
       
   584         list.clear();
       
   585         if (nodes != null) {
   579         if (nodes != null) {
   586             final int size = nodes.size();
   580             list = new ArrayList<>(nodes);
   587             for (int i = 0; i < size; ++i) {
       
   588                 list.add(nodes.get(i));
       
   589             }
       
   590         }
   581         }
   591         return list;
   582         return list;
   592     }
   583     }
   593 
   584 
   594      protected int getNamedItemIndex(String namespaceURI, String localName) {
   585      protected int getNamedItemIndex(String namespaceURI, String localName) {
   606 
   597 
   607     private void readObject(ObjectInputStream in)
   598     private void readObject(ObjectInputStream in)
   608         throws IOException, ClassNotFoundException {
   599         throws IOException, ClassNotFoundException {
   609         in.defaultReadObject();
   600         in.defaultReadObject();
   610         if (nodes != null) {
   601         if (nodes != null) {
   611             nodes = new ArrayList(nodes);
   602             // nodes are written as a Vector for compatibility.
       
   603             nodes = new ArrayList<>(nodes);
   612         }
   604         }
   613     }
   605     }
   614 
   606 
   615     private void writeObject(ObjectOutputStream out) throws IOException {
   607     private void writeObject(ObjectOutputStream out) throws IOException {
   616         List oldNodes = this.nodes;
   608         List<Node> oldNodes = this.nodes;
   617         try {
   609         try {
   618             if (oldNodes != null) {
   610             if (oldNodes != null) {
   619                 this.nodes = new Vector(oldNodes);
   611                 // convert to Vector for backward-compatibility
       
   612                 this.nodes = new Vector<>(oldNodes);
   620             }
   613             }
   621             out.defaultWriteObject();
   614             out.defaultWriteObject();
   622         }
   615         }
   623         // If the write fails for some reason ensure
   616         // If the write fails for some reason ensure
   624         // that we restore the original object.
   617         // that we restore the original object.