FileDocCategorySizeDatePackage
XMLSignatureInput.javaAPI DocJava SE 6 API18698Tue Jun 10 00:23:04 BST 2008com.sun.org.apache.xml.internal.security.signature

XMLSignatureInput

public class XMLSignatureInput extends Object implements Cloneable
Class XMLSignatureInput
author
Christian Geuer-Pollmann $todo$ check whether an XMLSignatureInput can be _both_, octet stream _and_ node set?

Fields Summary
static Logger
log
InputStream
_inputOctetStreamProxy
Some InputStreams do not support the {@link java.io.InputStream#reset} method, so we read it in completely and work on our Proxy.
Set
_inputNodeSet
The original NodeSet for this XMLSignatureInput
Node
_subNode
The original Element
Node
excludeNode
Exclude Node *for enveloped transformations*
boolean
excludeComments
boolean
isNodeSet
byte[]
bytes
A cached bytes
private String
_MIMEType
Some Transforms may require explicit MIME type, charset (IANA registered "character set"), or other such information concerning the data they are receiving from an earlier Transform or the source data, although no Transform algorithm specified in this document needs such explicit information. Such data characteristics are provided as parameters to the Transform algorithm and should be described in the specification for the algorithm.
private String
_SourceURI
Field _SourceURI
List
nodeFilters
Node Filter list.
boolean
needsToBeExpanded
OutputStream
outputStream
Constructors Summary
public XMLSignatureInput(byte[] inputOctets)
Construct a XMLSignatureInput from an octet array.

This is a comfort method, which internally converts the byte[] array into an InputStream

NOTE: no defensive copy

param
inputOctets an octet array which including XML document or node


                                            
      

      // NO  defensive copy
   	  
      //this._inputOctetStreamProxy = new ByteArrayInputStream(inputOctets);
      this.bytes=inputOctets;
   
public XMLSignatureInput(InputStream inputOctetStream)
Constructs a XMLSignatureInput from an octet stream. The stream is directly read.

param
inputOctetStream

   	  this._inputOctetStreamProxy=inputOctetStream;
   	  
      //this(JavaUtils.getBytesFromStream(inputOctetStream));

   
public XMLSignatureInput(String inputStr)
Construct a XMLSignatureInput from a String.

This is a comfort method, which internally converts the String into a byte[] array using the {@link java.lang.String#getBytes()} method.

deprecated
param
inputStr the input String which including XML document or node

      this(inputStr.getBytes());
   
public XMLSignatureInput(String inputStr, String encoding)
Construct a XMLSignatureInput from a String with a given encoding.

This is a comfort method, which internally converts the String into a byte[] array using the {@link java.lang.String#getBytes()} method.

deprecated
param
inputStr the input String with encoding encoding
param
encoding the encoding of inputStr
throws
UnsupportedEncodingException

      this(inputStr.getBytes(encoding));
   
public XMLSignatureInput(Node rootNode)
Construct a XMLSignatureInput from a subtree rooted by rootNode. This method included the node and all his descendants in the output.

param
rootNode

      this._subNode = rootNode;
   
public XMLSignatureInput(Set inputNodeSet)
Constructor XMLSignatureInput

param
inputNodeSet
param
usedXPathAPI

       this._inputNodeSet = inputNodeSet;
   
Methods Summary
public voidaddNodeFilter(com.sun.org.apache.xml.internal.security.signature.NodeFilter filter)

param
filter

	
		if (isOctetStream()) {
			try {
				convertToNodes();
			} catch (Exception e) {
				throw new XMLSecurityRuntimeException("signature.XMLSignatureInput.nodesetReference",e);
			}
		}
		nodeFilters.add(filter);
		
	
voidconvertToNodes()

		DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
        dfactory.setValidating(false);        
        dfactory.setNamespaceAware(true);
        DocumentBuilder db = dfactory.newDocumentBuilder();
        // select all nodes, also the comments.        
        try {
           db.setErrorHandler(new com.sun.org.apache.xml.internal.security.utils
              .IgnoreAllErrorHandler());

           Document doc = db.parse(this.getOctetStream());
           
           XMLUtils.circumventBug2650(doc);
           this._subNode=doc.getDocumentElement();                    
        } catch (SAXException ex) {

           // if a not-wellformed nodeset exists, put a container around it...
           ByteArrayOutputStream baos = new ByteArrayOutputStream();

           baos.write("<container>".getBytes());
           baos.write(this.getBytes());
           baos.write("</container>".getBytes());

           byte result[] = baos.toByteArray();
           Document document = db.parse(new ByteArrayInputStream(result));
           this._subNode=document.getDocumentElement().getFirstChild().getFirstChild();				
        }
        this._inputOctetStreamProxy=null;
        this.bytes=null;
	
public byte[]getBytes()
Returns the byte array from input which was specified as the parameter of {@link XMLSignatureInput} constructor

return
the byte[] from input which was specified as the parameter of {@link XMLSignatureInput} constructor
throws
CanonicalizationException
throws
IOException

    if (bytes!=null) {
        return bytes;      
      }
   	  InputStream is = getResetableInputStream();
   	  if (is!=null) {
        //reseatable can read again bytes. 
   	  	if (bytes==null) {
            is.reset();       
            bytes=JavaUtils.getBytesFromStream(is);
   	  	} 	  	
   	  	return bytes;   	  	      
      }                    
         Canonicalizer20010315OmitComments c14nizer =
         		new Canonicalizer20010315OmitComments();                  
        bytes=c14nizer.engineCanonicalize(this);         
        return bytes;
   
public org.w3c.dom.NodegetExcludeNode()
Gets the exclude node of this XMLSignatureInput

return
Returns the excludeNode.

	   return excludeNode;
    
public java.lang.StringgetHTMLRepresentation()
Method getHTMLRepresentation

throws
XMLSignatureException
return
The HTML representation for this XMLSignature


      XMLSignatureInputDebugger db = new XMLSignatureInputDebugger(this);

      return db.getHTMLRepresentation();
   
public java.lang.StringgetHTMLRepresentation(java.util.Set inclusiveNamespaces)
Method getHTMLRepresentation

param
inclusiveNamespaces
throws
XMLSignatureException
return
The HTML representation for this XMLSignature


      XMLSignatureInputDebugger db = new XMLSignatureInputDebugger( this,
                                        inclusiveNamespaces);

      return db.getHTMLRepresentation();
   
public java.lang.StringgetMIMEType()
Returns MIMEType

return
MIMEType

      return this._MIMEType;
   
public java.util.ListgetNodeFilters()

return
the node filters

		// TODO Auto-generated method stub
		return nodeFilters;
	
public java.util.SetgetNodeSet(boolean circunvent)
Returns the node set from input which was specified as the parameter of {@link XMLSignatureInput} constructor

param
circunvent
return
the node set
throws
SAXException
throws
IOException
throws
ParserConfigurationException
throws
CanonicalizationException
throws
CanonicalizationException
throws
IOException
throws
ParserConfigurationException
throws
SAXException

      if (this._inputNodeSet!=null) {
      	  return this._inputNodeSet;
      }
   	  if (this.isElement()) {
            
   	  	    if (circunvent) {           
   	  	    	XMLUtils.circumventBug2650(XMLUtils.getOwnerDocument(_subNode));
            }
            this._inputNodeSet = new HashSet();
            XMLUtils.getSet(_subNode,this._inputNodeSet, excludeNode, this.excludeComments);
            
   	  	    return this._inputNodeSet;
   	  }
       else if (this.isOctetStream()) {
         convertToNodes();
         HashSet result=new HashSet();
         XMLUtils.getSet(_subNode, result,null,false); 
            //this._inputNodeSet=result;
            return result;         
      }

      throw new RuntimeException(
         "getNodeSet() called but no input data present");
   
public java.util.SetgetNodeSet()
Returns the node set from input which was specified as the parameter of {@link XMLSignatureInput} constructor

return
the node set
throws
SAXException
throws
IOException
throws
ParserConfigurationException
throws
CanonicalizationException
throws
CanonicalizationException
throws
IOException
throws
ParserConfigurationException
throws
SAXException

   	      return getNodeSet(false);
   
public java.io.InputStreamgetOctetStream()
Returns the Octect stream(byte Stream) from input which was specified as the parameter of {@link XMLSignatureInput} constructor

return
the Octect stream(byte Stream) from input which was specified as the parameter of {@link XMLSignatureInput} constructor
throws
IOException

         	  
      return getResetableInputStream();                 

   
public java.io.InputStreamgetOctetStreamReal()

return
real octect stream

       return this._inputOctetStreamProxy;
   
protected java.io.InputStreamgetResetableInputStream()

    	
    	if ((_inputOctetStreamProxy instanceof ByteArrayInputStream) ) {            
            if (!_inputOctetStreamProxy.markSupported()) {
                throw new RuntimeException("Accepted as Markable but not truly been"+_inputOctetStreamProxy);
            }
           return _inputOctetStreamProxy;
        }
        if (bytes!=null) {
            _inputOctetStreamProxy=new ByteArrayInputStream(bytes);
            return _inputOctetStreamProxy;
        }
        if (_inputOctetStreamProxy ==null)
            return null;
        if (_inputOctetStreamProxy.markSupported()) {
            if (log.isLoggable(java.util.logging.Level.INFO))                                  log.log(java.util.logging.Level.INFO, "Mark Suported but not used as reset");
        }
    	bytes=JavaUtils.getBytesFromStream(_inputOctetStreamProxy);
    	_inputOctetStreamProxy.close();
    	_inputOctetStreamProxy=new ByteArrayInputStream(bytes);
        return _inputOctetStreamProxy;
    
public java.lang.StringgetSourceURI()
Return SourceURI

return
SourceURI

      return this._SourceURI;
   
public org.w3c.dom.NodegetSubNode()
Gets the node of this XMLSignatureInput

return
The excludeNode set.

  	    return _subNode;
     
public booleanisByteArray()
Determines if the object has been set up with a ByteArray

return
true is the object has been set up with an octet stream

      return ( (bytes!=null)
              && ((this._inputNodeSet == null) && _subNode ==null));
   
public booleanisElement()
Determines if the object has been set up with an Element

return
true is the object has been set up with a Node set

   		return ((this._inputOctetStreamProxy==null)&& (this._subNode!=null)
   				&& (this._inputNodeSet==null) && !isNodeSet
   				);
   
public booleanisExcludeComments()

return
Returns the excludeComments.

     	return excludeComments;
     
public booleanisInitialized()
Is the object correctly set up?

return
true if the object has been set up correctly

      return (this.isOctetStream() || this.isNodeSet());
   
public booleanisNeedsToBeExpanded()
Check if the structured is needed to be circumbented.

return
true if so.

                    
      
	   return needsToBeExpanded;
   
public booleanisNodeSet()
Determines if the object has been set up with a Node set

return
true is the object has been set up with a Node set

      return (( (this._inputOctetStreamProxy == null)
              && (this._inputNodeSet != null) ) || isNodeSet);
   
public booleanisOctetStream()
Determines if the object has been set up with an octet stream

return
true is the object has been set up with an octet stream

      return ( ((this._inputOctetStreamProxy != null) || bytes!=null)
              && ((this._inputNodeSet == null) && _subNode ==null));
   
public voidsetExcludeComments(boolean excludeComments)

param
excludeComments The excludeComments to set.

     	this.excludeComments = excludeComments;
     
public voidsetExcludeNode(org.w3c.dom.Node excludeNode)
Sets the exclude node of this XMLSignatureInput

param
excludeNode The excludeNode to set.

	    this.excludeNode = excludeNode;
     
public voidsetMIMEType(java.lang.String MIMEType)
Sets MIMEType

param
MIMEType

      this._MIMEType = MIMEType;
   
public voidsetNeedsToBeExpanded(boolean needsToBeExpanded)
Set if the structured is needed to be circumbented.

param
needsToBeExpanded true if so.

	this.needsToBeExpanded = needsToBeExpanded;
   
public voidsetNodeSet(boolean b)

param
b

		isNodeSet=b;
		
	
public voidsetOutputStream(java.io.OutputStream os)

param
os

		outputStream=os;
		
	
public voidsetSourceURI(java.lang.String SourceURI)
Sets SourceURI

param
SourceURI

      this._SourceURI = SourceURI;
   
public java.lang.StringtoString()
Method toString

inheritDoc


      if (this.isNodeSet()) {
         return "XMLSignatureInput/NodeSet/" + this._inputNodeSet.size()
                   + " nodes/" + this.getSourceURI();         
      } 
      if (this.isElement()) {
        return "XMLSignatureInput/Element/" + this._subNode
        + " exclude "+ this.excludeNode + " comments:" + 
        this.excludeComments
        +"/" + this.getSourceURI();
      }
         try {
            return "XMLSignatureInput/OctetStream/" + this.getBytes().length
                   + " octets/" + this.getSourceURI();
         } catch (Exception ex) {
            return "XMLSignatureInput/OctetStream//" + this.getSourceURI();
         }
      
   
public voidupdateOutputStream(java.io.OutputStream diOs)

param
diOs
throws
IOException
throws
CanonicalizationException

        
        if (diOs==outputStream) {
        	return;
        }
        if (bytes!=null) {
            diOs.write(bytes);
            return;      
         }else if (_inputOctetStreamProxy==null) {                    
             Canonicalizer20010315OmitComments c14nizer =
                    new Canonicalizer20010315OmitComments();       
             c14nizer.setWriter(diOs);
            c14nizer.engineCanonicalize(this); 
            return;                  
          } else {
            InputStream is = getResetableInputStream();
            if (bytes!=null) {
                //already read write it, can be rea.
            	diOs.write(bytes,0,bytes.length);
                return;
            }            
            is.reset();            
            int num;
            byte[] bytesT = new byte[1024];
            while ((num=is.read(bytesT))>0) {
            	diOs.write(bytesT,0,num);
            }
                
          }