jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315.java
changeset 10694 cf59e2badd14
parent 1337 e8d6cef36199
child 18780 f47b920867e7
equal deleted inserted replaced
10693:6e9ebed2e783 10694:cf59e2badd14
    55  * @author Christian Geuer-Pollmann <geuerp@apache.org>
    55  * @author Christian Geuer-Pollmann <geuerp@apache.org>
    56  * @version $Revision: 1.5 $
    56  * @version $Revision: 1.5 $
    57  */
    57  */
    58 public abstract class Canonicalizer20010315 extends CanonicalizerBase {
    58 public abstract class Canonicalizer20010315 extends CanonicalizerBase {
    59         boolean firstCall=true;
    59         boolean firstCall=true;
    60         final SortedSet result= new TreeSet(COMPARE);
    60         final SortedSet<Attr> result= new TreeSet<Attr>(COMPARE);
    61     static final String XMLNS_URI=Constants.NamespaceSpecNS;
    61     static final String XMLNS_URI=Constants.NamespaceSpecNS;
    62     static final String XML_LANG_URI=Constants.XML_LANG_SPACE_SpecNS;
    62     static final String XML_LANG_URI=Constants.XML_LANG_SPACE_SpecNS;
    63     static class XmlAttrStack {
    63     static class XmlAttrStack {
    64         int currentLevel=0;
    64         int currentLevel=0;
    65         int lastlevel=0;
    65         int lastlevel=0;
    66         XmlsStackElement cur;
    66         XmlsStackElement cur;
    67         static class XmlsStackElement {
    67         static class XmlsStackElement {
    68                 int level;
    68                 int level;
    69                 boolean rendered=false;
    69                 boolean rendered=false;
    70                 List nodes=new ArrayList();
    70                 List<Attr> nodes=new ArrayList<Attr>();
    71         };
    71         };
    72         List levels=new ArrayList();
    72         List<XmlsStackElement> levels=new ArrayList<XmlsStackElement>();
    73         void push(int level) {
    73         void push(int level) {
    74                 currentLevel=level;
    74                 currentLevel=level;
    75                 if (currentLevel==-1)
    75                 if (currentLevel==-1)
    76                         return;
    76                         return;
    77                 cur=null;
    77                 cur=null;
    79                         levels.remove(levels.size()-1);
    79                         levels.remove(levels.size()-1);
    80                         if (levels.size()==0) {
    80                         if (levels.size()==0) {
    81                                 lastlevel=0;
    81                                 lastlevel=0;
    82                                 return;
    82                                 return;
    83                         }
    83                         }
    84                         lastlevel=((XmlsStackElement)levels.get(levels.size()-1)).level;
    84                         lastlevel=(levels.get(levels.size()-1)).level;
    85                 }
    85                 }
    86         }
    86         }
    87         void addXmlnsAttr(Attr n) {
    87         void addXmlnsAttr(Attr n) {
    88                 if (cur==null) {
    88                 if (cur==null) {
    89                         cur=new XmlsStackElement();
    89                         cur=new XmlsStackElement();
    91                         levels.add(cur);
    91                         levels.add(cur);
    92                         lastlevel=currentLevel;
    92                         lastlevel=currentLevel;
    93                 }
    93                 }
    94                 cur.nodes.add(n);
    94                 cur.nodes.add(n);
    95         }
    95         }
    96         void getXmlnsAttr(Collection col) {
    96         void getXmlnsAttr(Collection<Attr> col) {
    97                 int size=levels.size()-1;
    97                 int size=levels.size()-1;
    98                 if (cur==null) {
    98                 if (cur==null) {
    99                         cur=new XmlsStackElement();
    99                         cur=new XmlsStackElement();
   100                         cur.level=currentLevel;
   100                         cur.level=currentLevel;
   101                         lastlevel=currentLevel;
   101                         lastlevel=currentLevel;
   104                 boolean parentRendered=false;
   104                 boolean parentRendered=false;
   105                 XmlsStackElement e=null;
   105                 XmlsStackElement e=null;
   106                 if (size==-1) {
   106                 if (size==-1) {
   107                         parentRendered=true;
   107                         parentRendered=true;
   108                 } else {
   108                 } else {
   109                         e=(XmlsStackElement)levels.get(size);
   109                         e=levels.get(size);
   110                         if (e.rendered && e.level+1==currentLevel)
   110                         if (e.rendered && e.level+1==currentLevel)
   111                                 parentRendered=true;
   111                                 parentRendered=true;
   112 
   112 
   113                 }
   113                 }
   114                 if (parentRendered) {
   114                 if (parentRendered) {
   115                                 col.addAll(cur.nodes);
   115                                 col.addAll(cur.nodes);
   116                                 cur.rendered=true;
   116                                 cur.rendered=true;
   117                                 return;
   117                                 return;
   118                         }
   118                         }
   119 
   119 
   120                         Map loa = new HashMap();
   120                         Map<String,Attr> loa = new HashMap<String,Attr>();
   121                 for (;size>=0;size--) {
   121                 for (;size>=0;size--) {
   122                         e=(XmlsStackElement)levels.get(size);
   122                         e=levels.get(size);
   123                         Iterator it=e.nodes.iterator();
   123                         Iterator<Attr> it=e.nodes.iterator();
   124                         while (it.hasNext()) {
   124                         while (it.hasNext()) {
   125                                 Attr n=(Attr)it.next();
   125                                 Attr n=it.next();
   126                                 if (!loa.containsKey(n.getName()))
   126                                 if (!loa.containsKey(n.getName()))
   127                                         loa.put(n.getName(),n);
   127                                         loa.put(n.getName(),n);
   128                         }
   128                         }
   129                         //if (e.rendered)
   129                         //if (e.rendered)
   130                                 //break;
   130                                 //break;
   159     * @param E
   159     * @param E
   160     * @param ns
   160     * @param ns
   161     * @return the Attr[]s to be outputted
   161     * @return the Attr[]s to be outputted
   162     * @throws CanonicalizationException
   162     * @throws CanonicalizationException
   163     */
   163     */
   164    Iterator handleAttributesSubtree(Element E,  NameSpaceSymbTable ns )
   164    Iterator<Attr> handleAttributesSubtree(Element E,  NameSpaceSymbTable ns )
   165            throws CanonicalizationException {
   165            throws CanonicalizationException {
   166           if (!E.hasAttributes() && !firstCall) {
   166           if (!E.hasAttributes() && !firstCall) {
   167          return null;
   167          return null;
   168       }
   168       }
   169       // result will contain the attrs which have to be outputted
   169       // result will contain the attrs which have to be outputted
   170       final SortedSet result = this.result;
   170       final SortedSet<Attr> result = this.result;
   171       result.clear();
   171       result.clear();
   172       NamedNodeMap attrs = E.getAttributes();
   172       NamedNodeMap attrs = E.getAttributes();
   173       int attrsLength = attrs.getLength();
   173       int attrsLength = attrs.getLength();
   174 
   174 
   175       for (int i = 0; i < attrsLength; i++) {
   175       for (int i = 0; i < attrsLength; i++) {
   192 
   192 
   193          Node n=ns.addMappingAndRender(NName,NValue,N);
   193          Node n=ns.addMappingAndRender(NName,NValue,N);
   194 
   194 
   195           if (n!=null) {
   195           if (n!=null) {
   196                  //Render the ns definition
   196                  //Render the ns definition
   197              result.add(n);
   197              result.add((Attr)n);
   198              if (C14nHelper.namespaceIsRelative(N)) {
   198              if (C14nHelper.namespaceIsRelative(N)) {
   199                 Object exArgs[] = { E.getTagName(), NName, N.getNodeValue() };
   199                 Object exArgs[] = { E.getTagName(), NName, N.getNodeValue() };
   200                 throw new CanonicalizationException(
   200                 throw new CanonicalizationException(
   201                    "c14n.Canonicalizer.RelativeNamespace", exArgs);
   201                    "c14n.Canonicalizer.RelativeNamespace", exArgs);
   202              }
   202              }
   204       }
   204       }
   205 
   205 
   206       if (firstCall) {
   206       if (firstCall) {
   207         //It is the first node of the subtree
   207         //It is the first node of the subtree
   208         //Obtain all the namespaces defined in the parents, and added to the output.
   208         //Obtain all the namespaces defined in the parents, and added to the output.
   209         ns.getUnrenderedNodes(result);
   209         ns.getUnrenderedNodes(getSortedSetAsCollection(result));
   210         //output the attributes in the xml namespace.
   210         //output the attributes in the xml namespace.
   211         xmlattrStack.getXmlnsAttr(result);
   211         xmlattrStack.getXmlnsAttr(result);
   212                 firstCall=false;
   212                 firstCall=false;
   213       }
   213       }
   214 
   214 
   225     * @param E
   225     * @param E
   226     * @param ns
   226     * @param ns
   227     * @return the Attr[]s to be outputted
   227     * @return the Attr[]s to be outputted
   228     * @throws CanonicalizationException
   228     * @throws CanonicalizationException
   229     */
   229     */
   230    Iterator handleAttributes(Element E,  NameSpaceSymbTable ns ) throws CanonicalizationException {
   230    Iterator<Attr> handleAttributes(Element E,  NameSpaceSymbTable ns ) throws CanonicalizationException {
   231     // result will contain the attrs which have to be outputted
   231     // result will contain the attrs which have to be outputted
   232         xmlattrStack.push(ns.getLevel());
   232         xmlattrStack.push(ns.getLevel());
   233     boolean isRealVisible=isVisibleDO(E,ns.getLevel())==1;
   233     boolean isRealVisible=isVisibleDO(E,ns.getLevel())==1;
   234     NamedNodeMap attrs = null;
   234     NamedNodeMap attrs = null;
   235     int attrsLength = 0;
   235     int attrsLength = 0;
   237         attrs=E.getAttributes();
   237         attrs=E.getAttributes();
   238        attrsLength= attrs.getLength();
   238        attrsLength= attrs.getLength();
   239     }
   239     }
   240 
   240 
   241 
   241 
   242     SortedSet result = this.result;
   242     SortedSet<Attr> result = this.result;
   243     result.clear();
   243     result.clear();
   244 
   244 
   245     for (int i = 0; i < attrsLength; i++) {
   245     for (int i = 0; i < attrsLength; i++) {
   246        Attr N = (Attr) attrs.item(i);
   246        Attr N = (Attr) attrs.item(i);
   247        String NUri =N.getNamespaceURI();
   247        String NUri =N.getNamespaceURI();
   275                 }
   275                 }
   276                         //The xpath select this node output it if needed.
   276                         //The xpath select this node output it if needed.
   277                 //Node n=ns.addMappingAndRenderXNodeSet(NName,NValue,N,isRealVisible);
   277                 //Node n=ns.addMappingAndRenderXNodeSet(NName,NValue,N,isRealVisible);
   278                 Node n=ns.addMappingAndRender(NName,NValue,N);
   278                 Node n=ns.addMappingAndRender(NName,NValue,N);
   279                         if (n!=null) {
   279                         if (n!=null) {
   280                                         result.add(n);
   280                                         result.add((Attr)n);
   281                     if (C14nHelper.namespaceIsRelative(N)) {
   281                     if (C14nHelper.namespaceIsRelative(N)) {
   282                        Object exArgs[] = { E.getTagName(), NName, N.getNodeValue() };
   282                        Object exArgs[] = { E.getTagName(), NName, N.getNodeValue() };
   283                        throw new CanonicalizationException(
   283                        throw new CanonicalizationException(
   284                           "c14n.Canonicalizer.RelativeNamespace", exArgs);
   284                           "c14n.Canonicalizer.RelativeNamespace", exArgs);
   285                    }
   285                    }
   304                 //then xmlns=""
   304                 //then xmlns=""
   305                 n=ns.addMappingAndRender(XMLNS,"",nullNode);
   305                 n=ns.addMappingAndRender(XMLNS,"",nullNode);
   306         }
   306         }
   307         //output the xmlns def if needed.
   307         //output the xmlns def if needed.
   308         if (n!=null) {
   308         if (n!=null) {
   309                         result.add(n);
   309                         result.add((Attr)n);
   310         }
   310         }
   311         //Float all xml:* attributes of the unselected parent elements to this one.
   311         //Float all xml:* attributes of the unselected parent elements to this one.
   312         //addXmlAttributes(E,result);
   312         //addXmlAttributes(E,result);
   313         xmlattrStack.getXmlnsAttr(result);
   313         xmlattrStack.getXmlnsAttr(result);
   314         ns.getUnrenderedNodes(result);
   314         ns.getUnrenderedNodes(getSortedSetAsCollection(result));
   315 
   315 
   316     }
   316     }
   317 
   317 
   318     return result.iterator();
   318     return result.iterator();
   319    }
   319    }
   323     * @param xpathNodeSet
   323     * @param xpathNodeSet
   324     * @param inclusiveNamespaces
   324     * @param inclusiveNamespaces
   325     * @return none it always fails
   325     * @return none it always fails
   326     * @throws CanonicalizationException always
   326     * @throws CanonicalizationException always
   327     */
   327     */
   328    public byte[] engineCanonicalizeXPathNodeSet(Set xpathNodeSet, String inclusiveNamespaces)
   328    public byte[] engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet, String inclusiveNamespaces)
   329            throws CanonicalizationException {
   329            throws CanonicalizationException {
   330 
   330 
   331       /** $todo$ well, should we throw UnsupportedOperationException ? */
   331       /** $todo$ well, should we throw UnsupportedOperationException ? */
   332       throw new CanonicalizationException(
   332       throw new CanonicalizationException(
   333          "c14n.Canonicalizer.UnsupportedOperation");
   333          "c14n.Canonicalizer.UnsupportedOperation");