All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jcp.xml.dsig.internal.dom.SubDocumentData Maven / Gradle / Ivy

The newest version!
/*     */ package org.jcp.xml.dsig.internal.dom;
/*     */ 
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.ListIterator;
/*     */ import java.util.NoSuchElementException;
/*     */ import javax.xml.crypto.NodeSetData;
/*     */ import org.w3c.dom.DOMException;
/*     */ import org.w3c.dom.NamedNodeMap;
/*     */ import org.w3c.dom.Node;
/*     */ import org.w3c.dom.traversal.NodeFilter;
/*     */ import org.w3c.dom.traversal.NodeIterator;
/*     */ 
/*     */ public class SubDocumentData
/*     */   implements NodeSetData
/*     */ {
/*     */   public static final int SHOW_ALL_EXCEPT_COMMENTS = 3967;
/*     */   private boolean withComments;
/*     */   private NodeIterator ni;
/*     */ 
/*     */   public SubDocumentData(Node root, boolean withComments, NodeFilter twFilter)
/*     */   {
/*  43 */     this.ni = new DelayedNodeIterator(root, withComments);
/*  44 */     this.withComments = withComments;
/*     */   }
/*     */ 
/*     */   public Iterator iterator() {
/*  48 */     return new Iterator() {
/*     */       public boolean hasNext() {
/*  50 */         if (SubDocumentData.this.ni.nextNode() == null) {
/*  51 */           return false;
/*     */         }
/*  53 */         SubDocumentData.this.ni.previousNode();
/*  54 */         return true;
/*     */       }
/*     */ 
/*     */       public Object next()
/*     */       {
/*  59 */         Node node = SubDocumentData.this.ni.nextNode();
/*  60 */         if (node == null) {
/*  61 */           throw new NoSuchElementException();
/*     */         }
/*  63 */         return node;
/*     */       }
/*     */ 
/*     */       public void remove()
/*     */       {
/*  68 */         throw new UnsupportedOperationException();
/*     */       }
/*     */     };
/*     */   }
/*     */ 
/*     */   public boolean withComments() {
/*  74 */     return this.withComments;
/*     */   }
/*     */ 
/*     */   public NodeIterator nodeIterator() {
/*  78 */     return this.ni;
/*     */   }
/*     */ 
/*     */   static class DelayedNodeIterator implements NodeIterator {
/*     */     private Node root;
/*     */     private List nodeSet;
/*     */     private ListIterator li;
/*  89 */     private boolean detached = false;
/*     */     private boolean withComments;
/*     */ 
/*     */     DelayedNodeIterator(Node root, boolean withComments) {
/*  93 */       this.root = root;
/*  94 */       this.withComments = withComments;
/*     */     }
/*     */ 
/*     */     public int getWhatToShow() {
/*  98 */       if (this.withComments)
/*     */       {
/* 100 */         return -1;
/*     */       }
/*     */ 
/* 103 */       return 3967;
/*     */     }
/*     */ 
/*     */     public Node getRoot()
/*     */     {
/* 108 */       return this.root;
/*     */     }
/*     */ 
/*     */     public NodeFilter getFilter() {
/* 112 */       return null;
/*     */     }
/*     */ 
/*     */     public Node nextNode() throws DOMException {
/* 116 */       if (this.detached) {
/* 117 */         throw new DOMException((short)11, "");
/*     */       }
/* 119 */       if (this.nodeSet == null) {
/* 120 */         this.nodeSet = dereferenceSameDocumentURI(this.root);
/* 121 */         this.li = this.nodeSet.listIterator();
/*     */       }
/* 123 */       if (this.li.hasNext()) {
/* 124 */         return (Node)this.li.next();
/*     */       }
/* 126 */       return null;
/*     */     }
/*     */ 
/*     */     public Node previousNode() throws DOMException
/*     */     {
/* 131 */       if (this.detached) {
/* 132 */         throw new DOMException((short)11, "");
/*     */       }
/* 134 */       if (this.nodeSet == null) {
/* 135 */         this.nodeSet = dereferenceSameDocumentURI(this.root);
/* 136 */         this.li = this.nodeSet.listIterator();
/*     */       }
/* 138 */       if (this.li.hasPrevious()) {
/* 139 */         return (Node)this.li.previous();
/*     */       }
/* 141 */       return null;
/*     */     }
/*     */ 
/*     */     public boolean getExpandEntityReferences()
/*     */     {
/* 146 */       return true;
/*     */     }
/*     */ 
/*     */     public void detach() {
/* 150 */       this.detached = true;
/*     */     }
/*     */ 
/*     */     private List dereferenceSameDocumentURI(Node node)
/*     */     {
/* 161 */       List nodeSet = new ArrayList();
/* 162 */       if (node != null) {
/* 163 */         nodeSetMinusCommentNodes(node, nodeSet, null);
/*     */       }
/* 165 */       return nodeSet;
/*     */     }
/*     */ 
/*     */     private void nodeSetMinusCommentNodes(Node node, List nodeSet, Node prevSibling)
/*     */     {
/* 179 */       switch (node.getNodeType()) {
/*     */       case 1:
/* 181 */         NamedNodeMap attrs = node.getAttributes();
/* 182 */         if (attrs != null) {
/* 183 */           int i = 0; for (int len = attrs.getLength(); i < len; i++) {
/* 184 */             nodeSet.add(attrs.item(i));
/*     */           }
/*     */         }
/* 187 */         nodeSet.add(node);
/* 188 */         Node pSibling = null;
/* 189 */         for (Node child = node.getFirstChild(); child != null; 
/* 190 */           child = child.getNextSibling()) {
/* 191 */           nodeSetMinusCommentNodes(child, nodeSet, pSibling);
/* 192 */           pSibling = child;
/*     */         }
/* 194 */         break;
/*     */       case 3:
/*     */       case 4:
/* 199 */         if ((prevSibling != null) && ((prevSibling.getNodeType() == 3) || (prevSibling.getNodeType() == 4)))
/*     */         {
/* 201 */           return;
/*     */         }
/*     */       case 7:
/* 204 */         nodeSet.add(node);
/* 205 */         break;
/*     */       case 8:
/* 207 */         if (this.withComments)
/* 208 */           nodeSet.add(node);
/*     */         break;
/*     */       case 2:
/*     */       case 5:
/*     */       case 6:
/*     */       }
/*     */     }
/*     */   }
/*     */ }

/* Location:           E:\HYN\Java\trunk\ref\lib-dep\xmldsig\xmldsig.jar
 * Qualified Name:     org.jcp.xml.dsig.internal.dom.SubDocumentData
 * JD-Core Version:    0.6.2
 */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy