jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/CanonicalizerBase.java
changeset 22325 f22d936a0ff5
parent 18780 f47b920867e7
child 22337 4dc66bdfd4ea
equal deleted inserted replaced
22324:54a74fc6e85c 22325:f22d936a0ff5
    32 import java.util.List;
    32 import java.util.List;
    33 import java.util.ListIterator;
    33 import java.util.ListIterator;
    34 import java.util.Map;
    34 import java.util.Map;
    35 import java.util.Set;
    35 import java.util.Set;
    36 
    36 
    37 import javax.xml.parsers.DocumentBuilder;
       
    38 import javax.xml.parsers.DocumentBuilderFactory;
       
    39 import javax.xml.parsers.ParserConfigurationException;
    37 import javax.xml.parsers.ParserConfigurationException;
    40 
    38 
    41 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
    39 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
    42 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizerSpi;
    40 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizerSpi;
    43 import com.sun.org.apache.xml.internal.security.c14n.helper.AttrCompare;
    41 import com.sun.org.apache.xml.internal.security.c14n.helper.AttrCompare;
    47 import com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream;
    45 import com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream;
    48 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
    46 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
    49 import org.w3c.dom.Attr;
    47 import org.w3c.dom.Attr;
    50 import org.w3c.dom.Comment;
    48 import org.w3c.dom.Comment;
    51 import org.w3c.dom.Element;
    49 import org.w3c.dom.Element;
       
    50 import org.w3c.dom.Document;
    52 import org.w3c.dom.NamedNodeMap;
    51 import org.w3c.dom.NamedNodeMap;
    53 import org.w3c.dom.Node;
    52 import org.w3c.dom.Node;
    54 import org.w3c.dom.ProcessingInstruction;
    53 import org.w3c.dom.ProcessingInstruction;
    55 import org.xml.sax.SAXException;
    54 import org.xml.sax.SAXException;
    56 
    55 
    62 public abstract class CanonicalizerBase extends CanonicalizerSpi {
    61 public abstract class CanonicalizerBase extends CanonicalizerSpi {
    63     public static final String XML = "xml";
    62     public static final String XML = "xml";
    64     public static final String XMLNS = "xmlns";
    63     public static final String XMLNS = "xmlns";
    65 
    64 
    66     protected static final AttrCompare COMPARE = new AttrCompare();
    65     protected static final AttrCompare COMPARE = new AttrCompare();
    67     protected static final Attr nullNode;
       
    68 
    66 
    69     private static final byte[] END_PI = {'?','>'};
    67     private static final byte[] END_PI = {'?','>'};
    70     private static final byte[] BEGIN_PI = {'<','?'};
    68     private static final byte[] BEGIN_PI = {'<','?'};
    71     private static final byte[] END_COMM = {'-','-','>'};
    69     private static final byte[] END_COMM = {'-','-','>'};
    72     private static final byte[] BEGIN_COMM = {'<','!','-','-'};
    70     private static final byte[] BEGIN_COMM = {'<','!','-','-'};
    82 
    80 
    83     protected static final int NODE_BEFORE_DOCUMENT_ELEMENT = -1;
    81     protected static final int NODE_BEFORE_DOCUMENT_ELEMENT = -1;
    84     protected static final int NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT = 0;
    82     protected static final int NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT = 0;
    85     protected static final int NODE_AFTER_DOCUMENT_ELEMENT = 1;
    83     protected static final int NODE_AFTER_DOCUMENT_ELEMENT = 1;
    86 
    84 
    87     static {
       
    88         // The null xmlns definition.
       
    89         try {
       
    90             DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
       
    91             nullNode = documentBuilder.newDocument().createAttributeNS(Constants.NamespaceSpecNS, XMLNS);
       
    92             nullNode.setValue("");
       
    93         } catch (Exception e) {
       
    94             throw new RuntimeException("Unable to create nullNode: " + e);
       
    95         }
       
    96     }
       
    97 
       
    98     private List<NodeFilter> nodeFilter;
    85     private List<NodeFilter> nodeFilter;
    99 
    86 
   100     private boolean includeComments;
    87     private boolean includeComments;
   101     private Set<Node> xpathNodeSet;
    88     private Set<Node> xpathNodeSet;
       
    89 
   102     /**
    90     /**
   103      * The node to be skipped/excluded from the DOM tree
    91      * The node to be skipped/excluded from the DOM tree
   104      * in subtree canonicalizations.
    92      * in subtree canonicalizations.
   105      */
    93      */
   106     private Node excludeNode;
    94     private Node excludeNode;
   107     private OutputStream writer = new ByteArrayOutputStream();
    95     private OutputStream writer = new ByteArrayOutputStream();
       
    96 
       
    97     /**
       
    98      * The null xmlns definition.
       
    99      */
       
   100     private Attr nullNode;
   108 
   101 
   109     /**
   102     /**
   110      * Constructor CanonicalizerBase
   103      * Constructor CanonicalizerBase
   111      *
   104      *
   112      * @param includeComments
   105      * @param includeComments
   639             handleParent(ele, ns);
   632             handleParent(ele, ns);
   640         }
   633         }
   641         parents.clear();
   634         parents.clear();
   642         Attr nsprefix;
   635         Attr nsprefix;
   643         if (((nsprefix = ns.getMappingWithoutRendered(XMLNS)) != null)
   636         if (((nsprefix = ns.getMappingWithoutRendered(XMLNS)) != null)
   644             && "".equals(nsprefix.getValue())) {
   637                 && "".equals(nsprefix.getValue())) {
   645             ns.addMappingAndRender(XMLNS, "", nullNode);
   638             ns.addMappingAndRender(
       
   639                     XMLNS, "", getNullNode(nsprefix.getOwnerDocument()));
   646         }
   640         }
   647     }
   641     }
   648 
   642 
   649     /**
   643     /**
   650      * Obtain the attributes to output for this node in XPathNodeSet c14n.
   644      * Obtain the attributes to output for this node in XPathNodeSet c14n.
   877             }
   871             }
   878             writer.write(toWrite);
   872             writer.write(toWrite);
   879         }
   873         }
   880     }
   874     }
   881 
   875 
       
   876     // The null xmlns definition.
       
   877     protected Attr getNullNode(Document ownerDocument) {
       
   878         if (nullNode == null) {
       
   879             try {
       
   880                 nullNode = ownerDocument.createAttributeNS(
       
   881                                     Constants.NamespaceSpecNS, XMLNS);
       
   882                 nullNode.setValue("");
       
   883             } catch (Exception e) {
       
   884                 throw new RuntimeException("Unable to create nullNode: " + e);
       
   885             }
       
   886         }
       
   887         return nullNode;
       
   888     }
       
   889 
   882 }
   890 }